[HD01] Small initial refactoring

Bug: 11281748
Change-Id: I3c64c75ecd17729bdacf03048311d76ca9f37ae2
This commit is contained in:
Jean Chalard 2013-12-17 21:34:41 +09:00
parent 0c186c31d2
commit 324adefca3
4 changed files with 46 additions and 34 deletions

View file

@ -73,51 +73,58 @@ bool HeaderPolicy::readRequiresGermanUmlautProcessing() const {
REQUIRES_GERMAN_UMLAUT_PROCESSING_KEY, false); REQUIRES_GERMAN_UMLAUT_PROCESSING_KEY, false);
} }
bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite, bool HeaderPolicy::fillInAndWriteHeaderToBuffer(const bool updatesLastUpdatedTime,
const bool updatesLastUpdatedTime, const bool updatesLastDecayedTime, const bool updatesLastDecayedTime, const int unigramCount, const int bigramCount,
const int unigramCount, const int bigramCount, const int extendedRegionSize) const { const int extendedRegionSize, BufferWithExtendableBuffer *const outBuffer) const {
int writingPos = 0; int writingPos = 0;
if (!HeaderReadWriteUtils::writeDictionaryVersion(bufferToWrite, mDictFormatVersion, HeaderReadWriteUtils::AttributeMap attributeMapToWrite(mAttributeMap);
fillInHeader(updatesLastDecayedTime, updatesLastDecayedTime,
unigramCount, bigramCount, extendedRegionSize, &attributeMapToWrite);
if (!HeaderReadWriteUtils::writeDictionaryVersion(outBuffer, mDictFormatVersion,
&writingPos)) { &writingPos)) {
return false; return false;
} }
if (!HeaderReadWriteUtils::writeDictionaryFlags(bufferToWrite, mDictionaryFlags, if (!HeaderReadWriteUtils::writeDictionaryFlags(outBuffer, mDictionaryFlags,
&writingPos)) { &writingPos)) {
return false; return false;
} }
// Temporarily writes a dummy header size. // Temporarily writes a dummy header size.
int headerSizeFieldPos = writingPos; int headerSizeFieldPos = writingPos;
if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, 0 /* size */, if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(outBuffer, 0 /* size */,
&writingPos)) { &writingPos)) {
return false; return false;
} }
HeaderReadWriteUtils::AttributeMap attributeMapTowrite(mAttributeMap); if (!HeaderReadWriteUtils::writeHeaderAttributes(outBuffer, &attributeMapToWrite,
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, UNIGRAM_COUNT_KEY, unigramCount);
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, BIGRAM_COUNT_KEY, bigramCount);
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, EXTENDED_REGION_SIZE_KEY,
extendedRegionSize);
if (updatesLastUpdatedTime) {
// Set current time as a last updated time.
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_UPDATED_TIME_KEY,
TimeKeeper::peekCurrentTime());
}
if (updatesLastDecayedTime) {
// Set current time as a last updated time.
HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_DECAYED_TIME_KEY,
TimeKeeper::peekCurrentTime());
}
if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite,
&writingPos)) { &writingPos)) {
return false; return false;
} }
// Writes an actual header size. // Writes the actual header size.
if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, writingPos, if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(outBuffer, writingPos,
&headerSizeFieldPos)) { &headerSizeFieldPos)) {
return false; return false;
} }
return true; return true;
} }
void HeaderPolicy::fillInHeader(const bool updatesLastUpdatedTime,
const bool updatesLastDecayedTime, const int unigramCount, const int bigramCount,
const int extendedRegionSize, HeaderReadWriteUtils::AttributeMap *outAttributeMap) const {
HeaderReadWriteUtils::setIntAttribute(outAttributeMap, UNIGRAM_COUNT_KEY, unigramCount);
HeaderReadWriteUtils::setIntAttribute(outAttributeMap, BIGRAM_COUNT_KEY, bigramCount);
HeaderReadWriteUtils::setIntAttribute(outAttributeMap, EXTENDED_REGION_SIZE_KEY,
extendedRegionSize);
if (updatesLastUpdatedTime) {
// Set current time as the last updated time.
HeaderReadWriteUtils::setIntAttribute(outAttributeMap, LAST_UPDATED_TIME_KEY,
TimeKeeper::peekCurrentTime());
}
if (updatesLastDecayedTime) {
// Set current time as the last updated time.
HeaderReadWriteUtils::setIntAttribute(outAttributeMap, LAST_DECAYED_TIME_KEY,
TimeKeeper::peekCurrentTime());
}
}
/* static */ HeaderReadWriteUtils::AttributeMap /* static */ HeaderReadWriteUtils::AttributeMap
HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) { HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) {
HeaderReadWriteUtils::AttributeMap attributeMap; HeaderReadWriteUtils::AttributeMap attributeMap;

View file

@ -149,9 +149,13 @@ class HeaderPolicy : public DictionaryHeaderStructurePolicy {
void readHeaderValueOrQuestionMark(const char *const key, void readHeaderValueOrQuestionMark(const char *const key,
int *outValue, int outValueSize) const; int *outValue, int outValueSize) const;
bool writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite, bool fillInAndWriteHeaderToBuffer(const bool updatesLastUpdatedTime,
const bool updatesLastUpdatedTime, const bool updatesLastDecayedTime, const bool updatesLastDecayedTime, const int unigramCount, const int bigramCount,
const int unigramCount, const int bigramCount, const int extendedRegionSize) const; const int extendedRegionSize, BufferWithExtendableBuffer *const outBuffer) const;
void fillInHeader(const bool updatesLastUpdatedTime, const bool updatesLastDecayedTime,
const int unigramCount, const int bigramCount, const int extendedRegionSize,
HeaderReadWriteUtils::AttributeMap *outAttributeMap) const;
private: private:
DISALLOW_COPY_AND_ASSIGN(HeaderPolicy); DISALLOW_COPY_AND_ASSIGN(HeaderPolicy);

View file

@ -39,8 +39,9 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const dictDirPat
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE); BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE);
const int extendedRegionSize = headerPolicy->getExtendedRegionSize() const int extendedRegionSize = headerPolicy->getExtendedRegionSize()
+ mBuffers->getTrieBuffer()->getUsedAdditionalBufferSize(); + mBuffers->getTrieBuffer()->getUsedAdditionalBufferSize();
if (!headerPolicy->writeHeaderToBuffer(&headerBuffer, false /* updatesLastUpdatedTime */, if (!headerPolicy->fillInAndWriteHeaderToBuffer(false /* updatesLastUpdatedTime */,
false /* updatesLastDecayedTime */, unigramCount, bigramCount, extendedRegionSize)) { false /* updatesLastDecayedTime */, unigramCount, bigramCount, extendedRegionSize,
&headerBuffer)) {
AKLOGE("Cannot write header structure to buffer. updatesLastUpdatedTime: %d, " AKLOGE("Cannot write header structure to buffer. updatesLastUpdatedTime: %d, "
"updatesLastDecayedTime: %d, unigramCount: %d, bigramCount: %d, " "updatesLastDecayedTime: %d, unigramCount: %d, bigramCount: %d, "
"extendedRegionSize: %d", false, false, unigramCount, bigramCount, "extendedRegionSize: %d", false, false, unigramCount, bigramCount,
@ -62,9 +63,9 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeAr
} }
BufferWithExtendableBuffer headerBuffer( BufferWithExtendableBuffer headerBuffer(
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE); BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE);
if (!headerPolicy->writeHeaderToBuffer(&headerBuffer, true /* updatesLastUpdatedTime */, if (!headerPolicy->fillInAndWriteHeaderToBuffer(true /* updatesLastUpdatedTime */,
true /* updatesLastDecayedTime */, unigramCount, bigramCount, true /* updatesLastDecayedTime */, unigramCount, bigramCount,
0 /* extendedRegionSize */)) { 0 /* extendedRegionSize */, &headerBuffer)) {
return; return;
} }
dictBuffers.get()->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer); dictBuffers.get()->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);

