diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-07 15:31:49 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-07 15:31:49 +0800 |
commit | 7b8d727d6048affa0e934e821007c5198be4b332 (patch) | |
tree | 4ae5e6c04db310ddc1b027d0cb3979de568076dc /src/storage/chewing_large_table2_bdb.cpp | |
parent | 5e8cd71e7c7238a37321941a8dd7dc5c31d087bc (diff) | |
download | libpinyin-7b8d727d6048affa0e934e821007c5198be4b332.tar.gz libpinyin-7b8d727d6048affa0e934e821007c5198be4b332.tar.xz libpinyin-7b8d727d6048affa0e934e821007c5198be4b332.zip |
begin to write chewing_large_table2_bdb.cpp
Diffstat (limited to 'src/storage/chewing_large_table2_bdb.cpp')
-rw-r--r-- | src/storage/chewing_large_table2_bdb.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/src/storage/chewing_large_table2_bdb.cpp b/src/storage/chewing_large_table2_bdb.cpp index a00d12d..1af3af2 100644 --- a/src/storage/chewing_large_table2_bdb.cpp +++ b/src/storage/chewing_large_table2_bdb.cpp @@ -21,3 +21,109 @@ #include "chewing_large_table2.h" #include "bdb_utils.h" + +namespace pinyin{ + +ChewingLargeTable2::ChewingLargeTable2() { + /* create in-memory db. */ + m_db = NULL; + 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); + assert(0 == ret); + + m_entries = NULL; + init_entries(); +} + +void ChewingLargeTable2::init_entries() { + assert(NULL == m_entries); + + m_entries = g_ptr_array_new(); + /* NULL for the first pointer. */ + g_ptr_array_set_size(m_entries, MAX_PHRASE_LENGTH + 1); + +#define CASE(len) case len: \ + { \ + ChewingTableEntry<len> * entry = \ + new ChewingTableEntry<len>; \ + g_ptr_array_index(m_entries, len) = entry; \ + } + + for (size_t i = 1; i < m_entries->len; i++) { + switch(i) { + CASE(1); + CASE(2); + CASE(3); + CASE(4); + CASE(5); + CASE(6); + CASE(7); + CASE(8); + CASE(9); + CASE(10); + CASE(11); + CASE(12); + CASE(13); + CASE(14); + CASE(15); + CASE(16); + default: + assert(false); + } + } + +#undef CASE +} + +void ChewingLargeTable2::reset() { + if (m_db) { + m_db->sync(m_db, 0); + m_db->close(m_db, 0); + m_db = NULL; + } + +#define CASE(len) case len: \ + { \ + ChewingTableEntry<len> * entry = \ + (ChewingTableEntry<len> *) \ + g_ptr_array_index(m_entries, len); \ + delete entry; \ + } + + if (m_entries) { + assert(MAX_PHRASE_LENGTH + 1 == m_entries->len); + + for (size_t i = 1; i < m_entries->len; i++) { + switch(i) { + CASE(1); + CASE(2); + CASE(3); + CASE(4); + CASE(5); + CASE(6); + CASE(7); + CASE(8); + CASE(9); + CASE(10); + CASE(11); + CASE(12); + CASE(13); + CASE(14); + CASE(15); + CASE(16); + default: + assert(false); + } + } + + g_ptr_array_free(m_entries, TRUE); + m_entries = NULL; + } + +#undef CASE +} + +}; |