summaryrefslogtreecommitdiffstats
path: root/src/storage/chewing_large_table2_bdb.cpp
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-03-08 15:55:09 +0800
committerPeng Wu <alexepico@gmail.com>2016-03-08 15:55:09 +0800
commit026f53bd34ba93050178b36fc4bdde2a7528b5cc (patch)
treea9606905aa7d5e3e1744aa6be31fd86396865681 /src/storage/chewing_large_table2_bdb.cpp
parent5d16830d7e1ce3df548bca0017a4deaccdc8cfb0 (diff)
downloadlibpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.tar.gz
libpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.tar.xz
libpinyin-026f53bd34ba93050178b36fc4bdde2a7528b5cc.zip
write search method
Diffstat (limited to 'src/storage/chewing_large_table2_bdb.cpp')
-rw-r--r--src/storage/chewing_large_table2_bdb.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/storage/chewing_large_table2_bdb.cpp b/src/storage/chewing_large_table2_bdb.cpp
index fb15eb2..877af40 100644
--- a/src/storage/chewing_large_table2_bdb.cpp
+++ b/src/storage/chewing_large_table2_bdb.cpp
@@ -215,4 +215,88 @@ bool ChewingLargeTable2::store_db(const char * new_filename) {
return true;
}
+template<size_t phrase_length>
+int ChewingLargeTable2::search_internal(/* in */ const ChewingKey keys[],
+ /* out */ PhraseIndexRanges ranges) const {
+ int result = SEARCH_NONE;
+
+ if (NULL == m_db)
+ return result;
+
+ ChewingTableEntry<phrase_length> * entry =
+ (ChewingTableEntry<phrase_length> *)
+ g_ptr_array_index(m_entries, phrase_length);
+ assert(NULL != entry);
+
+ DBT db_key;
+ memset(&db_key, 0, sizeof(DBT));
+ db_key.data = (void *) keys;
+ db_key.size = phrase_length * sizeof(ChewingKey);
+
+ DBT db_data;
+ memset(&db_data, 0, sizeof(DBT));
+ int ret = m_db->get(m_db, NULL, &db_key, &db_data, 0);
+ if (ret != 0)
+ return result;
+
+ /* continue searching. */
+ result |= SEARCH_CONTINUED;
+
+ entry->m_chunk.set_chunk(db_data.data, db_data.size, NULL);
+
+ result = entry->search(keys, ranges) | result;
+
+ return result;
+}
+
+
+int ChewingLargeTable2::search_internal(int phrase_length,
+ /* in */ const ChewingKey keys[],
+ /* out */ PhraseIndexRanges ranges) const {
+#define CASE(len) case len: \
+ { \
+ return search_internal<len>(keys, ranges); \
+ }
+
+ switch(phrase_length) {
+ 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
+
+ return SEARCH_NONE;
+}
+
+/* search method */
+int ChewingLargeTable2::search(int phrase_length,
+ /* in */ const ChewingKey keys[],
+ /* out */ PhraseIndexRanges ranges) {
+ if (contains_incomplete_pinyin(keys, phrase_length)) {
+ compute_incomplete_chewing_index(keys, m_cache_index, phrase_length);
+ return search_internal(phrase_length, m_cache_index, ranges);
+ } else {
+ compute_chewing_index(keys, m_cache_index, phrase_length);
+ return search_internal(phrase_length, m_cache_index, ranges);
+ }
+
+ return SEARCH_NONE;
+}
+
};