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/*
18 * !!!!! DO NOT EDIT THIS FILE !!!!!
19 *
20 * This file was generated from
21 *   dictionary/structure/v4/content/sparse_table_dict_content.h
22 */
23
24#ifndef LATINIME_BACKWARD_V402_SPARSE_TABLE_DICT_CONTENT_H
25#define LATINIME_BACKWARD_V402_SPARSE_TABLE_DICT_CONTENT_H
26
27#include "defines.h"
28#include "dictionary/structure/backward/v402/content/dict_content.h"
29#include "dictionary/structure/backward/v402/ver4_dict_constants.h"
30#include "dictionary/utils/buffer_with_extendable_buffer.h"
31#include "dictionary/utils/dict_file_writing_utils.h"
32#include "dictionary/utils/mmapped_buffer.h"
33#include "dictionary/utils/sparse_table.h"
34#include "utils/byte_array_view.h"
35
36namespace latinime {
37namespace backward {
38namespace v402 {
39
40// TODO: Support multiple contents.
41class SparseTableDictContent : public DictContent {
42 public:
43    AK_FORCE_INLINE SparseTableDictContent(const char *const dictPath,
44            const char *const lookupTableFileName, const char *const addressTableFileName,
45            const char *const contentFileName, const bool isUpdatable,
46            const int sparseTableBlockSize, const int sparseTableDataSize)
47            : mLookupTableBuffer(
48                      MmappedBuffer::openBuffer(dictPath, lookupTableFileName, isUpdatable)),
49              mAddressTableBuffer(
50                      MmappedBuffer::openBuffer(dictPath, addressTableFileName, isUpdatable)),
51              mContentBuffer(
52                      MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
53              mExpandableLookupTableBuffer(
54                      mLookupTableBuffer ? mLookupTableBuffer->getReadWriteByteArrayView() :
55                              ReadWriteByteArrayView(),
56                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
57              mExpandableAddressTableBuffer(
58                      mAddressTableBuffer ? mAddressTableBuffer->getReadWriteByteArrayView() :
59                              ReadWriteByteArrayView(),
60                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
61              mExpandableContentBuffer(
62                      mContentBuffer ? mContentBuffer->getReadWriteByteArrayView() :
63                              ReadWriteByteArrayView(),
64                      BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
65              mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
66                      sparseTableBlockSize, sparseTableDataSize),
67              mIsValid(mLookupTableBuffer && mAddressTableBuffer && mContentBuffer) {}
68
69    SparseTableDictContent(const int sparseTableBlockSize, const int sparseTableDataSize)
70            : mLookupTableBuffer(), mAddressTableBuffer(), mContentBuffer(),
71              mExpandableLookupTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
72              mExpandableAddressTableBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
73              mExpandableContentBuffer(Ver4DictConstants::MAX_DICTIONARY_SIZE),
74              mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
75                      sparseTableBlockSize, sparseTableDataSize), mIsValid(true) {}
76
77    virtual ~SparseTableDictContent() {}
78
79    virtual bool isValid() const {
80        return mIsValid;
81    }
82
83    bool isNearSizeLimit() const {
84        return mExpandableLookupTableBuffer.isNearSizeLimit()
85                || mExpandableAddressTableBuffer.isNearSizeLimit()
86                || mExpandableContentBuffer.isNearSizeLimit();
87    }
88
89 protected:
90    SparseTable *getUpdatableAddressLookupTable() {
91        return &mAddressLookupTable;
92    }
93
94    const SparseTable *getAddressLookupTable() const {
95        return &mAddressLookupTable;
96    }
97
98    BufferWithExtendableBuffer *getWritableContentBuffer() {
99        return &mExpandableContentBuffer;
100    }
101
102    const BufferWithExtendableBuffer *getContentBuffer() const {
103        return &mExpandableContentBuffer;
104    }
105
106    bool flush(const char *const dictDirPath, const char *const lookupTableFileName,
107            const char *const addressTableFileName, const char *const contentFileName) const;
108
109 private:
110    DISALLOW_IMPLICIT_CONSTRUCTORS(SparseTableDictContent);
111
112    const MmappedBuffer::MmappedBufferPtr mLookupTableBuffer;
113    const MmappedBuffer::MmappedBufferPtr mAddressTableBuffer;
114    const MmappedBuffer::MmappedBufferPtr mContentBuffer;
115    BufferWithExtendableBuffer mExpandableLookupTableBuffer;
116    BufferWithExtendableBuffer mExpandableAddressTableBuffer;
117    BufferWithExtendableBuffer mExpandableContentBuffer;
118    SparseTable mAddressLookupTable;
119    const bool mIsValid;
120};
121} // namespace v402
122} // namespace backward
123} // namespace latinime
124#endif /* LATINIME_BACKWARD_V402_SPARSE_TABLE_DICT_CONTENT_H */
125