summaryrefslogtreecommitdiffstats
path: root/src/storage/bdb_utils.h
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-03-03 16:54:14 +0800
committerPeng Wu <alexepico@gmail.com>2016-03-03 16:54:14 +0800
commit2c39a250adcef967493bcfc1201435f91249ad70 (patch)
tree7097b5f2e207e2a62ce674cfc6778721c734b4d9 /src/storage/bdb_utils.h
parent9181f558e6aa784d7734a728e1581a16ba36b422 (diff)
downloadlibpinyin-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.h33
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