librime 1.2
Rime Input Method Engine, the core library
text_db.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2013-04-14 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_TEXT_DB_H_
8#define RIME_TEXT_DB_H_
9
10#include <rime/dict/db.h>
11#include <rime/dict/tsv.h>
12
13namespace rime {
14
15class TextDb;
16
17using TextDbData = map<string, string>;
18
19class TextDbAccessor : public DbAccessor {
20 public:
21 TextDbAccessor(const TextDbData& data, const string& prefix);
22 virtual ~TextDbAccessor();
23
24 virtual bool Reset();
25 virtual bool Jump(const string& key);
26 virtual bool GetNextRecord(string* key, string* value);
27 virtual bool exhausted();
28
29 private:
30 const TextDbData& data_;
31 TextDbData::const_iterator iter_;
32};
33
34struct TextFormat {
38};
39
40class TextDb : public Db {
41 public:
42 TextDb(const path& file_path,
43 const string& db_name,
44 const string& db_type,
45 TextFormat format);
46 RIME_DLL virtual ~TextDb();
47
48 RIME_DLL bool Open() override;
49 RIME_DLL bool OpenReadOnly() override;
50 RIME_DLL bool Close() override;
51
52 RIME_DLL bool Backup(const path& snapshot_file) override;
53 RIME_DLL bool Restore(const path& snapshot_file) override;
54
55 RIME_DLL bool CreateMetadata() override;
56 RIME_DLL bool MetaFetch(const string& key, string* value) override;
57 RIME_DLL bool MetaUpdate(const string& key, const string& value) override;
58
61 RIME_DLL an<DbAccessor> Query(const string& key) override;
62 RIME_DLL bool Fetch(const string& key, string* value) override;
63 RIME_DLL bool Update(const string& key, const string& value) override;
64 RIME_DLL bool Erase(const string& key) override;
65
66 protected:
67 void Clear();
68 bool LoadFromFile(const path& file);
69 bool SaveToFile(const path& file);
70
71 string db_type_;
75 bool modified_ = false;
76};
77
78} // namespace rime
79
80#endif // RIME_TEXT_DB_H_
#define RIME_DLL
Definition rime_api.h:33
Definition algebra.cc:12
std::shared_ptr< T > an
Definition common.h:60
map< string, string > TextDbData
Definition text_db.h:17
function< bool(const string &key, const string &value, Tsv *row)> TsvFormatter
Definition tsv.h:18
function< bool(const Tsv &row, string *key, string *value)> TsvParser
Definition tsv.h:16
Definition common.h:84
DbAccessor()=default
Db(const path &file_path, const string &name)
Definition db.cc:39
const path & file_path() const
Definition db.h:60
virtual bool Reset()
Definition text_db.cc:21
virtual bool GetNextRecord(string *key, string *value)
Definition text_db.cc:31
virtual bool Jump(const string &key)
Definition text_db.cc:26
TextDbAccessor(const TextDbData &data, const string &prefix)
Definition text_db.cc:14
virtual bool exhausted()
Definition text_db.cc:40
Definition text_db.h:34
TsvParser parser
Definition text_db.h:35
string file_description
Definition text_db.h:37
TsvFormatter formatter
Definition text_db.h:36
Definition text_db.h:40
RIME_DLL bool Erase(const string &key) override
Definition text_db.cc:92
RIME_DLL bool OpenReadOnly() override
Definition text_db.cc:123
RIME_DLL bool Restore(const path &snapshot_file) override
Definition text_db.cc:168
TextDbData metadata_
Definition text_db.h:73
bool SaveToFile(const path &file)
Definition text_db.cc:218
RIME_DLL bool Open() override
Definition text_db.cc:102
RIME_DLL an< DbAccessor > QueryAll() override
Definition text_db.cc:63
RIME_DLL bool Fetch(const string &key, string *value) override
Definition text_db.cc:73
TextDb(const path &file_path, const string &db_name, const string &db_type, TextFormat format)
Definition text_db.cc:46
RIME_DLL bool MetaUpdate(const string &key, const string &value) override
Definition text_db.cc:194
RIME_DLL bool Close() override
Definition text_db.cc:138
void Clear()
Definition text_db.cc:151
RIME_DLL an< DbAccessor > Query(const string &key) override
Definition text_db.cc:67
RIME_DLL bool CreateMetadata() override
Definition text_db.cc:180
RIME_DLL bool MetaFetch(const string &key, string *value) override
Definition text_db.cc:184
string db_type_
Definition text_db.h:71
bool modified_
Definition text_db.h:75
TextFormat format_
Definition text_db.h:72
RIME_DLL an< DbAccessor > QueryMetadata() override
Definition text_db.cc:57
RIME_DLL bool Update(const string &key, const string &value) override
Definition text_db.cc:83
TextDbData data_
Definition text_db.h:74
RIME_DLL bool Backup(const path &snapshot_file) override
Definition text_db.cc:156
bool LoadFromFile(const path &file)
Definition text_db.cc:203