diff options
author | Peng Wu <alexepico@gmail.com> | 2022-07-15 11:13:50 +0800 |
---|---|---|
committer | Peng Wu <alexepico@gmail.com> | 2022-07-19 14:49:07 +0800 |
commit | bd0414a0eb1775c9e8831191f2474721b0e4d802 (patch) | |
tree | 8469386a662e7887b97ffc8f7712d7594aef23ff | |
parent | 2323c59d337ca56c585a8b58b855e84938ecfc27 (diff) | |
download | libpinyin-bd0414a0eb1775c9e8831191f2474721b0e4d802.tar.gz |
Write compare_phrase function
-rw-r--r-- | src/storage/phrase_large_table3_bdb.cpp | 39 | ||||
-rw-r--r-- | src/storage/phrase_large_table3_kyotodb.cpp | 37 |
2 files changed, 76 insertions, 0 deletions
diff --git a/src/storage/phrase_large_table3_bdb.cpp b/src/storage/phrase_large_table3_bdb.cpp index cb2847e..1576ca2 100644 --- a/src/storage/phrase_large_table3_bdb.cpp +++ b/src/storage/phrase_large_table3_bdb.cpp @@ -24,6 +24,45 @@ namespace pinyin{ +/* keep the following function synced between dbm implementations + for consistent phrase key compare. */ + +inline int compare_phrase(ucs4_t * lhs, ucs4_t * rhs, int phrase_length) { + int result; + for (int i = 0; i < phrase_length; ++i) { + result = lhs[i] - rhs[i]; + if (0 != result) + return result; + } + + return 0; +} + +/* keep dbm key compare function inside the corresponding dbm file + to get more flexibility. */ + +static bool bdb_phrase_continue_search(DB *db, + const DBT *dbt1, + const DBT *dbt2) { + ucs4_t * lhs_phrase = (ucs4_t *) dbt1->data; + int lhs_phrase_length = dbt1->size / sizeof(ucs4_t); + ucs4_t * rhs_phrase = (ucs4_t *) dbt2->data; + int rhs_phrase_length = dbt2->size / sizeof(ucs4_t); + + /* The key in dbm is longer than the key in application. */ + if (lhs_phrase_length >= rhs_phrase_length) + return false; + + int min_phrase_length = lhs_phrase_length; + + int result = compare_phrase (lhs_phrase, rhs_phrase, min_phrase_length); + if (0 != result) + return false; + + /* continue the longer phrase search. */ + return true; +} + PhraseLargeTable3::PhraseLargeTable3() { /* create in-memory db. */ m_db = NULL; diff --git a/src/storage/phrase_large_table3_kyotodb.cpp b/src/storage/phrase_large_table3_kyotodb.cpp index 571f97c..c8ce32c 100644 --- a/src/storage/phrase_large_table3_kyotodb.cpp +++ b/src/storage/phrase_large_table3_kyotodb.cpp @@ -28,6 +28,43 @@ using namespace kyotocabinet; namespace pinyin{ +/* keep the following function synced between dbm implementations + for consistent phrase key compare. */ +inline int compare_phrase(ucs4_t * lhs, ucs4_t * rhs, int phrase_length) { + int result; + for (int i = 0; i < phrase_length; ++i) { + result = lhs[i] - rhs[i]; + if (0 != result) + return result; + } + + return 0; +} + +/* keep dbm key compare function inside the corresponding dbm file + to get more flexibility. */ + +bool kyotodb_phrase_continue_search(const char* akbuf, size_t aksiz, + const char* bkbuf, size_t bksiz) { + ucs4_t * lhs_phrase = akbuf; + int lhs_phrase_length = aksiz / sizeof(ucs4_t); + ucs4_t * rhs_phrase = bkbuf; + int rhs_phrase_length = bkbuf / sizeof(ucs4_t); + + /* The key in dbm is longer than the key in application. */ + if (lhs_phrase_length >= rhs_phrase_length) + return false; + + int min_phrase_length = lhs_phrase_length; + + int result = compare_phrase (lhs_phrase, rhs_phrase, min_phrase_length); + if (0 != result) + return false; + + /* continue the longer phrase search. */ + return true; +} + PhraseLargeTable3::PhraseLargeTable3() { /* create in-memory db. */ m_db = new ProtoTreeDB; |