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 "defines.h"
24 #include "dictionary/header/header_policy.h"
25 #include "dictionary/structure/v4/content/language_model_dict_content.h"
26 #include "dictionary/structure/v4/content/shortcut_dict_content.h"
27 #include "dictionary/structure/v4/content/terminal_position_lookup_table.h"
28 #include "dictionary/structure/v4/ver4_dict_constants.h"
29 #include "dictionary/utils/buffer_with_extendable_buffer.h"
30 #include "dictionary/utils/mmapped_buffer.h"
31 
32 namespace latinime {
33 
34 class Ver4DictBuffers {
35  public:
36     typedef std::unique_ptr<Ver4DictBuffers> Ver4DictBuffersPtr;
37 
38     static Ver4DictBuffersPtr openVer4DictBuffers(const char *const dictDirPath,
39             MmappedBuffer::MmappedBufferPtr &&headerBuffer,
40             const FormatUtils::FORMAT_VERSION formatVersion);
41 
createVer4DictBuffers(const HeaderPolicy * const headerPolicy,const int maxTrieSize)42     static AK_FORCE_INLINE Ver4DictBuffersPtr createVer4DictBuffers(
43             const HeaderPolicy *const headerPolicy, const int maxTrieSize) {
44         return Ver4DictBuffersPtr(new Ver4DictBuffers(headerPolicy, maxTrieSize));
45     }
46 
isValid()47     AK_FORCE_INLINE bool isValid() const {
48         return mHeaderBuffer && mDictBuffer && mHeaderPolicy.isValid();
49     }
50 
isNearSizeLimit()51     AK_FORCE_INLINE bool isNearSizeLimit() const {
52         return mExpandableTrieBuffer.isNearSizeLimit()
53                 || mTerminalPositionLookupTable.isNearSizeLimit()
54                 || mLanguageModelDictContent.isNearSizeLimit()
55                 || mShortcutDictContent.isNearSizeLimit();
56     }
57 
getHeaderPolicy()58     AK_FORCE_INLINE const HeaderPolicy *getHeaderPolicy() const {
59         return &mHeaderPolicy;
60     }
61 
getWritableHeaderBuffer()62     AK_FORCE_INLINE BufferWithExtendableBuffer *getWritableHeaderBuffer() {
63         return &mExpandableHeaderBuffer;
64     }
65 
getWritableTrieBuffer()66     AK_FORCE_INLINE BufferWithExtendableBuffer *getWritableTrieBuffer() {
67         return &mExpandableTrieBuffer;
68     }
69 
getTrieBuffer()70     AK_FORCE_INLINE const BufferWithExtendableBuffer *getTrieBuffer() const {
71         return &mExpandableTrieBuffer;
72     }
73 
getMutableTerminalPositionLookupTable()74     AK_FORCE_INLINE TerminalPositionLookupTable *getMutableTerminalPositionLookupTable() {
75         return &mTerminalPositionLookupTable;
76     }
77 
getTerminalPositionLookupTable()78     AK_FORCE_INLINE const TerminalPositionLookupTable *getTerminalPositionLookupTable() const {
79         return &mTerminalPositionLookupTable;
80     }
81 
getMutableLanguageModelDictContent()82     AK_FORCE_INLINE LanguageModelDictContent *getMutableLanguageModelDictContent() {
83         return &mLanguageModelDictContent;
84     }
85 
getLanguageModelDictContent()86     AK_FORCE_INLINE const LanguageModelDictContent *getLanguageModelDictContent() const {
87         return &mLanguageModelDictContent;
88     }
89 
getMutableShortcutDictContent()90     AK_FORCE_INLINE ShortcutDictContent *getMutableShortcutDictContent() {
91         return &mShortcutDictContent;
92     }
93 
getShortcutDictContent()94     AK_FORCE_INLINE const ShortcutDictContent *getShortcutDictContent() const {
95         return &mShortcutDictContent;
96     }
97 
isUpdatable()98     AK_FORCE_INLINE bool isUpdatable() const {
99         return mIsUpdatable;
100     }
101 
flush(const char * const dictDirPath)102     bool flush(const char *const dictDirPath) const {
103         return flushHeaderAndDictBuffers(dictDirPath, &mExpandableHeaderBuffer);
104     }
105 
106     bool flushHeaderAndDictBuffers(const char *const dictDirPath,
107             const BufferWithExtendableBuffer *const headerBuffer) const;
108 
109  private:
110     DISALLOW_COPY_AND_ASSIGN(Ver4DictBuffers);
111 
112     Ver4DictBuffers(MmappedBuffer::MmappedBufferPtr &&headerBuffer,
113             MmappedBuffer::MmappedBufferPtr &&bodyBuffer,
114             const FormatUtils::FORMAT_VERSION formatVersion,
115             const std::vector<ReadWriteByteArrayView> &contentBuffers);
116 
117     Ver4DictBuffers(const HeaderPolicy *const headerPolicy, const int maxTrieSize);
118 
119     bool flushDictBuffers(FILE *const file) const;
120 
121     const MmappedBuffer::MmappedBufferPtr mHeaderBuffer;
122     const MmappedBuffer::MmappedBufferPtr mDictBuffer;
123     const HeaderPolicy mHeaderPolicy;
124     BufferWithExtendableBuffer mExpandableHeaderBuffer;
125     BufferWithExtendableBuffer mExpandableTrieBuffer;
126     TerminalPositionLookupTable mTerminalPositionLookupTable;
127     LanguageModelDictContent mLanguageModelDictContent;
128     ShortcutDictContent mShortcutDictContent;
129     const int mIsUpdatable;
130 };
131 } // namespace latinime
132 #endif /* LATINIME_VER4_DICT_BUFFER_H */
133