⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 villa.rb

📁 一个小型的基于Hash的Key=>Value的数据库
💻 RB
📖 第 1 页 / 共 2 页
字号:
  end  ##  # bool = villa.curput(val, cpmode);  # Method: Insert a record around the cursor.  # `val' specifies a value.  Although it must be an instance of String, binary data is okey.  # `cpmode' specifies detail adjustment: `Villa::CPCURRENT', which means that the value of the  # current record is overwritten, `Villa::CPBEFORE', which means that a new record is inserted  # before the current record, `Villa::CPAFTER', which means that a new record is inserted after  # the current record.  If it is omitted, `Villa::CPCURRENT' is specified.  # The return value is always true.  However, if the silent flag is true and no record  # corresponds to the cursor, false is returned instead of exception.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or no record  # corresponds to the cursor.  # After insertion, the cursor is moved to the inserted record.  #  def curput(val, cpmode = CPCURRENT)    if(@cmode == CMPOBJ)      val = Marshal.dump(val)    end    mod_curput(@index, val, cpmode)  end  ##  # bool = villa.curout();  # Method: Delete the record where the cursor is.  # The return value is always true.  However, if the silent flag is true and no record  # corresponds to the cursor, false is returned instead of exception.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or no record  # corresponds to the cursor.  # After deletion, the cursor is moved to the next record if possible.  #  def curout()    mod_curout(@index)  end  ##  # bool = villa.settuning(lrecmax, nidxmax, lcnum, ncnum)  # Method: Set the tuning parameters for performance.  # `lrecmax' specifies the max number of records in a leaf node of B+ tree.  If it is not more  # than 0, the default value is specified.  # `nidxmax' specifies the max number of indexes in a non-leaf node of B+ tree.  If it is not  # more than 0, the default value is specified.  # `lcnum' specifies the max number of caching leaf nodes.  If it is not more than 0, the  # default value is specified.  # `ncnum' specifies the max number of caching non-leaf nodes.  If it is not more than 0, the  # default value is specified.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # The default setting is equivalent to `vlsettuning(49, 192, 1024, 512)'.  Because tuning  # parameters are not saved in a database, you should specify them every opening a database.  #  def settuning(lrecmax, nidxmax, lcnum, ncnum)    mod_settuning(@index, lrecmax, nidxmax, lcnum, ncnum)  end  ##  # bool = villa.sync()  # Method: Synchronize updating contents with the file and the device.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # This method is useful when another process uses the connected database file.  This method  # should not be used while the transaction is activated.  #  def sync()    mod_sync(@index)  end  ##  # bool = villa.optimize()  # Method: Optimize the database.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # In an alternating succession of deleting and storing with overwrite or concatenate,  # dispensable regions accumulate.  This method is useful to do away with them.  This method  # should not be used while the transaction is activated.  #  def optimize()    mod_optimize(@index)  end  ##  # num = villa.fsiz()  # Method: Get the size of the database file.  # The return value is the size of the database file.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # Because of the I/O buffer, the return value may be less than the real size.  #  def fsiz()    mod_fsiz(@index)  end  ##  # num = villa.rnum()  # Method: Get the number of the records stored in the database.  # The return value is the number of the records stored in the database.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  #  def rnum()    mod_rnum(@index)  end  ##  # num = villa.length()  # Method: An alias of `rnum'.  #  alias length rnum  ##  # num = villa.size()  # Method: An alias of `rnum'.  #  alias size rnum  ##  # bool = villa.writable()  # Method: Check whether the database handle is a writer or not.  # The return value is true if the handle is a writer, false if not.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  #  def writable()    mod_writable(@index)  end  ##  # bool = villa.fatalerror()  # Method: Check whether the database has a fatal error or not.  # The return value is true if the database has a fatal error, false if not.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  #  def fatalerror()    mod_fatalerror(@index)  end  ##  # bool = villa.tranbegin()  # Method: Begin the transaction.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # If a thread is already in the transaction, the other threads block until the prius is out  # of the transaction.  Only one transaction can be activated with a database handle at the  # same time.  #  def tranbegin()    @tranmutex.lock()    MyMutex.synchronize() do      begin        mod_tranbegin(@index)      rescue        @tranmutex.unlock()        raise()      end    end  end  ##  # bool = villa.trancommit()  # Method: Commit the transaction.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # Updating a database in the transaction is fixed when it is committed successfully.  Any  # other thread except for the one which began the transaction should not call this method.  #  def trancommit()    begin      mod_trancommit(@index)    ensure      @tranmutex.unlock()    end  end  ##  # bool = villa.tranabort()  # Method: Abort the transaction.  # The return value is always true.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  # Updating a database in the transaction is discarded when it is aborted.  The state of the  # database is rollbacked to before transaction.  Any other thread except for the one which  # began the transaction should not call this method.  #  def tranabort()    begin      mod_tranabort(@index)    ensure      @tranmutex.unlock()    end  end  ##  # villa.transaction() do ... end  # Iterator Method: Perform an iterator block in the transaction.  # The specified iterator block is performed in the transaction.  If the block returns true,  # the transaction is committed.  If the block returns false or raises any exception, the  # transaction is aborted.  #  def transaction()    tranbegin()    begin      cmt = yield()    rescue      cmt = false      raise()    ensure      if(cmt)        trancommit()      else        tranabort()      end    end  end  ##  # villa.each() do |key, val| ... end  # Iterator Method: Iterate a process with a pair of a key and a value of each record.  #  def each()    MyMutex.synchronize() do      curfirst()      while(true)        begin          break unless key = curkey()          val = curval()          yield(key, val)          curnext()        rescue ENOITEM          break        end      end      curfirst()    end    self  end  ##  # villa.each_pair() do |key, val| ... end  # Iterator Method: An alias of `each'.  #  alias each_pair each  ##  # villa.each_key() do |key| ... end  # Iterator Method: Iterate a process with a key of each record.  #  def each_key()    MyMutex.synchronize() do      curfirst()      while(true)        begin          break unless key = curkey()          curnext()          yield(key)        rescue ENOITEM          break        end      end      curfirst()    end    self  end  ##  # villa.each_value() do |val| ... end  # Iterator Method: Iterate a process with a value of each record.  #  def each_value()    MyMutex.synchronize() do      curfirst()      while(true)        begin          break unless val = curval()          curnext()          yield(val)        rescue ENOITEM          break        end      end      curfirst()    end    self  end  ##  # ary = villa.keys()  # Method: Get an array of all keys.  # The return value is an array of all keys.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  #  def keys()    ary = Array::new(rnum())    MyMutex.synchronize() do      begin        curfirst()        0.upto(ary.length - 1) do |i|          ary[i] = curkey()          curnext()        end        curfirst()      rescue ENOITEM      end    end    ary  end  ##  # ary = villa.values()  # Method: Get an array of all values.  # The return value is an array of all values.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.  #  def values()    ary = Array::new(rnum())    MyMutex.synchronize() do      begin        curfirst()        0.upto(ary.length - 1) do |i|          ary[i] = curval()          curnext()        end        curfirst()      rescue ENOITEM      end    end    ary  end  ##  # str = villa.index(val)  # Method: Retrieve a record with a value.  # `val' specifies a value.  Although it must be an instance of String, binary data is okey.  # The return value is the key of the record with the specified value.  # An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or no record  # corresponds.  # If two or more records correspond, the first found record is selected.  #  def index(val)    key = nil    MyMutex.synchronize() do      curfirst()      while(true)        break unless tmp = curval()        if(tmp == val)          key = curkey()          break        end        curnext()      end      curfirst()    end    key  end  ##  # num = villa.to_int()  # Method: An alias of `rnum'.  #  alias to_int rnum  ##  # num = villa.to_i()  # Method: An alias of `to_int'.  #  alias to_i to_int  ##  # str = villa.to_str()  # Method: Get string standing for the instance.  #  def to_str    if(@index != -1)      sprintf("#<Villa:%#x:name=%s:state=open:rnum=%d>",              object_id(), @name, rnum())    else      sprintf("#<Villa:%#x:name=%s:state=closed>", object_id(), @name)    end  end  ##  # str = villa.to_s()  # Method: An alias of `to_str'.  #  alias to_s to_str  ##  # ary = villa.to_ary()  # Method: Get an array of alternation of each pair of a key and a value.  #  def to_ary    ary = Array::new(rnum())    i = 0    each() do |key, val|      ary[i] = [key, val]      i += 1    end    ary  end  ##  # ary = villa.to_a()  # Method: An alias of `to_ary'.  #  alias to_a to_ary  ##  # hash = villa.to_hash()  # Method: Get a hash storing all records.  #  def to_hash    hash = Hash::new()    each() do |key, val|      hash[key] || hash[key] = val    end    hash  end  ##  # hash = villa.to_h()  # Method: An alias of `to_hash'.  #  alias to_h to_hash  ##  # str = villa.inspect()  # Method: An alias of `to_str'.  #  alias inspect to_strend#----------------------------------------------------------------# Alias definition of class methods#----------------------------------------------------------------class << Villa  alias open newend# END OF FILE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -