forked from estraier/tkrzw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkrzw_dbm_common_impl.h
291 lines (261 loc) · 12.1 KB
/
tkrzw_dbm_common_impl.h
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*************************************************************************************************
* Common implementation components for database managers
*
* 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.
*************************************************************************************************/
#ifndef _TKRZW_DBM_COMMON_IMPL_H
#define _TKRZW_DBM_COMMON_IMPL_H
#include <string>
#include <string_view>
#include <cinttypes>
#include <cstdarg>
#include "tkrzw_dbm.h"
#include "tkrzw_hash_util.h"
#include "tkrzw_file.h"
#include "tkrzw_lib_common.h"
namespace tkrzw {
/**
* Primary hash function for the hash database.
* @param data The data to calculate the hash value for.
* @param num_buckets The number of buckets of the hash table.
* @return The hash value.
*/
inline uint64_t PrimaryHash(std::string_view data, uint64_t num_buckets) {
constexpr uint64_t seed = 19780211;
uint64_t hash = HashMurmur(data, seed);
if (num_buckets <= UINT32MAX) {
hash = (((hash & 0xffff000000000000ULL) >> 48) | ((hash & 0x0000ffff00000000ULL) >> 16)) ^
(((hash & 0x000000000000ffffULL) << 16) | ((hash & 0x00000000ffff0000ULL) >> 16));
}
return hash % num_buckets;
}
/**
* Secondary hash function for sharding.
* @param data The data to calculate the hash value for.
* @param num_shards The number of shards.
* @return The hash value.
*/
inline uint64_t SecondaryHash(std::string_view data, uint64_t num_shards) {
uint64_t hash = HashFNV(data);
if (num_shards <= UINT32MAX) {
hash = (((hash & 0xffff000000000000ULL) >> 48) | ((hash & 0x0000ffff00000000ULL) >> 16)) ^
(((hash & 0x000000000000ffffULL) << 16) | ((hash & 0x00000000ffff0000ULL) >> 16));
}
return hash % num_shards;
}
/**
* Returns true if an integer is a prime number.
* @param num The integer.
* @return True if the integer is a prime number.
*/
uint64_t IsPrimeNumber(uint64_t num);
/**
* Gets a proper bucket size for hashing.
* @param min_size The minimum size.
* @return The calculated bucket size.
*/
int64_t GetHashBucketSize(int64_t min_size);
/**
* Searches a database and get keys which match a pattern.
* @param dbm The DBM object of the database.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @param matcher A matching function which takes the pattern and a candidate.
* @return The result status.
* @details This scans the whole database so it can take long time.
*/
Status SearchDBM(
DBM* dbm, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0,
bool (*matcher)(std::string_view, std::string_view) = StrContains);
/**
* Searches an ordered database and get keys which match a boundary condition.
* @param dbm The DBM object of the database.
* @param pattern The boundary pattern of the origin.
* @param upper If true, keys whose positions are upper than the boundary pattern are picked up.
* If false, keys whose positions are lower than the boundary pattern are picked up.
* @param inclusive If true, keys whose position are the same as the boundary pattern are included.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
* @details Even if there's no matching record, the operation doesn't fail. This method is
* suppoerted only for ordered databases.
*/
Status SearchDBMOrder(DBM* dbm, std::string_view pattern, bool upper, bool inclusive,
std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a database and get keys which begin with a pattern.
* @param dbm The DBM object of the database.
* @param pattern The pattern for forward matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
* @details If the database is ordered, an efficient way is used. However, if the key comparator
* is not LexicalKeyComparator, all matching keys are not extracted. If the database is unordered,
* this scans the whole database so it can take long time.
*/
Status SearchDBMForwardMatch(
DBM* dbm, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a database and get keys which match a regular expression.
* @param dbm The DBM object of the database.
* @param pattern The regular expression pattern for partial matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
* @details This scans the whole database so it can take long time.
*/
Status SearchDBMRegex(
DBM* dbm, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a database and get keys whose edit distance with a UTF-8 pattern is the least.
* @param dbm The DBM object of the database.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @details This scans the whole database so it can take long time.
*/
Status SearchDBMEditDistance(
DBM* dbm, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a database and get keys whose edit distance with a binary pattern is the least.
* @param dbm The DBM object of the database.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @details This scans the whole database so it can take long time.
*/
Status SearchDBMEditDistanceBinary(
DBM* dbm, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a database and get keys which match a pattern, according to a mode expression.
* @param dbm The DBM object of the database.
* @param mode The search mode. "contain" extracts keys containing the pattern. "begin"
* extracts keys beginning with the pattern. "end" extracts keys ending with the pattern.
* "regex" extracts keys partially matches the pattern of a regular expression. "edit"
* extracts keys whose edit distance to the UTF-8 pattern is the least. "editbin" extracts
* keys whose edit distance to the binary pattern is the least. Ordered databases support
* "upper" and "lower" which extract keys whose positions are upper/lower than the pattern.
* "upperinc" and "lowerinc" are their inclusive versions.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
*/
Status SearchDBMModal(
DBM* dbm, std::string_view mode, std::string_view pattern,
std::vector<std::string>* matched, size_t capacity = 0);
/**
* Exports all records of a database to a flat record file.
* @param dbm The DBM object of the database.
* @param dest_file The file object to write records in.
* @return The result status.
* @details A flat record file contains a sequence of binary records without any high level
* structure so it is useful as a intermediate file for data migration.
*/
Status ExportDBMToFlatRecords(DBM* dbm, File* dest_file);
/**
* Imports records to a database from a flat record file.
* @param dbm The DBM object of the database.
* @param src_file The file object to read records from.
* @return The result status.
*/
Status ImportDBMFromFlatRecords(DBM* dbm, File* src_file);
/**
* Exports the keys of all records of a database to a flat record file.
* @param dbm The DBM object of the database.
* @param dest_file The file object to write keys in.
* @return The result status.
*/
Status ExportDBMKeysToFlatRecords(DBM* dbm, File* dest_file);
/**
* Exports all records of a database to a TSV file.
* @param dbm The DBM object of the database.
* @param dest_file The file object to write records in.
* @param escape If true, C-style escaping is applied to the output.
* @return The result status.
*/
Status ExportDBMToTSV(DBM* dbm, File* dest_file, bool escape = false);
/**
* Imports records to a database from a TSV file.
* @param dbm The DBM object of the database.
* @param dest_file The file object to read records from.
* @param unescape If true, C-style unescaping is applied to the input.
* @return The result status.
*/
Status ImportDBMFromTSV(DBM* dbm, File* dest_file, bool unescape = false);
/**
* Exports the keys of all records of a database as lines to a text file.
* @param dbm The DBM object of the database.
* @param dest_file The file object to write keys in.
* @return The result status.
*/
Status ExportDBMKeysAsLines(DBM* dbm, File* dest_file);
/**
* Searches a text file and get lines which match a pattern.
* @param file The file to search.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @param matcher A matching function which takes the pattern and a candidate.
* @return The result status.
*/
Status SearchTextFile(
File* file, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0,
bool (*matcher)(std::string_view, std::string_view) = StrContains);
/**
* Searches a text file and get lines which match a regular expression.
* @param file The file to search.
* @param pattern The regular expression pattern for partial matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
*/
Status SearchTextFileRegex(
File* file, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a text file and get lines whose edit distance with a UTF-8 pattern is the least.
* @param file The file to search.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
*/
Status SearchTextFileEditDistance(
File* file, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a text file and get lines whose edit distance with a binary pattern is the least.
* @param file The file to search.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
*/
Status SearchTextFileEditDistanceBinary(
File* file, std::string_view pattern, std::vector<std::string>* matched, size_t capacity = 0);
/**
* Searches a text file and get lines which match a pattern, according to a mode expression.
* @param file The file to search.
* @param mode The search mode. "contain" extracts keys containing the pattern. "begin"
* extracts keys beginning with the pattern. "end" extracts keys ending with the pattern.
* "regex" extracts keys partially matches the pattern of a regular expression. "edit"
* extracts keys whose edit distance to the UTF-8 pattern is the least. "editbin" extracts
* keys whose edit distance to the binary pattern is the least.
* @param pattern The pattern for matching.
* @param matched A vector to contain the result.
* @param capacity The maximum records to obtain. 0 means unlimited.
* @return The result status.
*/
Status SearchTextFileModal(
File* file, std::string_view mode, std::string_view pattern,
std::vector<std::string>* matched, size_t capacity = 0);
} // namespace tkrzw
#endif // _TKRZW_DBM_COMMON_IMPL_H
// END OF FILE