View file

@ -48,9 +48,9 @@ const char *const DictFileWritingUtils::TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE =
HeaderPolicy headerPolicy(FormatUtils::VERSION_4, attributeMap); HeaderPolicy headerPolicy(FormatUtils::VERSION_4, attributeMap);
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers = Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
Ver4DictBuffers::createVer4DictBuffers(&headerPolicy); Ver4DictBuffers::createVer4DictBuffers(&headerPolicy);
headerPolicy.writeHeaderToBuffer(dictBuffers.get()->getWritableHeaderBuffer(), headerPolicy.fillInAndWriteHeaderToBuffer(true /* updatesLastUpdatedTime */,
true /* updatesLastUpdatedTime */, true /* updatesLastDecayedTime */, true /* updatesLastDecayedTime */, 0 /* unigramCount */, 0 /* bigramCount */,
0 /* unigramCount */, 0 /* bigramCount */, 0 /* extendedRegionSize */); 0 /* extendedRegionSize */, dictBuffers.get()->getWritableHeaderBuffer());
if (!DynamicPtWritingUtils::writeEmptyDictionary( if (!DynamicPtWritingUtils::writeEmptyDictionary(
dictBuffers.get()->getWritableTrieBuffer(), 0 /* rootPos */)) { dictBuffers.get()->getWritableTrieBuffer(), 0 /* rootPos */)) {
AKLOGE("Empty ver4 dictionary structure cannot be created on memory."); AKLOGE("Empty ver4 dictionary structure cannot be created on memory.");