1/*
2 * Copyright (C) 2013, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "suggest/policyimpl/dictionary/header/header_policy.h"
18
19namespace latinime {
20
21// Note that these are corresponding definitions in Java side in FormatSpec.FileHeader.
22const char *const HeaderPolicy::MULTIPLE_WORDS_DEMOTION_RATE_KEY = "MULTIPLE_WORDS_DEMOTION_RATE";
23// TODO: Change attribute string to "IS_DECAYING_DICT".
24const char *const HeaderPolicy::IS_DECAYING_DICT_KEY = "USES_FORGETTING_CURVE";
25const char *const HeaderPolicy::LAST_UPDATED_TIME_KEY = "date";
26const char *const HeaderPolicy::LAST_DECAYED_TIME_KEY = "LAST_DECAYED_TIME";
27const char *const HeaderPolicy::UNIGRAM_COUNT_KEY = "UNIGRAM_COUNT";
28const char *const HeaderPolicy::BIGRAM_COUNT_KEY = "BIGRAM_COUNT";
29const char *const HeaderPolicy::EXTENDED_REGION_SIZE_KEY = "EXTENDED_REGION_SIZE";
30const int HeaderPolicy::DEFAULT_MULTIPLE_WORDS_DEMOTION_RATE = 100;
31const float HeaderPolicy::MULTIPLE_WORD_COST_MULTIPLIER_SCALE = 100.0f;
32
33// Used for logging. Question mark is used to indicate that the key is not found.
34void HeaderPolicy::readHeaderValueOrQuestionMark(const char *const key, int *outValue,
35        int outValueSize) const {
36    if (outValueSize <= 0) return;
37    if (outValueSize == 1) {
38        outValue[0] = '\0';
39        return;
40    }
41    std::vector<int> keyCodePointVector;
42    HeaderReadWriteUtils::insertCharactersIntoVector(key, &keyCodePointVector);
43    HeaderReadWriteUtils::AttributeMap::const_iterator it = mAttributeMap.find(keyCodePointVector);
44    if (it == mAttributeMap.end()) {
45        // The key was not found.
46        outValue[0] = '?';
47        outValue[1] = '\0';
48        return;
49    }
50    const int terminalIndex = min(static_cast<int>(it->second.size()), outValueSize - 1);
51    for (int i = 0; i < terminalIndex; ++i) {
52        outValue[i] = it->second[i];
53    }
54    outValue[terminalIndex] = '\0';
55}
56
57float HeaderPolicy::readMultipleWordCostMultiplier() const {
58    const int demotionRate = HeaderReadWriteUtils::readIntAttributeValue(&mAttributeMap,
59            MULTIPLE_WORDS_DEMOTION_RATE_KEY, DEFAULT_MULTIPLE_WORDS_DEMOTION_RATE);
60    if (demotionRate <= 0) {
61        return static_cast<float>(MAX_VALUE_FOR_WEIGHTING);
62    }
63    return MULTIPLE_WORD_COST_MULTIPLIER_SCALE / static_cast<float>(demotionRate);
64}
65
66bool HeaderPolicy::writeHeaderToBuffer(BufferWithExtendableBuffer *const bufferToWrite,
67        const bool updatesLastUpdatedTime, const bool updatesLastDecayedTime,
68        const int unigramCount, const int bigramCount, const int extendedRegionSize) const {
69    int writingPos = 0;
70    if (!HeaderReadWriteUtils::writeDictionaryVersion(bufferToWrite, mDictFormatVersion,
71            &writingPos)) {
72        return false;
73    }
74    if (!HeaderReadWriteUtils::writeDictionaryFlags(bufferToWrite, mDictionaryFlags,
75            &writingPos)) {
76        return false;
77    }
78    // Temporarily writes a dummy header size.
79    int headerSizeFieldPos = writingPos;
80    if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, 0 /* size */,
81            &writingPos)) {
82        return false;
83    }
84    HeaderReadWriteUtils::AttributeMap attributeMapTowrite(mAttributeMap);
85    HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, UNIGRAM_COUNT_KEY, unigramCount);
86    HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, BIGRAM_COUNT_KEY, bigramCount);
87    HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, EXTENDED_REGION_SIZE_KEY,
88            extendedRegionSize);
89    if (updatesLastUpdatedTime) {
90        // Set current time as a last updated time.
91        HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_UPDATED_TIME_KEY,
92                time(0));
93    }
94    if (updatesLastDecayedTime) {
95        // Set current time as a last updated time.
96        HeaderReadWriteUtils::setIntAttribute(&attributeMapTowrite, LAST_DECAYED_TIME_KEY,
97                time(0));
98    }
99    if (!HeaderReadWriteUtils::writeHeaderAttributes(bufferToWrite, &attributeMapTowrite,
100            &writingPos)) {
101        return false;
102    }
103    // Writes an actual header size.
104    if (!HeaderReadWriteUtils::writeDictionaryHeaderSize(bufferToWrite, writingPos,
105            &headerSizeFieldPos)) {
106        return false;
107    }
108    return true;
109}
110
111/* static */ HeaderReadWriteUtils::AttributeMap
112        HeaderPolicy::createAttributeMapAndReadAllAttributes(const uint8_t *const dictBuf) {
113    HeaderReadWriteUtils::AttributeMap attributeMap;
114    HeaderReadWriteUtils::fetchAllHeaderAttributes(dictBuf, &attributeMap);
115    return attributeMap;
116}
117
118} // namespace latinime
119