diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-14 13:58:50 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-14 13:58:50 +0800 |
commit | 3fb3c2825a2ee43f88b4ed9a5ec50663d8fcf8cd (patch) | |
tree | ece11d6901d71874930a8c355acf1649056af83d /src/storage/chewing_large_table2_kyotodb.cpp | |
parent | c8068ec8520d3ab1f34b7e55c9238827d8d56a68 (diff) | |
download | libpinyin-3fb3c2825a2ee43f88b4ed9a5ec50663d8fcf8cd.tar.gz libpinyin-3fb3c2825a2ee43f88b4ed9a5ec50663d8fcf8cd.tar.xz libpinyin-3fb3c2825a2ee43f88b4ed9a5ec50663d8fcf8cd.zip |
write class ChewingLargeTable2 in progress
Diffstat (limited to 'src/storage/chewing_large_table2_kyotodb.cpp')
-rw-r--r-- | src/storage/chewing_large_table2_kyotodb.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/storage/chewing_large_table2_kyotodb.cpp b/src/storage/chewing_large_table2_kyotodb.cpp index 83a9ab4..596edd6 100644 --- a/src/storage/chewing_large_table2_kyotodb.cpp +++ b/src/storage/chewing_large_table2_kyotodb.cpp @@ -20,6 +20,92 @@ */ #include "chewing_large_table2.h" +#include <kchashdb.h> +#include <kcprotodb.h> +#include "kyotodb_utils.h" + +using namespace kyotocabinet; namespace pinyin{ + +ChewingLargeTable2::ChewingLargeTable2() { + /* create in-memory db. */ + m_db = new ProtoTreeDB; + assert(m_db->open("-", BasicDB::OREADER|BasicDB::OWRITER|BasicDB::OCREATE)); + + init_entries(); +} + +void ChewingLargeTable2::reset() { + if (m_db) { + m_db->synchronize(); + m_db->close(); + delete m_db; + m_db = NULL; + } + + fini_entries(); +} + +/* attach method */ +bool ChewingLargeTable2::attach(const char * dbfile, guint32 flags) { + reset(); + + uint32_t mode = attach_options(flags); + + if (!dbfile) + return false; + + m_db = new TreeDB; + init_entries(); + + return m_db->open(dbfile, mode); +} + +/* load/store method */ +/* use in-memory DBM here, for better performance. */ +bool ChewingLargeTable2::load_db(const char * filename) { + reset(); + + /* create in-memory db. */ + m_db = new ProtoTreeDB; + + if (!m_db->open("-", BasicDB::OREADER|BasicDB::OWRITER|BasicDB::OCREATE)) + return false; + + /* load db into memory. */ + BasicDB * tmp_db = new TreeDB; + if (!tmp_db->open(filename, BasicDB::OREADER)) + return false; + + CopyVisitor visitor(m_db); + tmp_db->iterate(&visitor, false); + + tmp_db->close(); + delete tmp_db; + + init_entries(); + + return true; +} + +bool ChewingLargeTable2::store_db(const char * new_filename) { + int ret = unlink(new_filename); + if ( ret != 0 && errno != ENOENT) + return false; + + BasicDB * tmp_db = new TreeDB; + if (!tmp_db->open(new_filename, BasicDB::OWRITER|BasicDB::OCREATE)) + return false; + + CopyVisitor visitor(tmp_db); + m_db->iterate(&visitor, false); + + tmp_db->synchronize(); + tmp_db->close(); + delete tmp_db; + + return true; +} + }; |