Loading native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -250,7 +250,7 @@ void DynamicPatriciaTriePolicy::flush(const char *const filePath) { } DynamicPatriciaTrieWritingHelper writingHelper(&mBufferWithExtendableBuffer, &mBigramListPolicy, &mShortcutListPolicy); writingHelper.writeToDictFile(filePath, mBuffer->getBuffer(), mHeaderPolicy.getSize()); writingHelper.writeToDictFile(filePath, &mHeaderPolicy); } void DynamicPatriciaTriePolicy::flushWithGC(const char *const filePath) { Loading native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h +1 −1 Original line number Diff line number Diff line Loading @@ -33,7 +33,7 @@ class DicNodeVector; class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { public: DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer) : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()), : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer(), buffer->getBufferSize()), mBufferWithExtendableBuffer(mBuffer->getBuffer() + mHeaderPolicy.getSize(), mBuffer->getBufferSize() - mHeaderPolicy.getSize()), mShortcutListPolicy(&mBufferWithExtendableBuffer), Loading native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp +8 −2 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_helper.h" #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h" #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_utils.h" #include "suggest/policyimpl/dictionary/header/header_policy.h" #include "suggest/policyimpl/dictionary/patricia_trie_reading_utils.h" #include "suggest/policyimpl/dictionary/shortcut/dynamic_shortcut_list_policy.h" Loading Loading @@ -137,7 +138,11 @@ bool DynamicPatriciaTrieWritingHelper::removeBigramWords(const int word0Pos, con } void DynamicPatriciaTrieWritingHelper::writeToDictFile(const char *const fileName, const uint8_t *const headerBuf, const int headerSize) { const HeaderPolicy *const headerPolicy) { BufferWithExtendableBuffer headerBuffer(0 /* originalBuffer */, 0 /* originalBufferSize */); if (!headerPolicy->writeHeaderToBuffer(&headerBuffer, false /* updatesLastUpdatedTime */)) { return; } const int tmpFileNameBufSize = strlen(fileName) + strlen(TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE) + 1; char tmpFileName[tmpFileNameBufSize]; Loading @@ -148,7 +153,8 @@ void DynamicPatriciaTrieWritingHelper::writeToDictFile(const char *const fileNam return; } // Write header. if (fwrite(headerBuf, headerSize, 1, file) < 1) { if (fwrite(headerBuffer.getBuffer(true /* usesAdditionalBuffer */), headerBuffer.getTailPosition(), 1, file) < 1) { fclose(file); remove(tmpFileName); return; Loading native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h +4 −2 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ class DynamicBigramListPolicy; class DynamicPatriciaTrieNodeReader; class DynamicPatriciaTrieReadingHelper; class DynamicShortcutListPolicy; class HeaderPolicy; class DynamicPatriciaTrieWritingHelper { public: Loading @@ -48,8 +49,9 @@ class DynamicPatriciaTrieWritingHelper { // Remove a bigram relation from word0Pos to word1Pos. bool removeBigramWords(const int word0Pos, const int word1Pos); void writeToDictFile(const char *const fileName, const uint8_t *const headerBuf, const int headerSize); void writeToDictFile(const char *const fileName, const HeaderPolicy *const headerPolicy); void writeToDictFileWithGC(const char *const fileName, const HeaderPolicy *const headerPolicy); private: DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTrieWritingHelper); Loading native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp +54 −6 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ #include "suggest/policyimpl/dictionary/header/header_policy.h" #include <cstddef> #include <cstdio> #include <ctime> namespace latinime { Loading @@ -36,7 +38,7 @@ void HeaderPolicy::readHeaderValueOrQuestionMark(const char *const key, int *out } std::vector<int> keyCodePointVector; insertCharactersIntoVector(key, &keyCodePointVector); HeaderReadingUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector); HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector); if (it == mAttributeMap.end()) { // The key was not found. outValue[0] = '?'; Loading Loading @@ -85,7 +87,7 @@ int HeaderPolicy::readLastUpdatedTime() const { bool HeaderPolicy::getAttributeValueAsInt(const char *const key, int *const outValue) const { std::vector<int> keyVector; insertCharactersIntoVector(key, &keyVector); HeaderReadingUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyVector); HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyVector); if (it == mAttributeMap.end()) { // The key was not found. return false; Loading @@ -94,10 +96,56 @@ bool HeaderPolicy::getAttributeValueAsInt(const char *const key, int *const outV return true; } /* static */ HeaderReadingUtils::AttributeMap HeaderPolicy::createAttributeMapAndReadAllAttributes( const uint8_t *const dictBuf) { HeaderReadingUtils::AttributeMap attributeMap; HeaderReadingUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap); bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite, const bool updatesLastUpdatedTime) const { int writingPos = 0; if (!HeaderReadWriteUtils::writeDictionaryVersion(bufferToWrite, mDictFormatVersion, &writingPos)) { return false; } if (!HeaderReadWriteUtils::writeDictionaryFlags(bufferToWrite, mDictionaryFlags, &writingPos)) { return false; } // Temporarily writes a dummy header size. int headerSizeFieldPos = writingPos; if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, 0 /* size */, &writingPos)) { return false; } if (updatesLastUpdatedTime) { // Set current time as a last updated time. HeaderReadWriteUtils::AttributeMap attributeMapTowrite(mAttributeMap); std::vector<int> updatedTimekey; insertCharactersIntoVector(LAST_UPDATED_TIME_KEY, &updatedTimekey); const time_t currentTime = time(NULL); std::vector<int> updatedTimeValue; char charBuf[LARGEST_INT_DIGIT_COUNT + 1]; snprintf(charBuf, LARGEST_INT_DIGIT_COUNT + 1, "%ld", currentTime); insertCharactersIntoVector(charBuf, &updatedTimeValue); attributeMapTowrite[updatedTimekey] = updatedTimeValue; if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite, &writingPos)) { return false; } } else { if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &mAttributeMap, &writingPos)) { return false; } } // Writes an actual header size. if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, writingPos, &headerSizeFieldPos)) { return false; } return true; } /* static */ HeaderReadWriteUtils::AttributeMap HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) { HeaderReadWriteUtils::AttributeMap attributeMap; HeaderReadWriteUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap); return attributeMap; } Loading Loading
native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.cpp +1 −1 Original line number Diff line number Diff line Loading @@ -250,7 +250,7 @@ void DynamicPatriciaTriePolicy::flush(const char *const filePath) { } DynamicPatriciaTrieWritingHelper writingHelper(&mBufferWithExtendableBuffer, &mBigramListPolicy, &mShortcutListPolicy); writingHelper.writeToDictFile(filePath, mBuffer->getBuffer(), mHeaderPolicy.getSize()); writingHelper.writeToDictFile(filePath, &mHeaderPolicy); } void DynamicPatriciaTriePolicy::flushWithGC(const char *const filePath) { Loading
native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h +1 −1 Original line number Diff line number Diff line Loading @@ -33,7 +33,7 @@ class DicNodeVector; class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { public: DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer) : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()), : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer(), buffer->getBufferSize()), mBufferWithExtendableBuffer(mBuffer->getBuffer() + mHeaderPolicy.getSize(), mBuffer->getBufferSize() - mHeaderPolicy.getSize()), mShortcutListPolicy(&mBufferWithExtendableBuffer), Loading
native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp +8 −2 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_helper.h" #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_reading_utils.h" #include "suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_utils.h" #include "suggest/policyimpl/dictionary/header/header_policy.h" #include "suggest/policyimpl/dictionary/patricia_trie_reading_utils.h" #include "suggest/policyimpl/dictionary/shortcut/dynamic_shortcut_list_policy.h" Loading Loading @@ -137,7 +138,11 @@ bool DynamicPatriciaTrieWritingHelper::removeBigramWords(const int word0Pos, con } void DynamicPatriciaTrieWritingHelper::writeToDictFile(const char *const fileName, const uint8_t *const headerBuf, const int headerSize) { const HeaderPolicy *const headerPolicy) { BufferWithExtendableBuffer headerBuffer(0 /* originalBuffer */, 0 /* originalBufferSize */); if (!headerPolicy->writeHeaderToBuffer(&headerBuffer, false /* updatesLastUpdatedTime */)) { return; } const int tmpFileNameBufSize = strlen(fileName) + strlen(TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE) + 1; char tmpFileName[tmpFileNameBufSize]; Loading @@ -148,7 +153,8 @@ void DynamicPatriciaTrieWritingHelper::writeToDictFile(const char *const fileNam return; } // Write header. if (fwrite(headerBuf, headerSize, 1, file) < 1) { if (fwrite(headerBuffer.getBuffer(true /* usesAdditionalBuffer */), headerBuffer.getTailPosition(), 1, file) < 1) { fclose(file); remove(tmpFileName); return; Loading
native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h +4 −2 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ class DynamicBigramListPolicy; class DynamicPatriciaTrieNodeReader; class DynamicPatriciaTrieReadingHelper; class DynamicShortcutListPolicy; class HeaderPolicy; class DynamicPatriciaTrieWritingHelper { public: Loading @@ -48,8 +49,9 @@ class DynamicPatriciaTrieWritingHelper { // Remove a bigram relation from word0Pos to word1Pos. bool removeBigramWords(const int word0Pos, const int word1Pos); void writeToDictFile(const char *const fileName, const uint8_t *const headerBuf, const int headerSize); void writeToDictFile(const char *const fileName, const HeaderPolicy *const headerPolicy); void writeToDictFileWithGC(const char *const fileName, const HeaderPolicy *const headerPolicy); private: DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTrieWritingHelper); Loading
native/jni/src/suggest/policyimpl/dictionary/header/header_policy.cpp +54 −6 Original line number Diff line number Diff line Loading @@ -17,6 +17,8 @@ #include "suggest/policyimpl/dictionary/header/header_policy.h" #include <cstddef> #include <cstdio> #include <ctime> namespace latinime { Loading @@ -36,7 +38,7 @@ void HeaderPolicy::readHeaderValueOrQuestionMark(const char *const key, int *out } std::vector<int> keyCodePointVector; insertCharactersIntoVector(key, &keyCodePointVector); HeaderReadingUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector); HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector); if (it == mAttributeMap.end()) { // The key was not found. outValue[0] = '?'; Loading Loading @@ -85,7 +87,7 @@ int HeaderPolicy::readLastUpdatedTime() const { bool HeaderPolicy::getAttributeValueAsInt(const char *const key, int *const outValue) const { std::vector<int> keyVector; insertCharactersIntoVector(key, &keyVector); HeaderReadingUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyVector); HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyVector); if (it == mAttributeMap.end()) { // The key was not found. return false; Loading @@ -94,10 +96,56 @@ bool HeaderPolicy::getAttributeValueAsInt(const char *const key, int *const outV return true; } /* static */ HeaderReadingUtils::AttributeMap HeaderPolicy::createAttributeMapAndReadAllAttributes( const uint8_t *const dictBuf) { HeaderReadingUtils::AttributeMap attributeMap; HeaderReadingUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap); bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite, const bool updatesLastUpdatedTime) const { int writingPos = 0; if (!HeaderReadWriteUtils::writeDictionaryVersion(bufferToWrite, mDictFormatVersion, &writingPos)) { return false; } if (!HeaderReadWriteUtils::writeDictionaryFlags(bufferToWrite, mDictionaryFlags, &writingPos)) { return false; } // Temporarily writes a dummy header size. int headerSizeFieldPos = writingPos; if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, 0 /* size */, &writingPos)) { return false; } if (updatesLastUpdatedTime) { // Set current time as a last updated time. HeaderReadWriteUtils::AttributeMap attributeMapTowrite(mAttributeMap); std::vector<int> updatedTimekey; insertCharactersIntoVector(LAST_UPDATED_TIME_KEY, &updatedTimekey); const time_t currentTime = time(NULL); std::vector<int> updatedTimeValue; char charBuf[LARGEST_INT_DIGIT_COUNT + 1]; snprintf(charBuf, LARGEST_INT_DIGIT_COUNT + 1, "%ld", currentTime); insertCharactersIntoVector(charBuf, &updatedTimeValue); attributeMapTowrite[updatedTimekey] = updatedTimeValue; if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite, &writingPos)) { return false; } } else { if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &mAttributeMap, &writingPos)) { return false; } } // Writes an actual header size. if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, writingPos, &headerSizeFieldPos)) { return false; } return true; } /* static */ HeaderReadWriteUtils::AttributeMap HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) { HeaderReadWriteUtils::AttributeMap attributeMap; HeaderReadWriteUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap); return attributeMap; } Loading