-
Notifications
You must be signed in to change notification settings - Fork 20
/
treedbm_ex4.cc
160 lines (130 loc) · 6.25 KB
/
treedbm_ex4.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*************************************************************************************************
* Example for key comparators of the tree database
*
* Copyright 2020 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*************************************************************************************************/
#include "tkrzw_cmd_util.h"
#include "tkrzw_dbm_tree.h"
#include "tkrzw_str_util.h"
// Main routine.
int main(int argc, char** argv) {
// All symbols of Tkrzw are under the namespace "tkrzw".
using namespace tkrzw;
// Creates the database manager.
TreeDBM dbm;
// Opens a new database with the default key comparator (LexicalKeyComparator).
Status status = dbm.Open("casket.tkt", true, File::OPEN_TRUNCATE).OrDie();
// Sets records with the key being a big-endian binary of an integer.
// e.g: "\x00\x00\x00\x00\x00\x00\x00\x31" -> "hop"
dbm.Set(IntToStrBigEndian(1), "hop").OrDie();
dbm.Set(IntToStrBigEndian(256), "step").OrDie();
dbm.Set(IntToStrBigEndian(32), "jump").OrDie();
// Gets records with the key being a big-endian binary of an integer.
std::cout << dbm.GetSimple(IntToStrBigEndian(1)) << std::endl;
std::cout << dbm.GetSimple(IntToStrBigEndian(256)) << std::endl;
std::cout << dbm.GetSimple(IntToStrBigEndian(32)) << std::endl;
// Lists up all records, restoring keys into integers.
std::unique_ptr<DBM::Iterator> iter = dbm.MakeIterator();
iter->First();
std::string key, value;
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << StrToIntBigEndian(key) << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close().OrDie();
// Opens a new database with the decimal integer comparator.
TreeDBM::TuningParameters params;
params.key_comparator = DecimalKeyComparator;
dbm.OpenAdvanced("casket.tkt", true, File::OPEN_TRUNCATE, params).OrDie();
// Sets records with the key being a decimal string of an integer.
// e.g: "1" -> "hop"
dbm.Set(ToString(1), "hop").OrDie();
dbm.Set(ToString(256), "step").OrDie();
dbm.Set(ToString(32), "jump").OrDie();
// Gets records with the key being a decimal string of an integer.
std::cout << dbm.GetSimple(ToString(1)) << std::endl;
std::cout << dbm.GetSimple(ToString(256)) << std::endl;
std::cout << dbm.GetSimple(ToString(32)) << std::endl;
// Lists up all records, restoring keys into integers.
iter = dbm.MakeIterator();
iter->First();
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << StrToInt(key) << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close().OrDie();
// Opens a new database with the decimal real number comparator.
params.key_comparator = RealNumberKeyComparator;
dbm.OpenAdvanced("casket.tkt", true, File::OPEN_TRUNCATE, params).OrDie();
// Sets records with the key being a decimal string of a real number.
// e.g: "1.5" -> "hop"
dbm.Set(ToString(1.5), "hop").OrDie();
dbm.Set(ToString(256.5), "step").OrDie();
dbm.Set(ToString(32.5), "jump").OrDie();
// Gets records with the key being a decimal string of a real number.
std::cout << dbm.GetSimple(ToString(1.5)) << std::endl;
std::cout << dbm.GetSimple(ToString(256.5)) << std::endl;
std::cout << dbm.GetSimple(ToString(32.5)) << std::endl;
// Lists up all records, restoring keys into floating-point numbers.
iter = dbm.MakeIterator();
iter->First();
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << StrToDouble(key) << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close().OrDie();
// Opens a new database with the big-endian signed integers comparator.
params.key_comparator = SignedBigEndianKeyComparator;
dbm.OpenAdvanced("casket.tkt", true, File::OPEN_TRUNCATE, params).OrDie();
// Sets records with the key being a big-endian binary of a floating-point number.
// e.g: "\x00\x00\x00\x00\x00\x00\x00\x31" -> "hop"
dbm.Set(IntToStrBigEndian(-1), "hop").OrDie();
dbm.Set(IntToStrBigEndian(-256), "step").OrDie();
dbm.Set(IntToStrBigEndian(-32), "jump").OrDie();
// Gets records with the key being a big-endian binary of a floating-point number.
std::cout << dbm.GetSimple(IntToStrBigEndian(-1)) << std::endl;
std::cout << dbm.GetSimple(IntToStrBigEndian(-256)) << std::endl;
std::cout << dbm.GetSimple(IntToStrBigEndian(-32)) << std::endl;
// Lists up all records, restoring keys into floating-point numbers.
iter = dbm.MakeIterator();
iter->First();
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << static_cast<int64_t>(StrToIntBigEndian(key)) << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close().OrDie();
// Opens a new database with the big-endian floating-point numbers comparator.
params.key_comparator = FloatBigEndianKeyComparator;
dbm.OpenAdvanced("casket.tkt", true, File::OPEN_TRUNCATE, params).OrDie();
// Sets records with the key being a big-endian binary of a floating-point number.
// e.g: "\x3F\xF8\x00\x00\x00\x00\x00\x00" -> "hop"
dbm.Set(FloatToStrBigEndian(1.5), "hop").OrDie();
dbm.Set(FloatToStrBigEndian(256.5), "step").OrDie();
dbm.Set(FloatToStrBigEndian(32.5), "jump").OrDie();
// Gets records with the key being a big-endian binary of a floating-point number.
std::cout << dbm.GetSimple(FloatToStrBigEndian(1.5)) << std::endl;
std::cout << dbm.GetSimple(FloatToStrBigEndian(256.5)) << std::endl;
std::cout << dbm.GetSimple(FloatToStrBigEndian(32.5)) << std::endl;
// Lists up all records, restoring keys into floating-point numbers.
iter = dbm.MakeIterator();
iter->First();
while (iter->Get(&key, &value) == Status::SUCCESS) {
std::cout << StrToFloatBigEndian(key) << ":" << value << std::endl;
iter->Next();
}
// Closes the database.
dbm.Close().OrDie();
return 0;
}
// END OF FILE