From 941811cbd6ad34c65056fc685e0885ea374588cd Mon Sep 17 00:00:00 2001 From: Keisuke Kuroyanagi Date: Thu, 22 Aug 2013 18:05:15 +0900 Subject: [PATCH] Introduce ExtendableBuffer for dynamic update. Bug: 6669677 Change-Id: I6343c348334c0dace8b12a71eb992d6f040e8c95 --- native/jni/Android.mk | 6 +- .../dictionary/dynamic_patricia_trie_policy.h | 5 +- .../dictionary/utils/extendable_buffer.cpp | 25 +++++++ .../dictionary/utils/extendable_buffer.h | 70 +++++++++++++++++++ 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.cpp create mode 100644 native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.h diff --git a/native/jni/Android.mk b/native/jni/Android.mk index a51fe3c03..bf188971e 100644 --- a/native/jni/Android.mk +++ b/native/jni/Android.mk @@ -71,14 +71,16 @@ LATIN_IME_CORE_SRC_FILES := \ header/header_policy.cpp \ header/header_reading_utils.cpp \ shortcut/shortcut_list_reading_utils.cpp \ - utils/byte_array_utils.cpp \ - utils/format_utils.cpp \ dictionary_structure_with_buffer_policy_factory.cpp \ dynamic_patricia_trie_node_reader.cpp \ dynamic_patricia_trie_policy.cpp \ dynamic_patricia_trie_reading_utils.cpp \ patricia_trie_policy.cpp \ patricia_trie_reading_utils.cpp) \ + $(addprefix suggest/policyimpl/dictionary/utils/, \ + byte_array_utils.cpp \ + extendable_buffer.cpp \ + format_utils.cpp) \ suggest/policyimpl/gesture/gesture_suggest_policy_factory.cpp \ $(addprefix suggest/policyimpl/typing/, \ scoring_params.cpp \ diff --git a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h index 8ba057b20..0e5920f42 100644 --- a/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h +++ b/native/jni/src/suggest/policyimpl/dictionary/dynamic_patricia_trie_policy.h @@ -24,6 +24,7 @@ #include "suggest/policyimpl/dictionary/bigram/bigram_list_policy.h" #include "suggest/policyimpl/dictionary/header/header_policy.h" #include "suggest/policyimpl/dictionary/shortcut/shortcut_list_policy.h" +#include "suggest/policyimpl/dictionary/utils/extendable_buffer.h" #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" namespace latinime { @@ -34,7 +35,7 @@ class DicNodeVector; class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { public: DynamicPatriciaTriePolicy(const MmappedBuffer *const buffer) - : mBuffer(buffer), mHeaderPolicy(mBuffer->getBuffer()), + : mBuffer(buffer), mExtendableBuffer(), mHeaderPolicy(mBuffer->getBuffer()), mDictRoot(mBuffer->getBuffer() + mHeaderPolicy.getSize()), mBigramListPolicy(mDictRoot), mShortcutListPolicy(mDictRoot) {} @@ -87,8 +88,10 @@ class DynamicPatriciaTriePolicy : public DictionaryStructureWithBufferPolicy { static const int MAX_CHILD_COUNT_TO_AVOID_INFINITE_LOOP; const MmappedBuffer *const mBuffer; + const ExtendableBuffer mExtendableBuffer; const HeaderPolicy mHeaderPolicy; // TODO: Consolidate mDictRoot. + // CAVEAT!: Be careful about array out of bound access with mDictRoot const uint8_t *const mDictRoot; const BigramListPolicy mBigramListPolicy; const ShortcutListPolicy mShortcutListPolicy; diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.cpp b/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.cpp new file mode 100644 index 000000000..e55cc2410 --- /dev/null +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.cpp @@ -0,0 +1,25 @@ +/* + * 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. + */ + +#include "suggest/policyimpl/dictionary/utils/extendable_buffer.h" + +namespace latinime { + +const size_t ExtendableBuffer::INITIAL_BUFFER_SIZE = 16 * 1024; +const size_t ExtendableBuffer::MAX_BUFFER_SIZE = 1024 * 1024; +const size_t ExtendableBuffer::EXTEND_BUFFER_SIZE_STEP = 16 * 1024; + +} diff --git a/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.h b/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.h new file mode 100644 index 000000000..d902d19c8 --- /dev/null +++ b/native/jni/src/suggest/policyimpl/dictionary/utils/extendable_buffer.h @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#ifndef LATINIME_EXTENDABLE_BUFFER_H +#define LATINIME_EXTENDABLE_BUFFER_H + +#include +#include +#include + +#include "defines.h" + +namespace latinime { + +// This is used as a buffer that can be extended for updatable dictionaries. +class ExtendableBuffer { + public: + ExtendableBuffer() : mBuffer(INITIAL_BUFFER_SIZE), mUsedSize(0) {} + + AK_FORCE_INLINE uint8_t *getBuffer() { + return &mBuffer[0]; + } + + // Return if the buffer is successfully extended or not. + AK_FORCE_INLINE bool extendBuffer() { + if (mBuffer.size() + EXTEND_BUFFER_SIZE_STEP > MAX_BUFFER_SIZE) { + return false; + } + mBuffer.resize(mBuffer.size() + EXTEND_BUFFER_SIZE_STEP); + return true; + } + + AK_FORCE_INLINE int getAllocatedSize() const { + return mBuffer.size(); + } + + AK_FORCE_INLINE int getUsedSize() const { + return mUsedSize; + } + + AK_FORCE_INLINE void clear() { + mUsedSize = 0; + mBuffer.clear(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(ExtendableBuffer); + + static const size_t INITIAL_BUFFER_SIZE; + static const size_t MAX_BUFFER_SIZE; + static const size_t EXTEND_BUFFER_SIZE_STEP; + + std::vector mBuffer; + int mUsedSize; +}; +} +#endif /* LATINIME_MMAPED_BUFFER_H */