Skip to content

Commit

Permalink
feat(dict): use resource resolver to find dictionary files
Browse files Browse the repository at this point in the history
  • Loading branch information
kionz committed Oct 19, 2017
1 parent fdf8172 commit 8ea08b3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 36 deletions.
17 changes: 10 additions & 7 deletions src/rime/dict/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <rime/common.h>
#include <rime/resource.h>
#include <rime/service.h>
#include <rime/dict/db.h>

Expand All @@ -20,15 +21,17 @@ bool DbAccessor::MatchesPrefix(const string& key) {

// Db members

static const ResourceType kDbResourceType = {
"db", "", ""
};

Db::Db(const string& name) : name_(name) {
boost::filesystem::path path(name);
if (path.has_parent_path()) {
file_name_ = name;
}
else {
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
file_name_ = (dir / path).string();
static ResourceResolver db_resource_resolver(kDbResourceType);
if (db_resource_resolver.root_path().empty()) {
db_resource_resolver.set_root_path(
Service::instance().deployer().user_data_dir);
}
file_name_ = db_resource_resolver.ResolvePath(name).string();
}

bool Db::Exists() const {
Expand Down
26 changes: 21 additions & 5 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <utility>
#include <boost/filesystem.hpp>
#include <rime/common.h>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
Expand Down Expand Up @@ -279,7 +280,22 @@ bool Dictionary::loaded() const {

// DictionaryComponent members

DictionaryComponent::DictionaryComponent() {
static const ResourceType kPrismResourceType = {
"prism", "", ".prism.bin"
};

static const ResourceType kTableResourceType = {
"table", "", ".table.bin"
};

DictionaryComponent::DictionaryComponent()
: prism_resource_resolver_(
Service::instance().CreateResourceResolver(kPrismResourceType)),
table_resource_resolver_(
Service::instance().CreateResourceResolver(kTableResourceType)) {
}

DictionaryComponent::~DictionaryComponent() {
}

Dictionary* DictionaryComponent::Create(const Ticket& ticket) {
Expand Down Expand Up @@ -308,13 +324,13 @@ DictionaryComponent::CreateDictionaryWithName(const string& dict_name,
boost::filesystem::path path(Service::instance().deployer().user_data_dir);
auto table = table_map_[dict_name].lock();
if (!table) {
table = New<Table>((path / dict_name).string() + ".table.bin");
table_map_[dict_name] = table;
auto file_path = table_resource_resolver_->ResolvePath(dict_name).string();
table_map_[dict_name] = table = New<Table>(file_path);
}
auto prism = prism_map_[prism_name].lock();
if (!prism) {
prism = New<Prism>((path / prism_name).string() + ".prism.bin");
prism_map_[prism_name] = prism;
auto file_path = prism_resource_resolver_->ResolvePath(prism_name).string();
prism_map_[prism_name] = prism = New<Prism>(file_path);
}
return new Dictionary(dict_name, table, prism);
}
Expand Down
5 changes: 5 additions & 0 deletions src/rime/dict/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,21 @@ class Dictionary : public Class<Dictionary, const Ticket&> {
an<Prism> prism_;
};

class ResourceResolver;

class DictionaryComponent : public Dictionary::Component {
public:
DictionaryComponent();
~DictionaryComponent();
Dictionary* Create(const Ticket& ticket);
Dictionary* CreateDictionaryWithName(const string& dict_name,
const string& prism_name);

private:
map<string, weak<Prism>> prism_map_;
map<string, weak<Table>> table_map_;
the<ResourceResolver> prism_resource_resolver_;
the<ResourceResolver> table_resource_resolver_;
};

} // namespace rime
Expand Down
20 changes: 11 additions & 9 deletions src/rime/dict/preset_vocabulary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,27 @@
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <utf8.h>
#include <rime/resource.h>
#include <rime/service.h>
#include <rime/dict/preset_vocabulary.h>
#include <rime/dict/text_db.h>

namespace rime {

static const ResourceType kVocabularyResourceType = {
"vocabulary", "", ".txt"
};

static const string kDefaultVocabulary = "essay";

struct VocabularyDb : public TextDb {
explicit VocabularyDb(const string& path);
an<DbAccessor> cursor;
static const TextFormat format;
};

VocabularyDb::VocabularyDb(const string& path)
: TextDb(path, "vocabulary", VocabularyDb::format) {
: TextDb(path, kVocabularyResourceType.name, VocabularyDb::format) {
}

static bool rime_vocabulary_entry_parser(const Tsv& row,
Expand Down Expand Up @@ -50,14 +57,9 @@ const TextFormat VocabularyDb::format = {
};

string PresetVocabulary::DictFilePath() {
auto& deployer(Service::instance().deployer());
boost::filesystem::path path(deployer.user_data_dir);
path /= "essay.txt";
if (!boost::filesystem::exists(path)) {
path = deployer.shared_data_dir;
path /= "essay.txt";
}
return path.string();
the<ResourceResolver> resource_resolver(
Service::instance().CreateResourceResolver(kVocabularyResourceType));
return resource_resolver->ResolvePath(kDefaultVocabulary).string();
}

PresetVocabulary::PresetVocabulary() {
Expand Down
25 changes: 12 additions & 13 deletions src/rime/dict/reverse_lookup_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <rime/resource.h>
#include <rime/schema.h>
#include <rime/service.h>
#include <rime/ticket.h>
Expand All @@ -24,13 +25,8 @@ const size_t kReverseFormatPrefixLen = sizeof(kReverseFormatPrefix) - 1;

static const char* kStemKeySuffix = "\x1fstem";

static string reverse_db_file_name(const string& dict_name) {
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
return (dir / dict_name).string() + ".reverse.bin";
}

ReverseDb::ReverseDb(const string& dict_name)
: MappedFile(reverse_db_file_name(dict_name)) {
ReverseDb::ReverseDb(const string& file_name)
: MappedFile(file_name) {
}

bool ReverseDb::Load() {
Expand Down Expand Up @@ -200,10 +196,6 @@ ReverseLookupDictionary::ReverseLookupDictionary(an<ReverseDb> db)
: db_(db) {
}

ReverseLookupDictionary::ReverseLookupDictionary(const string& dict_name)
: db_(new ReverseDb(dict_name)) {
}

bool ReverseLookupDictionary::Load() {
return db_ && (db_->IsOpen() || db_->Load());
}
Expand Down Expand Up @@ -233,7 +225,13 @@ an<DictSettings> ReverseLookupDictionary::GetDictSettings() {
return settings;
}

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent() {
static const ResourceType kReverseDbResourceType = {
"reverse_db", "", ".reverse.bin"
};

ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()
: resource_resolver_(
Service::instance().CreateResourceResolver(kReverseDbResourceType)) {
}

ReverseLookupDictionary*
Expand All @@ -248,7 +246,8 @@ ReverseLookupDictionaryComponent::Create(const Ticket& ticket) {
}
auto db = db_pool_[dict_name].lock();
if (!db) {
db = New<ReverseDb>(dict_name);
auto file_path = resource_resolver_->ResolvePath(dict_name).string();
db = New<ReverseDb>(file_path);
db_pool_[dict_name] = db;
}
return new ReverseLookupDictionary(db);
Expand Down
6 changes: 4 additions & 2 deletions src/rime/dict/reverse_lookup_dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DictSettings;

class ReverseDb : public MappedFile {
public:
explicit ReverseDb(const string& dict_name);
explicit ReverseDb(const string& file_name);

bool Load();
bool Lookup(const string& text, string* result);
Expand All @@ -62,7 +62,6 @@ class ReverseLookupDictionary
: public Class<ReverseLookupDictionary, const Ticket&> {
public:
explicit ReverseLookupDictionary(an<ReverseDb> db);
explicit ReverseLookupDictionary(const string& dict_name);
bool Load();
bool ReverseLookup(const string& text, string* result);
bool LookupStems(const string& text, string* result);
Expand All @@ -72,13 +71,16 @@ class ReverseLookupDictionary
an<ReverseDb> db_;
};

class ResourceResolver;

class ReverseLookupDictionaryComponent
: public ReverseLookupDictionary::Component {
public:
ReverseLookupDictionaryComponent();
ReverseLookupDictionary* Create(const Ticket& ticket);
private:
map<string, weak<ReverseDb>> db_pool_;
the<ResourceResolver> resource_resolver_;
};

} // namespace rime
Expand Down

0 comments on commit 8ea08b3

Please sign in to comment.