summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-03-07 15:31:49 +0800
committerPeng Wu <alexepico@gmail.com>2016-03-07 15:31:49 +0800
commit7b8d727d6048affa0e934e821007c5198be4b332 (patch)
tree4ae5e6c04db310ddc1b027d0cb3979de568076dc
parent5e8cd71e7c7238a37321941a8dd7dc5c31d087bc (diff)
downloadlibpinyin-7b8d727d6048affa0e934e821007c5198be4b332.tar.gz
libpinyin-7b8d727d6048affa0e934e821007c5198be4b332.tar.xz
libpinyin-7b8d727d6048affa0e934e821007c5198be4b332.zip
begin to write chewing_large_table2_bdb.cpp
-rw-r--r--src/storage/chewing_large_table2_bdb.cpp106
-rw-r--r--src/storage/chewing_large_table2_bdb.h13
2 files changed, 118 insertions, 1 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
+}
+
+};
diff --git a/src/storage/chewing_large_table2_bdb.h b/src/storage/chewing_large_table2_bdb.h
index bcb3886..a05b933 100644
--- a/src/storage/chewing_large_table2_bdb.h
+++ b/src/storage/chewing_large_table2_bdb.h
@@ -37,11 +37,22 @@ private:
DB * m_db;
protected:
- /* Array of ChewingTableEntry. */
+ /* Array of ChewingTableEntry,
+ all elements are always available. */
GPtrArray * m_entries;
+ void init_entries();
+
void reset();
+protected:
+ template<size_t phrase_length>
+ int search_internal(/* in */ const ChewingKey keys[],
+ /* out */ PhraseIndexRanges ranges) const;
+
+ int search_internal(int phrase_length, /* in */ const ChewingKey keys[],
+ /* out */ PhraseIndexRanges ranges) const;
+
public:
ChewingLargeTable2();