librime: text_db.h Source File

| 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
17 using TextDbData = map<string, string>;
18
19 class 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
34 struct TextFormat {
35 TsvParser parser;
36 TsvFormatter formatter;
37 string file_description;
38};
39
40 class 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
59 RIME_DLL an<DbAccessor> QueryMetadata() override;
60 RIME_DLL an<DbAccessor> QueryAll() override;
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_;
72 TextFormat format_;
73 TextDbData metadata_;
74 TextDbData data_;
75 bool modified_ = false;
76};
77
78} // namespace rime
79
80#endif // RIME_TEXT_DB_H_
db.h
tsv.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::TextDbData
map< string, string > TextDbData
Definition text_db.h:17
rime::TsvFormatter
function< bool(const string &key, const string &value, Tsv *row)> TsvFormatter
Definition tsv.h:18
rime::TsvParser
function< bool(const Tsv &row, string *key, string *value)> TsvParser
Definition tsv.h:16
rime::path
Definition common.h:84
rime::DbAccessor::DbAccessor
DbAccessor()=default
rime::Db::Db
Db(const path &file_path, const string &name)
Definition db.cc:39
rime::Db::file_path
const path & file_path() const
Definition db.h:60
rime::TextDbAccessor::Reset
virtual bool Reset()
Definition text_db.cc:21
rime::TextDbAccessor::GetNextRecord
virtual bool GetNextRecord(string *key, string *value)
Definition text_db.cc:31
rime::TextDbAccessor::Jump
virtual bool Jump(const string &key)
Definition text_db.cc:26
rime::TextDbAccessor::TextDbAccessor
TextDbAccessor(const TextDbData &data, const string &prefix)
Definition text_db.cc:14
rime::TextDbAccessor::exhausted
virtual bool exhausted()
Definition text_db.cc:40
rime::TextFormat
Definition text_db.h:34
rime::TextFormat::parser
TsvParser parser
Definition text_db.h:35
rime::TextFormat::file_description
string file_description
Definition text_db.h:37
rime::TextFormat::formatter
TsvFormatter formatter
Definition text_db.h:36
rime::TextDb
Definition text_db.h:40
rime::TextDb::Erase
RIME_DLL bool Erase(const string &key) override
Definition text_db.cc:92
rime::TextDb::OpenReadOnly
RIME_DLL bool OpenReadOnly() override
Definition text_db.cc:123
rime::TextDb::Restore
RIME_DLL bool Restore(const path &snapshot_file) override
Definition text_db.cc:168
rime::TextDb::metadata_
TextDbData metadata_
Definition text_db.h:73
rime::TextDb::SaveToFile
bool SaveToFile(const path &file)
Definition text_db.cc:218
rime::TextDb::Open
RIME_DLL bool Open() override
Definition text_db.cc:102
rime::TextDb::QueryAll
RIME_DLL an< DbAccessor > QueryAll() override
Definition text_db.cc:63
rime::TextDb::Fetch
RIME_DLL bool Fetch(const string &key, string *value) override
Definition text_db.cc:73
rime::TextDb::TextDb
TextDb(const path &file_path, const string &db_name, const string &db_type, TextFormat format)
Definition text_db.cc:46
rime::TextDb::MetaUpdate
RIME_DLL bool MetaUpdate(const string &key, const string &value) override
Definition text_db.cc:194
rime::TextDb::Close
RIME_DLL bool Close() override
Definition text_db.cc:138
rime::TextDb::Clear
void Clear()
Definition text_db.cc:151
rime::TextDb::Query
RIME_DLL an< DbAccessor > Query(const string &key) override
Definition text_db.cc:67
rime::TextDb::CreateMetadata
RIME_DLL bool CreateMetadata() override
Definition text_db.cc:180
rime::TextDb::MetaFetch
RIME_DLL bool MetaFetch(const string &key, string *value) override
Definition text_db.cc:184
rime::TextDb::db_type_
string db_type_
Definition text_db.h:71
rime::TextDb::modified_
bool modified_
Definition text_db.h:75
rime::TextDb::format_
TextFormat format_
Definition text_db.h:72
rime::TextDb::QueryMetadata
RIME_DLL an< DbAccessor > QueryMetadata() override
Definition text_db.cc:57
rime::TextDb::Update
RIME_DLL bool Update(const string &key, const string &value) override
Definition text_db.cc:83
rime::TextDb::data_
TextDbData data_
Definition text_db.h:74
rime::TextDb::Backup
RIME_DLL bool Backup(const path &snapshot_file) override
Definition text_db.cc:156
rime::TextDb::LoadFromFile
bool LoadFromFile(const path &file)
Definition text_db.cc:203