diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-07 15:43:33 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-07 15:44:45 +0800 |
commit | 5d16830d7e1ce3df548bca0017a4deaccdc8cfb0 (patch) | |
tree | 498b9833b2deff655102fa6a3ae4770f35b08411 /src | |
parent | 7b8d727d6048affa0e934e821007c5198be4b332 (diff) | |
download | libpinyin-5d16830d7e1ce3df548bca0017a4deaccdc8cfb0.tar.gz libpinyin-5d16830d7e1ce3df548bca0017a4deaccdc8cfb0.tar.xz libpinyin-5d16830d7e1ce3df548bca0017a4deaccdc8cfb0.zip |
write chewing_large_table2_bdb.cpp in progress
Diffstat (limited to 'src')
-rw-r--r-- | src/storage/chewing_large_table2_bdb.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/storage/chewing_large_table2_bdb.cpp b/src/storage/chewing_large_table2_bdb.cpp index 1af3af2..fb15eb2 100644 --- a/src/storage/chewing_large_table2_bdb.cpp +++ b/src/storage/chewing_large_table2_bdb.cpp @@ -20,6 +20,7 @@ */ #include "chewing_large_table2.h" +#include <errno.h> #include "bdb_utils.h" namespace pinyin{ @@ -126,4 +127,92 @@ void ChewingLargeTable2::reset() { #undef CASE } +/* attach method */ +bool ChewingLargeTable2::attach(const char * dbfile, guint32 flags) { + reset(); + + u_int32_t db_flags = attach_options(flags); + + if (!dbfile) + return false; + + int ret = db_create(&m_db, NULL, 0); + assert(0 == ret); + + ret = m_db->open(m_db, NULL, dbfile, NULL, + DB_BTREE, db_flags, 0644); + if (ret != 0) + return false; + + init_entries(); + + return true; +} + +/* load/store method */ +bool ChewingLargeTable2::load_db(const char * filename) { + reset(); + + /* create in-memory db. */ + int ret = db_create(&m_db, NULL, 0); + assert(0 == ret); + + ret = m_db->open(m_db, NULL, NULL, NULL, + DB_BTREE, DB_CREATE, 0600); + if (ret != 0) + return false; + + /* load db into memory. */ + DB * tmp_db = NULL; + ret = db_create(&tmp_db, NULL, 0); + assert(0 == ret); + + if (NULL == tmp_db) + return false; + + ret = tmp_db->open(tmp_db, NULL, filename, NULL, + DB_BTREE, DB_RDONLY, 0600); + if (ret != 0) + return false; + + if (!copy_bdb(tmp_db, m_db)) + return false; + + if (tmp_db != NULL) + tmp_db->close(tmp_db, 0); + + init_entries(); + + return true; +} + +bool ChewingLargeTable2::store_db(const char * new_filename) { + DB * tmp_db = NULL; + + int ret = unlink(new_filename); + if (ret != 0 && errno != ENOENT) + return false; + + ret = db_create(&tmp_db, NULL, 0); + assert(0 == ret); + + if (NULL == tmp_db) + return false; + + ret = tmp_db->open(tmp_db, NULL, new_filename, NULL, + DB_BTREE, DB_CREATE, 0600); + if (ret != 0) + return false; + + if (!copy_bdb(m_db, tmp_db)) + return false; + + if (tmp_db != NULL) { + tmp_db->sync(m_db, 0); + tmp_db->close(tmp_db, 0); + } + + return true; +} + }; |