Quit passing header file path to native.

Bug: 12089197
Change-Id: Ia898f8d2836c0c131a1834dc0a396d9121ecb972
This commit is contained in:
Keisuke Kuroyanagi 2013-12-12 16:48:34 +09:00
parent 4e9d115401
commit 9ff51527a3
23 changed files with 180 additions and 155 deletions

View file

@ -60,10 +60,9 @@ abstract public class AbstractDictionaryWriter {
abstract protected void writeDictionary(final DictEncoder dictEncoder,
final Map<String, String> attributeMap) throws IOException, UnsupportedFormatException;
public void write(final String fileName, final Map<String, String> attributeMap) {
final String tempFileName = fileName + ".temp";
final File file = new File(mContext.getFilesDir(), fileName);
final File tempFile = new File(mContext.getFilesDir(), tempFileName);
public void write(final File file, final Map<String, String> attributeMap) {
final String tempFilePath = file.getAbsolutePath() + ".temp";
final File tempFile = new File(tempFilePath);
try {
final DictEncoder dictEncoder = new Ver3DictEncoder(tempFile);
writeDictionary(dictEncoder, attributeMap);

View file

@ -144,20 +144,8 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
return mBinaryDictionary.isValidDictionary();
}
protected String getFileNameToCreateDict(final String dictName) {
return dictName + DICT_FILE_EXTENSION;
}
protected String getFileNameToOpenDict(final String dictName) {
return getFileNameToCreateDict(dictName);
}
private File getFileToCreateDict() {
return new File(mContext.getFilesDir(), getFileNameToCreateDict(mDictName));
}
private File getFileToOpenDict() {
return new File(mContext.getFilesDir(), getFileNameToOpenDict(mDictName));
private File getDictFile() {
return new File(mContext.getFilesDir(), mDictName + DICT_FILE_EXTENSION);
}
/**
@ -270,17 +258,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
public void run() {
if (mDictionaryWriter == null) {
mBinaryDictionary.close();
final File file = getFileToCreateDict();
file.delete();
final File file = getDictFile();
if (file.exists() && !FileUtils.deleteRecursively(file)) {
Log.e(TAG, "Can't remove a file: " + file.getName());
}
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
DICTIONARY_FORMAT_VERSION, getHeaderAttributeMap());
// We have 'fileToOpen' in addition to 'file' for the v4 dictionary format
// where 'file' is a directory, and 'fileToOpen' is a normal file.
final File fileToOpen = getFileToOpenDict();
// TODO: Make BinaryDictionary's constructor be able to accept filename
// without extension.
mBinaryDictionary = new BinaryDictionary(
fileToOpen.getAbsolutePath(), 0 /* offset */, fileToOpen.length(),
file.getAbsolutePath(), 0 /* offset */, file.length(),
true /* useFullEditDistance */, null, mDictType, mIsUpdatable);
} else {
mDictionaryWriter.clear();
@ -531,7 +516,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
}
}
final File file = getFileToOpenDict();
final File file = getDictFile();
final String filename = file.getAbsolutePath();
final long length = file.length();
@ -572,14 +557,14 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
if (needsToReloadBeforeWriting()) {
mDictionaryWriter.clear();
loadDictionaryAsync();
mDictionaryWriter.write(getFileNameToCreateDict(mDictName), getHeaderAttributeMap());
mDictionaryWriter.write(getDictFile(), getHeaderAttributeMap());
} else {
if (mBinaryDictionary == null || !isValidDictionary()
// TODO: remove the check below
|| !matchesExpectedBinaryDictFormatVersionForThisType(
mBinaryDictionary.getFormatVersion())) {
final File file = getFileToCreateDict();
if (!FileUtils.deleteRecursively(file)) {
final File file = getDictFile();
if (file.exists() && !FileUtils.deleteRecursively(file)) {
Log.e(TAG, "Can't remove a file: " + file.getName());
}
BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
@ -706,8 +691,7 @@ abstract public class ExpandableBinaryDictionary extends Dictionary {
// TODO: cache the file's existence so that we avoid doing a disk access each time.
private boolean dictionaryFileExists() {
final File file = getFileToOpenDict();
return file.exists();
return getDictFile().exists();
}
/**

View file

@ -112,16 +112,6 @@ public abstract class DecayingExpandableBinaryDictionaryBase extends ExpandableB
return formatVersion == REQUIRED_BINARY_DICTIONARY_VERSION;
}
@Override
protected String getFileNameToCreateDict(final String dictName) {
return dictName;
}
@Override
protected String getFileNameToOpenDict(final String dictName) {
return dictName + "/" + dictName + FormatSpec.HEADER_FILE_EXTENSION;
}
public void addMultipleDictionaryEntriesToDictionary(
final ArrayList<LanguageModelParam> languageModelParams,
final ExpandableBinaryDictionary.AddMultipleDictionaryEntriesCallback callback) {

View file

@ -16,11 +16,13 @@
#include "suggest/policyimpl/dictionary/structure/dictionary_structure_with_buffer_policy_factory.h"
#include <climits>
#include <stdint.h>
#include "defines.h"
#include "suggest/policyimpl/dictionary/structure/v2/patricia_trie_policy.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_buffers.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_dict_constants.h"
#include "suggest/policyimpl/dictionary/structure/v4/ver4_patricia_trie_policy.h"
#include "suggest/policyimpl/dictionary/utils/file_utils.h"
#include "suggest/policyimpl/dictionary/utils/format_utils.h"
@ -32,41 +34,99 @@ namespace latinime {
DictionaryStructureWithBufferPolicyFactory
::newDictionaryStructureWithBufferPolicy(const char *const path,
const int bufOffset, const int size, const bool isUpdatable) {
// Allocated buffer in MmapedBuffer::newBuffer() will be freed in the destructor of
// MmappedBufferWrapper if the instance has the responsibility.
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset, size,
if (FileUtils::existsDir(path)) {
// Given path represents a directory.
return newPolicyforDirectoryDict(path, isUpdatable);
} else {
if (isUpdatable) {
AKLOGE("One file dictionaries don't support updating. path: %s", path);
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
return newPolicyforFileDict(path, bufOffset, size);
}
}
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
DictionaryStructureWithBufferPolicyFactory::newPolicyforDirectoryDict(
const char *const path, const bool isUpdatable) {
const int headerFilePathBufSize = PATH_MAX + 1 /* terminator */;
char headerFilePath[headerFilePathBufSize];
getHeaderFilePathInDictDir(path, headerFilePathBufSize, headerFilePath);
// Allocated buffer in MmapedBuffer::openBuffer() will be freed in the destructor of
// MmappedBufferPtr if the instance has the responsibility.
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(headerFilePath,
isUpdatable);
if (!mmappedBuffer.get()) {
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(),
mmappedBuffer.get()->getBufferSize())) {
case FormatUtils::VERSION_2:
AKLOGE("Given path is a directory but the format is version 2. path: %s", path);
break;
case FormatUtils::VERSION_4: {
const int dictDirPathBufSize = strlen(headerFilePath) + 1 /* terminator */;
char dictPath[dictDirPathBufSize];
if (!FileUtils::getFilePathWithoutSuffix(headerFilePath,
Ver4DictConstants::HEADER_FILE_EXTENSION, dictDirPathBufSize, dictPath)) {
AKLOGE("Dictionary file name is not valid as a ver4 dictionary. path: %s", path);
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
Ver4DictBuffers::openVer4DictBuffers(dictPath, mmappedBuffer);
if (!dictBuffers.get()->isValid()) {
AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements. path: %s",
path);
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new Ver4PatriciaTriePolicy(dictBuffers));
}
default:
AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path);
break;
}
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
/* static */ DictionaryStructureWithBufferPolicy::StructurePolicyPtr
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.
MmappedBuffer::MmappedBufferPtr mmappedBuffer = MmappedBuffer::openBuffer(path, bufOffset,
size, false /* isUpdatable */);
if (!mmappedBuffer.get()) {
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
switch (FormatUtils::detectFormatVersion(mmappedBuffer.get()->getBuffer(),
mmappedBuffer.get()->getBufferSize())) {
case FormatUtils::VERSION_2:
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new PatriciaTriePolicy(mmappedBuffer));
case FormatUtils::VERSION_4: {
const int dictDirPathBufSize = strlen(path) + 1 /* terminator */;
char dictDirPath[dictDirPathBufSize];
if (!FileUtils::getFilePathWithoutSuffix(path, Ver4DictConstants::HEADER_FILE_EXTENSION,
dictDirPathBufSize, dictDirPath)) {
// Dictionary file name is not valid as a version 4 dictionary.
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
const Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers =
Ver4DictBuffers::openVer4DictBuffers(dictDirPath, mmappedBuffer);
if (!dictBuffers.get()->isValid()) {
AKLOGE("DICT: The dictionary doesn't satisfy ver4 format requirements.");
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(
new Ver4PatriciaTriePolicy(dictBuffers));
}
case FormatUtils::VERSION_4:
AKLOGE("Given path is a file but the format is version 4. path: %s", path);
break;
default:
AKLOGE("DICT: dictionary format is unknown, bad magic number");
AKLOGE("DICT: dictionary format is unknown, bad magic number. path: %s", path);
break;
}
ASSERT(false);
return DictionaryStructureWithBufferPolicy::StructurePolicyPtr(0);
}
}
/* static */ void DictionaryStructureWithBufferPolicyFactory::getHeaderFilePathInDictDir(
const char *const dictDirPath, const int outHeaderFileBufSize,
char *const outHeaderFilePath) {
const int dictNameBufSize = strlen(dictDirPath) + 1 /* terminator */;
char dictName[dictNameBufSize];
FileUtils::getBasename(dictDirPath, dictNameBufSize, dictName);
snprintf(outHeaderFilePath, outHeaderFileBufSize, "%s/%s%s", dictDirPath,
dictName, Ver4DictConstants::HEADER_FILE_EXTENSION);
}
} // namespace latinime

View file

@ -33,6 +33,15 @@ class DictionaryStructureWithBufferPolicyFactory {
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(DictionaryStructureWithBufferPolicyFactory);
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newPolicyforDirectoryDict(const char *const path, const bool isUpdatable);
static DictionaryStructureWithBufferPolicy::StructurePolicyPtr
newPolicyforFileDict(const char *const path, const int bufOffset, const int size);
static void getHeaderFilePathInDictDir(const char *const dirPath,
const int outHeaderFileBufSize, char *const outHeaderFilePath);
};
} // namespace latinime
#endif // LATINIME_DICTIONARY_STRUCTURE_WITH_BUFFER_POLICY_FACTORY_H

View file

@ -27,9 +27,9 @@ namespace latinime {
class BigramDictContent : public SparseTableDictContent {
public:
BigramDictContent(const char *const dictDirPath, const bool hasHistoricalInfo,
BigramDictContent(const char *const dictPath, const bool hasHistoricalInfo,
const bool isUpdatable)
: SparseTableDictContent(dictDirPath,
: SparseTableDictContent(dictPath,
Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION,
Ver4DictConstants::BIGRAM_FILE_EXTENSION, isUpdatable,
@ -73,8 +73,8 @@ class BigramDictContent : public SparseTableDictContent {
bool copyBigramList(const int bigramListPos, const int toPos);
bool flushToFile(const char *const dictBasePath) const {
return flush(dictBasePath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
bool flushToFile(const char *const dictPath) const {
return flush(dictPath, Ver4DictConstants::BIGRAM_LOOKUP_TABLE_FILE_EXTENSION,
Ver4DictConstants::BIGRAM_CONTENT_TABLE_FILE_EXTENSION,
Ver4DictConstants::BIGRAM_FILE_EXTENSION);
}

View file

@ -71,7 +71,7 @@ bool ProbabilityDictContent::setProbabilityEntry(const int terminalId,
return writeEntry(probabilityEntry, entryPos);
}
bool ProbabilityDictContent::flushToFile(const char *const dictBasePath) const {
bool ProbabilityDictContent::flushToFile(const char *const dictPath) const {
if (getEntryPos(mSize) < getBuffer()->getTailPosition()) {
ProbabilityDictContent probabilityDictContentToWrite(mHasHistoricalInfo);
for (int i = 0; i < mSize; ++i) {
@ -81,10 +81,10 @@ bool ProbabilityDictContent::flushToFile(const char *const dictBasePath) const {
return false;
}
}
return probabilityDictContentToWrite.flush(dictBasePath,
return probabilityDictContentToWrite.flush(dictPath,
Ver4DictConstants::FREQ_FILE_EXTENSION);
} else {
return flush(dictBasePath, Ver4DictConstants::FREQ_FILE_EXTENSION);
return flush(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION);
}
}

View file

@ -29,9 +29,9 @@ class ProbabilityEntry;
class ProbabilityDictContent : public SingleDictContent {
public:
ProbabilityDictContent(const char *const dictDirPath, const bool hasHistoricalInfo,
ProbabilityDictContent(const char *const dictPath, const bool hasHistoricalInfo,
const bool isUpdatable)
: SingleDictContent(dictDirPath, Ver4DictConstants::FREQ_FILE_EXTENSION, isUpdatable),
: SingleDictContent(dictPath, Ver4DictConstants::FREQ_FILE_EXTENSION, isUpdatable),
mHasHistoricalInfo(hasHistoricalInfo),
mSize(getBuffer()->getTailPosition() / getEntrySize()) {}
@ -42,7 +42,7 @@ class ProbabilityDictContent : public SingleDictContent {
bool setProbabilityEntry(const int terminalId, const ProbabilityEntry *const probabilityEntry);
bool flushToFile(const char *const dictDirPath) const;
bool flushToFile(const char *const dictPath) const;
bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
const ProbabilityDictContent *const originalProbabilityDictContent);

View file

@ -46,8 +46,8 @@ int ShortcutDictContent::getShortcutListHeadPos(const int terminalId) const {
return addressLookupTable->get(terminalId);
}
bool ShortcutDictContent::flushToFile(const char *const dictBasePath) const {
return flush(dictBasePath, Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION,
bool ShortcutDictContent::flushToFile(const char *const dictPath) const {
return flush(dictPath, Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION,
Ver4DictConstants::SHORTCUT_CONTENT_TABLE_FILE_EXTENSION,
Ver4DictConstants::SHORTCUT_FILE_EXTENSION);
}

View file

@ -26,8 +26,8 @@ namespace latinime {
class ShortcutDictContent : public SparseTableDictContent {
public:
ShortcutDictContent(const char *const dictDirPath, const bool isUpdatable)
: SparseTableDictContent(dictDirPath,
ShortcutDictContent(const char *const dictPath, const bool isUpdatable)
: SparseTableDictContent(dictPath,
Ver4DictConstants::SHORTCUT_LOOKUP_TABLE_FILE_EXTENSION,
Ver4DictConstants::SHORTCUT_CONTENT_TABLE_FILE_EXTENSION,
Ver4DictConstants::SHORTCUT_FILE_EXTENSION, isUpdatable,
@ -53,7 +53,7 @@ class ShortcutDictContent : public SparseTableDictContent {
// Returns head position of shortcut list for a PtNode specified by terminalId.
int getShortcutListHeadPos(const int terminalId) const;
bool flushToFile(const char *const dictBasePath) const;
bool flushToFile(const char *const dictPath) const;
bool runGC(const TerminalPositionLookupTable::TerminalIdMap *const terminalIdMap,
const ShortcutDictContent *const originalShortcutDictContent);

View file

@ -28,9 +28,9 @@ namespace latinime {
class SingleDictContent : public DictContent {
public:
SingleDictContent(const char *const dictDirPath, const char *const contentFileName,
SingleDictContent(const char *const dictPath, const char *const contentFileName,
const bool isUpdatable)
: mMmappedBuffer(MmappedBuffer::openBuffer(dictDirPath, contentFileName, isUpdatable)),
: mMmappedBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
mExpandableContentBuffer(mMmappedBuffer.get() ? mMmappedBuffer.get()->getBuffer() : 0,
mMmappedBuffer.get() ? mMmappedBuffer.get()->getBufferSize() : 0,
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
@ -59,8 +59,8 @@ class SingleDictContent : public DictContent {
return &mExpandableContentBuffer;
}
bool flush(const char *const dictBasePath, const char *const contentFileNameSuffix) const {
return DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath,
bool flush(const char *const dictPath, const char *const contentFileNameSuffix) const {
return DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath,
contentFileNameSuffix, &mExpandableContentBuffer);
}

View file

@ -18,18 +18,18 @@
namespace latinime {
bool SparseTableDictContent::flush(const char *const dictBasePath,
bool SparseTableDictContent::flush(const char *const dictPath,
const char *const lookupTableFileNameSuffix, const char *const addressTableFileNameSuffix,
const char *const contentFileNameSuffix) const {
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, lookupTableFileNameSuffix,
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, lookupTableFileNameSuffix,
&mExpandableLookupTableBuffer)){
return false;
}
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, addressTableFileNameSuffix,
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, addressTableFileNameSuffix,
&mExpandableAddressTableBuffer)) {
return false;
}
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath, contentFileNameSuffix,
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath, contentFileNameSuffix,
&mExpandableContentBuffer)) {
return false;
}

View file

@ -30,15 +30,15 @@ namespace latinime {
// TODO: Support multiple contents.
class SparseTableDictContent : public DictContent {
public:
AK_FORCE_INLINE SparseTableDictContent(const char *const dictDirPath,
AK_FORCE_INLINE SparseTableDictContent(const char *const dictPath,
const char *const lookupTableFileName, const char *const addressTableFileName,
const char *const contentFileName, const bool isUpdatable,
const int sparseTableBlockSize, const int sparseTableDataSize)
: mLookupTableBuffer(
MmappedBuffer::openBuffer(dictDirPath, lookupTableFileName, isUpdatable)),
MmappedBuffer::openBuffer(dictPath, lookupTableFileName, isUpdatable)),
mAddressTableBuffer(
MmappedBuffer::openBuffer(dictDirPath, addressTableFileName, isUpdatable)),
mContentBuffer(MmappedBuffer::openBuffer(dictDirPath, contentFileName, isUpdatable)),
MmappedBuffer::openBuffer(dictPath, addressTableFileName, isUpdatable)),
mContentBuffer(MmappedBuffer::openBuffer(dictPath, contentFileName, isUpdatable)),
mExpandableLookupTableBuffer(
mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBuffer() : 0,
mLookupTableBuffer.get() ? mLookupTableBuffer.get()->getBufferSize() : 0,

View file

@ -50,7 +50,7 @@ bool TerminalPositionLookupTable::setTerminalPtNodePosition(
Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE, getEntryPos(terminalId));
}
bool TerminalPositionLookupTable::flushToFile(const char *const dictBasePath) const {
bool TerminalPositionLookupTable::flushToFile(const char *const dictPath) const {
// If the used buffer size is smaller than the actual buffer size, regenerate the lookup
// table and write the new table to the file.
if (getEntryPos(mSize) < getBuffer()->getTailPosition()) {
@ -63,12 +63,12 @@ bool TerminalPositionLookupTable::flushToFile(const char *const dictBasePath) co
return false;
}
}
return lookupTableToWrite.flush(dictBasePath,
return lookupTableToWrite.flush(dictPath,
Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION);
} else {
// We can simply use this lookup table because the buffer size has not been
// changed.
return flush(dictBasePath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION);
return flush(dictPath, Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION);
}
}

View file

@ -28,8 +28,8 @@ class TerminalPositionLookupTable : public SingleDictContent {
public:
typedef hash_map_compat<int, int> TerminalIdMap;
TerminalPositionLookupTable(const char *const dictDirPath, const bool isUpdatable)
: SingleDictContent(dictDirPath,
TerminalPositionLookupTable(const char *const dictPath, const bool isUpdatable)
: SingleDictContent(dictPath,
Ver4DictConstants::TERMINAL_ADDRESS_TABLE_FILE_EXTENSION, isUpdatable),
mSize(getBuffer()->getTailPosition()
/ Ver4DictConstants::TERMINAL_ADDRESS_TABLE_ADDRESS_SIZE) {}
@ -44,7 +44,7 @@ class TerminalPositionLookupTable : public SingleDictContent {
return mSize;
}
bool flushToFile(const char *const dictBasePath) const;
bool flushToFile(const char *const dictPath) const;
bool runGCTerminalIds(TerminalIdMap *const terminalIdMap);

View file

@ -27,10 +27,10 @@
namespace latinime {
/* static */ Ver4DictBuffers::Ver4DictBuffersPtr Ver4DictBuffers::openVer4DictBuffers(
const char *const dictDirPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer) {
const char *const dictPath, const MmappedBuffer::MmappedBufferPtr &headerBuffer) {
const bool isUpdatable = headerBuffer.get() ? headerBuffer.get()->isUpdatable() : false;
// TODO: take only dictDirPath, and open both header and trie files in the constructor below
return Ver4DictBuffersPtr(new Ver4DictBuffers(dictDirPath, headerBuffer, isUpdatable));
return Ver4DictBuffersPtr(new Ver4DictBuffers(dictPath, headerBuffer, isUpdatable));
}
bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath,
@ -57,38 +57,38 @@ bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath,
const int dictNameBufSize = strlen(dictDirPath) + 1 /* terminator */;
char dictName[dictNameBufSize];
FileUtils::getBasename(dictDirPath, dictNameBufSize, dictName);
const int dictBasePathBufSize = FileUtils::getFilePathBufSize(tmpDirPath, dictName);
char dictBasePath[dictBasePathBufSize];
FileUtils::getFilePath(tmpDirPath, dictName, dictBasePathBufSize, dictBasePath);
const int dictPathBufSize = FileUtils::getFilePathBufSize(tmpDirPath, dictName);
char dictPath[dictPathBufSize];
FileUtils::getFilePath(tmpDirPath, dictName, dictPathBufSize, dictPath);
// Write header file.
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath,
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath,
Ver4DictConstants::HEADER_FILE_EXTENSION, headerBuffer)) {
AKLOGE("Dictionary header file %s%s cannot be written.", tmpDirPath,
Ver4DictConstants::HEADER_FILE_EXTENSION);
return false;
}
// Write trie file.
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictBasePath,
if (!DictFileWritingUtils::flushBufferToFileWithSuffix(dictPath,
Ver4DictConstants::TRIE_FILE_EXTENSION, &mExpandableTrieBuffer)) {
AKLOGE("Dictionary trie file %s%s cannot be written.", tmpDirPath,
Ver4DictConstants::TRIE_FILE_EXTENSION);
return false;
}
// Write dictionary contents.
if (!mTerminalPositionLookupTable.flushToFile(dictBasePath)) {
if (!mTerminalPositionLookupTable.flushToFile(dictPath)) {
AKLOGE("Terminal position lookup table cannot be written. %s", tmpDirPath);
return false;
}
if (!mProbabilityDictContent.flushToFile(dictBasePath)) {
if (!mProbabilityDictContent.flushToFile(dictPath)) {
AKLOGE("Probability dict content cannot be written. %s", tmpDirPath);
return false;
}
if (!mBigramDictContent.flushToFile(dictBasePath)) {
if (!mBigramDictContent.flushToFile(dictPath)) {
AKLOGE("Bigram dict content cannot be written. %s", tmpDirPath);
return false;
}
if (!mShortcutDictContent.flushToFile(dictBasePath)) {
if (!mShortcutDictContent.flushToFile(dictPath)) {
AKLOGE("Shortcut dict content cannot be written. %s", tmpDirPath);
return false;
}
@ -107,10 +107,10 @@ bool Ver4DictBuffers::flushHeaderAndDictBuffers(const char *const dictDirPath,
return true;
}
Ver4DictBuffers::Ver4DictBuffers(const char *const dictDirPath,
Ver4DictBuffers::Ver4DictBuffers(const char *const dictPath,
const MmappedBuffer::MmappedBufferPtr &headerBuffer, const bool isUpdatable)
: mHeaderBuffer(headerBuffer),
mDictBuffer(MmappedBuffer::openBuffer(dictDirPath,
mDictBuffer(MmappedBuffer::openBuffer(dictPath,
Ver4DictConstants::TRIE_FILE_EXTENSION, isUpdatable)),
mHeaderPolicy(headerBuffer.get()->getBuffer(), FormatUtils::VERSION_4),
mExpandableHeaderBuffer(headerBuffer.get()->getBuffer(), mHeaderPolicy.getSize(),
@ -118,12 +118,12 @@ Ver4DictBuffers::Ver4DictBuffers(const char *const dictDirPath,
mExpandableTrieBuffer(mDictBuffer.get()->getBuffer(),
mDictBuffer.get()->getBufferSize(),
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE),
mTerminalPositionLookupTable(dictDirPath, isUpdatable),
mProbabilityDictContent(dictDirPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
mTerminalPositionLookupTable(dictPath, isUpdatable),
mProbabilityDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
isUpdatable),
mBigramDictContent(dictDirPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
mBigramDictContent(dictPath, mHeaderPolicy.hasHistoricalInfoOfWords(),
isUpdatable),
mShortcutDictContent(dictDirPath, isUpdatable),
mShortcutDictContent(dictPath, isUpdatable),
mIsUpdatable(isUpdatable) {}
Ver4DictBuffers::Ver4DictBuffers(const HeaderPolicy *const headerPolicy)

View file

@ -32,12 +32,9 @@
namespace latinime {
void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const trieFilePath,
void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const dictDirPath,
const int unigramCount, const int bigramCount) const {
const HeaderPolicy *const headerPolicy = mBuffers->getHeaderPolicy();
const int dirPathBufSize = strlen(trieFilePath) + 1 /* terminator */;
char dirPath[dirPathBufSize];
FileUtils::getDirPath(trieFilePath, dirPathBufSize, dirPath);
BufferWithExtendableBuffer headerBuffer(
BufferWithExtendableBuffer::DEFAULT_MAX_ADDITIONAL_BUFFER_SIZE);
const int extendedRegionSize = headerPolicy->getExtendedRegionSize()
@ -50,11 +47,11 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFile(const char *const trieFilePa
extendedRegionSize);
return;
}
mBuffers->flushHeaderAndDictBuffers(dirPath, &headerBuffer);
mBuffers->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
}
void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeArrayPos,
const char *const trieFilePath) {
const char *const dictDirPath) {
const HeaderPolicy *const headerPolicy = mBuffers->getHeaderPolicy();
Ver4DictBuffers::Ver4DictBuffersPtr dictBuffers(
Ver4DictBuffers::createVer4DictBuffers(headerPolicy));
@ -70,10 +67,7 @@ void Ver4PatriciaTrieWritingHelper::writeToDictFileWithGC(const int rootPtNodeAr
0 /* extendedRegionSize */)) {
return;
}
const int dirPathBufSize = strlen(trieFilePath) + 1 /* terminator */;
char dirPath[dirPathBufSize];
FileUtils::getDirPath(trieFilePath, dirPathBufSize, dirPath);
dictBuffers.get()->flushHeaderAndDictBuffers(dirPath, &headerBuffer);
dictBuffers.get()->flushHeaderAndDictBuffers(dictDirPath, &headerBuffer);
}
bool Ver4PatriciaTrieWritingHelper::runGC(const int rootPtNodeArrayPos,

View file

@ -33,11 +33,10 @@ class Ver4PatriciaTrieWritingHelper {
Ver4PatriciaTrieWritingHelper(Ver4DictBuffers *const buffers)
: mBuffers(buffers) {}
void writeToDictFile(const char *const trieFilePath, const int unigramCount,
void writeToDictFile(const char *const dictDirPath, const int unigramCount,
const int bigramCount) const;
void writeToDictFileWithGC(const int rootPtNodeArrayPos,
const char *const trieFilePath);
void writeToDictFileWithGC(const int rootPtNodeArrayPos, const char *const dictDirPath);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(Ver4PatriciaTrieWritingHelper);

View file

@ -147,7 +147,7 @@ namespace latinime {
const char *const baseName = basename(filePathBuf);
const int baseNameLength = strlen(baseName);
if (baseNameLength >= outNameBufSize) {
AKLOGE("outNameBufSize is too small. dirPath: %s, outNameBufSize: %d",
AKLOGE("outNameBufSize is too small. filePath: %s, outNameBufSize: %d",
filePath, outNameBufSize);
return;
}

View file

@ -39,25 +39,15 @@ class ExclusiveOwnershipPointer {
deletePointersIfHavingOwnership();
}
// Move the ownership.
AK_FORCE_INLINE ExclusiveOwnershipPointer<T> &operator=(
const ExclusiveOwnershipPointer<T> &pointer) {
// Delete pointers when this is an owner of another pointer.
deletePointersIfHavingOwnership();
mPointer = pointer.mPointer;
mSharedOwnerPtr = pointer.mSharedOwnerPtr;
transferOwnership(pointer);
return *this;
}
AK_FORCE_INLINE T *get() const {
return mPointer;
}
private:
// This class allows to copy and assign and ensures only one instance has the ownership of the
// This class allows to copy and ensures only one instance has the ownership of the
// managed pointer.
DISALLOW_DEFAULT_CONSTRUCTOR(ExclusiveOwnershipPointer);
DISALLOW_ASSIGNMENT_OPERATOR(ExclusiveOwnershipPointer);
void transferOwnership(const ExclusiveOwnershipPointer<T> *const src) {
if (*mSharedOwnerPtr != src) {

View file

@ -114,7 +114,6 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
final File file = File.createTempFile(dictId, TEST_DICT_FILE_EXTENSION,
getContext().getCacheDir());
FileUtils.deleteRecursively(file);
file.mkdir();
Map<String, String> attributeMap = new HashMap<String, String>();
attributeMap.put(FormatSpec.FileHeader.SUPPORTS_DYNAMIC_UPDATE_ATTRIBUTE,
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE);
@ -125,7 +124,7 @@ public class BinaryDictionaryDecayingTests extends AndroidTestCase {
final String headerFileName = file.getName() + FormatSpec.HEADER_FILE_EXTENSION;
if (BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
FormatSpec.VERSION4, attributeMap)) {
return new File(file, headerFileName);
return file;
} else {
throw new IOException("Empty dictionary " + file.getAbsolutePath() + " "
+ headerFileName + " cannot be created.");

View file

@ -25,6 +25,7 @@ import com.android.inputmethod.latin.BinaryDictionary.LanguageModelParam;
import com.android.inputmethod.latin.makedict.CodePointUtils;
import com.android.inputmethod.latin.makedict.FormatSpec;
import com.android.inputmethod.latin.makedict.FusionDictionary.WeightedString;
import com.android.inputmethod.latin.utils.FileUtils;
import com.android.inputmethod.latin.utils.UnigramProperty;
import java.io.File;
@ -70,13 +71,12 @@ public class BinaryDictionaryTests extends AndroidTestCase {
Map<String, String> attributeMap = new HashMap<String, String>();
attributeMap.put(FormatSpec.FileHeader.SUPPORTS_DYNAMIC_UPDATE_ATTRIBUTE,
FormatSpec.FileHeader.ATTRIBUTE_VALUE_TRUE);
final String headerFileName = file.getName() + FormatSpec.HEADER_FILE_EXTENSION;
if (BinaryDictionary.createEmptyDictFile(file.getAbsolutePath(),
FormatSpec.VERSION4, attributeMap)) {
return new File(file, headerFileName);
return file;
} else {
throw new IOException("Empty dictionary " + file.getAbsolutePath() + " "
+ headerFileName + " cannot be created.");
throw new IOException("Empty dictionary " + file.getAbsolutePath()
+ " cannot be created.");
}
}
@ -99,7 +99,7 @@ public class BinaryDictionaryTests extends AndroidTestCase {
binaryDictionary.close();
assertFalse("binaryDictionary must be invalid after closing.",
binaryDictionary.isValidDictionary());
dictFile.delete();
FileUtils.deleteRecursively(dictFile);
binaryDictionary = new BinaryDictionary(dictFile.getAbsolutePath(), 0 /* offset */,
dictFile.length(), true /* useFullEditDistance */, Locale.getDefault(),
TEST_LOCALE, true /* isUpdatable */);

View file

@ -20,6 +20,7 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import com.android.inputmethod.latin.ExpandableBinaryDictionary;
import com.android.inputmethod.latin.utils.CollectionUtils;
import java.io.File;
@ -136,7 +137,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
Log.d(TAG, "This test can be used for profiling.");
Log.d(TAG, "Usage: please set UserHistoryDictionary.PROFILE_SAVE_RESTORE to true.");
final String testFilenameSuffix = "test_random_words" + System.currentTimeMillis();
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix;
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
final int numberOfWords = 1000;
final Random random = new Random(123456);
@ -151,7 +153,6 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
final File dictFile = new File(getContext().getFilesDir(), fileName);
if (dictFile != null) {
assertTrue(dictFile.exists());
assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
dictFile.delete();
}
}
@ -170,7 +171,8 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
// Create filename suffixes for this test.
for (int i = 0; i < numberOfLanguages; i++) {
testFilenameSuffixes[i] = "test_switching_languages" + i;
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffixes[i];
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffixes[i]
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
dictFiles[i] = new File(getContext().getFilesDir(), fileName);
clearHistory(testFilenameSuffixes[i]);
}
@ -196,7 +198,6 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
for (final File file : dictFiles) {
if (file != null) {
assertTrue(file.exists());
assertTrue(file.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
file.delete();
}
}
@ -214,11 +215,11 @@ public class UserHistoryDictionaryTests extends AndroidTestCase {
} finally {
Log.d(TAG, "waiting for writing ...");
waitForWriting(testFilenameSuffix);
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix;
final String fileName = UserHistoryDictionary.NAME + "." + testFilenameSuffix
+ ExpandableBinaryDictionary.DICT_FILE_EXTENSION;
final File dictFile = new File(getContext().getFilesDir(), fileName);
if (dictFile != null) {
assertTrue(dictFile.exists());
assertTrue(dictFile.length() >= MIN_USER_HISTORY_DICTIONARY_FILE_SIZE);
dictFile.delete();
}
}