Merge "Make FormatUtils use ByteArrayView."

This commit is contained in:
Keisuke Kuroyanagi 2014-08-15 10:55:50 +00:00 committed by Android (Google) Code Review
commit d9d94f40c7
3 changed files with 10 additions and 11 deletions

View file

@ -111,8 +111,7 @@ 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->getReadOnlyByteArrayView().data(), mmappedBuffer->getReadOnlyByteArrayView());
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);
@ -174,8 +173,7 @@ template<class DictConstants, class DictBuffers, class DictBuffersPtr, class Str
if (!mmappedBuffer) { if (!mmappedBuffer) {
return nullptr; return nullptr;
} }
switch (FormatUtils::detectFormatVersion(mmappedBuffer->getReadOnlyByteArrayView().data(), switch (FormatUtils::detectFormatVersion(mmappedBuffer->getReadOnlyByteArrayView())) {
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

@ -23,7 +23,7 @@ namespace latinime {
const uint32_t FormatUtils::MAGIC_NUMBER = 0x9BC13AFE; const uint32_t FormatUtils::MAGIC_NUMBER = 0x9BC13AFE;
// Magic number (4 bytes), version (2 bytes), flags (2 bytes), header size (4 bytes) = 12 // Magic number (4 bytes), version (2 bytes), flags (2 bytes), header size (4 bytes) = 12
const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12; const size_t FormatUtils::DICTIONARY_MINIMUM_SIZE = 12;
/* static */ FormatUtils::FORMAT_VERSION FormatUtils::getFormatVersion(const int formatVersion) { /* static */ FormatUtils::FORMAT_VERSION FormatUtils::getFormatVersion(const int formatVersion) {
switch (formatVersion) { switch (formatVersion) {
@ -40,14 +40,14 @@ const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12;
} }
} }
/* static */ FormatUtils::FORMAT_VERSION FormatUtils::detectFormatVersion( /* static */ FormatUtils::FORMAT_VERSION FormatUtils::detectFormatVersion(
const uint8_t *const dict, const int dictSize) { const ReadOnlyByteArrayView dictBuffer) {
// The magic number is stored big-endian. // The magic number is stored big-endian.
// If the dictionary is less than 4 bytes, we can't even read the magic number, so we don't // If the dictionary is less than 4 bytes, we can't even read the magic number, so we don't
// understand this format. // understand this format.
if (dictSize < DICTIONARY_MINIMUM_SIZE) { if (dictBuffer.size() < DICTIONARY_MINIMUM_SIZE) {
return UNKNOWN_VERSION; return UNKNOWN_VERSION;
} }
const uint32_t magicNumber = ByteArrayUtils::readUint32(dict, 0); const uint32_t magicNumber = ByteArrayUtils::readUint32(dictBuffer.data(), 0);
switch (magicNumber) { switch (magicNumber) {
case MAGIC_NUMBER: case MAGIC_NUMBER:
// The layout of the header is as follows: // The layout of the header is as follows:
@ -58,7 +58,7 @@ const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12;
// Conceptually this converts the hardcoded value of the bytes in the file into // Conceptually this converts the hardcoded value of the bytes in the file into
// the symbolic value we use in the code. But we want the constants to be the // the symbolic value we use in the code. But we want the constants to be the
// same so we use them for both here. // same so we use them for both here.
return getFormatVersion(ByteArrayUtils::readUint16(dict, 4)); return getFormatVersion(ByteArrayUtils::readUint16(dictBuffer.data(), 4));
default: default:
return UNKNOWN_VERSION; return UNKNOWN_VERSION;
} }

View file

@ -20,6 +20,7 @@
#include <cstdint> #include <cstdint>
#include "defines.h" #include "defines.h"
#include "utils/byte_array_view.h"
namespace latinime { namespace latinime {
@ -42,12 +43,12 @@ class FormatUtils {
static const uint32_t MAGIC_NUMBER; static const uint32_t MAGIC_NUMBER;
static FORMAT_VERSION getFormatVersion(const int formatVersion); static FORMAT_VERSION getFormatVersion(const int formatVersion);
static FORMAT_VERSION detectFormatVersion(const uint8_t *const dict, const int dictSize); static FORMAT_VERSION detectFormatVersion(const ReadOnlyByteArrayView dictBuffer);
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(FormatUtils); DISALLOW_IMPLICIT_CONSTRUCTORS(FormatUtils);
static const int DICTIONARY_MINIMUM_SIZE; static const size_t DICTIONARY_MINIMUM_SIZE;
}; };
} // namespace latinime } // namespace latinime
#endif /* LATINIME_FORMAT_UTILS_H */ #endif /* LATINIME_FORMAT_UTILS_H */