librime: user_db.h Source File

| librime 1.2 Rime Input Method Engine, the core library

user_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_USER_DB_H_
8#define RIME_USER_DB_H_
9
10#include <stdint.h>
11#include <rime/component.h>
12#include <rime/dict/db.h>
13#include <rime/dict/db_utils.h>
14
15namespace rime {
16
17 using TickCount = uint64_t;
18
20 struct UserDbValue {
21 int commits = 0;
22 double dee = 0.0;
23 TickCount tick = 0;
24
25 UserDbValue() = default;
26 UserDbValue(const string& value);
27
28 string Pack() const;
29 bool Unpack(const string& value);
30};
31
39 class UserDb {
40 public:
41 static string snapshot_extension();
42
44 class Component : public Db::Component {
45 public:
46 virtual string extension() const = 0;
47 };
48
50 static Component* Require(const string& name) {
51 return dynamic_cast<Component*>(Db::Require(name));
52 }
53
54 UserDb() = delete;
55};
56
58 class UserDbHelper {
59 public:
60 UserDbHelper(Db* db) : db_(db) {}
61 UserDbHelper(const the<Db>& db) : db_(db.get()) {}
62 UserDbHelper(const an<Db>& db) : db_(db.get()) {}
63
64 RIME_DLL bool UpdateUserInfo();
65 RIME_DLL static bool IsUniformFormat(const path& file_path);
66 RIME_DLL bool UniformBackup(const path& snapshot_file);
67 RIME_DLL bool UniformRestore(const path& snapshot_file);
68
69 bool IsUserDb();
70 string GetDbName();
71 string GetUserId();
72 string GetRimeVersion();
73
74 protected:
75 Db* db_;
76};
77
79template <class BaseDb>
80 class UserDbWrapper : public BaseDb {
81 public:
82 RIME_DLL UserDbWrapper(const path& file_path, const string& db_name);
83
84 virtual bool CreateMetadata() {
85 return BaseDb::CreateMetadata() && UserDbHelper(this).UpdateUserInfo();
86 }
87 virtual bool Backup(const path& snapshot_file) {
88 return UserDbHelper::IsUniformFormat(snapshot_file)
89 ? UserDbHelper(this).UniformBackup(snapshot_file)
90 : BaseDb::Backup(snapshot_file);
91 }
92 virtual bool Restore(const path& snapshot_file) {
93 return UserDbHelper::IsUniformFormat(snapshot_file)
94 ? UserDbHelper(this).UniformRestore(snapshot_file)
95 : BaseDb::Restore(snapshot_file);
96 }
97};
98
100template <class BaseDb>
101 class UserDbComponent : public UserDb::Component, protected DbComponentBase {
102 public:
103 using UserDbImpl = UserDbWrapper<BaseDb>;
104 Db* Create(const string& name) override {
105 return new UserDbImpl(DbFilePath(name, extension()), name);
106 }
107
108 string extension() const override;
109};
110
111 class UserDbMerger : public Sink {
112 public:
113 explicit UserDbMerger(Db* db);
114 virtual ~UserDbMerger();
115
116 virtual bool MetaPut(const string& key, const string& value);
117 virtual bool Put(const string& key, const string& value);
118
119 void CloseMerge();
120
121 protected:
122 Db* db_;
123 TickCount our_tick_;
124 TickCount their_tick_;
125 TickCount max_tick_;
126 int merged_entries_;
127};
128
129 class UserDbImporter : public Sink {
130 public:
131 explicit UserDbImporter(Db* db);
132
133 virtual bool MetaPut(const string& key, const string& value);
134 virtual bool Put(const string& key, const string& value);
135
136 protected:
137 Db* db_;
138};
139
140} // namespace rime
141
142#endif // RIME_USER_DB_H_
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
component.h
db.h
db_utils.h
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::TickCount
uint64_t TickCount
Definition user_db.h:17
rime::path
Definition common.h:84
rime::Class< Db, const string & >::Require
static Component * Require(const string &name)
Definition component.h:29
rime::Db
Definition db.h:33
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::Sink
Definition db_utils.h:14
rime::UserDbValue::Unpack
bool Unpack(const string &value)
Definition user_db.cc:28
rime::UserDbValue::Pack
string Pack() const
Definition user_db.cc:22
rime::UserDbValue::dee
double dee
Definition user_db.h:22
rime::UserDbValue::commits
int commits
Definition user_db.h:21
rime::UserDbValue::UserDbValue
UserDbValue()=default
rime::UserDbValue::tick
TickCount tick
Definition user_db.h:23
rime::UserDb::snapshot_extension
static string snapshot_extension()
Definition user_db.cc:61
rime::UserDb::Require
static Component * Require(const string &name)
Requires a registered component for a user db class.
Definition user_db.h:50
rime::UserDb::UserDb
UserDb()=delete
rime::UserDb::Component
Abstract class for a user db component.
Definition user_db.h:44
rime::UserDb::Component::extension
virtual string extension() const =0
rime::UserDbHelper::UserDbHelper
UserDbHelper(const the< Db > &db)
Definition user_db.h:61
rime::UserDbHelper::GetDbName
string GetDbName()
Definition user_db.cc:148
rime::UserDbHelper::UserDbHelper
UserDbHelper(Db *db)
Definition user_db.h:60
rime::UserDbHelper::UniformBackup
RIME_DLL bool UniformBackup(const path &snapshot_file)
Definition user_db.cc:115
rime::UserDbHelper::db_
Db * db_
Definition user_db.h:75
rime::UserDbHelper::UpdateUserInfo
RIME_DLL bool UpdateUserInfo()
Definition user_db.cc:105
rime::UserDbHelper::IsUniformFormat
static RIME_DLL bool IsUniformFormat(const path &file_path)
Definition user_db.cc:110
rime::UserDbHelper::IsUserDb
bool IsUserDb()
Definition user_db.cc:143
rime::UserDbHelper::GetRimeVersion
string GetRimeVersion()
Definition user_db.cc:166
rime::UserDbHelper::UniformRestore
RIME_DLL bool UniformRestore(const path &snapshot_file)
Definition user_db.cc:129
rime::UserDbHelper::GetUserId
string GetUserId()
Definition user_db.cc:160
rime::UserDbHelper::UserDbHelper
UserDbHelper(const an< Db > &db)
Definition user_db.h:62
rime::UserDbWrapper
A template to define a user db class based on an implementation of rime::Db.
Definition user_db.h:80
rime::UserDbWrapper::CreateMetadata
virtual bool CreateMetadata()
Definition user_db.h:84
rime::UserDbWrapper::Restore
virtual bool Restore(const path &snapshot_file)
Definition user_db.h:92
rime::UserDbWrapper::Backup
virtual bool Backup(const path &snapshot_file)
Definition user_db.h:87
rime::UserDbWrapper::UserDbWrapper
RIME_DLL UserDbWrapper(const path &file_path, const string &db_name)
rime::UserDbComponent
Implements a component that serves as a factory for a user db class.
Definition user_db.h:101
rime::UserDbComponent::UserDbImpl
UserDbWrapper< BaseDb > UserDbImpl
Definition user_db.h:103
rime::UserDbComponent::Create
Db * Create(const string &name) override
Definition user_db.h:104
rime::UserDbComponent::extension
string extension() const override
rime::UserDbMerger::CloseMerge
void CloseMerge()
Definition user_db.cc:226
rime::UserDbMerger::merged_entries_
int merged_entries_
Definition user_db.h:126
rime::UserDbMerger::our_tick_
TickCount our_tick_
Definition user_db.h:123
rime::UserDbMerger::db_
Db * db_
Definition user_db.h:122
rime::UserDbMerger::max_tick_
TickCount max_tick_
Definition user_db.h:125
rime::UserDbMerger::Put
virtual bool Put(const string &key, const string &value)
Definition user_db.cc:204
rime::UserDbMerger::their_tick_
TickCount their_tick_
Definition user_db.h:124
rime::UserDbMerger::UserDbMerger
UserDbMerger(Db *db)
Definition user_db.cc:183
rime::UserDbMerger::MetaPut
virtual bool MetaPut(const string &key, const string &value)
Definition user_db.cc:193
rime::UserDbImporter::Put
virtual bool Put(const string &key, const string &value)
Definition user_db.cc:248
rime::UserDbImporter::UserDbImporter
UserDbImporter(Db *db)
Definition user_db.cc:242
rime::UserDbImporter::db_
Db * db_
Definition user_db.h:137
rime::UserDbImporter::MetaPut
virtual bool MetaPut(const string &key, const string &value)
Definition user_db.cc:244