[Back]

API of villa


require 'villa'
The library `villa' should be included in application codes. An instance of the class `Villa' is used as a database handle. `Villa' performs Mix-in of `Enumerable'. Each method of `Villa' throws an exception of `Villa::EANY' or its sub classes when an error occurs: `Villa::ENOERR', `Villa::EFATAL', `Villa::EMODE', `Villa::EBROKEN', `Villa::EKEEP', `Villa::ENOITEM', `Villa::EALLOC', `Villa::EMAP', `Villa::EOPEN', `Villa::ECLOSE', `Villa::ETRUNC', `Villa::ESYNC', `Villa::ESTAT', `Villa::ESEEK', `Villa::EREAD', `Villa::EWRITE', `Villa::ELOCK', `Villa::EUNLINK', `Villa::EMKDIR', `Villa::ERMDIR' and `Villa::EMISC'.
villa = Villa::new(name, omode, cmode)
Constructor: Get a database handle. `name' specifies the name of a database file. `omode' specifies the connection mode: `Villa::OWRITER' as a writer, `Villa::OREADER' as a reader. If the mode is `Villa::OWRITER', the following may be added by bitwise or: `Villa::OCREAT', which means it creates a new database if not exist, `Villa::OTRUNC', which means it creates a new database regardless if one exists, `Villa::OZCOMP', which means leaves in the database are compressed with ZLIB, `Villa::OYCOMP', which means leaves in the database are compressed with LZO, `Villa::OXCOMP', which means leaves in the database are compressed with BZIP2. Both of `Villa::OREADER' and `Villa::OWRITER' can be added to by bitwise or: `Villa::ONOLCK', which means it opens a database file without file locking, or `Villa::OLCKNB', which means locking is performed without blocking. If it is omitted, `Villa::OREADER' is specified. `cmode' specifies the comparing function: `Villa::CMPLEX' comparing keys in lexical order, `Villa::CMPDEC' comparing keys as decimal strings, `Villa::CMPOBJ' comparing keys as serialized objects implementing the method `<=>'. The comparing function should be kept same in the life of a database. If `Villa::CMPOBJ' is used, such methods as `put', `out', `get' and so on can treat not only instances of `String' but also any serializable and comparable object. If so, objects being stored are serialized and objects being retrieved are deserialized. The return value is the database handle. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs. If a block parameter is given, this method works as an iterator. A database handle is opened and passed via the first argument of the block. The database handle is surely closed when the block is over. While connecting as a writer, an exclusive lock is invoked to the database file. While connecting as a reader, a shared lock is invoked to the database file. The thread blocks until the lock is achieved. `Villa::OZCOMP', `Villa::OYCOMP', and `Villa::OXCOMP' are available only if QDBM was built each with ZLIB, LZO, and BZIP2 enabled. If `Villa::ONOLCK' is used, the application is responsible for exclusion control.
villa = Villa::open(name, omode, cmode)
Constructor: An alias of `new'.
bool = villa.close()
Method: Close the database handle. The return value is always true. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs. Because the region of a closed handle is released, it becomes impossible to use the handle. Updating a database is assured to be written when the handle is closed. If a writer opens a database but does not close it appropriately, the database will be broken. If the transaction is activated and not committed, it is aborted.
villa.silent = bool
Method: Set the flag whether to repress frequent exceptions. The return value is the assigned value.
bool = villa.put(key, val, dmode)
Method: Store a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. `val' specifies a value. Although it must be an instance of String, binary data is okey. `dmode' specifies behavior when the key overlaps, by the following values: `Villa::DOVER', which means the specified value overwrites the existing one, `Villa::DKEEP', which means the existing value is kept, `Villa::DCAT', which means the specified value is concatenated at the end of the existing value, `Villa::DDUP', which means duplication of keys is allowed and the specified value is added as the last one, `Villa::DDUPR', which means duplication of keys is allowed and the specified value is added as the first one. If it is omitted, `Villa::DOVER' is specified. The return value is always true. However, if the silent flag is true and replace is cancelled, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or replace is cancelled. The cursor becomes unavailable due to updating database.
bool = villa.store(key, val)
Method: An alias of `put'.
villa[key] = val
Method: An alias of `put'.
bool = villa.out(key)
Method: Delete a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. The return value is always true. However, if the silent flag is true and no record corresponds, 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. When the key of duplicated records is specified, the value of the first record of the same key is selected. The cursor becomes unavailable due to updating database.
bool = villa.delete(key)
Method: An alias of `out'.
bool = villa.clear()
Method: Delete all records. The return value is always true. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.
str = villa.get(key)
Method: Retrieve a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. The return value is an instance of the value of the corresponding record. If the silent flag is true and no record corresponds, nil is returned instead of exception. When the key of duplicated records is specified, the first record of the same key is deleted. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or no record corresponds.
str = villa.fetch(key, defval)
Method: Retrieve a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. `defval' specifies the default value used when no record corresponds. If it is omitted, nil is specified. The return value is an instance of the value of the corresponding record, or the default value if no record corresponds. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.
str = villa[key]
Method: An alias of `fetch'.
num = villa.vsiz(key)
Method: Get the size of the value of a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. The return value is the size of the value of the corresponding record. If the silent flag is true and no record corresponds, -1 is returned instead of exception. If multiple records correspond, the size of the first is returned. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.
num = villa.vnum(key)
Method: Get the number of records corresponding a key. `key' specifies a key. Although it must be an instance of String, binary data is okey. The return value is the size of the value of the corresponding record. If no record corresponds, 0 is returned. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs.
bool = villa.curfirst()
Method: Move the cursor to the first record. The return value is always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or there is no record in the database.
bool = villa.curlast()
Method: Move the cursor to the last record. The return value is always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or there is no record in the database.
bool = villa.curprev()
Method: Move the cursor to the previous record. The return value is always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or there is no previous record.
bool = villa.curnext()
Method: Move the cursor to the next record. The return value is always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or there is no next record.
bool = villa.curjump(key, jmode)
Method: Move the cursor to a position around a record. `key' specifies a key. Although it must be an instance of String, binary data is okey. `jmode' specifies detail adjustment: `Villa::JFORWARD', which means that the cursor is set to the first record of the same key and that the cursor is set to the next substitute if completely matching record does not exist, `Villa::JBACKWARD', which means that the cursor is set to the last record of the same key and that the cursor is set to the previous substitute if completely matching record does not exist. If it is omitted, `Villa::JFORWARD' is specified. The return value is always true. However, if the silent flag is true and no record corresponds, false is returned instead of exception. An exception of `Villa::EANY' or its sub classes is thrown if an error occurs or there is no record corresponding the condition.
str = villa.curkey()
Method: Get the key of the record where the cursor is. The return value is the key of the corresponding record. If the silent flag is true and no record corresponds, nil 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.
str = villa.curval()
Method: Get the value of the record where the cursor is. The return value is the value of the corresponding record. If the silent flag is true and no record corresponds, nil 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.
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.
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.
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.
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.
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.
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.
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.
num = villa.length()
Method: An alias of `rnum'.
num = villa.size()
Method: An alias of `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.
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.
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.
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.
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.
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.
villa.each() do |key, val| ... end
Iterator Method: Iterate a process with a pair of a key and a value of each record.
villa.each_pair() do |key, val| ... end
Iterator Method: An alias of `each'.
villa.each_key() do |key| ... end
Iterator Method: Iterate a process with a key of each record.
villa.each_value() do |val| ... end
Iterator Method: Iterate a process with a value of each record.
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.
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.
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.
num = villa.to_int()
Method: An alias of `rnum'.
num = villa.to_i()
Method: An alias of `to_int'.
str = villa.to_str()
Method: Get string standing for the instance.
str = villa.to_s()
Method: An alias of `to_str'.
ary = villa.to_ary()
Method: Get an array of alternation of each pair of a key and a value.
ary = villa.to_a()
Method: An alias of `to_ary'.
hash = villa.to_hash()
Method: Get a hash storing all records.
hash = villa.to_h()
Method: An alias of `to_hash'.
str = villa.inspect()
Method: An alias of `to_str'.

Generated by myrbdoc, 2007/03/05.