Merge "Revert emoji key position to JB-mr2"
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index 06f4b47..a779c6e 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -174,10 +174,10 @@
     <!-- Spoken description when there is no text entered -->
     <string name="spoken_no_text_entered">No text entered</string>
 
-    <!-- Spoken description to let the user know what auto-correction will be performed when a key is pressed. -->
-    <string name="spoken_auto_correct"><xliff:g id="key" example="Space">%1$s</xliff:g> corrects <xliff:g id="original">%2$s</xliff:g> to <xliff:g id="corrected">%3$s</xliff:g></string>
+    <!-- Spoken description to let the user know what auto-correction will be performed when a key is pressed. An auto-correction replaces a single word with one or more words. -->
+    <string name="spoken_auto_correct"><xliff:g id="key" example="Space">%1$s</xliff:g> corrects <xliff:g id="original_word">%2$s</xliff:g> to <xliff:g id="corrected">%3$s</xliff:g></string>
     <!-- Spoken description used during obscured (e.g. password) entry to let the user know that auto-correction will be performed when a key is pressed. -->
-    <string name="spoken_auto_correct_obscured"><xliff:g id="key" example="Space">%1$s</xliff:g> has auto-correction</string>
+    <string name="spoken_auto_correct_obscured"><xliff:g id="key" example="Space">%1$s</xliff:g> performs auto-correction</string>
 
     <!-- Spoken description for unknown keyboard keys. -->
     <string name="spoken_description_unknown">Key code %d</string>
diff --git a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
index 0985aae..c79a4ff 100644
--- a/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/ExpandableBinaryDictionary.java
@@ -249,6 +249,9 @@
                     final File file = new File(mContext.getFilesDir(), mFilename);
                     BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
                             DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
+                    mBinaryDictionary = new BinaryDictionary(
+                            file.getAbsolutePath(), 0 /* offset */, file.length(),
+                            true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
                 } else {
                     mDictionaryWriter.clear();
                 }
@@ -273,11 +276,26 @@
                 lastModifiedTime);
     }
 
