diff options
author | Peng Wu <alexepico@gmail.com> | 2016-03-16 11:19:33 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2016-03-16 11:19:33 +0800 |
commit | e0808dcd36761dfca7f363cf139f073d1d94db6a (patch) | |
tree | ed9b99db63bfa26caa9e122d8ac40f0760d352b0 | |
parent | 5488e7375d6c9bb55eb69e32246192f01bde3e61 (diff) | |
download | libpinyin-e0808dcd36761dfca7f363cf139f073d1d94db6a.tar.gz libpinyin-e0808dcd36761dfca7f363cf139f073d1d94db6a.tar.xz libpinyin-e0808dcd36761dfca7f363cf139f073d1d94db6a.zip |
write remove_index method
-rw-r--r-- | src/storage/chewing_large_table2_kyotodb.cpp | 71 | ||||
-rw-r--r-- | src/storage/phrase_large_table3_kyotodb.cpp | 2 |
2 files changed, 72 insertions, 1 deletions
diff --git a/src/storage/chewing_large_table2_kyotodb.cpp b/src/storage/chewing_large_table2_kyotodb.cpp index a2f58e2..8d7da13 100644 --- a/src/storage/chewing_large_table2_kyotodb.cpp +++ b/src/storage/chewing_large_table2_kyotodb.cpp @@ -274,4 +274,75 @@ int ChewingLargeTable2::add_index_internal(int phrase_length, return ERROR_FILE_CORRUPTION; } +template<int phrase_length> +int ChewingLargeTable2::remove_index_internal(/* in */ const ChewingKey index[], + /* in */ const ChewingKey keys[], + /* in */ phrase_token_t token) { + ChewingTableEntry<phrase_length> * entry = + (ChewingTableEntry<phrase_length> *) + g_ptr_array_index(m_entries, phrase_length); + assert(NULL != entry); + + const char * kbuf = (char *) index; + const size_t ksiz = phrase_length * sizeof(ChewingKey); + char * vbuf = NULL; + int32_t vsiz = m_db->check(kbuf, ksiz); + if (vsiz < (signed) sizeof(phrase_token_t)) + return ERROR_REMOVE_ITEM_DONOT_EXISTS; + + /* contains at least one index item. */ + entry->m_chunk.set_size(vsiz); + /* m_chunk may re-allocate here. */ + vbuf = (char *) entry->m_chunk.begin(); + assert(vsiz == m_db->get(kbuf, ksiz, vbuf, vsiz)); + + int result = entry->remove_index(keys, token); + if (ERROR_OK != result) + return result; + + /* for safety. */ + vbuf = (char *) entry->m_chunk.begin(); + vsiz = entry->m_chunk.size(); + + if (!m_db->set(kbuf, ksiz, vbuf, vsiz)) + return ERROR_FILE_CORRUPTION; + + return ERROR_OK; +} + +int ChewingLargeTable2::remove_index_internal(int phrase_length, + /* in */ const ChewingKey index[], + /* in */ const ChewingKey keys[], + /* in */ phrase_token_t token) { +#define CASE(len) case len: \ + { \ + return remove_index_internal<len>(index, keys, token); \ + } + + 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 ERROR_FILE_CORRUPTION; +} + }; diff --git a/src/storage/phrase_large_table3_kyotodb.cpp b/src/storage/phrase_large_table3_kyotodb.cpp index e0f8c55..bd02bf0 100644 --- a/src/storage/phrase_large_table3_kyotodb.cpp +++ b/src/storage/phrase_large_table3_kyotodb.cpp @@ -215,7 +215,7 @@ int PhraseLargeTable3::remove_index(int phrase_length, const size_t ksiz = phrase_length * sizeof(ucs4_t); char * vbuf = NULL; int32_t vsiz = m_db->check(kbuf, ksiz); - if (vsiz < (signed) sizeof(ucs4_t)) + if (vsiz < (signed) sizeof(phrase_token_t)) return ERROR_REMOVE_ITEM_DONOT_EXISTS; /* contains at least one token. */ |