From e531c2241eb8d5a1462c43ce0deffaf6c769cc23 Mon Sep 17 00:00:00 2001
From: Keisuke Kuroyanagi <ksk@google.com>
Date: Tue, 10 Sep 2013 18:45:39 +0900
Subject: [PATCH] Move a flag about switching dynamic update to java.

Bug: 6669677

Change-Id: I6aa99cae4a227f9202179c2873d13473a773e024
---
 .../latin/ExpandableBinaryDictionary.java     | 17 +++++++
 ...oid_inputmethod_latin_BinaryDictionary.cpp |  5 +++
 .../dynamic_patricia_trie_writing_helper.cpp  | 45 ++++++-------------
 .../dynamic_patricia_trie_writing_helper.h    |  1 -
 4 files changed, 35 insertions(+), 33 deletions(-)

diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 740b86d868..998896b2e6 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -49,6 +49,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
     /** Whether to print debug output to log */
     private static boolean DEBUG = false;
 
+    // TODO: Remove and enable dynamic update in native code.
+    /** Whether to call binary dictionary dynamically updating methods. */
+    private static boolean ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE = false;
+
     /**
      * The maximum length of a word in this dictionary.
      */
@@ -72,6 +76,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
      */
     private BinaryDictionary mBinaryDictionary;
 
+    // TODO: Remove and handle dictionaries in native code.
     /** The in-memory dictionary used to generate the binary dictionary. */
     protected AbstractDictionaryWriter mDictionaryWriter;
 
