Skip to content

Commit

Permalink
fix(user_db): unwanted implicit instantiation of UserDbFormat template
Browse files Browse the repository at this point in the history
Fixes #188: UserDbFormat<BaseDb>::extension not working
  • Loading branch information
lotem authored and Prcuvu committed Mar 5, 2018
1 parent 9e1114e commit 3cbc9cb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
8 changes: 6 additions & 2 deletions src/rime/dict/level_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,14 @@ bool LevelDb::CommitTransaction() {
}

template <>
const string UserDbFormat<LevelDb>::extension(".userdb");
string UserDbComponent<LevelDb>::extension() const {
return ".userdb";
}

template <>
const string UserDbFormat<LevelDb>::snapshot_extension(".userdb.txt");
string UserDbComponent<LevelDb>::snapshot_extension() const {
return ".userdb.txt";
}

template <>
UserDbWrapper<LevelDb>::UserDbWrapper(const string& db_name)
Expand Down
13 changes: 9 additions & 4 deletions src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ bool UserDbValue::Unpack(const string& value) {
return true;
}

static const string plain_userdb_extension(".userdb.txt");

template <>
const string UserDbFormat<TextDb>::extension(".userdb.txt");
string UserDbComponent<TextDb>::extension() const {
return plain_userdb_extension;
}

template <>
const string UserDbFormat<TextDb>::snapshot_extension(".userdb.txt");
string UserDbComponent<TextDb>::snapshot_extension() const {
return plain_userdb_extension;
}

// key ::= code <space> <Tab> phrase

Expand Down Expand Up @@ -110,8 +116,7 @@ bool UserDbHelper::UpdateUserInfo() {
}

bool UserDbHelper::IsUniformFormat(const string& file_name) {
return boost::ends_with(file_name,
UserDbFormat<TextDb>::snapshot_extension);
return boost::ends_with(file_name, plain_userdb_extension);
}

bool UserDbHelper::UniformBackup(const string& snapshot_file) {
Expand Down
17 changes: 3 additions & 14 deletions src/rime/dict/user_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,16 @@ class UserDbWrapper : public BaseDb {
}
};

/// Provides information of the db file format by its base class.
template <class BaseDb>
struct UserDbFormat {
static const string extension;
static const string snapshot_extension;
};

/// Implements a component that serves as a factory for a user db class.
template <class BaseDb>
class UserDbComponent : public UserDb::Component {
public:
virtual Db* Create(const string& name) {
Db* Create(const string& name) override {
return new UserDbWrapper<BaseDb>(name + extension());
}

virtual string extension() const {
return UserDbFormat<BaseDb>::extension;
}
virtual string snapshot_extension() const {
return UserDbFormat<BaseDb>::snapshot_extension;
}
string extension() const override;
string snapshot_extension() const override;
};

class UserDbMerger : public Sink {
Expand Down
9 changes: 3 additions & 6 deletions src/rime/lever/user_dict_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ bool UserDictManager::Backup(const string& dict_name) {
return false;
}
}
string snapshot_file =
dict_name + UserDbFormat<TextDb>::snapshot_extension;
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
return db->Backup((dir / snapshot_file).string());
}

Expand Down Expand Up @@ -178,8 +177,7 @@ bool UserDictManager::UpgradeUserDict(const string& dict_name) {
return false;
}
}
string snapshot_file =
dict_name + UserDbFormat<TextDb>::snapshot_extension;
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
fs::path snapshot_path = trash / snapshot_file;
return legacy_db->Backup(snapshot_path.string()) &&
legacy_db->Close() &&
Expand All @@ -199,8 +197,7 @@ bool UserDictManager::Synchronize(const string& dict_name) {
}
}
// *.userdb.txt
string snapshot_file =
dict_name + UserDbFormat<TextDb>::snapshot_extension;
string snapshot_file = dict_name + user_db_component_->snapshot_extension();
for (fs::directory_iterator it(sync_dir), end; it != end; ++it) {
if (!fs::is_directory(it->path()))
continue;
Expand Down

0 comments on commit 3cbc9cb

Please sign in to comment.