am 903be5bb: Implement latinime_BinaryDictionary_createOnMemory().

* commit '903be5bbd33b664c7e691d8bee0dd4d6376947bc':
  Implement latinime_BinaryDictionary_createOnMemory().
main
Keisuke Kuroyanagi 2014-04-22 19:15:49 +00:00 committed by Android Git Automerger
commit 1538117960
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);
sourceDirChars[sourceDirUtf8Length] = '\0';
DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy(
DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy(
DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
isUpdatable == JNI_TRUE));
if (!dictionaryStructureWithBufferPolicy) {
@ -68,8 +68,29 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
static jlong latinime_BinaryDictionary_createOnMemory(JNIEnv *env, jclass clazz,
jlong formatVersion, jstring locale, jobjectArray attributeKeyStringArray,
jobjectArray attributeValueStringArray) {
// TODO: Implement.
return 0;
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;
}
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,

View File

@ -36,9 +36,14 @@ static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env,
char filePathChars[filePathUtf8Length + 1];
env->GetStringUTFRegion(filePath, 0, env->GetStringLength(filePath), filePathChars);
filePathChars[filePathUtf8Length] = '\0';
jsize localeLength = env->GetStringLength(locale);
jchar localeCodePoints[localeLength];
env->GetStringRegion(locale, 0, localeLength, localeCodePoints);
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) {
@ -48,7 +53,7 @@ static jboolean latinime_BinaryDictionaryUtils_createEmptyDictFile(JNIEnv *env,
JniDataUtils::constructAttributeMap(env, attributeKeyStringArray,
attributeValueStringArray);
return DictFileWritingUtils::createEmptyDictFile(filePathChars, static_cast<int>(dictVersion),
CharUtils::convertShortArrayToIntVector(localeCodePoints, localeLength), &attributeMap);
localeCodePoints, &attributeMap);
}
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.
HeaderPolicy(const FormatUtils::FORMAT_VERSION dictFormatVersion,
const std::vector<int> locale,
const std::vector<int> &locale,
const DictionaryHeaderStructurePolicy::AttributeMap *const attributeMap)
: mDictFormatVersion(dictFormatVersion),
mDictionaryFlags(HeaderReadWriteUtils::createAndGetDictionaryFlagsUsingAttributeMap(

View File

@ -30,24 +30,45 @@
namespace latinime {
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory
::newDictionaryStructureWithBufferPolicy(const char *const path,
const int bufOffset, const int size, const bool isUpdatable) {
DictionaryStructureWithBufferPolicyFactory::newPolicyForExistingDictFile(
const char *const path, const int bufOffset, const int size,
const bool isUpdatable) {
if (FileUtils::existsDir(path)) {
// Given path represents a directory.
return newPolicyforDirectoryDict(path, isUpdatable);
return newPolicyForDirectoryDict(path, isUpdatable);
} else {
if (isUpdatable) {
AKLOGE("One file dictionaries don't support updating. path: %s", path);
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(nullptr);
}
return newPolicyforFileDict(path, bufOffset, size);
return newPolicyForFileDict(path, bufOffset, size);
}
}
/* 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 int headerFilePathBufSize = PATH_MAX + 1 /* terminator */;
char headerFilePath[headerFilePathBufSize];
@ -93,7 +114,7 @@ namespace latinime {
}
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyforFileDict(
DictionaryStructureWithBufferPolicyFactory::newPolicyForFileDict(
const char *const path, const int bufOffset, const int size) {
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
// MmappedBufferPtr if the instance has the responsibility.

View File

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

View File

@ -86,13 +86,6 @@ class CharUtils {
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 const std::vector<int> EMPTY_STRING;