@@ -225,6 +230,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
         // TODO: Use a queue to reflect what needs to be reflected.
         if (mLocalDictionaryController.writeLock().tryLock()) {
             try {
+                if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    mBinaryDictionary.addUnigramWord(word, frequency);
+                }
+                // TODO: Remove.
                 mDictionaryWriter.addUnigramWord(word, shortcutTarget, frequency, isNotAWord);
             } finally {
                 mLocalDictionaryController.writeLock().unlock();
@@ -245,6 +254,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
         // TODO: Use a queue to reflect what needs to be reflected.
         if (mLocalDictionaryController.writeLock().tryLock()) {
             try {
+                if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    mBinaryDictionary.addBigramWords(word0, word1, frequency);
+                }
+                // TODO: Remove.
                 mDictionaryWriter.addBigramWords(word0, word1, frequency, isValid,
                         0 /* lastTouchedTime */);
             } finally {
@@ -265,6 +278,10 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
         // TODO: Use a queue to reflect what needs to be reflected.
         if (mLocalDictionaryController.writeLock().tryLock()) {
             try {
+                if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    mBinaryDictionary.removeBigramWords(word0, word1);
+                }
+                // TODO: Remove.
                 mDictionaryWriter.removeBigramWords(word0, word1);
             } finally {
                 mLocalDictionaryController.writeLock().unlock();
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 86c2394d1c..8da1859c4e 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -204,6 +204,7 @@ static void latinime_BinaryDictionary_addUnigramWord(JNIEnv *env, jclass clazz,
     }
     jsize wordLength = env->GetArrayLength(word);
     int codePoints[wordLength];
+    env->GetIntArrayRegion(word, 0, wordLength, codePoints);
     dictionary->addUnigramWord(codePoints, wordLength, probability);
 }
 
@@ -215,8 +216,10 @@ static void latinime_BinaryDictionary_addBigramWords(JNIEnv *env, jclass clazz,
     }
     jsize word0Length = env->GetArrayLength(word0);
     int word0CodePoints[word0Length];
+    env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
     jsize word1Length = env->GetArrayLength(word1);
     int word1CodePoints[word1Length];
+    env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
     dictionary->addBigramWords(word0CodePoints, word0Length, word1CodePoints,
             word1Length, probability);
 }
@@ -229,8 +232,10 @@ static void latinime_BinaryDictionary_removeBigramWords(JNIEnv *env, jclass claz
     }
     jsize word0Length = env->GetArrayLength(word0);
     int word0CodePoints[word0Length];
+    env->GetIntArrayRegion(word0, 0, word0Length, word0CodePoints);
     jsize word1Length = env->GetArrayLength(word1);
     int word1CodePoints[word1Length];
+    env->GetIntArrayRegion(word1, 0, word1Length, word1CodePoints);
     dictionary->removeBigramWords(word0CodePoints, word0Length, word1CodePoints,
             word1Length);
 }
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp
index 2c28da9afb..99a983f218 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.cpp
@@ -26,9 +26,6 @@
 
 namespace latinime {
 
-// TODO: Enable dynamic update and remove this flag.
-const bool DynamicPatriciaTrieWritingHelper::ENABLE_DYNAMIC_UPDATE = false;
-
 bool DynamicPatriciaTrieWritingHelper::addUnigramWord(
         DynamicPatriciaTrieReadingHelper *const readingHelper,
         const int *const wordCodePoints, const int codePointCount, const int probability) {
@@ -49,33 +46,21 @@ bool DynamicPatriciaTrieWritingHelper::addUnigramWord(
             const int nextIndex = matchedCodePointCount + j;
             if (nextIndex >= codePointCount || !readingHelper->isMatchedCodePoint(j,
                     wordCodePoints[matchedCodePointCount + j])) {
-                if (ENABLE_DYNAMIC_UPDATE) {
-                    return reallocatePtNodeAndAddNewPtNodes(nodeReader,
-                            readingHelper->getMergedNodeCodePoints(), j, probability,
-                            wordCodePoints + matchedCodePointCount,
-                            codePointCount - matchedCodePointCount);
-                } else {
-                    return false;
-                }
+                return reallocatePtNodeAndAddNewPtNodes(nodeReader,
+                        readingHelper->getMergedNodeCodePoints(), j, probability,
+                        wordCodePoints + matchedCodePointCount,
+                        codePointCount - matchedCodePointCount);
             }
         }
         // All characters are matched.
         if (codePointCount == readingHelper->getTotalCodePointCount()) {
-            if (ENABLE_DYNAMIC_UPDATE) {
-                return setPtNodeProbability(nodeReader, probability,
-                        readingHelper->getMergedNodeCodePoints());
-            } else {
-                return false;
-            }
+            return setPtNodeProbability(nodeReader, probability,
+                    readingHelper->getMergedNodeCodePoints());
         }
         if (!nodeReader->hasChildren()) {
-            if (ENABLE_DYNAMIC_UPDATE) {
-                return createChildrenPtNodeArrayAndAChildPtNode(nodeReader, probability,
-                        wordCodePoints + readingHelper->getTotalCodePointCount(),
-                        codePointCount - readingHelper->getTotalCodePointCount());
-            } else {
-                return false;
-            }
+            return createChildrenPtNodeArrayAndAChildPtNode(nodeReader, probability,
+                    wordCodePoints + readingHelper->getTotalCodePointCount(),
+                    codePointCount - readingHelper->getTotalCodePointCount());
         }
         // Advance to the children nodes.
         parentPos = nodeReader->getNodePos();
@@ -86,14 +71,10 @@ bool DynamicPatriciaTrieWritingHelper::addUnigramWord(
         return false;
     }
     int pos = readingHelper->getPosOfLastForwardLinkField();
-    if (ENABLE_DYNAMIC_UPDATE) {
-        return createAndInsertNodeIntoPtNodeArray(parentPos,
-                wordCodePoints + readingHelper->getPrevTotalCodePointCount(),
-                codePointCount - readingHelper->getPrevTotalCodePointCount(),
-                probability, &pos);
-    } else {
-        return false;
-    }
+    return createAndInsertNodeIntoPtNodeArray(parentPos,
+            wordCodePoints + readingHelper->getPrevTotalCodePointCount(),
+            codePointCount - readingHelper->getPrevTotalCodePointCount(),
+            probability, &pos);
 }
 
 bool DynamicPatriciaTrieWritingHelper::addBigramWords(const int word0Pos, const int word1Pos,
diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h
index ebde24d4d3..ada634a547 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h
+++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_writing_helper.h
@@ -49,7 +49,6 @@ class DynamicPatriciaTrieWritingHelper {
  private:
     DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPatriciaTrieWritingHelper);
 
-    static const bool ENABLE_DYNAMIC_UPDATE;
     BufferWithExtendableBuffer *const mBuffer;
     DynamicBigramListPolicy *const mBigramPolicy;
     DynamicShortcutListPolicy *const mShortcutPolicy;
-- 
GitLab