diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-03 16:54:14 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-03 16:54:14 +0800 |
commit | 2c39a250adcef967493bcfc1201435f91249ad70 (patch) | |
tree | 7097b5f2e207e2a62ce674cfc6778721c734b4d9 /src/storage/bdb_utils.h | |
parent | 9181f558e6aa784d7734a728e1581a16ba36b422 (diff) | |
download | libpinyin-2c39a250adcef967493bcfc1201435f91249ad70.tar.gz libpinyin-2c39a250adcef967493bcfc1201435f91249ad70.tar.xz libpinyin-2c39a250adcef967493bcfc1201435f91249ad70.zip |
write copy_bdb function
Diffstat (limited to 'src/storage/bdb_utils.h')
-rw-r--r-- | src/storage/bdb_utils.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/storage/bdb_utils.h b/src/storage/bdb_utils.h index f908d23..8aa0f40 100644 --- a/src/storage/bdb_utils.h +++ b/src/storage/bdb_utils.h @@ -25,6 +25,8 @@ #include <assert.h> #include <db.h> +namespace pinyin{ + inline u_int32_t attach_option(guint32 flags) { u_int32_t db_flags = 0; @@ -38,4 +40,35 @@ inline u_int32_t attach_option(guint32 flags) { return db_flags; } + +inline bool copy_bdb(DB * srcdb, DB * destdb) { + int ret = 0; + + DBC * cursorp = NULL; + DBT key, data; + /* Get a cursor */ + srcdb->cursor(srcdb, NULL, &cursorp, 0); + + if (NULL == cursorp) + return false; + + /* Initialize our DBTs. */ + memset(&key, 0, sizeof(DBT)); + memset(&data, 0, sizeof(DBT)); + + /* Iterate over the database, retrieving each record in turn. */ + while ((ret = cursorp->c_get(cursorp, &key, &data, DB_NEXT)) == 0) { + ret = destdb->put(destdb, NULL, &key, &data, 0); + assert(0 == ret); + } + assert(DB_NOTFOUND == ret); + + /* Cursors must be closed */ + if ( cursorp != NULL ) + cursorp->c_close(cursorp); + + return true; +} + +}; #endif |