Make MmappedBuffer use byte array view.

Bug: 16691311
Change-Id: I2122c01ee27c33e11dec52643925c069927bea2b
main
Keisuke Kuroyanagi 2014-08-01 19:26:01 +09:00
parent 0257e40345
commit c0c674cdc0
13 changed files with 76 additions and 53 deletions

View File

@ -30,6 +30,7 @@
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
namespace backward { namespace backward {
@ -40,8 +41,9 @@ class SingleDictContent : public DictContent {
SingleDictContent(const char *const dictPath, const char *const contentFileName, SingleDictContent(const char *const dictPath, const char *const contentFileName,
const bool isUpdatable) const bool isUpdatable)
: mMmappedBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)), : mMmappedBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
mExpandableContentBuffer(mMmappedBuffer ? mMmappedBuffer->getBuffer() : nullptr, mExpandableContentBuffer(
mMmappedBuffer ? mMmappedBuffer->getBufferSize() : 0, mMmappedBuffer ? mMmappedBuffer->getReadWriteByteArrayView() :
ReadWriteByteArrayView(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mIsValid(mMmappedBuffer) {} mIsValid(mMmappedBuffer) {}

View File

@ -31,6 +31,7 @@
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
#include "suggest/policyimpl/dictionary/utils/sparse_table.h" #include "suggest/policyimpl/dictionary/utils/sparse_table.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
namespace backward { namespace backward {
@ -50,15 +51,16 @@ class SparseTableDictContent : public DictContent {
mContentBuffer( mContentBuffer(
MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)), MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
mExpandableLookupTableBuffer( mExpandableLookupTableBuffer(
mLookupTableBuffer ? mLookupTableBuffer->getBuffer() : nullptr, mLookupTableBuffer ? mLookupTableBuffer->getReadWriteByteArrayView() :
mLookupTableBuffer ? mLookupTableBuffer->getBufferSize() : 0, ReadWriteByteArrayView(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableAddressTableBuffer( mExpandableAddressTableBuffer(
mAddressTableBuffer ? mAddressTableBuffer->getBuffer() : nullptr, mAddressTableBuffer ? mAddressTableBuffer->getReadWriteByteArrayView() :
mAddressTableBuffer ? mAddressTableBuffer->getBufferSize() : 0, ReadWriteByteArrayView(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableContentBuffer(mContentBuffer ? mContentBuffer->getBuffer() : nullptr, mExpandableContentBuffer(
mContentBuffer ? mContentBuffer->getBufferSize() : 0, mContentBuffer ? mContentBuffer->getReadWriteByteArrayView() :
ReadWriteByteArrayView(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer, mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
sparseTableBlockSize, sparseTableDataSize), sparseTableBlockSize, sparseTableDataSize),

View File

@ -30,6 +30,7 @@
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
#include "suggest/policyimpl/dictionary/utils/file_utils.h" #include "suggest/policyimpl/dictionary/utils/file_utils.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
namespace backward { namespace backward {
@ -130,12 +131,12 @@ Ver4DictBuffers::Ver4DictBuffers(const char *const dictPath,
: mHeaderBuffer(std::move(headerBuffer)), : mHeaderBuffer(std::move(headerBuffer)),
mDictBuffer(MmappedBuffer::openBuffer(dictPath, mDictBuffer(MmappedBuffer::openBuffer(dictPath,
Ver4DictConstants::TRIE_FILE_EXTENSION, isUpdatable)), Ver4DictConstants::TRIE_FILE_EXTENSION, isUpdatable)),
mHeaderPolicy(mHeaderBuffer->getBuffer(), formatVersion), mHeaderPolicy(mHeaderBuffer->getReadOnlyByteArrayView().data(), formatVersion),
mExpandableHeaderBuffer(mHeaderBuffer ? mHeaderBuffer->getBuffer() : nullptr, mExpandableHeaderBuffer(mHeaderBuffer->getReadWriteByteArrayView(),
mHeaderPolicy.getSize(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableTrieBuffer(mDictBuffer ? mDictBuffer->getBuffer() : nullptr, mExpandableTrieBuffer(
mDictBuffer ? mDictBuffer->getBufferSize() : 0, mDictBuffer ? mDictBuffer->getReadWriteByteArrayView() :
ReadWriteByteArrayView(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mTerminalPositionLookupTable(dictPath, isUpdatable), mTerminalPositionLookupTable(dictPath, isUpdatable),
mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable), mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(), isUpdatable),

View File

@ -31,6 +31,7 @@
#include "suggest/policyimpl/dictionary/utils/file_utils.h" #include "suggest/policyimpl/dictionary/utils/file_utils.h"
#include "suggest/policyimpl/dictionary/utils/format_utils.h" #include "suggest/policyimpl/dictionary/utils/format_utils.h"
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -110,7 +111,8 @@ template<class DictConstants, class DictBuffers, class DictBuffersPtr, class Str
return nullptr; return nullptr;
} }
const FormatUtils::FORMAT_VERSION formatVersion = FormatUtils::detectFormatVersion( const FormatUtils::FORMAT_VERSION formatVersion = FormatUtils::detectFormatVersion(
mmappedBuffer->getBuffer(), mmappedBuffer->getBufferSize()); mmappedBuffer->getReadOnlyByteArrayView().data(),
mmappedBuffer->getReadOnlyByteArrayView().size());
switch (formatVersion) { switch (formatVersion) {
case FormatUtils::VERSION_2: case FormatUtils::VERSION_2:
AKLOGE("Given path is a directory but the format is version 2. path: %s", path); AKLOGE("Given path is a directory but the format is version 2. path: %s", path);
@ -172,8 +174,8 @@ template<class DictConstants, class DictBuffers, class DictBuffersPtr, class Str
if (!mmappedBuffer) { if (!mmappedBuffer) {
return nullptr; return nullptr;
} }
switch (FormatUtils::detectFormatVersion(mmappedBuffer->getBuffer(), switch (FormatUtils::detectFormatVersion(mmappedBuffer->getReadOnlyByteArrayView().data(),
mmappedBuffer->getBufferSize())) { mmappedBuffer->getReadOnlyByteArrayView().size())) {
case FormatUtils::VERSION_2: case FormatUtils::VERSION_2:
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr( return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new PatriciaTriePolicy(std::move(mmappedBuffer))); new PatriciaTriePolicy(std::move(mmappedBuffer)));

View File

@ -29,6 +29,7 @@
#include "suggest/policyimpl/dictionary/structure/v2/ver2_pt_node_array_reader.h" #include "suggest/policyimpl/dictionary/structure/v2/ver2_pt_node_array_reader.h"
#include "suggest/policyimpl/dictionary/utils/format_utils.h" #include "suggest/policyimpl/dictionary/utils/format_utils.h"
#include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h" #include "suggest/policyimpl/dictionary/utils/mmapped_buffer.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -39,9 +40,12 @@ class PatriciaTriePolicy : public DictionaryStructureWithBufferPolicy {
public: public:
PatriciaTriePolicy(MmappedBuffer::MmappedBufferPtr mmappedBuffer) PatriciaTriePolicy(MmappedBuffer::MmappedBufferPtr mmappedBuffer)
: mMmappedBuffer(std::move(mmappedBuffer)), : mMmappedBuffer(std::move(mmappedBuffer)),
mHeaderPolicy(mMmappedBuffer->getBuffer(), FormatUtils::VERSION_2), mHeaderPolicy(mMmappedBuffer->getReadOnlyByteArrayView().data(),
mDictRoot(mMmappedBuffer->getBuffer() + mHeaderPolicy.getSize()), FormatUtils::VERSION_2),
mDictBufferSize(mMmappedBuffer->getBufferSize() - mHeaderPolicy.getSize()), mDictRoot(mMmappedBuffer->getReadOnlyByteArrayView().data()
+ mHeaderPolicy.getSize()),
mDictBufferSize(mMmappedBuffer->getReadOnlyByteArrayView().size()
- mHeaderPolicy.getSize()),
mBigramListPolicy(mDictRoot, mDictBufferSize), mShortcutListPolicy(mDictRoot), mBigramListPolicy(mDictRoot, mDictBufferSize), mShortcutListPolicy(mDictRoot),
mPtNodeReader(mDictRoot, mDictBufferSize, &mBigramListPolicy, &mShortcutListPolicy), mPtNodeReader(mDictRoot, mDictBufferSize, &mBigramListPolicy, &mShortcutListPolicy),
mPtNodeArrayReader(mDictRoot, mDictBufferSize), mPtNodeArrayReader(mDictRoot, mDictBufferSize),

View File

@ -24,13 +24,14 @@
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
class SingleDictContent { class SingleDictContent {
public: public:
SingleDictContent(uint8_t *const buffer, const int bufferSize) SingleDictContent(uint8_t *const buffer, const int bufferSize)
: mExpandableContentBuffer(buffer, bufferSize, : mExpandableContentBuffer(ReadWriteByteArrayView(buffer, bufferSize),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {} BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
SingleDictContent() SingleDictContent()

View File

@ -24,6 +24,7 @@
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h" #include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
#include "suggest/policyimpl/dictionary/utils/sparse_table.h" #include "suggest/policyimpl/dictionary/utils/sparse_table.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -32,14 +33,17 @@ class SparseTableDictContent {
public: public:
AK_FORCE_INLINE SparseTableDictContent(uint8_t *const *buffers, const int *bufferSizes, AK_FORCE_INLINE SparseTableDictContent(uint8_t *const *buffers, const int *bufferSizes,
const int sparseTableBlockSize, const int sparseTableDataSize) const int sparseTableBlockSize, const int sparseTableDataSize)
: mExpandableLookupTableBuffer(buffers[LOOKUP_TABLE_BUFFER_INDEX], : mExpandableLookupTableBuffer(
bufferSizes[LOOKUP_TABLE_BUFFER_INDEX], ReadWriteByteArrayView(buffers[LOOKUP_TABLE_BUFFER_INDEX],
bufferSizes[LOOKUP_TABLE_BUFFER_INDEX]),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableAddressTableBuffer(buffers[ADDRESS_TABLE_BUFFER_INDEX], mExpandableAddressTableBuffer(
bufferSizes[ADDRESS_TABLE_BUFFER_INDEX], ReadWriteByteArrayView(buffers[ADDRESS_TABLE_BUFFER_INDEX],
bufferSizes[ADDRESS_TABLE_BUFFER_INDEX]),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableContentBuffer(buffers[CONTENT_BUFFER_INDEX], mExpandableContentBuffer(
bufferSizes[CONTENT_BUFFER_INDEX], ReadWriteByteArrayView(buffers[CONTENT_BUFFER_INDEX],
bufferSizes[CONTENT_BUFFER_INDEX]),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer, mAddressLookupTable(&mExpandableLookupTableBuffer, &mExpandableAddressTableBuffer,
sparseTableBlockSize, sparseTableDataSize) {} sparseTableBlockSize, sparseTableDataSize) {}

View File

@ -26,6 +26,7 @@
#include "suggest/policyimpl/dictionary/utils/byte_array_utils.h" #include "suggest/policyimpl/dictionary/utils/byte_array_utils.h"
#include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h" #include "suggest/policyimpl/dictionary/utils/dict_file_writing_utils.h"
#include "suggest/policyimpl/dictionary/utils/file_utils.h" #include "suggest/policyimpl/dictionary/utils/file_utils.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -46,14 +47,16 @@ namespace latinime {
} }
std::vector<uint8_t *> buffers; std::vector<uint8_t *> buffers;
std::vector<int> bufferSizes; std::vector<int> bufferSizes;
uint8_t *const buffer = bodyBuffer->getBuffer(); const ReadWriteByteArrayView buffer = bodyBuffer->getReadWriteByteArrayView();
int position = 0; int position = 0;
while (position < bodyBuffer->getBufferSize()) { while (position < static_cast<int>(buffer.size())) {
const int bufferSize = ByteArrayUtils::readUint32AndAdvancePosition(buffer, &position); const int bufferSize = ByteArrayUtils::readUint32AndAdvancePosition(
buffers.push_back(buffer + position); buffer.data(), &position);
bufferSizes.push_back(bufferSize); const ReadWriteByteArrayView subBuffer = buffer.subView(position, bufferSize);
buffers.push_back(subBuffer.data());
bufferSizes.push_back(subBuffer.size());
position += bufferSize; position += bufferSize;
if (bufferSize < 0 || position < 0 || position > bodyBuffer->getBufferSize()) { if (bufferSize < 0 || position < 0 || position > static_cast<int>(buffer.size())) {
AKLOGE("The dict body file is corrupted."); AKLOGE("The dict body file is corrupted.");
return Ver4DictBuffersPtr(nullptr); return Ver4DictBuffersPtr(nullptr);
} }
@ -177,12 +180,12 @@ Ver4DictBuffers::Ver4DictBuffers(MmappedBuffer::MmappedBufferPtr &&headerBuffer,
const FormatUtils::FORMAT_VERSION formatVersion, const FormatUtils::FORMAT_VERSION formatVersion,
const std::vector<uint8_t *> &contentBuffers, const std::vector<int> &contentBufferSizes) const std::vector<uint8_t *> &contentBuffers, const std::vector<int> &contentBufferSizes)
: mHeaderBuffer(std::move(headerBuffer)), mDictBuffer(std::move(bodyBuffer)), : mHeaderBuffer(std::move(headerBuffer)), mDictBuffer(std::move(bodyBuffer)),
mHeaderPolicy(mHeaderBuffer->getBuffer(), formatVersion), mHeaderPolicy(mHeaderBuffer->getReadOnlyByteArrayView().data(), formatVersion),
mExpandableHeaderBuffer(mHeaderBuffer ? mHeaderBuffer->getBuffer() : nullptr, mExpandableHeaderBuffer(mHeaderBuffer->getReadWriteByteArrayView(),
mHeaderPolicy.getSize(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mExpandableTrieBuffer(contentBuffers[Ver4DictConstants::TRIE_BUFFER_INDEX], mExpandableTrieBuffer(
contentBufferSizes[Ver4DictConstants::TRIE_BUFFER_INDEX], ReadWriteByteArrayView(contentBuffers[Ver4DictConstants::TRIE_BUFFER_INDEX],
contentBufferSizes[Ver4DictConstants::TRIE_BUFFER_INDEX]),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE), BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mTerminalPositionLookupTable( mTerminalPositionLookupTable(
contentBuffers[Ver4DictConstants::TERMINAL_ADDRESS_LOOKUP_TABLE_BUFFER_INDEX], contentBuffers[Ver4DictConstants::TERMINAL_ADDRESS_LOOKUP_TABLE_BUFFER_INDEX],

View File

@ -35,15 +35,14 @@ class BufferWithExtendableBuffer {
public: public:
static const size_t DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE; static const size_t DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE;
BufferWithExtendableBuffer(uint8_t *const originalBuffer, const int originalBufferSize, BufferWithExtendableBuffer(const ReadWriteByteArrayView originalBuffer,
const int maxAdditionalBufferSize) const int maxAdditionalBufferSize)
: mOriginalBuffer(originalBuffer, originalBufferSize), : mOriginalBuffer(originalBuffer), mAdditionalBuffer(), mUsedAdditionalBufferSize(0),
mAdditionalBuffer(0), mUsedAdditionalBufferSize(0),
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {} mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
// Without original buffer. // Without original buffer.
BufferWithExtendableBuffer(const int maxAdditionalBufferSize) BufferWithExtendableBuffer(const int maxAdditionalBufferSize)
: mOriginalBuffer(), mAdditionalBuffer(0), mUsedAdditionalBufferSize(0), : mOriginalBuffer(), mAdditionalBuffer(), mUsedAdditionalBufferSize(0),
mMaxAdditionalBufferSize(maxAdditionalBufferSize) {} mMaxAdditionalBufferSize(maxAdditionalBufferSize) {}
AK_FORCE_INLINE int getTailPosition() const { AK_FORCE_INLINE int getTailPosition() const {

View File

@ -21,6 +21,7 @@
#include <memory> #include <memory>
#include "defines.h" #include "defines.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -39,12 +40,12 @@ class MmappedBuffer {
~MmappedBuffer(); ~MmappedBuffer();
AK_FORCE_INLINE uint8_t *getBuffer() const { ReadWriteByteArrayView getReadWriteByteArrayView() const {
return mBuffer; return mByteArrayView;
} }
AK_FORCE_INLINE int getBufferSize() const { ReadOnlyByteArrayView getReadOnlyByteArrayView() const {
return mBufferSize; return mByteArrayView.getReadOnlyView();
} }
AK_FORCE_INLINE bool isUpdatable() const { AK_FORCE_INLINE bool isUpdatable() const {
@ -55,18 +56,17 @@ class MmappedBuffer {
AK_FORCE_INLINE MmappedBuffer(uint8_t *const buffer, const int bufferSize, AK_FORCE_INLINE MmappedBuffer(uint8_t *const buffer, const int bufferSize,
void *const mmappedBuffer, const int alignedSize, const int mmapFd, void *const mmappedBuffer, const int alignedSize, const int mmapFd,
const bool isUpdatable) const bool isUpdatable)
: mBuffer(buffer), mBufferSize(bufferSize), mMmappedBuffer(mmappedBuffer), : mByteArrayView(buffer, bufferSize), mMmappedBuffer(mmappedBuffer),
mAlignedSize(alignedSize), mMmapFd(mmapFd), mIsUpdatable(isUpdatable) {} mAlignedSize(alignedSize), mMmapFd(mmapFd), mIsUpdatable(isUpdatable) {}
// Empty file. We have to handle an empty file as a valid part of a dictionary. // Empty file. We have to handle an empty file as a valid part of a dictionary.
AK_FORCE_INLINE MmappedBuffer(const bool isUpdatable) AK_FORCE_INLINE MmappedBuffer(const bool isUpdatable)
: mBuffer(nullptr), mBufferSize(0), mMmappedBuffer(nullptr), mAlignedSize(0), : mByteArrayView(), mMmappedBuffer(nullptr), mAlignedSize(0),
mMmapFd(0), mIsUpdatable(isUpdatable) {} mMmapFd(0), mIsUpdatable(isUpdatable) {}
DISALLOW_IMPLICIT_CONSTRUCTORS(MmappedBuffer); DISALLOW_IMPLICIT_CONSTRUCTORS(MmappedBuffer);
uint8_t *const mBuffer; const ReadWriteByteArrayView mByteArrayView;
const int mBufferSize;
void *const mMmappedBuffer; void *const mMmappedBuffer;
const int mAlignedSize; const int mAlignedSize;
const int mMmapFd; const int mMmapFd;

View File

@ -43,9 +43,8 @@ TrieMap::TrieMap() : mBuffer(MAX_BUFFER_SIZE) {
writeEntry(EMPTY_BITMAP_ENTRY, ROOT_BITMAP_ENTRY_INDEX); writeEntry(EMPTY_BITMAP_ENTRY, ROOT_BITMAP_ENTRY_INDEX);
} }
TrieMap::TrieMap(uint8_t *const buffer, const int bufferSize) TrieMap::TrieMap(const ReadWriteByteArrayView buffer)
: mBuffer(buffer, bufferSize, : mBuffer(buffer, BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE) {}
void TrieMap::dump(const int from, const int to) const { void TrieMap::dump(const int from, const int to) const {
AKLOGI("BufSize: %d", mBuffer.getTailPosition()); AKLOGI("BufSize: %d", mBuffer.getTailPosition());

View File

@ -24,6 +24,7 @@
#include "defines.h" #include "defines.h"
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h" #include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -161,7 +162,7 @@ class TrieMap {
TrieMap(); TrieMap();
// Construct TrieMap using existing data in the memory region written by save(). // Construct TrieMap using existing data in the memory region written by save().
TrieMap(uint8_t *const buffer, const int bufferSize); TrieMap(const ReadWriteByteArrayView buffer);
void dump(const int from = 0, const int to = 0) const; void dump(const int from = 0, const int to = 0) const;
bool isNearSizeLimit() const { bool isNearSizeLimit() const {

View File

@ -71,6 +71,11 @@ class ReadWriteByteArrayView {
return ReadOnlyByteArrayView(mPtr, mSize); return ReadOnlyByteArrayView(mPtr, mSize);
} }
ReadWriteByteArrayView subView(const size_t start, const size_t n) const {
ASSERT(start + n <= mSize);
return ReadWriteByteArrayView(mPtr + start, n);
}
private: private:
DISALLOW_ASSIGNMENT_OPERATOR(ReadWriteByteArrayView); DISALLOW_ASSIGNMENT_OPERATOR(ReadWriteByteArrayView);