C. php extension Cabinet

Source: Internet
Author: User

This extension provides some APIs of Tokyo cabinet for PHP client calls.

The following is an introduction to Tokyo cabinet from Baidu Encyclopedia:

Tokyo cabinet is a dBm implementation. The database here consists of a series of key-value pairs. Both key and value can be any length in the byte order.

Column, either binary or string. There is no concept of data types and data tables. When used as a hash table database, each key must be different, so the same values of two keys cannot be stored. The following access methods are provided: the key and value parameters are provided for storage, records are deleted by key, and records are read by key. In addition, traversal of keys is also supported, although the order is arbitrary, it cannot be guaranteed. These methods are the same as the Unix-standard dBm, such as gdbm and ndbm, but are much better than their performance (so they can be replaced ).

Similar to nosql such as redis, the high-performance Key-value Database supports memory read/write and file synchronization.

1. First, let's take a look at the instructions for using the implemented cabinet class:

Class cabinet {protected $ db; public function _ construct (); // kcdbnew/*** open the database file * @ Param string $ PATH file path * If yes -, the database will be a prototype hash database * if it is +, the database will be a prototype tree database * if it is:, the database will be stored * if it is *, the database will cache the hash database * if it is %, the database will be a cache tree database * if its suffix is. kch, the database will hash a file database * if its suffix is. kct: a database that stores a file tree. * If the suffix is. KCD, the database will be a directory hash database. * If the suffix is. KCF, the database will create a directory tree database. * If the suffix is. kcx, the database will be a plain text database. * Adjust the parameters. The names that can be tracked are separated. * The names and values of each parameter are separated by "=. If "type" parameter is specified, * "-", "+", ":", "*", "%", "kch ", "kct", "KCD", KCF ", and" kcx. * All Types of databases support "log" record parameters, "logkinds", and "logpx ". The prototype hash database and prototype tree database do not support any other adjustment parameters. * The stash Database supports "bnum ". The cache hash Database supports "opts", "bnum", "zcomp", "capcnt", "capsiz", and "zkey ". * The cache tree Database supports all parameters except capacity cache hash database restrictions and supports "psiz", "rcomp", and "pccap ". * The file hash Database supports "fpow", "select to use", "bnum", "msiz", "dfunit", and "zcomp" in "apow ", and "zkey. * The file hash database and the "psiz" and "rcomp" parameters supported by the file tree database are "pccap ". * Directory hash databases support selecting "medium", "zcomp", and "zkey". * The database in the directory tree supports directory hash databases with all parameters and "psiz", "rcomp", and "except pccap ". * Text-only databases do not support any other adjustment parameters. ** @ Waning "-", "+", ":", "*", and "%" currently set one of them. The data lifecycle is only in a single request, data does not reside in a multi-process environment. * @ Param int $ mode connection mode * kcowriter write mode * kcoreader read mode * kcocreate if the file does not exist, create a new database * kcotruncate, always create a new database * kcoautotran, update and create each time. all are implicit transactions. Like automatic submission of MySQL, * kcoautosync implicitly synchronizes all operations such as update creation to the file immediately * kconolock unlock open database * kcotrylock tries to lock open the database, non-blocking * kconorepair is not clear about its usefulness * @ return Boolean if it fails to be opened, false */Public Function kcdbopen ($ path, $ mode) is returned ); /***** shut down database resources */Public Function kcdbclose ();/***** last error code * @ return int */Public Function kcdbecode (); /*** last error Description * @ return string */Public Function kcdbemsg ();/*** adds a record * @ Param string $ key name * @ Param Mi Xed $ value key value * @ return Boolean * @ waning if the key name already exists, overwrite the original record */Public Function kcdbset ($ key, $ value ); /*** Add a record * @ Param string $ key name * @ Param mixed $ value key value * @ waning if the key name already exists, false */Public Function kcdbadd ($ key, $ value) is returned ); ** update record value * @ Param string $ key name * @ Param mixed $ value new value * @ return Boolean * @ waning if the key name does not exist, false is returned. Public Function kcdbreplace ($ key, $ value ); /*** append record value * @ Param string $ key name * @ Param mixed $ Value Added-value * @ return Boolean * @ waning if the key name does not exist, create and store value. If yes, value is appended to the original record. */Public Function kcdbappend ($ key, $ value);/*** delete a record * @ Param string $ key * @ return Boolean * @ waning if the key name cannot be, returns false. */Public Function kcdbremove ($ key);/*** get a record * @ Param string $ key * @ return mixed * @ waning if the key name does not exist, returns NULL */Public Function kcdbget ($ key ); /*** check whether the record exists * @ Param string $ key * @ return Boolean */Public Function kcdbcheck ($ key ); /*** start transaction ** @ return Boolean returns true if the transaction is successfully created */Public Function kcdbbegintran (); /*** end transaction ** @ Param $ commit true commit transaction false cancel transaction * @ return Boolean */Public Function kcdbendtran ($ commit ); /*** create a database copy ** @ Param string $ DEST copy storage path * // *** CLEAR database records * @ return Boolean */Public Function kcdbclear (); /*** total number of returned records ** @ return int */Public Function kcdbcount ();/*** returns the current database file size * @ return long */Public Function kcdbsize (); /*** return the current database file path */Public Function kcdbpath ();/*** return the status description of the current database */Public Function kcdbstatus (); /*** return the set with the specified key name prefix * @ Param string $ prefix the key name prefix. For example, pp2012 _ * @ Param int indicates the maximum retrieval quantity * @ return array */Public Function kcdbmatchprefix ($ prefix, $ max ); /*** set of key names matching by regular expressions * @ Param string $ RegEx * @ Param int maximum retrieval quantity * @ return array */Public Function kcdbmatchregex ($ RegEx, $ max ); /*** data merging ** @ Param object $ db instantiated cabinet object * @ Param int $ mode merge mode * kcmset overwrites existing values * kcmadd retains existing values * kcmreplace modifies existing * Add new value to kcmappend */Public Function kcdbmerge ($ dbarray, $ mode );}

Ii. test files

Function Test ($ MSG, $ result) {if ($ result) {echo "-------- {$ MSG} success -------- \ n ";} else {echo "-------- {$ MSG} failed --------- \ n" ;}// test kcdbset $ cabinet1 = new Cabinet (); $ cabinet1-> kcdbopen ("casket2.kch ", kcowriter | kcocreate); test ("test kcdbset", $ cabinet1-> kcdbset ("ppcc2011_1", "ppcc2011_1"); test ("test kcdbadd ", $ cabinet1-> kcdbadd ("ppcc2012_2", "ppcc2012_2"); test ("test kcdbadd", $ cabinet1-> kcdbadd ("principal", "ppcc2012_3 ")); test ("test kcdbcheck", $ cabinet1-> kcdbcheck ("ppcc2011_1"); // test kcdbmerge $ cabinet1 = new Cabinet (); $ cabinet1-> kcdbopen ("casket2.kch", kcowriter | kcocreate); $ cabinet2 = new Cabinet (); $ cabinet2-> kcdbopen ("casket. kch ", kcowriter | kcocreate); test (" test kcdbmerge ", $ cabinet2-> kcdbmerge (Array ($ cabinet1), kcmadd )); // test kcdbmatchprefixtest ("test kcdbmatchprefix", $ cabinet1-> kcdbmatchprefix ("ppcc201", 100); // test kcdbcopytest ("test kcdbcopy ", $ cabinet1-> kcdbcopy ("copy. kch "); // test transactiontest (" test kcdbbegintran ", $ cabinet1-> kcdbbegintran (); $ cabinet1-> kcdbadd (" ppcc2012_1 "," quit "); $ cabinet1-> kcdbendtran (false); // cancel the transaction $ cabinet1-> kcdbbegintran (); $ cabinet1-> kcdbadd ("ppcc2012_1", "ppcc2012_1 "); $ cabinet1-> kcdbendtran (false); // submit the transaction // test kcdbstatus kcdbcount kcdbpath kcdbclear kcdbget kcdbremove kcdbappend kcdbreplacetest ("test kcdbappend ", $ cabinet1-> kcdbappend ('ppcc2012 _ 1', '_ append'); test ("test kcdbreplace", $ cabinet1-> kcdbreplace ('ppcc2012 _ 1 ', 'New _ value'); test ("test kcdbremove", $ cabinet1-> kcdbremove ('ppcc2012 _ 1'); test ("test kcdbstatus ", $ cabinet1-> kcdbstatus (); test ("test kcdbcount", $ cabinet1-> kcdbcount (); test ("test kcdbclear ", $ cabinet1-> kcdbclear (); test ("test kcdbpath", $ cabinet1-> kcdbpath ());

Test results:

Source code package: cabinet.zip

Php5.3-nt dll extension download: http://l9.yunpan.cn/lk/QEcgvkyCGXhDY

New support for Linux Compilation

For instructions on using Linux, see cabinet_linux.php.

For instructions on using Windows, see cabinet_win32.php.

Previous review:

C. Extension of common image processing tools for image_tool in PHP

C. Implement PHP extension fetch_url Data Capturing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.