[RF3] Cleanups

Make the version number a single number on native and java side.
Also, remove the hasValidContents method. It's useless since the
native code already checks this when creating the dictionary (I
wish I had known that when I added it).

Bug: 11281748
Change-Id: I572d37429972b2f280e4bdb748b709e5d0d7737e
This commit is contained in:
Jean Chalard 2013-12-05 18:02:20 +09:00
parent cb27d955f3
commit a72e8f1ede
16 changed files with 42 additions and 65 deletions

View file

@ -121,7 +121,6 @@ public final class BinaryDictionary extends Dictionary {
String[] attributeKeyStringArray, String[] attributeValueStringArray); String[] attributeKeyStringArray, String[] attributeValueStringArray);
private static native long openNative(String sourceDir, long dictOffset, long dictSize, private static native long openNative(String sourceDir, long dictOffset, long dictSize,
boolean isUpdatable); boolean isUpdatable);
private static native boolean hasValidContentsNative(long dict);
private static native void flushNative(long dict, String filePath); private static native void flushNative(long dict, String filePath);
private static native boolean needsToRunGCNative(long dict, boolean mindsBlockByGC); private static native boolean needsToRunGCNative(long dict, boolean mindsBlockByGC);
private static native void flushWithGCNative(long dict, String filePath); private static native void flushWithGCNative(long dict, String filePath);
@ -243,10 +242,6 @@ public final class BinaryDictionary extends Dictionary {
return mNativeDict != 0; return mNativeDict != 0;
} }
public boolean hasValidContents() {
return hasValidContentsNative(mNativeDict);
}
public int getFormatVersion() { public int getFormatVersion() {
return getFormatVersionNative(mNativeDict); return getFormatVersionNative(mNativeDict);
} }

View file

@ -63,7 +63,7 @@ public final class DictionaryFactory {
final ReadOnlyBinaryDictionary readOnlyBinaryDictionary = final ReadOnlyBinaryDictionary readOnlyBinaryDictionary =
new ReadOnlyBinaryDictionary(f.mFilename, f.mOffset, f.mLength, new ReadOnlyBinaryDictionary(f.mFilename, f.mOffset, f.mLength,
useFullEditDistance, locale, Dictionary.TYPE_MAIN); useFullEditDistance, locale, Dictionary.TYPE_MAIN);
if (readOnlyBinaryDictionary.hasValidContents()) { if (readOnlyBinaryDictionary.isValidDictionary()) {
dictList.add(readOnlyBinaryDictionary); dictList.add(readOnlyBinaryDictionary);
} else { } else {
readOnlyBinaryDictionary.close(); readOnlyBinaryDictionary.close();

View file

@ -63,7 +63,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
*/ */
protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH; protected static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
private static final int DICTIONARY_FORMAT_VERSION = 4; private static final int DICTIONARY_FORMAT_VERSION = FormatSpec.VERSION4;
/** /**
* A static map of update controllers, each of which records the time of accesses to a single * A static map of update controllers, each of which records the time of accesses to a single
@ -139,8 +139,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return formatVersion == 2; return formatVersion == 2;
} }
public boolean hasValidContents() { public boolean isValidDictionary() {
return mBinaryDictionary.hasValidContents(); return mBinaryDictionary.isValidDictionary();
} }
protected String getFileNameExtentionToOpenDict() { protected String getFileNameExtentionToOpenDict() {
@ -563,8 +563,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
loadDictionaryAsync(); loadDictionaryAsync();
mDictionaryWriter.write(mFilename, getHeaderAttributeMap()); mDictionaryWriter.write(mFilename, getHeaderAttributeMap());
} else { } else {
if (mBinaryDictionary == null || !mBinaryDictionary.isValidDictionary() if (mBinaryDictionary == null || !isValidDictionary()
|| !hasValidContents()
// TODO: remove the check below // TODO: remove the check below
|| !matchesExpectedBinaryDictFormatVersionForThisType( || !matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion())) { mBinaryDictionary.getFormatVersion())) {
@ -665,8 +664,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// load the shared dictionary. // load the shared dictionary.
loadBinaryDictionary(); loadBinaryDictionary();
} }
if (mBinaryDictionary != null && !(mBinaryDictionary.isValidDictionary() if (mBinaryDictionary != null && !(isValidDictionary()
&& hasValidContents()
// TODO: remove the check below // TODO: remove the check below
&& matchesExpectedBinaryDictFormatVersionForThisType( && matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion()))) { mBinaryDictionary.getFormatVersion()))) {

View file

@ -44,15 +44,6 @@ public final class ReadOnlyBinaryDictionary extends Dictionary {
locale, dictType, false /* isUpdatable */); locale, dictType, false /* isUpdatable */);
} }
public boolean hasValidContents() {
mLock.readLock().lock();
try {
return mBinaryDictionary.hasValidContents();
} finally {
mLock.readLock().unlock();
}
}
public boolean isValidDictionary() { public boolean isValidDictionary() {
return mBinaryDictionary.isValidDictionary(); return mBinaryDictionary.isValidDictionary();
} }

View file

@ -204,8 +204,8 @@ public final class FormatSpec {
static final int NOT_A_VERSION_NUMBER = -1; static final int NOT_A_VERSION_NUMBER = -1;
static final int FIRST_VERSION_WITH_DYNAMIC_UPDATE = 3; static final int FIRST_VERSION_WITH_DYNAMIC_UPDATE = 3;
static final int FIRST_VERSION_WITH_TERMINAL_ID = 4; static final int FIRST_VERSION_WITH_TERMINAL_ID = 4;
static final int VERSION3 = 3; public static final int VERSION3 = 3;
static final int VERSION4 = 4; public static final int VERSION4 = 4;
// These options need to be the same numeric values as the one in the native reading code. // These options need to be the same numeric values as the one in the native reading code.
static final int GERMAN_UMLAUT_PROCESSING_FLAG = 0x1; static final int GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;

View file

@ -161,7 +161,7 @@ public class Ver4DictDecoder extends AbstractDictDecoder {
} }
final FileHeader header = super.readHeader(mDictBuffer); final FileHeader header = super.readHeader(mDictBuffer);
final int version = header.mFormatOptions.mVersion; final int version = header.mFormatOptions.mVersion;
if (version != 4) { if (version != FormatSpec.VERSION4) {
throw new UnsupportedFormatException("File header has a wrong version : " + version); throw new UnsupportedFormatException("File header has a wrong version : " + version);
} }
return header; return header;

View file

@ -86,7 +86,7 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
char sourceDirChars[sourceDirUtf8Length + 1]; char sourceDirChars[sourceDirUtf8Length + 1];
env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars); env->GetStringUTFRegion(sourceDir, 0, env->GetStringLength(sourceDir), sourceDirChars);
sourceDirChars[sourceDirUtf8Length] = '\0'; sourceDirChars[sourceDirUtf8Length] = '\0';
DictionaryStructureWithBufferPolicy::StructurePoilcyPtr dictionaryStructureWithBufferPolicy = DictionaryStructureWithBufferPolicy::StructurePolicyPtr dictionaryStructureWithBufferPolicy =
DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy( DictionaryStructureWithBufferPolicyFactory::newDictionaryStructureWithBufferPolicy(
sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize), sourceDirChars, static_cast<int>(dictOffset), static_cast<int>(dictSize),
isUpdatable == JNI_TRUE); isUpdatable == JNI_TRUE);
@ -135,14 +135,6 @@ static void latinime_BinaryDictionary_close(JNIEnv *env, jclass clazz, jlong dic
delete dictionary; delete dictionary;
} }
static bool latinime_BinaryDictionary_hasValidContents(JNIEnv *env, jclass clazz,
jlong dict) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return false;
// TODO: check format version
return true;
}
static int latinime_BinaryDictionary_getFormatVersion(JNIEnv *env, jclass clazz, jlong dict) { static int latinime_BinaryDictionary_getFormatVersion(JNIEnv *env, jclass clazz, jlong dict) {
Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict); Dictionary *dictionary = reinterpret_cast<Dictionary *>(dict);
if (!dictionary) return 0; if (!dictionary) return 0;
@ -466,11 +458,6 @@ static const JNINativeMethod sMethods[] = {
const_cast<char *>("(J)V"), const_cast<char *>("(J)V"),
reinterpret_cast<void *>(latinime_BinaryDictionary_close) reinterpret_cast<void *>(latinime_BinaryDictionary_close)
}, },
{
const_cast<char *>("hasValidContentsNative"),
const_cast<char *>("(J)Z"),
reinterpret_cast<void *>(latinime_BinaryDictionary_hasValidContents)
},
{ {
const_cast<char *>("getFormatVersionNative"), const_cast<char *>("getFormatVersionNative"),
const_cast<char *>("(J)I"), const_cast<char *>("(J)I"),

View file

@ -34,7 +34,7 @@ namespace latinime {
const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32; const int Dictionary::HEADER_ATTRIBUTE_BUFFER_SIZE = 32;
Dictionary::Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePoilcyPtr Dictionary::Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
&dictionaryStructureWithBufferPolicy) &dictionaryStructureWithBufferPolicy)
: mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy), : mDictionaryStructureWithBufferPolicy(dictionaryStructureWithBufferPolicy),
mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy.get())), mBigramDictionary(new BigramDictionary(mDictionaryStructureWithBufferPolicy.get())),

View file

@ -56,8 +56,8 @@ class Dictionary {
static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; static const int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000;
static const int KIND_FLAG_EXACT_MATCH = 0x40000000; static const int KIND_FLAG_EXACT_MATCH = 0x40000000;
Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePoilcyPtr Dictionary(JNIEnv *env, const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
&dictionaryStructureWithBufferPoilcy); &dictionaryStructureWithBufferPolicy);
int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession, int getSuggestions(ProximityInfo *proximityInfo, DicTraverseSession *traverseSession,
int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints, int *xcoordinates, int *ycoordinates, int *times, int *pointerIds, int *inputCodePoints,
@ -109,7 +109,7 @@ class Dictionary {
static const int HEADER_ATTRIBUTE_BUFFER_SIZE; static const int HEADER_ATTRIBUTE_BUFFER_SIZE;
const DictionaryStructureWithBufferPolicy::StructurePoilcyPtr const DictionaryStructureWithBufferPolicy::StructurePolicyPtr
mDictionaryStructureWithBufferPolicy; mDictionaryStructureWithBufferPolicy;
const BigramDictionaryPtr mBigramDictionary; const BigramDictionaryPtr mBigramDictionary;
const SuggestInterfacePtr mGestureSuggest; const SuggestInterfacePtr mGestureSuggest;

View file

@ -29,12 +29,12 @@ class DictionaryHeaderStructurePolicy;
class DictionaryShortcutsStructurePolicy; class DictionaryShortcutsStructurePolicy;
/* /*
* This class abstracts structure of dictionaries. * This class abstracts the structure of dictionaries.
* Implement this policy to support additional dictionaries. * Implement this policy to support additional dictionaries.
*/ */
class DictionaryStructureWithBufferPolicy { class DictionaryStructureWithBufferPolicy {
public: public:
typedef ExclusiveOwnershipPointer<DictionaryStructureWithBufferPolicy> StructurePoilcyPtr; typedef ExclusiveOwnershipPointer<DictionaryStructureWithBufferPolicy> StructurePolicyPtr;
virtual ~DictionaryStructureWithBufferPolicy() {} virtual ~DictionaryStructureWithBufferPolicy() {}

View file

@ -78,15 +78,18 @@ class HeaderPolicy : public DictionaryHeaderStructurePolicy {
~HeaderPolicy() {} ~HeaderPolicy() {}
virtual int getFormatVersionNumber() const { virtual int getFormatVersionNumber() const {
// Conceptually this converts the symbolic value we use in the code into the
// hardcoded of the bytes in the file. But we want the constants to be the
// same so we use them for both here.
switch (mDictFormatVersion) { switch (mDictFormatVersion) {
case FormatUtils::VERSION_2: case FormatUtils::VERSION_2:
return 2; return FormatUtils::VERSION_2;
case FormatUtils::VERSION_3: case FormatUtils::VERSION_3:
return 3; return FormatUtils::VERSION_3;
case FormatUtils::VERSION_4: case FormatUtils::VERSION_4:
return 4; return FormatUtils::VERSION_4;
default: default:
return 0; return FormatUtils::UNKNOWN_VERSION;
} }
} }

View file

@ -28,7 +28,7 @@
namespace latinime { namespace latinime {
/* static */ DictionaryStructureWithBufferPolicy::StructurePoilcyPtr /* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory DictionaryStructureWithBufferPolicyFactory
::newDictionaryStructureWithBufferPolicy(const char *const path, ::newDictionaryStructureWithBufferPolicy(const char *const path,
const int bufOffset, const int size, const bool isUpdatable) { const int bufOffset, const int size, const bool isUpdatable) {
@ -37,12 +37,12 @@ namespace latinime {
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset, size, MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset, size,
isUpdatable); isUpdatable);
if (!mmappedBuffer.get()) { if (!mmappedBuffer.get()) {
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr(0); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
} }
switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(), switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(),
mmappedBuffer.get()->getBufferSize())) { mmappedBuffer.get()->getBufferSize())) {
case FormatUtils::VERSION_2: case FormatUtils::VERSION_2:
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr( return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new PatriciaTriePolicy(mmappedBuffer)); new PatriciaTriePolicy(mmappedBuffer));
case FormatUtils::VERSION_4: { case FormatUtils::VERSION_4: {
const int dictDirPathBufSize = strlen(path) + 1 /* terminator */; const int dictDirPathBufSize = strlen(path) + 1 /* terminator */;
@ -50,22 +50,22 @@ namespace latinime {
if (!FileUtils::getFilePathWithoutSuffix(path, Ver4DictConstants::TRIE_FILE_EXTENSION, if (!FileUtils::getFilePathWithoutSuffix(path, Ver4DictConstants::TRIE_FILE_EXTENSION,
dictDirPathBufSize, dictDirPath)) { dictDirPathBufSize, dictDirPath)) {
// Dictionary file name is not valid as a version 4 dictionary. // Dictionary file name is not valid as a version 4 dictionary.
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr(0); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
} }
const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers = const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
Ver4DictBuffers::openVer4DictBuffers(dictDirPath, mmappedBuffer); Ver4DictBuffers::openVer4DictBuffers(dictDirPath, mmappedBuffer);
if (!dictBuffers.get()->isValid()) { if (!dictBuffers.get()->isValid()) {
AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements."); AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements.");
ASSERT(false); ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr(0); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
} }
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr( return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new Ver4PatriciaTriePolicy(dictBuffers)); new Ver4PatriciaTriePolicy(dictBuffers));
} }
default: default:
AKLOGE("DICT: dictionary format is unknown, bad magic number"); AKLOGE("DICT: dictionary format is unknown, bad magic number");
ASSERT(false); ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePoilcyPtr(0); return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
} }
} }

View file

@ -27,7 +27,7 @@ namespace latinime {
class DictionaryStructureWithBufferPolicyFactory { class DictionaryStructureWithBufferPolicyFactory {
public: public:
static DictionaryStructureWithBufferPolicy::StructurePoilcyPtr static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newDictionaryStructureWithBufferPolicy(const char *const path, const int bufOffset, newDictionaryStructureWithBufferPolicy(const char *const path, const int bufOffset,
const int size, const bool isUpdatable); const int size, const bool isUpdatable);

View file

@ -34,7 +34,7 @@ const char *const DictFileWritingUtils::TEMP_FILE_SUFFIX_FOR_WRITING_DICT_FILE =
const int dictVersion, const HeaderReadWriteUtils::AttributeMap *const attributeMap) { const int dictVersion, const HeaderReadWriteUtils::AttributeMap *const attributeMap) {
TimeKeeper::setCurrentTime(); TimeKeeper::setCurrentTime();
switch (dictVersion) { switch (dictVersion) {
case 4: case FormatUtils::VERSION_4:
return createEmptyV4DictFile(filePath, attributeMap); return createEmptyV4DictFile(filePath, attributeMap);
default: default:
AKLOGE("Cannot create dictionary %s because format version %d is not supported.", AKLOGE("Cannot create dictionary %s because format version %d is not supported.",

View file

@ -41,11 +41,14 @@ const int FormatUtils::DICTIONARY_MINIMUM_SIZE = 12;
// Dictionary format version number (2 bytes) // Dictionary format version number (2 bytes)
// Options (2 bytes) // Options (2 bytes)
// Header size (4 bytes) : integer, big endian // Header size (4 bytes) : integer, big endian
if (ByteArrayUtils::readUint16(dict, 4) == 2) { // 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
// same so we use them for both here.
if (ByteArrayUtils::readUint16(dict, 4) == VERSION_2) {
return VERSION_2; return VERSION_2;
} else if (ByteArrayUtils::readUint16(dict, 4) == 3) { } else if (ByteArrayUtils::readUint16(dict, 4) == VERSION_3) {
return VERSION_3; return VERSION_3;
} else if (ByteArrayUtils::readUint16(dict, 4) == 4) { } else if (ByteArrayUtils::readUint16(dict, 4) == VERSION_4) {
return VERSION_4; return VERSION_4;
} else { } else {
return UNKNOWN_VERSION; return UNKNOWN_VERSION;

View file

@ -29,10 +29,10 @@ namespace latinime {
class FormatUtils { class FormatUtils {
public: public:
enum FORMAT_VERSION { enum FORMAT_VERSION {
VERSION_2, VERSION_2 = 2,
VERSION_3, VERSION_3 = 3,
VERSION_4, VERSION_4 = 4,
UNKNOWN_VERSION UNKNOWN_VERSION = -1
}; };
// 32 bit magic number is stored at the beginning of the dictionary header to reject // 32 bit magic number is stored at the beginning of the dictionary header to reject