| OLD | NEW |
| (Empty) | |
| 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 #ifndef LATINIME_VER4_DICT_BUFFER_H |
| 18 #define LATINIME_VER4_DICT_BUFFER_H |
| 19 |
| 20 #include <cstdio> |
| 21 #include <memory> |
| 22 |
| 23 #include "third_party/prediction/defines.h" |
| 24 #include "third_party/prediction/suggest/policyimpl/dictionary/header/header_pol
icy.h" |
| 25 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/cont
ent/bigram_dict_content.h" |
| 26 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/cont
ent/language_model_dict_content.h" |
| 27 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/cont
ent/shortcut_dict_content.h" |
| 28 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/cont
ent/terminal_position_lookup_table.h" |
| 29 #include "third_party/prediction/suggest/policyimpl/dictionary/structure/v4/ver4
_dict_constants.h" |
| 30 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/buffer_with
_extendable_buffer.h" |
| 31 #include "third_party/prediction/suggest/policyimpl/dictionary/utils/mmapped_buf
fer.h" |
| 32 |
| 33 namespace latinime { |
| 34 |
| 35 class Ver4DictBuffers { |
| 36 public: |
| 37 typedef std::unique_ptr<Ver4DictBuffers> Ver4DictBuffersPtr; |
| 38 |
| 39 static Ver4DictBuffersPtr openVer4DictBuffers( |
| 40 const char* const dictDirPath, |
| 41 MmappedBuffer::MmappedBufferPtr&& headerBuffer, |
| 42 const FormatUtils::FORMAT_VERSION formatVersion); |
| 43 |
| 44 static AK_FORCE_INLINE Ver4DictBuffersPtr |
| 45 createVer4DictBuffers(const HeaderPolicy* const headerPolicy, |
| 46 const int maxTrieSize) { |
| 47 return Ver4DictBuffersPtr(new Ver4DictBuffers(headerPolicy, maxTrieSize)); |
| 48 } |
| 49 |
| 50 AK_FORCE_INLINE bool isValid() const { |
| 51 return mHeaderBuffer && mDictBuffer && mHeaderPolicy.isValid(); |
| 52 } |
| 53 |
| 54 AK_FORCE_INLINE bool isNearSizeLimit() const { |
| 55 return mExpandableTrieBuffer.isNearSizeLimit() || |
| 56 mTerminalPositionLookupTable.isNearSizeLimit() || |
| 57 mLanguageModelDictContent.isNearSizeLimit() || |
| 58 mBigramDictContent.isNearSizeLimit() || |
| 59 mShortcutDictContent.isNearSizeLimit(); |
| 60 } |
| 61 |
| 62 AK_FORCE_INLINE const HeaderPolicy* getHeaderPolicy() const { |
| 63 return &mHeaderPolicy; |
| 64 } |
| 65 |
| 66 AK_FORCE_INLINE BufferWithExtendableBuffer* getWritableHeaderBuffer() { |
| 67 return &mExpandableHeaderBuffer; |
| 68 } |
| 69 |
| 70 AK_FORCE_INLINE BufferWithExtendableBuffer* getWritableTrieBuffer() { |
| 71 return &mExpandableTrieBuffer; |
| 72 } |
| 73 |
| 74 AK_FORCE_INLINE const BufferWithExtendableBuffer* getTrieBuffer() const { |
| 75 return &mExpandableTrieBuffer; |
| 76 } |
| 77 |
| 78 AK_FORCE_INLINE TerminalPositionLookupTable* |
| 79 getMutableTerminalPositionLookupTable() { |
| 80 return &mTerminalPositionLookupTable; |
| 81 } |
| 82 |
| 83 AK_FORCE_INLINE const TerminalPositionLookupTable* |
| 84 getTerminalPositionLookupTable() const { |
| 85 return &mTerminalPositionLookupTable; |
| 86 } |
| 87 |
| 88 AK_FORCE_INLINE LanguageModelDictContent* |
| 89 getMutableLanguageModelDictContent() { |
| 90 return &mLanguageModelDictContent; |
| 91 } |
| 92 |
| 93 AK_FORCE_INLINE const LanguageModelDictContent* getLanguageModelDictContent() |
| 94 const { |
| 95 return &mLanguageModelDictContent; |
| 96 } |
| 97 |
| 98 AK_FORCE_INLINE BigramDictContent* getMutableBigramDictContent() { |
| 99 return &mBigramDictContent; |
| 100 } |
| 101 |
| 102 AK_FORCE_INLINE const BigramDictContent* getBigramDictContent() const { |
| 103 return &mBigramDictContent; |
| 104 } |
| 105 |
| 106 AK_FORCE_INLINE ShortcutDictContent* getMutableShortcutDictContent() { |
| 107 return &mShortcutDictContent; |
| 108 } |
| 109 |
| 110 AK_FORCE_INLINE const ShortcutDictContent* getShortcutDictContent() const { |
| 111 return &mShortcutDictContent; |
| 112 } |
| 113 |
| 114 AK_FORCE_INLINE bool isUpdatable() const { return mIsUpdatable; } |
| 115 |
| 116 bool flush(const char* const dictDirPath) const { |
| 117 return flushHeaderAndDictBuffers(dictDirPath, &mExpandableHeaderBuffer); |
| 118 } |
| 119 |
| 120 bool flushHeaderAndDictBuffers( |
| 121 const char* const dictDirPath, |
| 122 const BufferWithExtendableBuffer* const headerBuffer) const; |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(Ver4DictBuffers); |
| 126 |
| 127 Ver4DictBuffers(MmappedBuffer::MmappedBufferPtr&& headerBuffer, |
| 128 MmappedBuffer::MmappedBufferPtr&& bodyBuffer, |
| 129 const FormatUtils::FORMAT_VERSION formatVersion, |
| 130 const std::vector<uint8_t*>& contentBuffers, |
| 131 const std::vector<int>& contentBufferSizes); |
| 132 |
| 133 Ver4DictBuffers(const HeaderPolicy* const headerPolicy, |
| 134 const int maxTrieSize); |
| 135 |
| 136 bool flushDictBuffers(FILE* const file) const; |
| 137 |
| 138 const MmappedBuffer::MmappedBufferPtr mHeaderBuffer; |
| 139 const MmappedBuffer::MmappedBufferPtr mDictBuffer; |
| 140 const HeaderPolicy mHeaderPolicy; |
| 141 BufferWithExtendableBuffer mExpandableHeaderBuffer; |
| 142 BufferWithExtendableBuffer mExpandableTrieBuffer; |
| 143 TerminalPositionLookupTable mTerminalPositionLookupTable; |
| 144 LanguageModelDictContent mLanguageModelDictContent; |
| 145 BigramDictContent mBigramDictContent; |
| 146 ShortcutDictContent mShortcutDictContent; |
| 147 const int mIsUpdatable; |
| 148 }; |
| 149 } // namespace latinime |
| 150 #endif /* LATINIME_VER4_DICT_BUFFER_H */ |
| OLD | NEW |