diff options
Diffstat (limited to 'native/jni/src')
17 files changed, 143 insertions, 91 deletions
diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.cpp index dfb110cdd..c81c61d23 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.cpp @@ -16,11 +16,13 @@ #include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h" +#include <climits> #include <stdint.h> #include "defines.h" #include "suggest/policyimpl/dictionary/structure/v2/patricia_trie_policy.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.h" +#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h" #include "suggest/policyimpl/dictionary/utils/file_utils.h" #include "suggest/policyimpl/dictionary/utils/format_utils.h" @@ -32,9 +34,28 @@ namespace latinime { DictionaryStructureWithBufferPolicyFactory ::newDictionaryStructureWithBufferPolicy(const char *const path, const int bufOffset, const int size, const bool isUpdatable) { - // Allocated buffer in MmapedBuffer::newBuffer() will be freed in the destructor of - // MmappedBufferWrapper if the instance has the responsibility. - MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset, size, + if (FileUtils::existsDir(path)) { + // Given path represents a directory. + return newPolicyforDirectoryDict(path, isUpdatable); + } else { + if (isUpdatable) { + AKLOGE("One file dictionaries don't support updating. path: %s", path); + ASSERT(false); + return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); + } + return newPolicyforFileDict(path, bufOffset, size); + } +} + +/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr + DictionaryStructureWithBufferPolicyFactory::newPolicyforDirectoryDict( + const char *const path, const bool isUpdatable) { + const int headerFilePathBufSize = PATH_MAX + 1 /* terminator */; + char headerFilePath[headerFilePathBufSize]; + getHeaderFilePathInDictDir(path, headerFilePathBufSize, headerFilePath); + // Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of + // MmappedBufferPtr if the instance has the responsibility. + MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(headerFilePath, isUpdatable); if (!mmappedBuffer.get()) { return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); @@ -42,20 +63,22 @@ namespace latinime { switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(), mmappedBuffer.get()->getBufferSize())) { case FormatUtils::VERSION_2: - return DictionaryStructureWithBufferPolicy::StructurePolicyPtr( - new PatriciaTriePolicy(mmappedBuffer)); + AKLOGE("Given path is a directory but the format is version 2. path: %s", path); + break; case FormatUtils::VERSION_4: { - const int dictDirPathBufSize = strlen(path) + 1 /* terminator */; - char dictDirPath[dictDirPathBufSize]; - if (!FileUtils::getFilePathWithoutSuffix(path, Ver4DictConstants::HEADER_FILE_EXTENSION, - dictDirPathBufSize, dictDirPath)) { - // Dictionary file name is not valid as a version 4 dictionary. + const int dictDirPathBufSize = strlen(headerFilePath) + 1 /* terminator */; + char dictPath[dictDirPathBufSize]; + if (!FileUtils::getFilePathWithoutSuffix(headerFilePath, + Ver4DictConstants::HEADER_FILE_EXTENSION, dictDirPathBufSize, dictPath)) { + AKLOGE("Dictionary file name is not valid as a ver4 dictionary. path: %s", path); + ASSERT(false); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); } const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers = - Ver4DictBuffers::openVer4DictBuffers(dictDirPath, mmappedBuffer); + Ver4DictBuffers::openVer4DictBuffers(dictPath, mmappedBuffer); if (!dictBuffers.get()->isValid()) { - AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements."); + AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements. path: %s", + path); ASSERT(false); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); } @@ -63,10 +86,47 @@ namespace latinime { new Ver4PatriciaTriePolicy(dictBuffers)); } default: - AKLOGE("DICT: dictionary format is unknown, bad magic number"); - ASSERT(false); - return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); + AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path); + break; } + ASSERT(false); + return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); +} + +/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr + DictionaryStructureWithBufferPolicyFactory::newPolicyforFileDict( + const char *const path, const int bufOffset, const int size) { + // Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of + // MmappedBufferPtr if the instance has the responsibility. + MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset, + size, false /* isUpdatable */); + if (!mmappedBuffer.get()) { + return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); + } + switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(), + mmappedBuffer.get()->getBufferSize())) { + case FormatUtils::VERSION_2: + return DictionaryStructureWithBufferPolicy::StructurePolicyPtr( + new PatriciaTriePolicy(mmappedBuffer)); + case FormatUtils::VERSION_4: + AKLOGE("Given path is a file but the format is version 4. path: %s", path); + break; + default: + AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path); + break; + } + ASSERT(false); + return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0); +} + +/* static */ void DictionaryStructureWithBufferPolicyFactory::getHeaderFilePathInDictDir( + const char *const dictDirPath, const int outHeaderFileBufSize, + char *const outHeaderFilePath) { + const int dictNameBufSize = strlen(dictDirPath) + 1 /* terminator */; + char dictName[dictNameBufSize]; + FileUtils::getBasename(dictDirPath, dictNameBufSize, dictName); + snprintf(outHeaderFilePath, outHeaderFileBufSize, "%s/%s%s", dictDirPath, + dictName, Ver4DictConstants::HEADER_FILE_EXTENSION); } } // namespace latinime diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h b/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h index 45237e4aa..45ab52931 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h @@ -33,6 +33,15 @@ class DictionaryStructureWithBufferPolicyFactory { private: DISALLOW_IMPLICIT_CONSTRUCTORS(DictionaryStructureWithBufferPolicyFactory); + + static DictionaryStructureWithBufferPolicy::StructurePolicyPtr + newPolicyforDirectoryDict(const char *const path, const bool isUpdatable); + + static DictionaryStructureWithBufferPolicy::StructurePolicyPtr + newPolicyforFileDict(const char *const path, const int bufOffset, const int size); + + static void getHeaderFilePathInDictDir(const char *const dirPath, + const int outHeaderFileBufSize, char *const outHeaderFilePath); }; } // namespace latinime #endif // LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/bigram_dict_content.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/bigram_dict_content.h index 95ee74f99..ba2a05209 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/bigram_dict_content.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/bigram_dict_content.h @@ -27,9 +27,9 @@ namespace latinime { class BigramDictContent : public SparseTableDictContent { public: - BigramDictContent(const char *const dictDirPath, const bool hasHistoricalInfo, + BigramDictContent(const char *const dictPath, const bool hasHistoricalInfo, const bool isUpdatable) - : SparseTableDictContent(dictDirPath, + : SparseTableDictContent(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_FILE_EXTENSION, isUpdatable, @@ -73,8 +73,8 @@ class BigramDictContent : public SparseTableDictContent { bool copyBigramList(const int bigramListPos, const int toPos); - bool flushToFile(const char *const dictBasePath) const { - return flush(dictBasePath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION, + bool flushToFile(const char *const dictPath) const { + return flush(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION, Ver4DictConstants::BIGRAM_FILE_EXTENSION); } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.cpp index 749e3fe8c..3b7c70efd 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.cpp @@ -71,7 +71,7 @@ bool ProbabilityDictContent::setProbabilityEntry(const int terminalId, return writeEntry(probabilityEntry, entryPos); } -bool ProbabilityDictContent::flushToFile(const char *const dictBasePath) const { +bool ProbabilityDictContent::flushToFile(const char *const dictPath) const { if (getEntryPos(mSize) < getBuffer()->getTailPosition()) { ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo); for (int i = 0; i < mSize; ++i) { @@ -81,10 +81,10 @@ bool ProbabilityDictContent::flushToFile(const char *const dictBasePath) const { return false; } } - return probabilityDictContentToWrite.flush(dictBasePath, + return probabilityDictContentToWrite.flush(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION); } else { - return flush(dictBasePath, Ver4DictConstants::FREQ_FILE_EXTENSION); + return flush(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION); } } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.h index db96f9082..b065bc954 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/probability_dict_content.h @@ -29,9 +29,9 @@ class ProbabilityEntry; class ProbabilityDictContent : public SingleDictContent { public: - ProbabilityDictContent(const char *const dictDirPath, const bool hasHistoricalInfo, + ProbabilityDictContent(const char *const dictPath, const bool hasHistoricalInfo, const bool isUpdatable) - : SingleDictContent(dictDirPath, Ver4DictConstants::FREQ_FILE_EXTENSION, isUpdatable), + : SingleDictContent(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION, isUpdatable), mHasHistoricalInfo(hasHistoricalInfo), mSize(getBuffer()->getTailPosition() / getEntrySize()) {} @@ -42,7 +42,7 @@ class ProbabilityDictContent : public SingleDictContent { bool setProbabilityEntry(const int terminalId, const ProbabilityEntry *const probabilityEntry); - bool flushToFile(const char *const dictDirPath) const; + bool flushToFile(const char *const dictPath) const; bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap, const ProbabilityDictContent *const originalProbabilityDictContent); diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.cpp index 555217837..29972a4e8 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.cpp @@ -46,8 +46,8 @@ int ShortcutDictContent::getShortcutListHeadPos(const int terminalId) const { return addressLookupTable->get(terminalId); } -bool ShortcutDictContent::flushToFile(const char *const dictBasePath) const { - return flush(dictBasePath, Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION, +bool ShortcutDictContent::flushToFile(const char *const dictPath) const { + return flush(dictPath, Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION, Ver4DictConstants::SHORTCUT_CONTENT_TABLE_FILE_EXTENSION, Ver4DictConstants::SHORTCUT_FILE_EXTENSION); } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.h index a52214ca2..eaafc27bc 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/shortcut_dict_content.h @@ -26,8 +26,8 @@ namespace latinime { class ShortcutDictContent : public SparseTableDictContent { public: - ShortcutDictContent(const char *const dictDirPath, const bool isUpdatable) - : SparseTableDictContent(dictDirPath, + ShortcutDictContent(const char *const dictPath, const bool isUpdatable) + : SparseTableDictContent(dictPath, Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION, Ver4DictConstants::SHORTCUT_CONTENT_TABLE_FILE_EXTENSION, Ver4DictConstants::SHORTCUT_FILE_EXTENSION, isUpdatable, @@ -53,7 +53,7 @@ class ShortcutDictContent : public SparseTableDictContent { // Returns head position of shortcut list for a PtNode specified by terminalId. int getShortcutListHeadPos(const int terminalId) const; - bool flushToFile(const char *const dictBasePath) const; + bool flushToFile(const char *const dictPath) const; bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap, const ShortcutDictContent *const originalShortcutDictContent); diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h index d8eedf36e..9064b7e72 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/single_dict_content.h @@ -28,9 +28,9 @@ namespace latinime { class SingleDictContent : public DictContent { public: - SingleDictContent(const char *const dictDirPath, const char *const contentFileName, + SingleDictContent(const char *const dictPath, const char *const contentFileName, const bool isUpdatable) - : mMmappedBuffer(MmappedBuffer::openBuffer(dictDirPath, contentFileName, isUpdatable)), + : mMmappedBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)), mExpandableContentBuffer(mMmappedBuffer.get() ? mMmappedBuffer.get()->getBuffer() : 0, mMmappedBuffer.get() ? mMmappedBuffer.get()->getBufferSize() : 0, BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), @@ -59,8 +59,8 @@ class SingleDictContent : public DictContent { return &mExpandableContentBuffer; } - bool flush(const char *const dictBasePath, const char *const contentFileNameSuffix) const { - return DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, + bool flush(const char *const dictPath, const char *const contentFileNameSuffix) const { + return DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, contentFileNameSuffix, &mExpandableContentBuffer); } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.cpp index abb7d5fd2..63c6ea3a4 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.cpp @@ -18,18 +18,18 @@ namespace latinime { -bool SparseTableDictContent::flush(const char *const dictBasePath, +bool SparseTableDictContent::flush(const char *const dictPath, const char *const lookupTableFileNameSuffix, const char *const addressTableFileNameSuffix, const char *const contentFileNameSuffix) const { - if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, lookupTableFileNameSuffix, + if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, lookupTableFileNameSuffix, &mExpandableLookupTableBuffer)){ return false; } - if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, addressTableFileNameSuffix, + if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, addressTableFileNameSuffix, &mExpandableAddressTableBuffer)) { return false; } - if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, contentFileNameSuffix, + if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, contentFileNameSuffix, &mExpandableContentBuffer)) { return false; } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h index 9a4f1e1c0..a82e3f50a 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/sparse_table_dict_content.h @@ -30,15 +30,15 @@ namespace latinime { // TODO: Support multiple contents. class SparseTableDictContent : public DictContent { public: - AK_FORCE_INLINE SparseTableDictContent(const char *const dictDirPath, + AK_FORCE_INLINE SparseTableDictContent(const char *const dictPath, const char *const lookupTableFileName, const char *const addressTableFileName, const char *const contentFileName, const bool isUpdatable, const int sparseTableBlockSize, const int sparseTableDataSize) : mLookupTableBuffer( - MmappedBuffer::openBuffer(dictDirPath, lookupTableFileName, isUpdatable)), + MmappedBuffer::openBuffer(dictPath, lookupTableFileName, isUpdatable)), mAddressTableBuffer( - MmappedBuffer::openBuffer(dictDirPath, addressTableFileName, isUpdatable)), - mContentBuffer(MmappedBuffer::openBuffer(dictDirPath, contentFileName, isUpdatable)), + MmappedBuffer::openBuffer(dictPath, addressTableFileName, isUpdatable)), + mContentBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)), mExpandableLookupTableBuffer( mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBuffer() : 0, mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBufferSize() : 0, diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp index c889cf5d1..0b17a009d 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.cpp @@ -50,7 +50,7 @@ bool TerminalPositionLookupTable::setTerminalPtNodePosition( Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(terminalId)); } -bool TerminalPositionLookupTable::flushToFile(const char *const dictBasePath) const { +bool TerminalPositionLookupTable::flushToFile(const char *const dictPath) const { // If the used buffer size is smaller than the actual buffer size, regenerate the lookup // table and write the new table to the file. if (getEntryPos(mSize) < getBuffer()->getTailPosition()) { @@ -63,12 +63,12 @@ bool TerminalPositionLookupTable::flushToFile(const char *const dictBasePath) co return false; } } - return lookupTableToWrite.flush(dictBasePath, + return lookupTableToWrite.flush(dictPath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION); } else { // We can simply use this lookup table because the buffer size has not been // changed. - return flush(dictBasePath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION); + return flush(dictPath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION); } } diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h index 5a28f52fd..f73e22754 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/content/terminal_position_lookup_table.h @@ -28,8 +28,8 @@ class TerminalPositionLookupTable : public SingleDictContent { public: typedef hash_map_compat<int, int> TerminalIdMap; - TerminalPositionLookupTable(const char *const dictDirPath, const bool isUpdatable) - : SingleDictContent(dictDirPath, + TerminalPositionLookupTable(const char *const dictPath, const bool isUpdatable) + : SingleDictContent(dictPath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION, isUpdatable), mSize(getBuffer()->getTailPosition() / Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE) {} @@ -44,7 +44,7 @@ class TerminalPositionLookupTable : public SingleDictContent { return mSize; } - bool flushToFile(const char *const dictBasePath) const; + bool flushToFile(const char *const dictPath) const; bool runGCTerminalIds(TerminalIdMap *const terminalIdMap); diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.cpp index e2355407a..918c02ba2 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.cpp @@ -27,10 +27,10 @@ namespace latinime { /* static */ Ver4DictBuffers::Ver4DictBuffersPtr Ver4DictBuffers::openVer4DictBuffers( - const char *const dictDirPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer) { + const char *const dictPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer) { const bool isUpdatable = headerBuffer.get() ? headerBuffer.get()->isUpdatable() : false; // TODO: take only dictDirPath, and open both header and trie files in the constructor below - return Ver4DictBuffersPtr(new Ver4DictBuffers(dictDirPath, headerBuffer, isUpdatable)); + return Ver4DictBuffersPtr(new Ver4DictBuffers(dictPath, headerBuffer, isUpdatable)); } bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath, @@ -57,38 +57,38 @@ bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath, const int dictNameBufSize = strlen(dictDirPath) + 1 /* terminator */; char dictName[dictNameBufSize]; FileUtils::getBasename(dictDirPath, dictNameBufSize, dictName); - const int dictBasePathBufSize = FileUtils::getFilePathBufSize(tmpDirPath, dictName); - char dictBasePath[dictBasePathBufSize]; - FileUtils::getFilePath(tmpDirPath, dictName, dictBasePathBufSize, dictBasePath); + const int dictPathBufSize = FileUtils::getFilePathBufSize(tmpDirPath, dictName); + char dictPath[dictPathBufSize]; + FileUtils::getFilePath(tmpDirPath, dictName, dictPathBufSize, dictPath); // Write header file. - if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, + if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, Ver4DictConstants::HEADER_FILE_EXTENSION, headerBuffer)) { AKLOGE("Dictionary header file %s%s cannot be written.", tmpDirPath, Ver4DictConstants::HEADER_FILE_EXTENSION); return false; } // Write trie file. - if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, + if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, Ver4DictConstants::TRIE_FILE_EXTENSION, &mExpandableTrieBuffer)) { AKLOGE("Dictionary trie file %s%s cannot be written.", tmpDirPath, Ver4DictConstants::TRIE_FILE_EXTENSION); return false; } // Write dictionary contents. - if (!mTerminalPositionLookupTable.flushToFile(dictBasePath)) { + if (!mTerminalPositionLookupTable.flushToFile(dictPath)) { AKLOGE("Terminal position lookup table cannot be written. %s", tmpDirPath); return false; } - if (!mProbabilityDictContent.flushToFile(dictBasePath)) { + if (!mProbabilityDictContent.flushToFile(dictPath)) { AKLOGE("Probability dict content cannot be written. %s", tmpDirPath); return false; } - if (!mBigramDictContent.flushToFile(dictBasePath)) { + if (!mBigramDictContent.flushToFile(dictPath)) { AKLOGE("Bigram dict content cannot be written. %s", tmpDirPath); return false; } - if (!mShortcutDictContent.flushToFile(dictBasePath)) { + if (!mShortcutDictContent.flushToFile(dictPath)) { AKLOGE("Shortcut dict content cannot be written. %s", tmpDirPath); return false; } @@ -107,10 +107,10 @@ bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath, return true; } -Ver4DictBuffers::Ver4DictBuffers(const char *const dictDirPath, +Ver4DictBuffers::Ver4DictBuffers(const char *const dictPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer, const bool isUpdatable) : mHeaderBuffer(headerBuffer), - mDictBuffer(MmappedBuffer::openBuffer(dictDirPath, + mDictBuffer(MmappedBuffer::openBuffer(dictPath, Ver4DictConstants::TRIE_FILE_EXTENSION, isUpdatable)), mHeaderPolicy(headerBuffer.get()->getBuffer(), FormatUtils::VERSION_4), mExpandableHeaderBuffer(headerBuffer.get()->getBuffer(), mHeaderPolicy.getSize(), @@ -118,12 +118,12 @@ Ver4DictBuffers::Ver4DictBuffers(const char *const dictDirPath, mExpandableTrieBuffer(mDictBuffer.get()->getBuffer(), mDictBuffer.get()->getBufferSize(), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), - mTerminalPositionLookupTable(dictDirPath, isUpdatable), - mProbabilityDictContent(dictDirPath, mHeaderPolicy.hasHistoricalInfoOfWords(), + mTerminalPositionLookupTable(dictPath, isUpdatable), + mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable), - mBigramDictContent(dictDirPath, mHeaderPolicy.hasHistoricalInfoOfWords(), + mBigramDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable), - mShortcutDictContent(dictDirPath, isUpdatable), + mShortcutDictContent(dictPath, isUpdatable), mIsUpdatable(isUpdatable) {} Ver4DictBuffers::Ver4DictBuffers(const HeaderPolicy *const headerPolicy) diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp index 25f87efae..43227635c 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.cpp @@ -32,12 +32,9 @@ namespace latinime { -void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const trieFilePath, +void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const dictDirPath, const int unigramCount, const int bigramCount) const { const HeaderPolicy *const headerPolicy = mBuffers->getHeaderPolicy(); - const int dirPathBufSize = strlen(trieFilePath) + 1 /* terminator */; - char dirPath[dirPathBufSize]; - FileUtils::getDirPath(trieFilePath, dirPathBufSize, dirPath); BufferWithExtendableBuffer headerBuffer( BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE); const int extendedRegionSize = headerPolicy->getExtendedRegionSize() @@ -50,11 +47,11 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const trieFilePa extendedRegionSize); return; } - mBuffers->flushHeaderAndDictBuffers(dirPath, &headerBuffer); + mBuffers->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer); } void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeArrayPos, - const char *const trieFilePath) { + const char *const dictDirPath) { const HeaderPolicy *const headerPolicy = mBuffers->getHeaderPolicy(); Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers( Ver4DictBuffers::createVer4DictBuffers(headerPolicy)); @@ -70,10 +67,7 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeAr 0 /* extendedRegionSize */)) { return; } - const int dirPathBufSize = strlen(trieFilePath) + 1 /* terminator */; - char dirPath[dirPathBufSize]; - FileUtils::getDirPath(trieFilePath, dirPathBufSize, dirPath); - dictBuffers.get()->flushHeaderAndDictBuffers(dirPath, &headerBuffer); + dictBuffers.get()->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer); } bool Ver4PatriciaTrieWritingHelper::runGC(const int rootPtNodeArrayPos, diff --git a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.h b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.h index 198c10878..c3a155e0e 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.h +++ b/native/jni/src/suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_writing_helper.h @@ -33,11 +33,10 @@ class Ver4PatriciaTrieWritingHelper { Ver4PatriciaTrieWritingHelper(Ver4DictBuffers *const buffers) : mBuffers(buffers) {} - void writeToDictFile(const char *const trieFilePath, const int unigramCount, + void writeToDictFile(const char *const dictDirPath, const int unigramCount, const int bigramCount) const; - void writeToDictFileWithGC(const int rootPtNodeArrayPos, - const char *const trieFilePath); + void writeToDictFileWithGC(const int rootPtNodeArrayPos, const char *const dictDirPath); private: DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTrieWritingHelper); diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/file_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/file_utils.cpp index 49ae7f156..1f25cfa1e 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/utils/file_utils.cpp +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/file_utils.cpp @@ -147,7 +147,7 @@ namespace latinime { const char *const baseName = basename(filePathBuf); const int baseNameLength = strlen(baseName); if (baseNameLength >= outNameBufSize) { - AKLOGE("outNameBufSize is too small. dirPath: %s, outNameBufSize: %d", + AKLOGE("outNameBufSize is too small. filePath: %s, outNameBufSize: %d", filePath, outNameBufSize); return; } diff --git a/native/jni/src/utils/exclusive_ownership_pointer.h b/native/jni/src/utils/exclusive_ownership_pointer.h index 617b34968..081802e8b 100644 --- a/native/jni/src/utils/exclusive_ownership_pointer.h +++ b/native/jni/src/utils/exclusive_ownership_pointer.h @@ -39,25 +39,15 @@ class ExclusiveOwnershipPointer { deletePointersIfHavingOwnership(); } - // Move the ownership. - AK_FORCE_INLINE ExclusiveOwnershipPointer<T> &operator=( - const ExclusiveOwnershipPointer<T> &pointer) { - // Delete pointers when this is an owner of another pointer. - deletePointersIfHavingOwnership(); - mPointer = pointer.mPointer; - mSharedOwnerPtr = pointer.mSharedOwnerPtr; - transferOwnership(pointer); - return *this; - } - AK_FORCE_INLINE T *get() const { return mPointer; } private: - // This class allows to copy and assign and ensures only one instance has the ownership of the + // This class allows to copy and ensures only one instance has the ownership of the // managed pointer. DISALLOW_DEFAULT_CONSTRUCTOR(ExclusiveOwnershipPointer); + DISALLOW_ASSIGNMENT_OPERATOR(ExclusiveOwnershipPointer); void transferOwnership(const ExclusiveOwnershipPointer<T> *const src) { if (*mSharedOwnerPtr != src) { |