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