+    /**
+     * Check whether GC is needed and run GC if required.
+     */
     protected void runGCIfRequired(final boolean mindsBlockByGC) {
         if (!ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) return;
+        getExecutor(mFilename).execute(new Runnable() {
+            @Override
+            public void run() {
+                runGCIfRequiredInternalLocked(mindsBlockByGC);
+            }
+        });
+    }
+
+    private void runGCIfRequiredInternalLocked(final boolean mindsBlockByGC) {
+        if (!ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) return;
+        // Calls to needsToRunGC() need to be serialized.
         if (mBinaryDictionary.needsToRunGC(mindsBlockByGC)) {
             if (setIsRegeneratingIfNotRegenerating()) {
-                getExecutor(mFilename).execute(new Runnable() {
+                // Run GC after currently existing time sensitive operations.
+                getExecutor(mFilename).executePrioritized(new Runnable() {
                     @Override
                     public void run() {
                         try {
@@ -300,11 +318,11 @@
             Log.w(TAG, "addWordDynamically is called for non-updatable dictionary: " + mFilename);
             return;
         }
-        runGCIfRequired(true /* mindsBlockByGC */);
         getExecutor(mFilename).execute(new Runnable() {
             @Override
             public void run() {
                 if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
                     mBinaryDictionary.addUnigramWord(word, frequency);
                 } else {
                     // TODO: Remove.
@@ -324,11 +342,11 @@
                     + mFilename);
             return;
         }
-        runGCIfRequired(true /* mindsBlockByGC */);
         getExecutor(mFilename).execute(new Runnable() {
             @Override
             public void run() {
                 if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
                     mBinaryDictionary.addBigramWords(word0, word1, frequency);
                 } else {
                     // TODO: Remove.
@@ -348,11 +366,11 @@
                     + mFilename);
             return;
         }
-        runGCIfRequired(true /* mindsBlockByGC */);
         getExecutor(mFilename).execute(new Runnable() {
             @Override
             public void run() {
                 if (ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE) {
+                    runGCIfRequiredInternalLocked(true /* mindsBlockByGC */);
                     mBinaryDictionary.removeBigramWords(word0, word1);
                 } else {
                     // TODO: Remove.
@@ -479,8 +497,8 @@
         final long length = file.length();
 
         // Build the new binary dictionary
-        final BinaryDictionary newBinaryDictionary = new BinaryDictionary(filename, 0, length,
-                true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
+        final BinaryDictionary newBinaryDictionary = new BinaryDictionary(filename, 0 /* offset */,
+                length, true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
 
         // Ensure all threads accessing the current dictionary have finished before
         // swapping in the new one.
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
index e901376..0f7d2f6 100644
--- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtils.java
@@ -288,42 +288,6 @@
         return BinaryDictEncoderUtils.getByteSize(value);
     }
 
-    // TODO: Remove this method.
-    @Deprecated
-    static void skipPtNode(final DictBuffer dictBuffer, final FormatOptions formatOptions) {
-        final int flags = dictBuffer.readUnsignedByte();
-        BinaryDictDecoderUtils.readParentAddress(dictBuffer, formatOptions);
-        skipString(dictBuffer, (flags & FormatSpec.FLAG_HAS_MULTIPLE_CHARS) != 0);
-        BinaryDictDecoderUtils.readChildrenAddress(dictBuffer, flags, formatOptions);
-        if ((flags & FormatSpec.FLAG_IS_TERMINAL) != 0) dictBuffer.readUnsignedByte();
-        if ((flags & FormatSpec.FLAG_HAS_SHORTCUT_TARGETS) != 0) {
-            final int shortcutsSize = dictBuffer.readUnsignedShort();
-            dictBuffer.position(dictBuffer.position() + shortcutsSize
-                    - FormatSpec.PTNODE_SHORTCUT_LIST_SIZE_SIZE);
-        }
-        if ((flags & FormatSpec.FLAG_HAS_BIGRAMS) != 0) {
-            int bigramCount = 0;
-            while (bigramCount++ < FormatSpec.MAX_BIGRAMS_IN_A_PTNODE) {
-                final int bigramFlags = dictBuffer.readUnsignedByte();
-                switch (bigramFlags & FormatSpec.MASK_BIGRAM_ATTR_ADDRESS_TYPE) {
-                    case FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_ONEBYTE:
-                        dictBuffer.readUnsignedByte();
-                        break;
-                    case FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_TWOBYTES:
-                        dictBuffer.readUnsignedShort();
-                        break;
-                    case FormatSpec.FLAG_BIGRAM_ATTR_ADDRESS_TYPE_THREEBYTES:
-                        dictBuffer.readUnsignedInt24();
-                        break;
-                }
-                if ((bigramFlags & FormatSpec.FLAG_BIGRAM_SHORTCUT_ATTR_HAS_NEXT) == 0) break;
-            }
-            if (bigramCount >= FormatSpec.MAX_BIGRAMS_IN_A_PTNODE) {
-                throw new RuntimeException("Too many bigrams in a PtNode.");
-            }
-        }
-    }
-
     static void skipString(final DictBuffer dictBuffer,
             final boolean hasMultipleChars) {
         if (hasMultipleChars) {
diff --git a/java/src/com/android/inputmethod/latin/makedict/DictUpdater.java b/java/src/com/android/inputmethod/latin/makedict/DictUpdater.java
new file mode 100644
index 0000000..413d030
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/makedict/DictUpdater.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.latin.makedict;
+
+import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+/**
+ * An interface of a binary dictionary updater.
+ */
+public interface DictUpdater {
+
+    /**
+     * Deletes the word from the binary dictionary.
+     *
+     * @param word the word to be deleted.
+     */
+    public void deleteWord(final String word) throws IOException, UnsupportedFormatException;
+
+    /**
+     * Inserts a word into a binary dictionary.
+     *
+     * @param word the word to be inserted.
+     * @param frequency the frequency of the new word.
+     * @param bigramStrings bigram list, or null if none.
+     * @param shortcuts shortcut list, or null if none.
+     * @param isBlackListEntry whether this should be a blacklist entry.
+     */
+    // TODO: Support batch insertion.
+    public void insertWord(final String word, final int frequency,
+            final ArrayList<WeightedString> bigramStrings,
+            final ArrayList<WeightedString> shortcuts, final boolean isNotAWord,
+            final boolean isBlackListEntry) throws IOException, UnsupportedFormatException;
+}
diff --git a/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java b/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
index 5c69941..3362771 100644
--- a/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
+++ b/java/src/com/android/inputmethod/latin/makedict/DynamicBinaryDictIOUtils.java
@@ -42,44 +42,22 @@
         // This utility class is not publicly instantiable.
     }
 
-    private static int markAsDeleted(final int flags) {
+    /* package */ static int markAsDeleted(final int flags) {
         return (flags & (~FormatSpec.MASK_CHILDREN_ADDRESS_TYPE)) | FormatSpec.FLAG_IS_DELETED;
     }
 
     /**
-     * Delete the word from the binary file.
-     *
-     * @param dictDecoder the dict decoder.
-     * @param word the word we delete
-     * @throws IOException
-     * @throws UnsupportedFormatException
-     */
-    @UsedForTesting
-    public static void deleteWord(final Ver3DictDecoder dictDecoder, final String word)
-            throws IOException, UnsupportedFormatException {
-        final DictBuffer dictBuffer = dictDecoder.getDictBuffer();
-        dictBuffer.position(0);
-        dictDecoder.readHeader();
-        final int wordPosition = dictDecoder.getTerminalPosition(word);
-        if (wordPosition == FormatSpec.NOT_VALID_WORD) return;
-
-        dictBuffer.position(wordPosition);
-        final int flags = dictBuffer.readUnsignedByte();
-        dictBuffer.position(wordPosition);
-        dictBuffer.put((byte)markAsDeleted(flags));
-    }
-
-    /**
      * Update a parent address in a PtNode that is referred to by ptNodeOriginAddress.
      *
-     * @param dictBuffer the DictBuffer to write.
+     * @param dictUpdater the DictUpdater to write.
      * @param ptNodeOriginAddress the address of the PtNode.
      * @param newParentAddress the absolute address of the parent.
      * @param formatOptions file format options.
      */
-    private static void updateParentAddress(final DictBuffer dictBuffer,
+    private static void updateParentAddress(final Ver3DictUpdater dictUpdater,
             final int ptNodeOriginAddress, final int newParentAddress,
             final FormatOptions formatOptions) {
+        final DictBuffer dictBuffer = dictUpdater.getDictBuffer();
         final int originalPosition = dictBuffer.position();
         dictBuffer.position(ptNodeOriginAddress);
         if (!formatOptions.mSupportsDynamicUpdate) {
@@ -104,41 +82,41 @@
     /**
      * Update parent addresses in a node array stored at ptNodeOriginAddress.
      *
-     * @param dictBuffer the DictBuffer to be modified.
+     * @param dictUpdater the DictUpdater to be modified.
      * @param ptNodeOriginAddress the address of the node array to update.
      * @param newParentAddress the address to be written.
      * @param formatOptions file format options.
      */
-    private static void updateParentAddresses(final DictBuffer dictBuffer,
+    private static void updateParentAddresses(final Ver3DictUpdater dictUpdater,
             final int ptNodeOriginAddress, final int newParentAddress,
             final FormatOptions formatOptions) {
-        final int originalPosition = dictBuffer.position();
-        dictBuffer.position(ptNodeOriginAddress);
+        final int originalPosition = dictUpdater.getPosition();
+        dictUpdater.setPosition(ptNodeOriginAddress);
         do {
-            final int count = BinaryDictDecoderUtils.readPtNodeCount(dictBuffer);
+            final int count = dictUpdater.readPtNodeCount();
             for (int i = 0; i < count; ++i) {
-                updateParentAddress(dictBuffer, dictBuffer.position(), newParentAddress,
+                updateParentAddress(dictUpdater, dictUpdater.getPosition(), newParentAddress,
                         formatOptions);
-                BinaryDictIOUtils.skipPtNode(dictBuffer, formatOptions);
+                dictUpdater.skipPtNode(formatOptions);
             }
-            final int forwardLinkAddress = dictBuffer.readUnsignedInt24();
-            dictBuffer.position(forwardLinkAddress);
-        } while (formatOptions.mSupportsDynamicUpdate
-                && dictBuffer.position() != FormatSpec.NO_FORWARD_LINK_ADDRESS);
-        dictBuffer.position(originalPosition);
+            if (!dictUpdater.readAndFollowForwardLink()) break;
+            if (dictUpdater.getPosition() == FormatSpec.NO_FORWARD_LINK_ADDRESS) break;
+        } while (formatOptions.mSupportsDynamicUpdate);
+        dictUpdater.setPosition(originalPosition);
     }
 
     /**
      * Update a children address in a PtNode that is addressed by ptNodeOriginAddress.
      *
-     * @param dictBuffer the DictBuffer to write.
+     * @param dictUpdater the DictUpdater to write.
      * @param ptNodeOriginAddress the address of the PtNode.
      * @param newChildrenAddress the absolute address of the child.
      * @param formatOptions file format options.
      */
-    private static void updateChildrenAddress(final DictBuffer dictBuffer,
+    private static void updateChildrenAddress(final Ver3DictUpdater dictUpdater,
             final int ptNodeOriginAddress, final int newChildrenAddress,
             final FormatOptions formatOptions) {
+        final DictBuffer dictBuffer = dictUpdater.getDictBuffer();
         final int originalPosition = dictBuffer.position();
         dictBuffer.position(ptNodeOriginAddress);
         final int flags = dictBuffer.readUnsignedByte();
@@ -155,31 +133,33 @@
      * Helper method to move a PtNode to the tail of the file.
      */
     private static int movePtNode(final OutputStream destination,
-            final DictBuffer dictBuffer, final PtNodeInfo info,
+            final Ver3DictUpdater dictUpdater, final PtNodeInfo info,
             final int nodeArrayOriginAddress, final int oldNodeAddress,
             final FormatOptions formatOptions) throws IOException {
-        updateParentAddress(dictBuffer, oldNodeAddress, dictBuffer.limit() + 1, formatOptions);
+        final DictBuffer dictBuffer = dictUpdater.getDictBuffer();
+        updateParentAddress(dictUpdater, oldNodeAddress, dictBuffer.limit() + 1, formatOptions);
         dictBuffer.position(oldNodeAddress);
         final int currentFlags = dictBuffer.readUnsignedByte();
         dictBuffer.position(oldNodeAddress);
         dictBuffer.put((byte)(FormatSpec.FLAG_IS_MOVED | (currentFlags
                 & (~FormatSpec.MASK_MOVE_AND_DELETE_FLAG))));
         int size = FormatSpec.PTNODE_FLAGS_SIZE;
-        updateForwardLink(dictBuffer, nodeArrayOriginAddress, dictBuffer.limit(), formatOptions);
+        updateForwardLink(dictUpdater, nodeArrayOriginAddress, dictBuffer.limit(), formatOptions);
         size += BinaryDictIOUtils.writeNodes(destination, new PtNodeInfo[] { info });
         return size;
     }
 
     @SuppressWarnings("unused")
-    private static void updateForwardLink(final DictBuffer dictBuffer,
+    private static void updateForwardLink(final Ver3DictUpdater dictUpdater,
             final int nodeArrayOriginAddress, final int newNodeArrayAddress,
             final FormatOptions formatOptions) {
-        dictBuffer.position(nodeArrayOriginAddress);
+        final DictBuffer dictBuffer = dictUpdater.getDictBuffer();
+        dictUpdater.setPosition(nodeArrayOriginAddress);
         int jumpCount = 0;
         while (jumpCount++ < MAX_JUMPS) {
-            final int count = BinaryDictDecoderUtils.readPtNodeCount(dictBuffer);
+            final int count = dictUpdater.readPtNodeCount();
             for (int i = 0; i < count; ++i) {
-                BinaryDictIOUtils.skipPtNode(dictBuffer, formatOptions);
+                dictUpdater.readPtNode(dictUpdater.getPosition(), formatOptions);
             }
             final int forwardLinkAddress = dictBuffer.readUnsignedInt24();
             if (forwardLinkAddress == FormatSpec.NO_FORWARD_LINK_ADDRESS) {
@@ -207,7 +187,7 @@
      * @param shortcutTargets the shortcut targets for this PtNode.
      * @param bigrams the bigrams for this PtNode.
      * @param destination the stream representing the tail of the file.
-     * @param dictBuffer the DictBuffer representing the (constant-size) body of the file.
+     * @param dictUpdater the DictUpdater.
      * @param oldPtNodeArrayOrigin the origin of the old PtNode array this PtNode was a part of.
      * @param oldPtNodeOrigin the old origin where this PtNode used to be stored.
      * @param formatOptions format options for this dictionary.
@@ -218,7 +198,7 @@
             final int length, final int flags, final int frequency, final int parentAddress,
             final ArrayList<WeightedString> shortcutTargets,
             final ArrayList<PendingAttribute> bigrams, final OutputStream destination,
-            final DictBuffer dictBuffer, final int oldPtNodeArrayOrigin,
+            final Ver3DictUpdater dictUpdater, final int oldPtNodeArrayOrigin,
             final int oldPtNodeOrigin, final FormatOptions formatOptions) throws IOException {
         int size = 0;
         final int newPtNodeOrigin = fileEndAddress + 1;
@@ -231,7 +211,7 @@
                 flags, writtenCharacters, frequency, parentAddress,
                 fileEndAddress + 1 + size + FormatSpec.FORWARD_LINK_ADDRESS_SIZE, shortcutTargets,
                 bigrams);
-        movePtNode(destination, dictBuffer, newInfo, oldPtNodeArrayOrigin, oldPtNodeOrigin,
+        movePtNode(destination, dictUpdater, newInfo, oldPtNodeArrayOrigin, oldPtNodeOrigin,
                 formatOptions);
         return 1 + size + FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
     }
@@ -239,7 +219,7 @@
     /**
      * Insert a word into a binary dictionary.
      *
-     * @param dictDecoder the dict decoder.
+     * @param dictUpdater the dict updater.
      * @param destination a stream to the underlying file, with the pointer at the end of the file.
      * @param word the word to insert.
      * @param frequency the frequency of the new word.
@@ -252,17 +232,17 @@
     // TODO: Support batch insertion.
     // TODO: Remove @UsedForTesting once UserHistoryDictionary is implemented by BinaryDictionary.
     @UsedForTesting
-    public static void insertWord(final Ver3DictDecoder dictDecoder,
+    public static void insertWord(final Ver3DictUpdater dictUpdater,
             final OutputStream destination, final String word, final int frequency,
             final ArrayList<WeightedString> bigramStrings,
             final ArrayList<WeightedString> shortcuts, final boolean isNotAWord,
             final boolean isBlackListEntry)
                     throws IOException, UnsupportedFormatException {
         final ArrayList<PendingAttribute> bigrams = new ArrayList<PendingAttribute>();
-        final DictBuffer dictBuffer = dictDecoder.getDictBuffer();
+        final DictBuffer dictBuffer = dictUpdater.getDictBuffer();
         if (bigramStrings != null) {
             for (final WeightedString bigram : bigramStrings) {
-                int position = dictDecoder.getTerminalPosition(bigram.mWord);
+                int position = dictUpdater.getTerminalPosition(bigram.mWord);
                 if (position == FormatSpec.NOT_VALID_WORD) {
                     // TODO: figure out what is the correct thing to do here.
                 } else {
@@ -277,7 +257,7 @@
 
         // find the insert position of the word.
         if (dictBuffer.position() != 0) dictBuffer.position(0);
-        final FileHeader fileHeader = dictDecoder.readHeader();
+        final FileHeader fileHeader = dictUpdater.readHeader();
 
         int wordPos = 0, address = dictBuffer.position(), nodeOriginAddress = dictBuffer.position();
         final int[] codePoints = FusionDictionary.getCodePoints(word);
@@ -292,7 +272,7 @@
 
             for (int i = 0; i < ptNodeCount; ++i) {
                 address = dictBuffer.position();
-                final PtNodeInfo currentInfo = dictDecoder.readPtNode(address,
+                final PtNodeInfo currentInfo = dictUpdater.readPtNode(address,
                         fileHeader.mFormatOptions);
                 final boolean isMovedNode = BinaryDictIOUtils.isMovedPtNode(currentInfo.mFlags,
                         fileHeader.mFormatOptions);
@@ -318,12 +298,12 @@
                                 false /* isBlackListEntry */, fileHeader.mFormatOptions);
                         int written = movePtNode(newNodeAddress, currentInfo.mCharacters, p, flags,
                                 frequency, nodeParentAddress, shortcuts, bigrams, destination,
-                                dictBuffer, nodeOriginAddress, address, fileHeader.mFormatOptions);
+                                dictUpdater, nodeOriginAddress, address, fileHeader.mFormatOptions);
 
                         final int[] characters2 = Arrays.copyOfRange(currentInfo.mCharacters, p,
                                 currentInfo.mCharacters.length);
                         if (currentInfo.mChildrenAddress != FormatSpec.NO_CHILDREN_ADDRESS) {
-                            updateParentAddresses(dictBuffer, currentInfo.mChildrenAddress,
+                            updateParentAddresses(dictUpdater, currentInfo.mChildrenAddress,
                                     newNodeAddress + written + 1, fileHeader.mFormatOptions);
                         }
                         final PtNodeInfo newInfo2 = new PtNodeInfo(
@@ -359,13 +339,13 @@
                                     fileHeader.mFormatOptions);
                             int written = movePtNode(newNodeAddress, currentInfo.mCharacters, p,
                                     prefixFlags, -1 /* frequency */, nodeParentAddress, null, null,
-                                    destination, dictBuffer, nodeOriginAddress, address,
+                                    destination, dictUpdater, nodeOriginAddress, address,
                                     fileHeader.mFormatOptions);
 
                             final int[] suffixCharacters = Arrays.copyOfRange(
                                     currentInfo.mCharacters, p, currentInfo.mCharacters.length);
                             if (currentInfo.mChildrenAddress != FormatSpec.NO_CHILDREN_ADDRESS) {
-                                updateParentAddresses(dictBuffer, currentInfo.mChildrenAddress,
+                                updateParentAddresses(dictUpdater, currentInfo.mChildrenAddress,
                                         newNodeAddress + written + 1, fileHeader.mFormatOptions);
                             }
                             final int suffixFlags = BinaryDictEncoderUtils.makePtNodeFlags(
@@ -416,7 +396,7 @@
                                 -1 /* endAddress */, flags, currentInfo.mCharacters, frequency,
                                 nodeParentAddress, currentInfo.mChildrenAddress, shortcuts,
                                 bigrams);
-                        movePtNode(destination, dictBuffer, newInfo, nodeOriginAddress, address,
+                        movePtNode(destination, dictUpdater, newInfo, nodeOriginAddress, address,
                                 fileHeader.mFormatOptions);
                         return;
                     }
@@ -435,7 +415,7 @@
                          * ab - cd - e
                          */
                         final int newNodeArrayAddress = dictBuffer.limit();
-                        updateChildrenAddress(dictBuffer, address, newNodeArrayAddress,
+                        updateChildrenAddress(dictUpdater, address, newNodeArrayAddress,
                                 fileHeader.mFormatOptions);
                         final int newNodeAddress = newNodeArrayAddress + 1;
                         final boolean hasMultipleChars = (wordLen - wordPos) > 1;
diff --git a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java
index 9481a8c..a5516bd 100644
--- a/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java
+++ b/java/src/com/android/inputmethod/latin/makedict/FormatSpec.java
@@ -266,11 +266,14 @@
     // tat = Terminal Address Table
     static final String TERMINAL_ADDRESS_TABLE_FILE_EXTENSION = ".tat";
     static final String BIGRAM_FILE_EXTENSION = ".bigram";
-    static final String BIGRAM_LOOKUP_TABLE_FILE_EXTENSION = ".bigram_lookup";
-    static final String BIGRAM_ADDRESS_TABLE_FILE_EXTENSION = ".bigram_index";
+    static final String LOOKUP_TABLE_FILE_SUFFIX = "_lookup";
+    static final String CONTENT_TABLE_FILE_SUFFIX = "_index";
     static final int FREQUENCY_AND_FLAGS_SIZE = 2;
     static final int TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE = 3;
     static final int BIGRAM_ADDRESS_TABLE_BLOCK_SIZE = 4;
+    static final int BIGRAM_CONTENT_COUNT = 1;
+    static final int BIGRAM_FREQ_CONTENT_INDEX = 0;
+    static final String BIGRAM_FREQ_CONTENT_ID = "_freq";
 
     static final int NO_CHILDREN_ADDRESS = Integer.MIN_VALUE;
     static final int NO_PARENT_ADDRESS = 0;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
index bf5a28d..b87259c 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver3DictDecoder.java
@@ -53,9 +53,9 @@
         }
     }
 
-    private final File mDictionaryBinaryFile;
+    protected final File mDictionaryBinaryFile;
     private final DictionaryBufferFactory mBufferFactory;
-    private DictBuffer mDictBuffer;
+    protected DictBuffer mDictBuffer;
 
     /* package */ Ver3DictDecoder(final File file, final int factoryFlag) {
         mDictionaryBinaryFile = file;
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver3DictUpdater.java b/java/src/com/android/inputmethod/latin/makedict/Ver3DictUpdater.java
new file mode 100644
index 0000000..fa7ae31
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver3DictUpdater.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.inputmethod.latin.makedict;
+
+import com.android.inputmethod.annotations.UsedForTesting;
+import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+
+/**
+ * An implementation of DictUpdater for version 3 binary dictionary.
+ */
+@UsedForTesting
+public class Ver3DictUpdater extends Ver3DictDecoder implements DictUpdater {
+    private OutputStream mOutStream;
+
+    @UsedForTesting
+    public Ver3DictUpdater(final File dictFile, final int factoryType) {
+        // DictUpdater must have an updatable DictBuffer.
+        super(dictFile, ((factoryType & MASK_DICTBUFFER) == USE_BYTEARRAY)
+                ? USE_BYTEARRAY : USE_WRITABLE_BYTEBUFFER);
+        mOutStream = null;
+    }
+
+    private void openStreamAndBuffer() throws FileNotFoundException, IOException {
+        super.openDictBuffer();
+        mOutStream = new FileOutputStream(mDictionaryBinaryFile, true /* append */);
+    }
+
+    private void close() throws IOException {
+        if (mOutStream != null) {
+            mOutStream.close();
+            mOutStream = null;
+        }
+    }
+
+    @Override @UsedForTesting
+    public void deleteWord(final String word) throws IOException, UnsupportedFormatException {
+        if (mOutStream == null) openStreamAndBuffer();
+        mDictBuffer.position(0);
+        super.readHeader();
+        final int wordPos = getTerminalPosition(word);
+        if (wordPos != FormatSpec.NOT_VALID_WORD) {
+            mDictBuffer.position(wordPos);
+            final int flags = mDictBuffer.readUnsignedByte();
+            mDictBuffer.position(wordPos);
+            mDictBuffer.put((byte) DynamicBinaryDictIOUtils.markAsDeleted(flags));
+        }
+        close();
+    }
+
+    @Override @UsedForTesting
+    public void insertWord(final String word, final int frequency,
+            final ArrayList<WeightedString> bigramStrings,
+            final ArrayList<WeightedString> shortcuts,
+            final boolean isNotAWord, final boolean isBlackListEntry)
+                    throws IOException, UnsupportedFormatException {
+        if (mOutStream == null) openStreamAndBuffer();
+        DynamicBinaryDictIOUtils.insertWord(this, mOutStream, word, frequency, bigramStrings,
+                shortcuts, isNotAWord, isBlackListEntry);
+        close();
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
index 624b278..5089687 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictDecoder.java
@@ -42,7 +42,7 @@
     private static final int FILETYPE_TRIE = 1;
     private static final int FILETYPE_FREQUENCY = 2;
     private static final int FILETYPE_TERMINAL_ADDRESS_TABLE = 3;
-    private static final int FILETYPE_BIGRAM = 4;
+    private static final int FILETYPE_BIGRAM_FREQ = 4;
 
     private final File mDictDirectory;
     private final DictionaryBufferFactory mBufferFactory;
@@ -85,9 +85,10 @@
         } else if (fileType == FILETYPE_TERMINAL_ADDRESS_TABLE) {
             return new File(mDictDirectory,
                     mDictDirectory.getName() + FormatSpec.TERMINAL_ADDRESS_TABLE_FILE_EXTENSION);
-        } else if (fileType == FILETYPE_BIGRAM) {
+        } else if (fileType == FILETYPE_BIGRAM_FREQ) {
             return new File(mDictDirectory,
-                    mDictDirectory.getName() + FormatSpec.BIGRAM_FILE_EXTENSION);
+                    mDictDirectory.getName() + FormatSpec.BIGRAM_FILE_EXTENSION
+                            + FormatSpec.BIGRAM_FREQ_CONTENT_ID);
         } else {
             throw new RuntimeException("Unsupported kind of file : " + fileType);
         }
@@ -99,7 +100,7 @@
         mFrequencyBuffer = mBufferFactory.getDictionaryBuffer(getFile(FILETYPE_FREQUENCY));
         mTerminalAddressTableBuffer = mBufferFactory.getDictionaryBuffer(
                 getFile(FILETYPE_TERMINAL_ADDRESS_TABLE));
-        mBigramBuffer = mBufferFactory.getDictionaryBuffer(getFile(FILETYPE_BIGRAM));
+        mBigramBuffer = mBufferFactory.getDictionaryBuffer(getFile(FILETYPE_BIGRAM_FREQ));
         loadBigramAddressSparseTable();
     }
 
@@ -126,11 +127,12 @@
     }
 
     private void loadBigramAddressSparseTable() throws IOException {
-        final File lookupIndexFile = new File(mDictDirectory,
-                mDictDirectory.getName() + FormatSpec.BIGRAM_LOOKUP_TABLE_FILE_EXTENSION);
-        final File contentFile = new File(mDictDirectory,
-                mDictDirectory.getName() + FormatSpec.BIGRAM_ADDRESS_TABLE_FILE_EXTENSION);
-        mBigramAddressTable = SparseTable.readFromFiles(lookupIndexFile, new File[] { contentFile },
+        final File lookupIndexFile = new File(mDictDirectory, mDictDirectory.getName()
+                + FormatSpec.BIGRAM_FILE_EXTENSION + FormatSpec.LOOKUP_TABLE_FILE_SUFFIX);
+        final File freqsFile = new File(mDictDirectory, mDictDirectory.getName()
+                + FormatSpec.BIGRAM_FILE_EXTENSION + FormatSpec.CONTENT_TABLE_FILE_SUFFIX
+                + FormatSpec.BIGRAM_FREQ_CONTENT_ID);
+        mBigramAddressTable = SparseTable.readFromFiles(lookupIndexFile, new File[] { freqsFile },
                 FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE);
     }
 
diff --git a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
index a403e25..b38c330 100644
--- a/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
+++ b/java/src/com/android/inputmethod/latin/makedict/Ver4DictEncoder.java
@@ -26,7 +26,6 @@
 import com.android.inputmethod.latin.makedict.FusionDictionary.PtNodeArray;
 import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
 
-import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -44,19 +43,115 @@
     private byte[] mTrieBuf;
     private int mTriePos;
     private int mHeaderSize;
-    private SparseTable mBigramAddressTable;
     private OutputStream mTrieOutStream;
     private OutputStream mFreqOutStream;
     private OutputStream mTerminalAddressTableOutStream;
-    private OutputStream mBigramOutStream;
     private File mDictDir;
     private String mBaseFilename;
+    private BigramContentWriter mBigramWriter;
 
     @UsedForTesting
     public Ver4DictEncoder(final File dictPlacedDir) {
         mDictPlacedDir = dictPlacedDir;
     }
 
+    private interface SparseTableContentWriterInterface {
+        public void write(final OutputStream outStream) throws IOException;
+    }
+
+    private static class SparseTableContentWriter {
+        private final int mContentCount;
+        private final SparseTable mSparseTable;
+        private final File mLookupTableFile;
+        protected final File mBaseDir;
+        private final File[] mAddressTableFiles;
+        private final File[] mContentFiles;
+        protected final OutputStream[] mContentOutStreams;
+
+        public SparseTableContentWriter(final String name, final int contentCount,
+                final int initialCapacity, final int blockSize, final File baseDir,
+                final String[] contentFilenames, final String[] contentIds) {
+            if (contentFilenames.length != contentIds.length) {
+                throw new RuntimeException("The length of contentFilenames and the length of"
+                        + " contentIds are different " + contentFilenames.length + ", "
+                        + contentIds.length);
+            }
+            mContentCount = contentCount;
+            mSparseTable = new SparseTable(initialCapacity, blockSize, contentCount);
+            mLookupTableFile = new File(baseDir, name + FormatSpec.LOOKUP_TABLE_FILE_SUFFIX);
+            mAddressTableFiles = new File[mContentCount];
+            mContentFiles = new File[mContentCount];
+            mBaseDir = baseDir;
+            for (int i = 0; i < mContentCount; ++i) {
+                mAddressTableFiles[i] = new File(mBaseDir,
+                        name + FormatSpec.CONTENT_TABLE_FILE_SUFFIX + contentIds[i]);
+                mContentFiles[i] = new File(mBaseDir, contentFilenames[i] + contentIds[i]);
+            }
+            mContentOutStreams = new OutputStream[mContentCount];
+        }
+
+        public void openStreams() throws FileNotFoundException {
+            for (int i = 0; i < mContentCount; ++i) {
+                mContentOutStreams[i] = new FileOutputStream(mContentFiles[i]);
+            }
+        }
+
+        protected void write(final int contentIndex, final int index,
+                final SparseTableContentWriterInterface writer) throws IOException {
+            mSparseTable.set(contentIndex, index, (int) mContentFiles[contentIndex].length());
+            writer.write(mContentOutStreams[contentIndex]);
+            mContentOutStreams[contentIndex].flush();
+        }
+
+        public void closeStreams() throws IOException {
+            mSparseTable.writeToFiles(mLookupTableFile, mAddressTableFiles);
+            for (int i = 0; i < mContentCount; ++i) {
+                mContentOutStreams[i].close();
+            }
+        }
+    }
+
+    private static class BigramContentWriter extends SparseTableContentWriter {
+
+        public BigramContentWriter(final String name, final int initialCapacity,
+                final File baseDir) {
+            super(name + FormatSpec.BIGRAM_FILE_EXTENSION, FormatSpec.BIGRAM_CONTENT_COUNT,
+                    initialCapacity, FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE, baseDir,
+                    new String[] { name + FormatSpec.BIGRAM_FILE_EXTENSION },
+                    new String[] { FormatSpec.BIGRAM_FREQ_CONTENT_ID });
+        }
+
+        public void writeBigramsForOneWord(final int terminalId,
+                final Iterator<WeightedString> bigramIterator, final FusionDictionary dict)
+                        throws IOException {
+            write(FormatSpec.BIGRAM_FREQ_CONTENT_INDEX, terminalId,
+                    new SparseTableContentWriterInterface() {
+                        @Override
+                        public void write(final OutputStream outStream) throws IOException {
+                            writeBigramsForOneWordInternal(outStream, bigramIterator, dict);
+                        }
+            });
+        }
+
+        private void writeBigramsForOneWordInternal(final OutputStream outStream,
+                final Iterator<WeightedString> bigramIterator, final FusionDictionary dict)
+                        throws IOException {
+            while (bigramIterator.hasNext()) {
+                final WeightedString bigram = bigramIterator.next();
+                final PtNode target =
+                        FusionDictionary.findWordInTree(dict.mRootNodeArray, bigram.mWord);
+                final int unigramFrequencyForThisWord = target.mFrequency;
+                final int bigramFlags = BinaryDictEncoderUtils.makeBigramFlags(
+                        bigramIterator.hasNext(), 0, bigram.mFrequency,
+                        unigramFrequencyForThisWord, bigram.mWord);
+                BinaryDictEncoderUtils.writeUIntToStream(outStream, bigramFlags,
+                        FormatSpec.PTNODE_ATTRIBUTE_FLAGS_SIZE);
+                BinaryDictEncoderUtils.writeUIntToStream(outStream, target.mTerminalId,
+                        FormatSpec.PTNODE_ATTRIBUTE_MAX_ADDRESS_SIZE);
+            }
+        }
+    }
+
     private void openStreams(final FormatOptions formatOptions, final DictionaryOptions dictOptions)
             throws FileNotFoundException, IOException {
         final FileHeader header = new FileHeader(0, dictOptions, formatOptions);
@@ -66,8 +161,6 @@
         final File freqFile = new File(mDictDir, mBaseFilename + FormatSpec.FREQ_FILE_EXTENSION);
         final File terminalAddressTableFile = new File(mDictDir,
                 mBaseFilename + FormatSpec.TERMINAL_ADDRESS_TABLE_FILE_EXTENSION);
-        final File bigramFile = new File(mDictDir,
-                mBaseFilename + FormatSpec.BIGRAM_FILE_EXTENSION);
         if (!mDictDir.isDirectory()) {
             if (mDictDir.exists()) mDictDir.delete();
             mDictDir.mkdirs();
@@ -78,7 +171,6 @@
         mTrieOutStream = new FileOutputStream(trieFile);
         mFreqOutStream = new FileOutputStream(freqFile);
         mTerminalAddressTableOutStream = new FileOutputStream(terminalAddressTableFile);
-        mBigramOutStream = new FileOutputStream(bigramFile);
     }
 
     private void close() throws IOException {
@@ -92,14 +184,10 @@
             if (mTerminalAddressTableOutStream != null) {
                 mTerminalAddressTableOutStream.close();
             }
-            if (mBigramOutStream != null) {
-                mBigramOutStream.close();
-            }
         } finally {
             mTrieOutStream = null;
             mFreqOutStream = null;
             mTerminalAddressTableOutStream = null;
-            mBigramOutStream = null;
         }
     }
 
@@ -135,10 +223,8 @@
         if (MakedictLog.DBG) BinaryDictEncoderUtils.checkFlatPtNodeArrayList(flatNodes);
 
         writeTerminalData(flatNodes, terminalCount);
-        mBigramAddressTable = new SparseTable(terminalCount,
-                FormatSpec.BIGRAM_ADDRESS_TABLE_BLOCK_SIZE, 1 /* contentTableCount */);
+        mBigramWriter = new BigramContentWriter(mBaseFilename, terminalCount, mDictDir);
         writeBigrams(flatNodes, dict);
-        writeBigramAddressSparseTable();
 
         final PtNodeArray lastNodeArray = flatNodes.get(flatNodes.size() - 1);
         final int bufferSize = lastNodeArray.mCachedAddressAfterUpdate + lastNodeArray.mCachedSize;
@@ -245,40 +331,16 @@
 
     private void writeBigrams(final ArrayList<PtNodeArray> flatNodes, final FusionDictionary dict)
             throws IOException {
-        final ByteArrayOutputStream bigramBuffer = new ByteArrayOutputStream();
-
+        mBigramWriter.openStreams();
         for (final PtNodeArray nodeArray : flatNodes) {
             for (final PtNode ptNode : nodeArray.mData) {
                 if (ptNode.mBigrams != null) {
-                    final int startPos = bigramBuffer.size();
-                    mBigramAddressTable.set(0 /* contentTableIndex */, ptNode.mTerminalId,
-                            startPos);
-                    final Iterator<WeightedString> bigramIterator = ptNode.mBigrams.iterator();
-                    while (bigramIterator.hasNext()) {
-                        final WeightedString bigram = bigramIterator.next();
-                        final PtNode target =
-                            FusionDictionary.findWordInTree(dict.mRootNodeArray, bigram.mWord);
-                        final int unigramFrequencyForThisWord = target.mFrequency;
-                        final int bigramFlags = BinaryDictEncoderUtils.makeBigramFlags(
-                                bigramIterator.hasNext(), 0, bigram.mFrequency,
-                                unigramFrequencyForThisWord, bigram.mWord);
-                        BinaryDictEncoderUtils.writeUIntToStream(bigramBuffer, bigramFlags,
-                                FormatSpec.PTNODE_ATTRIBUTE_FLAGS_SIZE);
-                        BinaryDictEncoderUtils.writeUIntToStream(bigramBuffer, target.mTerminalId,
-                                FormatSpec.PTNODE_ATTRIBUTE_MAX_ADDRESS_SIZE);
-                    }
+                    mBigramWriter.writeBigramsForOneWord(ptNode.mTerminalId,
+                            ptNode.mBigrams.iterator(), dict);
                 }
             }
         }
-        bigramBuffer.writeTo(mBigramOutStream);
-    }
-
-    private void writeBigramAddressSparseTable() throws IOException {
-        final File lookupIndexFile =
-                new File(mDictDir, mBaseFilename + FormatSpec.BIGRAM_LOOKUP_TABLE_FILE_EXTENSION);
-        final File contentFile =
-                new File(mDictDir, mBaseFilename + FormatSpec.BIGRAM_ADDRESS_TABLE_FILE_EXTENSION);
-        mBigramAddressTable.writeToFiles(lookupIndexFile, new File[] { contentFile });
+        mBigramWriter.closeStreams();
     }
 
     @Override
diff --git a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
index 2662164..c8b62b6 100644
--- a/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
+++ b/java/src/com/android/inputmethod/latin/personalization/DecayingExpandableBinaryDictionaryBase.java
@@ -230,6 +230,7 @@
         mSessions.remove(session);
     }
 
+    @UsedForTesting
     public void clearAndFlushDictionary() {
         // Clear the node structure on memory
         clear();
diff --git a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
index 38159b0..8f21c50 100644
--- a/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
+++ b/native/jni/com_android_inputmethod_latin_BinaryDictionary.cpp
@@ -67,7 +67,6 @@
         valueChars[valueUtf8Length] = '\0';
         HeaderReadWriteUtils::AttributeMap::mapped_type value;
         HeaderReadWriteUtils::insertCharactersIntoVector(valueChars, &value);
-
         attributeMap[key] = value;
     }
 
diff --git a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
index 2694ce8..5ded8f6 100644
--- a/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
+++ b/native/jni/src/suggest/policyimpl/dictionary/header/header_read_write_utils.cpp
@@ -139,6 +139,9 @@
         int *const writingPos) {
     for (AttributeMap::const_iterator it = headerAttributes->begin();
             it != headerAttributes->end(); ++it) {
+        if (it->first.empty() || it->second.empty()) {
+            continue;
+        }
         // Write a key.
         if (!buffer->writeCodePointsAndAdvancePosition(&(it->first.at(0)), it->first.size(),
                 true /* writesTerminator */, writingPos)) {
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
index 0cf4ef9..aa16583 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictDecoderEncoderTests.java
@@ -657,27 +657,21 @@
         addUnigrams(sWords.size(), dict, sWords, null /* shortcutMap */);
         timeWritingDictToFile(file, dict, VERSION3_WITH_DYNAMIC_UPDATE);
 
-        final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file, DictDecoder.USE_BYTEARRAY);
-        try {
-            dictDecoder.openDictBuffer();
-        } catch (IOException e) {
-            // ignore
-            Log.e(TAG, "IOException while opening the buffer", e);
-        }
-        assertTrue("Can't get the buffer", dictDecoder.isDictBufferOpen());
+        final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
+                DictDecoder.USE_WRITABLE_BYTEBUFFER);
 
         try {
             MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
-                    dictDecoder.getTerminalPosition(sWords.get(0)));
-            DynamicBinaryDictIOUtils.deleteWord(dictDecoder, sWords.get(0));
+                    dictUpdater.getTerminalPosition(sWords.get(0)));
+            dictUpdater.deleteWord(sWords.get(0));
             assertEquals(FormatSpec.NOT_VALID_WORD,
-                    dictDecoder.getTerminalPosition(sWords.get(0)));
+                    dictUpdater.getTerminalPosition(sWords.get(0)));
 
             MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD,
-                    dictDecoder.getTerminalPosition(sWords.get(5)));
-            DynamicBinaryDictIOUtils.deleteWord(dictDecoder, sWords.get(5));
+                    dictUpdater.getTerminalPosition(sWords.get(5)));
+            dictUpdater.deleteWord(sWords.get(5));
             assertEquals(FormatSpec.NOT_VALID_WORD,
-                    dictDecoder.getTerminalPosition(sWords.get(5)));
+                    dictUpdater.getTerminalPosition(sWords.get(5)));
         } catch (IOException e) {
         } catch (UnsupportedFormatException e) {
         }
diff --git a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
index a837494..acd6585 100644
--- a/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/makedict/BinaryDictIOUtilsTests.java
@@ -27,9 +27,7 @@
 import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
 import com.android.inputmethod.latin.utils.CollectionUtils;
 
-import java.io.BufferedOutputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -186,46 +184,31 @@
     private long insertAndCheckWord(final File file, final String word, final int frequency,
             final boolean exist, final ArrayList<WeightedString> bigrams,
             final ArrayList<WeightedString> shortcuts) {
-        BufferedOutputStream outStream = null;
         long amountOfTime = -1;
         try {
-            final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
+            final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
                     DictDecoder.USE_WRITABLE_BYTEBUFFER);
-            dictDecoder.openDictBuffer();
-            outStream = new BufferedOutputStream(new FileOutputStream(file, true));
 
             if (!exist) {
                 assertEquals(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
             }
             final long now = System.nanoTime();
-            DynamicBinaryDictIOUtils.insertWord(dictDecoder, outStream, word, frequency, bigrams,
-                    shortcuts, false, false);
+            dictUpdater.insertWord(word, frequency, bigrams, shortcuts, false, false);
             amountOfTime = System.nanoTime() - now;
-            outStream.flush();
             MoreAsserts.assertNotEqual(FormatSpec.NOT_VALID_WORD, getWordPosition(file, word));
-            outStream.close();
         } catch (IOException e) {
             Log.e(TAG, "Raised an IOException while inserting a word", e);
         } catch (UnsupportedFormatException e) {
             Log.e(TAG, "Raised an UnsupportedFormatException error while inserting a word", e);
-        } finally {
-            if (outStream != null) {
-                try {
-                    outStream.close();
-                } catch (IOException e) {
-                    Log.e(TAG, "Failed to close the output stream", e);
-                }
-            }
         }
         return amountOfTime;
     }
 
     private void deleteWord(final File file, final String word) {
         try {
-            final Ver3DictDecoder dictDecoder = new Ver3DictDecoder(file,
+            final Ver3DictUpdater dictUpdater = new Ver3DictUpdater(file,
                     DictDecoder.USE_WRITABLE_BYTEBUFFER);
-            dictDecoder.openDictBuffer();
-            DynamicBinaryDictIOUtils.deleteWord(dictDecoder, word);
+            dictUpdater.deleteWord(word);
         } catch (IOException e) {
         } catch (UnsupportedFormatException e) {
         }
diff --git a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
index ddc9546..7c1decb 100644
--- a/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
+++ b/tests/src/com/android/inputmethod/latin/personalization/UserHistoryDictionaryTests.java
@@ -109,35 +109,54 @@
         dict.close();
     }
 
+    /**
+     * Clear all entries in the user history dictionary.
+     * @param testFilenameSuffix file name suffix used for testing.
+     */
+    private void clearHistory(final String testFilenameSuffix) {
+        final UserHistoryDictionary dict =
+                PersonalizationHelper.getUserHistoryDictionary(getContext(),
+                        testFilenameSuffix /* locale */, mPrefs);
+        dict.clearAndFlushDictionary();
+        dict.close();
+    }
+
+    /**
+     * Shut down executer and wait until all operations of user history are done.
+     * @param testFilenameSuffix file name suffix used for testing.
+     */
+    private void waitForWriting(final String testFilenameSuffix) {
+        try {
+            final UserHistoryDictionary dict =
+                    PersonalizationHelper.getUserHistoryDictionary(getContext(),
+                            testFilenameSuffix, mPrefs);
+            dict.shutdownExecutorForTests();
+            while (!dict.isTerminatedForTests()) {
+                Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
+            }
+        } catch (InterruptedException e) {
+            Log.d(TAG, "InterruptedException: ", e);
+        }
+    }
+
     public void testRandomWords() {
-        File dictFile = null;
         Log.d(TAG, "This test can be used for profiling.");
         Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
         final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
+        final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
+                + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
+
         final int numberOfWords = 1000;
         final Random random = new Random(123456);
 
         try {
+            clearHistory(testFilenameSuffix);
             addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random,
                     true /* checksContents */);
         } finally {
-            try {
-                final UserHistoryDictionary dict =
-                        PersonalizationHelper.getUserHistoryDictionary(getContext(),
-                                testFilenameSuffix, mPrefs);
-                Log.d(TAG, "waiting for writing ...");
-                dict.shutdownExecutorForTests();
-                while (!dict.isTerminatedForTests()) {
-                    Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
-                }
-            } catch (InterruptedException e) {
-                Log.d(TAG, "InterruptedException: " + e);
-            }
-
-            final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
-                    + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
-            dictFile = new File(getContext().getFilesDir(), fileName);
-
+            Log.d(TAG, "waiting for writing ...");
+            waitForWriting(testFilenameSuffix);
+            final File dictFile = new File(getContext().getFilesDir(), fileName);
             if (dictFile != null) {
                 assertTrue(dictFile.exists());
                 assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
@@ -162,6 +181,7 @@
                 final String fileName = UserHistoryDictionary.NAME + "." +
                         testFilenameSuffixes[i] + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
                 dictFiles[i] = new File(getContext().getFilesDir(), fileName);
+                clearHistory(testFilenameSuffixes[i]);
             }
 
             final long start = System.currentTimeMillis();
@@ -178,19 +198,9 @@
             Log.d(TAG, "testStressTestForSwitchingLanguageAndAddingWords took "
                     + (end - start) + " ms");
         } finally {
-            try {
-                Log.d(TAG, "waiting for writing ...");
-                for (int i = 0; i < numberOfLanguages; i++) {
-                    final UserHistoryDictionary dict =
-                            PersonalizationHelper.getUserHistoryDictionary(getContext(),
-                                    testFilenameSuffixes[i], mPrefs);
-                    dict.shutdownExecutorForTests();
-                    while (!dict.isTerminatedForTests()) {
-                        Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
-                    }
-                }
-            } catch (InterruptedException e) {
-                Log.d(TAG, "InterruptedException: " + e);
+            Log.d(TAG, "waiting for writing ...");
+            for (int i = 0; i < numberOfLanguages; i++) {
+                waitForWriting(testFilenameSuffixes[i]);
             }
             for (final File file : dictFiles) {
                 if (file != null) {
@@ -203,33 +213,21 @@
     }
 
     public void testAddManyWords() {
-        File dictFile = null;
         final String testFilenameSuffix = "testRandomWords" + System.currentTimeMillis();
         final int numberOfWords =
                 ExpandableBinaryDictionary.ENABLE_BINARY_DICTIONARY_DYNAMIC_UPDATE ?
                         10000 : 1000;
         final Random random = new Random(123456);
-
-        UserHistoryDictionary dict =
-                PersonalizationHelper.getUserHistoryDictionary(getContext(),
-                        testFilenameSuffix, mPrefs);
+        clearHistory(testFilenameSuffix);
         try {
             addAndWriteRandomWords(testFilenameSuffix, numberOfWords, random,
                     true /* checksContents */);
-            dict.close();
         } finally {
-            try {
-                Log.d(TAG, "waiting for writing ...");
-                dict.shutdownExecutorForTests();
-                while (!dict.isTerminatedForTests()) {
-                    Thread.sleep(WAIT_TERMINATING_IN_MILLISECONDS);
-                }
-            } catch (InterruptedException e) {
-                Log.d(TAG, "InterruptedException: ", e);
-            }
+            Log.d(TAG, "waiting for writing ...");
+            waitForWriting(testFilenameSuffix);
             final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
                     + ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
-            dictFile = new File(getContext().getFilesDir(), fileName);
+            final File dictFile = new File(getContext().getFilesDir(), fileName);
             if (dictFile != null) {
                 assertTrue(dictFile.exists());
                 assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);