Implement latinime_BinaryDictionary_createOnMemory().

Bug: 14166482
Change-Id: If7ec3345ab34edcd6bc5cef9e72580ced894a0e3
This commit is contained in:
Keisuke Kuroyanagi 2014-04-22 12:01:22 -07:00
parent 361881b82b
commit 903be5bbd3
6 changed files with 73 additions and 25 deletions

View file

@ -51,7 +51,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars); env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars);
sourceDirChars[sourceDirUtf8Length] = '\0'; sourceDirChars[sourceDirUtf8Length] = '\0';
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy( DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy(
DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy( DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize), sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
isUpdatable == JNI_TRUE)); isUpdatable == JNI_TRUE));
if (!dictionaryStructureWithBufferPolicy) { if (!dictionaryStructureWithBufferPolicy) {
@ -68,9 +68,30 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
static jlong latinime_BinaryDictionary_createOnMemory(JNIEnv *env, jclass clazz, static jlong latinime_BinaryDictionary_createOnMemory(JNIEnv *env, jclass clazz,
jlong formatVersion, jstring locale, jobjectArray attributeKeyStringArray, jlong formatVersion, jstring locale, jobjectArray attributeKeyStringArray,
jobjectArray attributeValueStringArray) { jobjectArray attributeValueStringArray) {
// TODO: Implement. const jsize localeUtf8Length = env->GetStringUTFLength(locale);
char localeChars[localeUtf8Length + 1];
env->GetStringUTFRegion(locale, 0, env->GetStringLength(locale), localeChars);
localeChars[localeUtf8Length] = '\0';
std::vector<int> localeCodePoints;
HeaderReadWriteUtils::insertCharactersIntoVector(localeChars, &localeCodePoints);
const int keyCount = env->GetArrayLength(attributeKeyStringArray);
const int valueCount = env->GetArrayLength(attributeValueStringArray);
if (keyCount != valueCount) {
return false;
}
DictionaryHeaderStructurePolicy::AttributeMap attributeMap =
JniDataUtils::constructAttributeMap(env, attributeKeyStringArray,
attributeValueStringArray);
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy =
DictionaryStructureWithBufferPolicyFactory::newPolicyForOnMemoryDict(
formatVersion, localeCodePoints, &attributeMap);
if (!dictionaryStructureWithBufferPolicy) {
return 0; return 0;
} }
Dictionary *const dictionary =
new Dictionary(env, std::move(dictionaryStructureWithBufferPolicy));
return reinterpret_cast<jlong>(dictionary);
}
static void latinime_BinaryDictionary_flush(JNIEnv *env, jclass clazz, jlong dict, static void latinime_BinaryDictionary_flush(JNIEnv *env, jclass clazz, jlong dict,
jstring filePath) { jstring filePath) {

View file

@ -36,9 +36,14 @@ static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env,
char filePathChars[filePathUtf8Length + 1]; char filePathChars[filePathUtf8Length + 1];
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars); env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
filePathChars[filePathUtf8Length] = '\0'; filePathChars[filePathUtf8Length] = '\0';
jsize localeLength = env->GetStringLength(locale);
jchar localeCodePoints[localeLength]; const jsize localeUtf8Length = env->GetStringUTFLength(locale);
env->GetStringRegion(locale, 0, localeLength, localeCodePoints); char localeChars[localeUtf8Length + 1];
env->GetStringUTFRegion(locale, 0, env->GetStringLength(locale), localeChars);
localeChars[localeUtf8Length] = '\0';
std::vector<int> localeCodePoints;
HeaderReadWriteUtils::insertCharactersIntoVector(localeChars, &localeCodePoints);
const int keyCount = env->GetArrayLength(attributeKeyStringArray); const int keyCount = env->GetArrayLength(attributeKeyStringArray);
const int valueCount = env->GetArrayLength(attributeValueStringArray); const int valueCount = env->GetArrayLength(attributeValueStringArray);
if (keyCount != valueCount) { if (keyCount != valueCount) {
@ -48,7 +53,7 @@ static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env,
JniDataUtils::constructAttributeMap(env, attributeKeyStringArray, JniDataUtils::constructAttributeMap(env, attributeKeyStringArray,
attributeValueStringArray); attributeValueStringArray);
return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion), return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion),
CharUtils::convertShortArrayToIntVector(localeCodePoints, localeLength), &attributeMap); localeCodePoints, &attributeMap);
} }
static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jclass clazz, static jfloat latinime_BinaryDictionaryUtils_calcNormalizedScore(JNIEnv *env, jclass clazz,

View file

@ -69,7 +69,7 @@ class HeaderPolicy : public DictionaryHeaderStructurePolicy {
// Constructs header information using an attribute map. // Constructs header information using an attribute map.
HeaderPolicy(const FormatUtils::FORMAT_VERSION dictFormatVersion, HeaderPolicy(const FormatUtils::FORMAT_VERSION dictFormatVersion,
const std::vector<int> locale, const std::vector<int> &locale,
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap) const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap)
: mDictFormatVersion(dictFormatVersion), : mDictFormatVersion(dictFormatVersion),
mDictionaryFlags(HeaderReadWriteUtils::createAndGetDictionaryFlagsUsingAttributeMap( mDictionaryFlags(HeaderReadWriteUtils::createAndGetDictionaryFlagsUsingAttributeMap(

View file

@ -30,24 +30,45 @@
namespace latinime { namespace latinime {
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr /* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
::newDictionaryStructureWithBufferPolicy(const char *const path, const char *const path, const int bufOffset, const int size,
const int bufOffset, const int size, const bool isUpdatable) { const bool isUpdatable) {
if (FileUtils::existsDir(path)) { if (FileUtils::existsDir(path)) {
// Given path represents a directory. // Given path represents a directory.
return newPolicyforDirectoryDict(path, isUpdatable); return newPolicyForDirectoryDict(path, isUpdatable);
} else { } else {
if (isUpdatable) { if (isUpdatable) {
AKLOGE("One file dictionaries don't support updating. path: %s", path); AKLOGE("One file dictionaries don't support updating. path: %s", path);
ASSERT(false); ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
} }
return newPolicyforFileDict(path, bufOffset, size); return newPolicyForFileDict(path, bufOffset, size);
} }
} }
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr /* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyforDirectoryDict( DictionaryStructureWithBufferPolicyFactory:: newPolicyForOnMemoryDict(
const int formatVersion, const std::vector<int> &locale,
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap) {
switch (formatVersion) {
case FormatUtils::VERSION_4: {
HeaderPolicy headerPolicy(FormatUtils::VERSION_4, locale, attributeMap);
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
Ver4DictBuffers::createVer4DictBuffers(&headerPolicy,
Ver4DictConstants::MAX_DICT_EXTENDED_REGION_SIZE);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new Ver4PatriciaTriePolicy(std::move(dictBuffers)));
}
default:
AKLOGE("DICT: dictionary format %d is not supported for on memory dictionary",
formatVersion);
break;
}
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
}
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyForDirectoryDict(
const char *const path, const bool isUpdatable) { const char *const path, const bool isUpdatable) {
const int headerFilePathBufSize = PATH_MAX + 1 /* terminator */; const int headerFilePathBufSize = PATH_MAX + 1 /* terminator */;
char headerFilePath[headerFilePathBufSize]; char headerFilePath[headerFilePathBufSize];
@ -93,7 +114,7 @@ namespace latinime {
} }
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr /* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyforFileDict( DictionaryStructureWithBufferPolicyFactory::newPolicyForFileDict(
const char *const path, const int bufOffset, const int size) { const char *const path, const int bufOffset, const int size) {
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of // Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
// MmappedBufferPtr if the instance has the responsibility. // MmappedBufferPtr if the instance has the responsibility.

View file

@ -17,7 +17,10 @@
#ifndef LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H #ifndef LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H
#define LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H #define LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H
#include <vector>
#include "defines.h" #include "defines.h"
#include "suggest/core/policy/dictionary_header_structure_policy.h"
#include "suggest/core/policy/dictionary_structure_with_buffer_policy.h" #include "suggest/core/policy/dictionary_structure_with_buffer_policy.h"
namespace latinime { namespace latinime {
@ -25,17 +28,22 @@ namespace latinime {
class DictionaryStructureWithBufferPolicyFactory { class DictionaryStructureWithBufferPolicyFactory {
public: public:
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newDictionaryStructureWithBufferPolicy(const char *const path, const int bufOffset, newPolicyForExistingDictFile(const char *const path, const int bufOffset,
const int size, const bool isUpdatable); const int size, const bool isUpdatable);
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newPolicyForOnMemoryDict(const int formatVersion,
const std::vector<int> &locale,
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap);
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DictionaryStructureWithBufferPolicyFactory); DISALLOW_IMPLICIT_CONSTRUCTORS(DictionaryStructureWithBufferPolicyFactory);
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newPolicyforDirectoryDict(const char *const path, const bool isUpdatable); newPolicyForDirectoryDict(const char *const path, const bool isUpdatable);
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newPolicyforFileDict(const char *const path, const int bufOffset, const int size); newPolicyForFileDict(const char *const path, const int bufOffset, const int size);
static void getHeaderFilePathInDictDir(const char *const dirPath, static void getHeaderFilePathInDictDir(const char *const dirPath,
const int outHeaderFileBufSize, char *const outHeaderFilePath); const int outHeaderFileBufSize, char *const outHeaderFilePath);

View file

@ -86,13 +86,6 @@ class CharUtils {
return spaceCount; return spaceCount;
} }
static AK_FORCE_INLINE std::vector<int> convertShortArrayToIntVector(
const unsigned short *const source, const int length) {
std::vector<int> destination;
destination.insert(destination.end(), source, source + length);
return destination; // Copies the vector
}
static unsigned short latin_tolower(const unsigned short c); static unsigned short latin_tolower(const unsigned short c);
static const std::vector<int> EMPTY_STRING; static const std::vector<int> EMPTY_STRING;