lmdb存儲的一些庫相關函數


MDB_env 為一個結構體,Opaque structure for a database environment.

MDB_txn :Opaque structure for a transaction handle.

typedef unsigned int MDB_dbi, A handle for an individual database in the DB environment.

struct MDB_val  :Generic structure used for passing keys and data in and out of the database.

                         它包含:size_t mv_size, void* mv_data;

structure MDB_cursor: Opaque structure for navigating through a database.

 

函數 int mdb_env_create(MDB_env** env)

它的作用就是創建一個LMDB的環境handle,就是指向一個分配了內存的地址啦,這個函數會為MDB_env結構體分配一個內存。它我們使用handle  的時候,首先要用mdb_env_open()函數打開,最后也要調用mdb_env_close()函數釋放掉內存並discard handle。

參數:[out] env,The address where the new handle will be stored。

返回:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數 :mdb_env_open()

int mdb_env_open  ( MDB_env *  env,  
                    const char *  path,  
                    unsigned int  flags,  
                    mdb_mode_t  mode  
                    )

它的作用是:打開一個環境handle。

注:如果函數失敗,我們要調用 mdb_env_close() 來丟棄  MDB_env handle.

參數說明:

[in] env :就是我們的環境handle。

[in] path :數據的路徑。注意:它必須存在,並且可寫;

[in] flags: 對於環境的一些特別的選項,它必須被設為0 或着把不同的選項 or在一起,

       常見的flag:http://104.237.133.194/doc/ group__mdb.html#ga32a193c6bf4d7d5c5d579e71f22e9340

[in] mode:對於linux來說 ,就是這個操作的對於文件權限(如:0644),windows忽略;

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數 :mdb_env_close ()

void mdb_env_close ( MDB_env * env)

作用:Close the environment and release the memory map.

 

函數: int mdb_env_set_mapsize()

int mdb_env_set_mapsize  ( MDB_env *  env,  
                            size_t    size  
                          )

作用:設置環境的memory map 的大小;注意:默認大小 為:10485760 bytes,該函數用在mdb_env_create()之后,且mdb_env_open()之前,當然,在后面它也可能被調用的;

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

 

函數:int mdb_txn_begin()

int mdb_txn_begin  ( MDB_env *     env,  
                     MDB_txn *     parent,  
                     unsigned int  flags,  
                     MDB_txn **     txn  
                    )

 

作用:Create a transaction for use with the environment.

參數:[in] env:An environment handle returned by mdb_env_create() 

         [in] parent:可以為空,即NULL,如果不是空的話,the new transaction will be a nested transaction。

         [in] flags: 指對於操作來說一些特別的選項,可以為0或着其它:如MDB_RDONLY,表示:它不會執行任何寫操作;

        [in] txn  ,Address where the new MDB_txn handle will be stored .

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

另外:The transaction handle may be discarded using mdb_txn_abort() or mdb_txn_commit().

 

函數:mdb_dbi_open()

int mdb_dbi_open  ( MDB_txn *  txn,  
  const char *  name,  
  unsigned int  flags,  
  MDB_dbi *  dbi  
 )

 

作用:在環境里打開一個database;

注意:database handle denotes the name and parameters of a database, independently of whether such a database exists.The database handle may be discarded by calling mdb_dbi_close().

參數:

[in] txn :A transaction 的handle;

[in]  name:打開的database的名字,If only a single database is needed in the environment, this value may be NULL.

[in]  flags:Special options for this database. 可以為0或着其它選項

[out] dbi :Address where the new MDB_dbi handle will be store;

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數:int mdb_put()

int mdb_put  ( MDB_txn *  txn,  
  MDB_dbi  dbi,  
  MDB_val *  key,  
  MDB_val *  data,  
  unsigned int  flags  
 )

作用:store items into a datagase,也就是說,把key/data pairs存放到database里面。

注意:當鍵值有相同的時候,The default behavior is to enter the new key/data pair, replacing any previously existing

key if duplicates are disallowed, or adding a duplicate data item if duplicates are allowed 。

參數:

[in] txn: A transaction handle returned by mdb_txn_begin() 
[in] dbi :A database handle returned by mdb_dbi_open() 
[in] key :The key to store in the database 
[in,out] data: The data to store 
[in] flags Special options for this operation. This parameter must be set to 0 or by bitwise OR'ing together one or more of the values described here.

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數:int mdb_txn_commit()

int mdb_txn_commit  ( MDB_txn *  txn )

作用:提交所有的操作:Commit all the operations of a transaction into the database.

注意:提交完以后,The transaction handle is freed. It and its cursors must not be used again after this call, except with mdb_cursor_renew().

參數:[in] txn:即我們要提交的那個transaction的handle;

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數:int mdb_cursor_open ()

int mdb_cursor_open  ( MDB_txn *  txn,  
  MDB_dbi  dbi,  
  MDB_cursor **  cursor  
 )

作用:創建一個cursor的handle。

備注:A cursor is associated with a specific transaction and database。

參數:

[in] txn A transaction handle returned by mdb_txn_begin()  
[in] dbi A database handle returned by mdb_dbi_open()  
[out] cursor Address where the new MDB_cursor handle will be stored

返回值:如果錯誤,返回一個非零的值,如果正確,返回零;

 

函數:mdb_cursor_get()

int mdb_cursor_get  ( MDB_cursor *  cursor,  
  MDB_val *  key,  
  MDB_val *  data,  
  MDB_cursor_op  op  
 )

功能:通過cursor讀取數據

參數:

[in] cursor: A cursor handle returned by mdb_cursor_open()  
[in,out] key: The key for a retrieved item  
[in,out] data: The data of a retrieved item  
[in] op: A cursor operation MDB_cursor_op

其中:enum MDB_cursor_op,它枚舉了許多Cursor Get 的操作,如下所示:

MDB_FIRST  
Position at first key/data item 
 
MDB_FIRST_DUP  
Position at first data item of current key. Only for MDB_DUPSORT 
 
MDB_GET_BOTH  
Position at key/data pair. Only for MDB_DUPSORT 
 
MDB_GET_BOTH_RANGE  
position at key, nearest data. Only for MDB_DUPSORT 
 
MDB_GET_CURRENT  
Return key/data at current cursor position 
 
MDB_GET_MULTIPLE  
Return key and up to a page of duplicate data items from current cursor position. Move cursor to prepare for MDB_NEXT_MULTIPLE. Only for MDB_DUPFIXED 
 
MDB_LAST  
Position at last key/data item 
 
MDB_LAST_DUP  
Position at last data item of current key. Only for MDB_DUPSORT 
 
MDB_NEXT  
Position at next data item 
 
MDB_NEXT_DUP  
Position at next data item of current key. Only for MDB_DUPSORT 
 
MDB_NEXT_MULTIPLE  
Return key and up to a page of duplicate data items from next cursor position. Move cursor to prepare for MDB_NEXT_MULTIPLE. Only for MDB_DUPFIXED 
 
MDB_NEXT_NODUP  
Position at first data item of next key 
 
MDB_PREV  
Position at previous data item 
 
MDB_PREV_DUP  
Position at previous data item of current key. Only for MDB_DUPSORT 
 
MDB_PREV_NODUP  
Position at last data item of previous key 
 
MDB_SET  
Position at specified key 
 
MDB_SET_KEY  
Position at specified key, return key + data 
 
MDB_SET_RANGE  
Position at first key greater than or equal to specified key.

先寫到這里吧。。

看的時候,可以結合一個例子:http://blog.csdn.net/u012235274/article/details/51899598

還有很多:http://104.237.133.194/doc/index.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM