Merge "Allow mmapping dictionary file in writable mode in openNative."

main
Keisuke Kuroynagi 2013-06-25 07:09:37 +00:00 committed by Android (Google) Code Review
commit 8bc0b51369
5 changed files with 19 additions and 13 deletions

View File

@ -91,7 +91,8 @@ public final class BinaryDictionary extends Dictionary {
JniUtils.loadNativeLibrary(); JniUtils.loadNativeLibrary();
} }
private static native long openNative(String sourceDir, long dictOffset, long dictSize); private static native long openNative(String sourceDir, long dictOffset, long dictSize,
boolean isUpdatable);
private static native void closeNative(long dict); private static native void closeNative(long dict);
private static native int getProbabilityNative(long dict, int[] word); private static native int getProbabilityNative(long dict, int[] word);
private static native boolean isValidBigramNative(long dict, int[] word1, int[] word2); private static native boolean isValidBigramNative(long dict, int[] word1, int[] word2);
@ -106,7 +107,7 @@ public final class BinaryDictionary extends Dictionary {
// TODO: Move native dict into session // TODO: Move native dict into session
private final void loadDictionary(final String path, final long startOffset, private final void loadDictionary(final String path, final long startOffset,
final long length) { final long length) {
mNativeDict = openNative(path, startOffset, length); mNativeDict = openNative(path, startOffset, length, false /* isUpdatable */);
} }
@Override @Override

View File

@ -40,7 +40,7 @@ class ProximityInfo;
static void releaseDictBuf(const void *dictBuf, const size_t length, const int fd); static void releaseDictBuf(const void *dictBuf, const size_t length, const int fd);
static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir, static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring sourceDir,
jlong dictOffset, jlong dictSize) { jlong dictOffset, jlong dictSize, jboolean isUpdatable) {
PROF_OPEN; PROF_OPEN;
PROF_START(66); PROF_START(66);
const jsize sourceDirUtf8Length = env->GetStringUTFLength(sourceDir); const jsize sourceDirUtf8Length = env->GetStringUTFLength(sourceDir);
@ -54,7 +54,9 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
int fd = 0; int fd = 0;
void *dictBuf = 0; void *dictBuf = 0;
int offset = 0; int offset = 0;
fd = open(sourceDirChars, O_RDONLY); const bool updatableMmap = (isUpdatable == JNI_TRUE);
const int openMode = updatableMmap ? O_RDWR : O_RDONLY;
fd = open(sourceDirChars, openMode);
if (fd < 0) { if (fd < 0) {
AKLOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno); AKLOGE("DICT: Can't open sourceDir. sourceDirChars=%s errno=%d", sourceDirChars, errno);
return 0; return 0;
@ -63,7 +65,8 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
offset = static_cast<int>(dictOffset) % pagesize; offset = static_cast<int>(dictOffset) % pagesize;
int adjDictOffset = static_cast<int>(dictOffset) - offset; int adjDictOffset = static_cast<int>(dictOffset) - offset;
int adjDictSize = static_cast<int>(dictSize) + offset; int adjDictSize = static_cast<int>(dictSize) + offset;
dictBuf = mmap(0, adjDictSize, PROT_READ, MAP_PRIVATE, fd, adjDictOffset); const int protMode = updatableMmap ? PROT_READ | PROT_WRITE : PROT_READ;
dictBuf = mmap(0, adjDictSize, protMode, MAP_PRIVATE, fd, adjDictOffset);
if (dictBuf == MAP_FAILED) { if (dictBuf == MAP_FAILED) {
AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno); AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno);
return 0; return 0;
@ -80,7 +83,8 @@ static jlong latinime_BinaryDictionary_open(JNIEnv *env, jclass clazz, jstring s
AKLOGE("DICT: dictionary format is unknown, bad magic number"); AKLOGE("DICT: dictionary format is unknown, bad magic number");
releaseDictBuf(static_cast<const char *>(dictBuf) - offset, adjDictSize, fd); releaseDictBuf(static_cast<const char *>(dictBuf) - offset, adjDictSize, fd);
} else { } else {
dictionary = new Dictionary(dictBuf, static_cast<int>(dictSize), fd, offset); dictionary = new Dictionary(
dictBuf, static_cast<int>(dictSize), fd, offset, updatableMmap);
} }
PROF_END(66); PROF_END(66);
PROF_CLOSE; PROF_CLOSE;
@ -245,7 +249,7 @@ static void releaseDictBuf(const void *dictBuf, const size_t length, const int f
static const JNINativeMethod sMethods[] = { static const JNINativeMethod sMethods[] = {
{ {
const_cast<char *>("openNative"), const_cast<char *>("openNative"),
const_cast<char *>("(Ljava/lang/String;JJ)J"), const_cast<char *>("(Ljava/lang/String;JJZ)J"),
reinterpret_cast<void *>(latinime_BinaryDictionary_open) reinterpret_cast<void *>(latinime_BinaryDictionary_open)
}, },
{ {

View File

@ -30,9 +30,9 @@ class BinaryDictionaryHeader;
class BinaryDictionaryInfo { class BinaryDictionaryInfo {
public: public:
BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize, const int mmapFd, BinaryDictionaryInfo(const uint8_t *const dictBuf, const int dictSize, const int mmapFd,
const int dictBufOffset) const int dictBufOffset, const bool isUpdatable)
: mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd), : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd),
mDictBufOffset(dictBufOffset), mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable),
mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, mDictSize)), mDictionaryFormat(BinaryDictionaryFormat::detectFormatVersion(mDictBuf, mDictSize)),
mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {} mDictionaryHeader(this), mDictRoot(mDictBuf + mDictionaryHeader.getSize()) {}
@ -75,6 +75,7 @@ class BinaryDictionaryInfo {
const int mDictSize; const int mDictSize;
const int mMmapFd; const int mMmapFd;
const int mDictBufOffset; const int mDictBufOffset;
const bool mIsUpdatable;
const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat; const BinaryDictionaryFormat::FORMAT_VERSION mDictionaryFormat;
const BinaryDictionaryHeader mDictionaryHeader; const BinaryDictionaryHeader mDictionaryHeader;
const uint8_t *const mDictRoot; const uint8_t *const mDictRoot;

View File

@ -32,9 +32,9 @@
namespace latinime { namespace latinime {
Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset) Dictionary::Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset, bool isUpdatable)
: mBinaryDictionaryInfo( : mBinaryDictionaryInfo(static_cast<const uint8_t *>(dict), dictSize, mmapFd,
static_cast<const uint8_t *>(dict), dictSize, mmapFd, dictBufOffset), dictBufOffset, isUpdatable),
mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)), mBigramDictionary(new BigramDictionary(&mBinaryDictionaryInfo)),
mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())), mGestureSuggest(new Suggest(GestureSuggestPolicyFactory::getGestureSuggestPolicy())),
mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) { mTypingSuggest(new Suggest(TypingSuggestPolicyFactory::getTypingSuggestPolicy())) {

View File

@ -52,7 +52,7 @@ 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(void *dict, int dictSize, int mmapFd, int dictBufOffset); Dictionary(void *dict, int dictSize, int mmapFd, int dictBufOffset, bool isUpdatable);
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,