Skip to content

Commit f21af46

Browse files
committed
refactor(db): let DbComponent handle file path resolution
1 parent d84eccf commit f21af46

14 files changed

Lines changed: 89 additions & 43 deletions

src/rime/component.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ template <class T, class Arg>
2121
struct Class {
2222
using Initializer = Arg;
2323

24-
class Component : public ComponentBase {
24+
class Component : virtual public ComponentBase {
2525
public:
2626
virtual T* Create(Initializer arg) = 0;
2727
};

src/rime/dict/db.cc

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@ bool DbAccessor::MatchesPrefix(const string& key) {
1919
return boost::starts_with(key, prefix_);
2020
}
2121

22-
// Db members
22+
// DbComponentBase
2323

2424
static const ResourceType kDbResourceType = {
2525
"db", "", ""
2626
};
2727

28-
Db::Db(const string& name) : name_(name) {
29-
// TODO: avoid global object; move the resousrce resolver to db components.
30-
static the<ResourceResolver> db_resource_resolver(
31-
Service::instance().CreateResourceResolver(kDbResourceType));
32-
file_name_ = db_resource_resolver->ResolvePath(name).string();
28+
DbComponentBase::DbComponentBase()
29+
: db_resource_resolver_(
30+
Service::instance().CreateResourceResolver(kDbResourceType)) {}
31+
32+
DbComponentBase::~DbComponentBase() {}
33+
34+
string DbComponentBase::DbFilePath(const string& name,
35+
const string& extension) const {
36+
return db_resource_resolver_->ResolvePath(name + extension).string();
3337
}
3438

39+
// Db members
40+
41+
Db::Db(const string& file_name, const string& name)
42+
: name_(name),
43+
file_name_(file_name) {}
44+
3545
bool Db::Exists() const {
3646
return boost::filesystem::exists(file_name());
3747
}

src/rime/dict/db.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DbAccessor {
3333

3434
class Db : public Class<Db, const string&> {
3535
public:
36-
explicit Db(const string& name);
36+
Db(const string& file_name, const string& name);
3737
virtual ~Db() = default;
3838

3939
RIME_API bool Exists() const;
@@ -91,6 +91,30 @@ class Recoverable {
9191
virtual bool Recover() = 0;
9292
};
9393

94+
class ResourceResolver;
95+
96+
class DbComponentBase : virtual public ComponentBase {
97+
public:
98+
DbComponentBase();
99+
virtual ~DbComponentBase();
100+
101+
string DbFilePath(const string& name, const string& extension) const;
102+
103+
protected:
104+
the<ResourceResolver> db_resource_resolver_;
105+
};
106+
107+
template <class DbClass>
108+
class DbComponent : public DbClass::Component,
109+
protected DbComponentBase {
110+
public:
111+
virtual string extension() const { return string(); }
112+
113+
DbClass* Create(const string& name) override {
114+
return new DbClass(DbFilePath(name, extension()), name);
115+
}
116+
};
117+
94118
} // namespace rime
95119

96120
#endif // RIME_DB_H_

src/rime/dict/dict_module.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <rime_api.h>
99
#include <rime/common.h>
1010
#include <rime/registry.h>
11-
11+
#include <rime/dict/db.h>
1212
#include <rime/dict/level_db.h>
1313
#include <rime/dict/table_db.h>
1414
#include <rime/dict/text_db.h>
@@ -25,8 +25,8 @@ static void rime_dict_initialize() {
2525
LOG(INFO) << "registering components from module 'dict'.";
2626
Registry& r = Registry::instance();
2727

28-
r.Register("tabledb", new Component<TableDb>);
29-
r.Register("stabledb", new Component<StableDb>);
28+
r.Register("tabledb", new DbComponent<TableDb>);
29+
r.Register("stabledb", new DbComponent<StableDb>);
3030
r.Register("plain_userdb", new UserDbComponent<TextDb>);
3131
r.Register("userdb", new UserDbComponent<LevelDb>);
3232
// NOTE: register a legacy_userdb component in your plugin if you wish to

src/rime/dict/level_db.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ bool LevelDbAccessor::exhausted() {
154154

155155
// LevelDb members
156156

157-
LevelDb::LevelDb(const string& name, const string& db_type)
158-
: Db(name), db_type_(db_type) {
157+
LevelDb::LevelDb(const string& file_name,
158+
const string& db_name,
159+
const string& db_type)
160+
: Db(file_name, db_name),
161+
db_type_(db_type) {
159162
}
160163

161164
LevelDb::~LevelDb() {
@@ -242,12 +245,12 @@ bool LevelDb::Recover() {
242245

243246
bool LevelDb::Remove() {
244247
if (loaded()) {
245-
LOG(ERROR) << "attempt to remove opened db '" << name_ << "'.";
248+
LOG(ERROR) << "attempt to remove opened db '" << name() << "'.";
246249
return false;
247250
}
248251
auto status = leveldb::DestroyDB(file_name(), leveldb::Options());
249252
if (!status.ok()) {
250-
LOG(ERROR) << "Error removing db '" << name_ << "': " << status.ToString();
253+
LOG(ERROR) << "Error removing db '" << name() << "': " << status.ToString();
251254
return false;
252255
}
253256
return true;
@@ -271,7 +274,7 @@ bool LevelDb::Open() {
271274
}
272275
}
273276
else {
274-
LOG(ERROR) << "Error opening db '" << name_ << "': " << status.ToString();
277+
LOG(ERROR) << "Error opening db '" << name() << "': " << status.ToString();
275278
}
276279
return loaded_;
277280
}
@@ -285,7 +288,7 @@ bool LevelDb::OpenReadOnly() {
285288
loaded_ = status.ok();
286289

287290
if (!loaded_) {
288-
LOG(ERROR) << "Error opening db '" << name_ << "' read-only.";
291+
LOG(ERROR) << "Error opening db '" << name() << "' read-only.";
289292
}
290293
return loaded_;
291294
}
@@ -296,7 +299,7 @@ bool LevelDb::Close() {
296299

297300
db_->Release();
298301

299-
LOG(INFO) << "closed db '" << name_ << "'.";
302+
LOG(INFO) << "closed db '" << name() << "'.";
300303
loaded_ = false;
301304
readonly_ = false;
302305
in_transaction_ = false;
@@ -352,8 +355,9 @@ string UserDbComponent<LevelDb>::snapshot_extension() const {
352355
}
353356

354357
template <>
355-
UserDbWrapper<LevelDb>::UserDbWrapper(const string& db_name)
356-
: LevelDb(db_name, "userdb") {
358+
UserDbWrapper<LevelDb>::UserDbWrapper(const string& file_name,
359+
const string& db_name)
360+
: LevelDb(file_name, db_name, "userdb") {
357361
}
358362

359363
} // namespace rime

src/rime/dict/level_db.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class LevelDb : public Db,
3737
public Recoverable,
3838
public Transactional {
3939
public:
40-
LevelDb(const string& name, const string& db_type = "");
40+
LevelDb(const string& file_name,
41+
const string& db_name,
42+
const string& db_type = "");
4143
virtual ~LevelDb();
4244

4345
virtual bool Remove();

src/rime/dict/preset_vocabulary.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ static const ResourceType kVocabularyResourceType = {
1919
};
2020

2121
struct VocabularyDb : public TextDb {
22-
explicit VocabularyDb(const string& path);
22+
VocabularyDb(const string& path, const string& name);
2323
an<DbAccessor> cursor;
2424
static const TextFormat format;
2525
};
2626

27-
VocabularyDb::VocabularyDb(const string& path)
28-
: TextDb(path, kVocabularyResourceType.name, VocabularyDb::format) {
27+
VocabularyDb::VocabularyDb(const string& path, const string& name)
28+
: TextDb(path, name, kVocabularyResourceType.name, VocabularyDb::format) {
2929
}
3030

3131
static bool rime_vocabulary_entry_parser(const Tsv& row,
@@ -61,7 +61,7 @@ string PresetVocabulary::DictFilePath(const string& vocabulary) {
6161
}
6262

6363
PresetVocabulary::PresetVocabulary(const string& vocabulary) {
64-
db_.reset(new VocabularyDb(DictFilePath(vocabulary)));
64+
db_.reset(new VocabularyDb(DictFilePath(vocabulary), vocabulary));
6565
if (db_ && db_->OpenReadOnly()) {
6666
db_->cursor = db_->QueryAll();
6767
}

src/rime/dict/table_db.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ const TextFormat TableDb::format = {
6565
"Rime table",
6666
};
6767

68-
TableDb::TableDb(const string& name)
69-
: TextDb(name + ".txt", "tabledb", TableDb::format) {
68+
TableDb::TableDb(const string& file_name, const string& db_name)
69+
: TextDb(file_name, db_name, "tabledb", TableDb::format) {
7070
}
7171

7272
// StableDb
7373

74-
StableDb::StableDb(const string& name)
75-
: TableDb(name) {}
74+
StableDb::StableDb(const string& file_name, const string& db_name)
75+
: TableDb(file_name, db_name) {}
7676

7777
bool StableDb::Open() {
7878
if (loaded()) return false;

src/rime/dict/table_db.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace rime {
1313

1414
class TableDb : public TextDb {
1515
public:
16-
explicit TableDb(const string& name);
16+
TableDb(const string& file_name, const string& db_name);
1717

1818
static const TextFormat format;
1919
};
2020

2121
// read-only tabledb
2222
class StableDb : public TableDb {
2323
public:
24-
explicit StableDb(const string& name);
24+
StableDb(const string& file_name, const string& db_name);
2525

2626
virtual bool Open();
2727
};

src/rime/dict/text_db.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@ bool TextDbAccessor::exhausted() {
4545

4646
// TextDb members
4747

48-
TextDb::TextDb(const string& name,
48+
TextDb::TextDb(const string& file_name,
49+
const string& db_name,
4950
const string& db_type,
5051
TextFormat format)
51-
: Db(name), db_type_(db_type), format_(format) {
52+
: Db(file_name, db_name), db_type_(db_type), format_(format) {
5253
}
5354

5455
TextDb::~TextDb() {
@@ -117,7 +118,7 @@ bool TextDb::Open() {
117118
}
118119
}
119120
else {
120-
LOG(ERROR) << "Error opening db '" << name_ << "'.";
121+
LOG(ERROR) << "Error opening db '" << name() << "'.";
121122
}
122123
modified_ = false;
123124
return loaded_;

0 commit comments

Comments
 (0)