1 //
2 // Copyright RIME Developers
3 // Distributed under the BSD License
4 //
5 // 2011-11-02 GONG Chen <chen.sst@gmail.com>
6 //
7 #include <boost/algorithm/string.hpp>
8 #include <boost/filesystem.hpp>
9 #include <rime/common.h>
10 #include <rime/resource.h>
11 #include <rime/service.h>
12 #include <rime/dict/db.h>
13
14 namespace rime {
15
16 // DbAccessor members
17
MatchesPrefix(const string & key)18 bool DbAccessor::MatchesPrefix(const string& key) {
19 return boost::starts_with(key, prefix_);
20 }
21
22 // DbComponentBase
23
24 static const ResourceType kDbResourceType = {
25 "db", "", ""
26 };
27
DbComponentBase()28 DbComponentBase::DbComponentBase()
29 : db_resource_resolver_(
30 Service::instance().CreateResourceResolver(kDbResourceType)) {}
31
~DbComponentBase()32 DbComponentBase::~DbComponentBase() {}
33
DbFilePath(const string & name,const string & extension) const34 string DbComponentBase::DbFilePath(const string& name,
35 const string& extension) const {
36 return db_resource_resolver_->ResolvePath(name + extension).string();
37 }
38
39 // Db members
40
Db(const string & file_name,const string & name)41 Db::Db(const string& file_name, const string& name)
42 : name_(name),
43 file_name_(file_name) {}
44
Exists() const45 bool Db::Exists() const {
46 return boost::filesystem::exists(file_name());
47 }
48
Remove()49 bool Db::Remove() {
50 if (loaded()) {
51 LOG(ERROR) << "attempt to remove opened db '" << name_ << "'.";
52 return false;
53 }
54 return boost::filesystem::remove(file_name());
55 }
56
CreateMetadata()57 bool Db::CreateMetadata() {
58 LOG(INFO) << "creating metadata for db '" << name_ << "'.";
59 return MetaUpdate("/db_name", name_) &&
60 MetaUpdate("/rime_version", RIME_VERSION);
61 }
62
63 } // namespace rime
64