librime: db.h Source File

| librime 1.2 Rime Input Method Engine, the core library

db.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2013-04-16 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_DB_H_
8#define RIME_DB_H_
9
10#include <rime_api.h>
11#include <rime/common.h>
12#include <rime/component.h>
13
14namespace rime {
15
16 class DbAccessor {
17 public:
18 DbAccessor() = default;
19 explicit DbAccessor(const string& prefix) : prefix_(prefix) {}
20 virtual ~DbAccessor() = default;
21
22 virtual bool Reset() = 0;
23 virtual bool Jump(const string& key) = 0;
24 virtual bool GetNextRecord(string* key, string* value) = 0;
25 virtual bool exhausted() = 0;
26
27 protected:
28 bool MatchesPrefix(const string& key);
29
30 string prefix_;
31};
32
33 class Db : public Class<Db, const string&> {
34 public:
35 Db(const path& file_path, const string& name);
36 virtual ~Db() = default;
37
38 RIME_DLL bool Exists() const;
39 RIME_DLL virtual bool Remove();
40
41 virtual bool Open() = 0;
42 virtual bool OpenReadOnly() = 0;
43 virtual bool Close() = 0;
44
45 virtual bool Backup(const path& snapshot_file) = 0;
46 virtual bool Restore(const path& snapshot_file) = 0;
47
48 virtual bool CreateMetadata();
49 virtual bool MetaFetch(const string& key, string* value) = 0;
50 virtual bool MetaUpdate(const string& key, const string& value) = 0;
51
52 virtual an<DbAccessor> QueryMetadata() = 0;
53 virtual an<DbAccessor> QueryAll() = 0;
54 virtual an<DbAccessor> Query(const string& key) = 0;
55 virtual bool Fetch(const string& key, string* value) = 0;
56 virtual bool Update(const string& key, const string& value) = 0;
57 virtual bool Erase(const string& key) = 0;
58
59 const string& name() const { return name_; }
60 const path& file_path() const { return file_path_; }
61 bool loaded() const { return loaded_; }
62 bool readonly() const { return readonly_; }
63 bool disabled() const { return disabled_; }
64 void disable() { disabled_ = true; }
65 void enable() { disabled_ = false; }
66
67 protected:
68 string name_;
69 path file_path_;
70 bool loaded_ = false;
71 bool readonly_ = false;
72 bool disabled_ = false;
73};
74
75 class Transactional {
76 public:
77 Transactional() = default;
78 virtual ~Transactional() = default;
79 virtual bool BeginTransaction() { return false; }
80 virtual bool AbortTransaction() { return false; }
81 virtual bool CommitTransaction() { return false; }
82 bool in_transaction() const { return in_transaction_; }
83
84 protected:
85 bool in_transaction_ = false;
86};
87
88 class Recoverable {
89 public:
90 virtual ~Recoverable() = default;
91 virtual bool Recover() = 0;
92};
93
94class ResourceResolver;
95
96 class RIME_DLL DbComponentBase {
97 public:
98 DbComponentBase();
99 virtual ~DbComponentBase();
100
101 path DbFilePath(const string& name, const string& extension) const;
102
103 protected:
104 the<ResourceResolver> db_resource_resolver_;
105};
106
107template <class DbClass>
108 class DbComponent : public DbClass::Component, protected DbComponentBase {
109 public:
110 virtual string extension() const;
111
112 DbClass* Create(const string& name) override {
113 return new DbClass(DbFilePath(name, extension()), name);
114 }
115};
116
117} // namespace rime
118
119#endif // RIME_DB_H_
common.h
component.h
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::path
Definition common.h:84
rime::Class
Definition component.h:21
rime::DbAccessor::MatchesPrefix
bool MatchesPrefix(const string &key)
Definition db.cc:18
rime::DbAccessor::exhausted
virtual bool exhausted()=0
rime::DbAccessor::Reset
virtual bool Reset()=0
rime::DbAccessor::~DbAccessor
virtual ~DbAccessor()=default
rime::DbAccessor::prefix_
string prefix_
Definition db.h:30
rime::DbAccessor::Jump
virtual bool Jump(const string &key)=0
rime::DbAccessor::DbAccessor
DbAccessor(const string &prefix)
Definition db.h:19
rime::DbAccessor::GetNextRecord
virtual bool GetNextRecord(string *key, string *value)=0
rime::DbAccessor::DbAccessor
DbAccessor()=default
rime::Db::Open
virtual bool Open()=0
rime::Db::file_path_
path file_path_
Definition db.h:69
rime::Db::MetaUpdate
virtual bool MetaUpdate(const string &key, const string &value)=0
rime::Db::name_
string name_
Definition db.h:68
rime::Db::name
const string & name() const
Definition db.h:59
rime::Db::loaded
bool loaded() const
Definition db.h:61
rime::Db::readonly_
bool readonly_
Definition db.h:71
rime::Db::Fetch
virtual bool Fetch(const string &key, string *value)=0
rime::Db::Query
virtual an< DbAccessor > Query(const string &key)=0
rime::Db::Restore
virtual bool Restore(const path &snapshot_file)=0
rime::Db::Backup
virtual bool Backup(const path &snapshot_file)=0
rime::Db::enable
void enable()
Definition db.h:65
rime::Db::Db
Db(const path &file_path, const string &name)
Definition db.cc:39
rime::Db::QueryMetadata
virtual an< DbAccessor > QueryMetadata()=0
rime::Db::Remove
virtual RIME_DLL bool Remove()
Definition db.cc:46
rime::Db::Update
virtual bool Update(const string &key, const string &value)=0
rime::Db::Erase
virtual bool Erase(const string &key)=0
rime::Db::CreateMetadata
virtual bool CreateMetadata()
Definition db.cc:54
rime::Db::~Db
virtual ~Db()=default
rime::Db::Exists
RIME_DLL bool Exists() const
Definition db.cc:42
rime::Db::loaded_
bool loaded_
Definition db.h:70
rime::Db::MetaFetch
virtual bool MetaFetch(const string &key, string *value)=0
rime::Db::disabled
bool disabled() const
Definition db.h:63
rime::Db::Close
virtual bool Close()=0
rime::Db::disable
void disable()
Definition db.h:64
rime::Db::disabled_
bool disabled_
Definition db.h:72
rime::Db::file_path
const path & file_path() const
Definition db.h:60
rime::Db::QueryAll
virtual an< DbAccessor > QueryAll()=0
rime::Db::OpenReadOnly
virtual bool OpenReadOnly()=0
rime::Db::readonly
bool readonly() const
Definition db.h:62
rime::Transactional::BeginTransaction
virtual bool BeginTransaction()
Definition db.h:79
rime::Transactional::Transactional
Transactional()=default
rime::Transactional::CommitTransaction
virtual bool CommitTransaction()
Definition db.h:81
rime::Transactional::AbortTransaction
virtual bool AbortTransaction()
Definition db.h:80
rime::Transactional::in_transaction
bool in_transaction() const
Definition db.h:82
rime::Transactional::~Transactional
virtual ~Transactional()=default
rime::Transactional::in_transaction_
bool in_transaction_
Definition db.h:85
rime::Recoverable
Definition db.h:88
rime::Recoverable::Recover
virtual bool Recover()=0
rime::Recoverable::~Recoverable
virtual ~Recoverable()=default
rime::DbComponentBase::DbFilePath
path DbFilePath(const string &name, const string &extension) const
Definition db.cc:32
rime::DbComponentBase::DbComponentBase
DbComponentBase()
Definition db.cc:26
rime::DbComponentBase::db_resource_resolver_
the< ResourceResolver > db_resource_resolver_
Definition db.h:104
rime::DbComponent
Definition db.h:108
rime::DbComponent::extension
virtual string extension() const
rime::DbComponent::Create
DbClass * Create(const string &name) override
Definition db.h:112
rime::ResourceResolver
Definition resource.h:22