Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e89a179b authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi Committed by Android (Google) Code Review
Browse files

Merge "Separate "GC" and "GC with decay"."

parents cc81a93b 69953109
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -344,8 +344,7 @@ static jstring latinime_BinaryDictionary_getProperty(JNIEnv *env, jclass clazz,
    static const int GET_PROPERTY_RESULT_LENGTH = 100;
    char resultChars[GET_PROPERTY_RESULT_LENGTH];
    resultChars[0] = '\0';
    dictionary->getDictionaryStructurePolicy()->getProperty(queryChars, resultChars,
            GET_PROPERTY_RESULT_LENGTH);
    dictionary->getProperty(queryChars, resultChars, GET_PROPERTY_RESULT_LENGTH);
    return env->NewStringUTF(resultChars);
}

+1 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ bool Dictionary::needsToRunGC(const bool mindsBlockByGC) {
}

void Dictionary::getProperty(const char *const query, char *const outResult,
        const int maxResultLength) const {
        const int maxResultLength) {
    return mDictionaryStructureWithBufferPolicy->getProperty(query, outResult, maxResultLength);
}

+1 −1
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ class Dictionary {
    bool needsToRunGC(const bool mindsBlockByGC);

    void getProperty(const char *const query, char *const outResult,
            const int maxResultLength) const;
            const int maxResultLength);

    const DictionaryStructureWithBufferPolicy *getDictionaryStructurePolicy() const {
        return mDictionaryStructureWithBufferPolicy;
+3 −1
Original line number Diff line number Diff line
@@ -80,8 +80,10 @@ class DictionaryStructureWithBufferPolicy {

    virtual bool needsToRunGC(const bool mindsBlockByGC) const = 0;

    // Currently, this method is used only for testing. You may want to consider creating new
    // dedicated method instead of this if you want to use this in the production.
    virtual void getProperty(const char *const query, char *const outResult,
            const int maxResultLength) const = 0;
            const int maxResultLength) = 0;

 protected:
    DictionaryStructureWithBufferPolicy() {}
+22 −6
Original line number Diff line number Diff line
@@ -33,12 +33,16 @@

namespace latinime {

// Note that these are corresponding definitions in Java side in BinaryDictionaryTests and
// BinaryDictionaryDecayingTests.
const char *const DynamicPatriciaTriePolicy::UNIGRAM_COUNT_QUERY = "UNIGRAM_COUNT";
const char *const DynamicPatriciaTriePolicy::BIGRAM_COUNT_QUERY = "BIGRAM_COUNT";
const char *const DynamicPatriciaTriePolicy::SET_NEEDS_TO_DECAY_FOR_TESTING_QUERY =
        "SET_NEEDS_TO_DECAY_FOR_TESTING";
const int DynamicPatriciaTriePolicy::MAX_DICT_EXTENDED_REGION_SIZE = 1024 * 1024;
const int DynamicPatriciaTriePolicy::MIN_DICT_SIZE_TO_REFUSE_DYNAMIC_OPERATIONS =
        DynamicPatriciaTrieWritingHelper::MAX_DICTIONARY_SIZE - 1024;
const int DynamicPatriciaTriePolicy::MIN_SECONDS_TO_REQUIRE_GC_WHEN_WRITING = 2 * 60 * 60;
const int DynamicPatriciaTriePolicy::DECAY_INTERVAL_FOR_DECAYING_DICTS = 2 * 60 * 60;

void DynamicPatriciaTriePolicy::createAndGetAllChildNodes(const DicNode *const dicNode,
        DicNodeVector *const childDicNodes) const {
@@ -301,7 +305,7 @@ void DynamicPatriciaTriePolicy::flush(const char *const filePath) {
        return;
    }
    DynamicPatriciaTrieWritingHelper writingHelper(&mBufferWithExtendableBuffer,
            &mBigramListPolicy, &mShortcutListPolicy, mHeaderPolicy.isDecayingDict());
            &mBigramListPolicy, &mShortcutListPolicy, false /* needsToDecay */);
    writingHelper.writeToDictFile(filePath, &mHeaderPolicy, mUnigramCount, mBigramCount);
}

@@ -310,9 +314,15 @@ void DynamicPatriciaTriePolicy::flushWithGC(const char *const filePath) {
        AKLOGI("Warning: flushWithGC() is called for non-updatable dictionary.");
        return;
    }
    const bool runGCwithDecay = needsToDecay();
    DynamicBigramListPolicy bigramListPolicyForGC(&mBufferWithExtendableBuffer,
            &mShortcutListPolicy, runGCwithDecay);
    DynamicPatriciaTrieWritingHelper writingHelper(&mBufferWithExtendableBuffer,
            &mBigramListPolicy, &mShortcutListPolicy, mHeaderPolicy.isDecayingDict());
            &bigramListPolicyForGC, &mShortcutListPolicy, runGCwithDecay);
    writingHelper.writeToDictFileWithGC(getRootPosition(), filePath, &mHeaderPolicy);
    if (runGCwithDecay) {
        mNeedsToDecayForTesting = false;
    }
}

bool DynamicPatriciaTriePolicy::needsToRunGC(const bool mindsBlockByGC) const {
@@ -340,8 +350,7 @@ bool DynamicPatriciaTriePolicy::needsToRunGC(const bool mindsBlockByGC) const {
        } else if (mBigramCount >= DecayingUtils::MAX_BIGRAM_COUNT) {
            // Bigram count exceeds the limit.
            return true;
        } else if (mindsBlockByGC && mHeaderPolicy.getLastUpdatedTime()
                + MIN_SECONDS_TO_REQUIRE_GC_WHEN_WRITING < time(0)) {
        } else if (mindsBlockByGC && needsToDecay()) {
            // Time to update probabilities for decaying.
            return true;
        }
@@ -350,12 +359,19 @@ bool DynamicPatriciaTriePolicy::needsToRunGC(const bool mindsBlockByGC) const {
}

void DynamicPatriciaTriePolicy::getProperty(const char *const query, char *const outResult,
        const int maxResultLength) const {
        const int maxResultLength) {
    if (strncmp(query, UNIGRAM_COUNT_QUERY, maxResultLength) == 0) {
        snprintf(outResult, maxResultLength, "%d", mUnigramCount);
    } else if (strncmp(query, BIGRAM_COUNT_QUERY, maxResultLength) == 0) {
        snprintf(outResult, maxResultLength, "%d", mBigramCount);
    } else if (strncmp(query, SET_NEEDS_TO_DECAY_FOR_TESTING_QUERY, maxResultLength) == 0) {
        mNeedsToDecayForTesting = true;
    }
}

bool DynamicPatriciaTriePolicy::needsToDecay() const {
    return mHeaderPolicy.isDecayingDict() && (mNeedsToDecayForTesting
            || mHeaderPolicy.getLastDecayedTime() + DECAY_INTERVAL_FOR_DECAYING_DICTS < time(0));
}

} // namespace latinime
Loading