summaryrefslogtreecommitdiffstats
path: root/src/storage
diff options
context:
space:
mode:
authorPeng Wu <alexepico@gmail.com>2016-03-23 13:57:11 +0800
committerPeng Wu <alexepico@gmail.com>2016-03-23 16:18:45 +0800
commitd563bccd2f70d58ccdf8522d740e72772205c6d7 (patch)
tree5cbbd3dcafb2080de8c3f80d023d542bb4b7dee2 /src/storage
parent041ed16d24ad87b45db861681be9bce742e974f6 (diff)
downloadlibpinyin-d563bccd2f70d58ccdf8522d740e72772205c6d7.tar.gz
libpinyin-d563bccd2f70d58ccdf8522d740e72772205c6d7.tar.xz
libpinyin-d563bccd2f70d58ccdf8522d740e72772205c6d7.zip
update class FacadePhraseTable2
Diffstat (limited to 'src/storage')
-rw-r--r--src/storage/facade_phrase_table2.h62
1 files changed, 50 insertions, 12 deletions
diff --git a/src/storage/facade_phrase_table2.h b/src/storage/facade_phrase_table2.h
index 3ef1c37..7a4053e 100644
--- a/src/storage/facade_phrase_table2.h
+++ b/src/storage/facade_phrase_table2.h
@@ -74,40 +74,78 @@ public:
/**
* FacadePhraseTable2::load:
- * @system: the memory chunk of the system phrase table.
- * @user: the memory chunk of the user phrase table.
+ * @system_filename: the file name of the system phrase table.
+ * @user_filename: the file name of the user phrase table.
* @returns: whether the load operation is successful.
*
- * Load the system or user phrase table from the memory chunks.
+ * Load the system or user phrase table from the files.
*
*/
- bool load(MemoryChunk * system, MemoryChunk * user) {
+ bool load(const char * system_filename, const char * user_filename) {
reset();
+ MemoryChunk * chunk = NULL;
+
bool result = false;
- if (system) {
+
+ /* load system phrase table */
+ if (system_filename) {
+ chunk = new MemoryChunk;
+
+#ifdef LIBPINYIN_USE_MMAP
+ if (!chunk->mmap(system_filename)) {
+ fprintf(stderr, "mmap %s failed!\n", system_filename);
+ return false;
+ }
+#else
+ if (!chunk->load(system_filename)) {
+ fprintf(stderr, "open %s failed!\n", system_filename);
+ return false;
+ }
+#endif
+
m_system_phrase_table = new PhraseLargeTable2;
- result = m_system_phrase_table->load(system) || result;
+ result = m_system_phrase_table->load(chunk) || result;
+ chunk = NULL;
}
- if (user) {
+
+ /* load user phrase table */
+ if (user_filename) {
+ chunk = new MemoryChunk;
+
+ if (!chunk->load(user_filename)) {
+ /* hack here: use local Phrase Table to create empty memory chunk. */
+ PhraseLargeTable2 table;
+ table.store(chunk);
+ }
+
m_user_phrase_table = new PhraseLargeTable2;
- result = m_user_phrase_table->load(user) || result;
+ result = m_user_phrase_table->load(chunk) || result;
}
+
return result;
}
/**
* FacadePhraseTable2::store:
- * @new_user: the memory chunk to store the user phrase table.
+ * @new_user_filename: the file name to store the user phrase table.
* @returns: whether the store operation is successful.
*
- * Store the user phrase table to the memory chunk.
+ * Store the user phrase table to the file.
*
*/
- bool store(MemoryChunk * new_user) {
+ bool store(const char * new_user_filename) {
if (NULL == m_user_phrase_table)
return false;
- return m_user_phrase_table->store(new_user);
+
+ /* save user phrase table */
+ MemoryChunk * chunk = new MemoryChunk;
+ bool retval = m_user_phrase_table->store(chunk);
+ assert(retval);
+
+ retval = chunk->save(new_user_filename);
+ delete chunk;
+ return retval;
}
/**