-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonDb.cpp
executable file
·375 lines (296 loc) · 12 KB
/
JsonDb.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright (C) 2010 Wouter van Kleunen
This file is part of JsonDb.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JsonDb. If not, see <http://www.gnu.org/licenses/>.
*/
#include "JsonDb.h"
#include "JsonDbValues.h"
#include "JsonDbParser.h"
#include "JsonDbPathParser.h"
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/tokenizer.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>
#include <sstream>
#include <map>
#include <deque>
class CharPtr
: public boost::shared_ptr<char>
{
public:
CharPtr(char *object = NULL)
: boost::shared_ptr<char>(object, cbfree)
{ }
};
static void CloseDatabase(VILLA *villa)
{
if(villa != NULL)
vlclose(villa);
}
JsonDb::Transaction::Transaction(std::string const &filename, ValuePointer const &_null_element)
{
/* The null element */
null_element = _null_element;
/* open the database */
db = StorageDbPointer(vlopen(filename.c_str(), VL_OWRITER | VL_OCREAT, VL_CMPINT), CloseDatabase);
/* Start the transaction */
vltranbegin(db.get());
if(db.get() == NULL)
throw std::runtime_error((boost::format("Failed to open database: %s") % dperrmsg(dpecode)).str().c_str());
ValuePointer root = Retrieve(root_key);
if(root.get() == NULL)
{
root = ValuePointer(new ValueObject(root_key));
Store(root->GetKey(), root);
}
ValuePointer next_id_value = Retrieve(next_id_key);
if(next_id_value.get() == NULL)
{
// Initialize values
next_id = initial_next_id;
start_next_id = next_id;
} else
{
// Retrieve values from database
next_id = next_id_value->GetValueInt();
start_next_id = next_id;
}
// std::cout << "Start transaction, next id: " << next_id << std::endl;
}
void JsonDb::Transaction::Store(ValueKey key, ValuePointer value)
{
if(key != null_key)
{
std::ostringstream output;
value->Serialize(output);
std::string output_string = output.str();
vlput(db.get(), (char const *)&key, sizeof(ValueKey), &output_string[0], output_string.size(), VL_DOVER);
// std::cout << "Store: key=" << key << std::endl;
}
}
ValuePointer JsonDb::Transaction::Retrieve(ValueKey key)
{
if(key == null_key)
return null_element;
// std::cout << "Retrieve: key=" << key << std::endl;
// Then retrieve the actual data
int value_size;
CharPtr val = CharPtr(vlget(db.get(), (char const *)&key, sizeof(ValueKey), &value_size));
if(val.get() == NULL)
return ValuePointer();
if(value_size <= 0)
throw std::runtime_error((boost::format("Element has an invalid size: %d") % key).str().c_str());
std::string val_str(val.get(), value_size);
std::istringstream input(val_str);
ValuePointer result = Value::Unserialize(key, input);
return result;
}
void JsonDb::Transaction::Delete(ValueKey key)
{
vlout(db.get(), (char const *)&key, sizeof(ValueKey));
// std::cout << "Delete: key=" << key << std::endl;
}
void JsonDb::Transaction::Commit()
{
if(next_id != start_next_id)
{
Store(next_id_key, ValuePointer(new ValueNumberInteger(next_id_key, next_id)));
// std::cout << "Commit transaction, next id: " << next_id << std::endl;
}
vltrancommit(db.get());
start_next_id = next_id;
}
std::set<ValueKey> JsonDb::Transaction::Walk()
{
std::set<ValueKey> keys;
// initialize the iterator
if(!vlcurfirst(db.get()))
throw std::runtime_error("Failed to initialize database iterator");
CharPtr key;
while((key = CharPtr(vlcurkey(db.get(), NULL))) != NULL)
{
keys.insert(*(ValueKey const *)key.get());
vlcurnext(db.get());
}
return keys;
}
JsonDb::JsonDb(std::string const &_filename)
: filename(_filename)
{
null_element = ValuePointer(new ValueNull(null_key));
}
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, ValuePointer new_value, bool create_if_not_exists)
{
std::pair<ValuePointer, ValuePointer> old_value = Get(transaction, path, create_if_not_exists ? create : throw_exception);
new_value->SetKey(old_value.second->GetKey());
old_value.second->Delete(transaction);
transaction->Store(new_value->GetKey(), new_value);
}
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, int value, bool create_if_not_exists)
{
Set(transaction, path, ValuePointer(new ValueNumberInteger(null_key, value)), create_if_not_exists);
}
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, std::string const &value, bool create_if_not_exists)
{
Set(transaction, path, ValuePointer(new ValueString(null_key, value)), create_if_not_exists);
}
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, double value, bool create_if_not_exists)
{
Set(transaction, path, ValuePointer(new ValueNumberReal(null_key, value)), create_if_not_exists);
}
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, bool value, bool create_if_not_exists)
{
Set(transaction, path, ValuePointer(new ValueNumberBoolean(null_key, value)), create_if_not_exists);
}
void JsonDb::SetArray(TransactionHandle &transaction, std::string const &path, size_t total_elements, bool create_if_not_exists)
{
ValueArray::Type elements(total_elements);
for(ValueArray::Type::iterator i = elements.begin(); i != elements.end(); ++i)
{
ValuePointer new_element(new ValueNull(transaction->GenerateKey()));
*i = new_element->GetKey();
transaction->Store(new_element->GetKey(), new_element);
}
Set(transaction, path, ValuePointer(new ValueArray(null_key, elements)), create_if_not_exists);
}
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, ValuePointer const &value)
{
std::pair<ValuePointer, ValuePointer> old_value = Get(transaction, path, throw_exception);
old_value.second->Append(transaction, value->GetKey());
transaction->Store(value->GetKey(), value);
}
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, int value)
{
AppendArray(transaction, path, ValuePointer(new ValueNumberInteger(transaction->GenerateKey(), value)));
}
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, bool value)
{
AppendArray(transaction, path, ValuePointer(new ValueNumberBoolean(transaction->GenerateKey(), value)));
}
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, std::string const &value)
{
AppendArray(transaction, path, ValuePointer(new ValueString(transaction->GenerateKey(), value)));
}
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, double value)
{
AppendArray(transaction, path, ValuePointer(new ValueNumberReal(transaction->GenerateKey(), value)));
}
void JsonDb::SetJson(TransactionHandle &transaction, std::string const &path, std::string const &value, bool create_if_not_exists)
{
std::pair<ValuePointer, ValuePointer> old_value = Get(transaction, path, create_if_not_exists ? create : throw_exception);
JsonDb_ParseJsonExpression(transaction, value, old_value.second);
//Set(transaction, path, ValuePointer(new ValueNumberBoolean(null_key, value)), create_if_not_exists);
}
void JsonDb::AppendArrayJson(TransactionHandle &transaction, std::string const &path, std::string const &value_str)
{
ValuePointer value(new ValueNull(transaction->GenerateKey()));
std::pair<ValuePointer, ValuePointer> old_value = Get(transaction, path, throw_exception);
old_value.second->Append(transaction, value->GetKey());
JsonDb_ParseJsonExpression(transaction, value_str, value);
}
std::pair<ValuePointer, ValuePointer> JsonDb::Get(TransactionHandle &transaction, std::string const &path, NotExistsResolution not_exists_resolution)
{
return JsonDb_ParseJsonPathExpression(transaction, path, transaction->GetRoot(), not_exists_resolution);
}
int JsonDb::GetInt(TransactionHandle &transaction, std::string const &path)
{
return Get(transaction, path, throw_exception).second->GetValueInt();
}
std::string JsonDb::GetString(TransactionHandle &transaction, std::string const &path)
{
return Get(transaction, path, throw_exception).second->GetValueString();
}
bool JsonDb::GetBool(TransactionHandle &transaction, std::string const &path)
{
return Get(transaction, path, throw_exception).second->GetValueBoolean();
}
double JsonDb::GetReal(TransactionHandle &transaction, std::string const &path)
{
return Get(transaction, path, throw_exception).second->GetValueReal();
}
bool JsonDb::Exists(TransactionHandle &transaction, std::string const &path)
{
return Get(transaction, path, return_null).second != NULL;
}
void JsonDb::Delete(TransactionHandle &transaction, ValuePointer value)
{
if(value != NULL)
value->Delete(transaction);
}
void JsonDb::Delete(TransactionHandle &transaction, std::string const &path)
{
std::pair<ValuePointer, ValuePointer> element = Get(transaction, path, return_null);
element.first->Delete(transaction, element.second);
}
void JsonDb::Print(TransactionHandle &transaction, std::string const &path, std::ostream &output)
{
std::pair<ValuePointer, ValuePointer> element = Get(transaction, path, return_null);
element.second->Print(transaction, output, 1);
}
void JsonDb::Print(TransactionHandle &transaction, std::ostream &output)
{
transaction->GetRoot()->Print(transaction, output, 1);
}
std::set<ValueKey> JsonDb::WalkTree(TransactionHandle &transaction)
{
std::set<ValueKey> result;
result.insert(transaction->GetRoot()->GetKey());
transaction->GetRoot()->Walk(transaction, result);
return result;
}
void JsonDb::Delete()
{
// Remove the database directory and all it's subdirectories
boost::filesystem::remove_all(boost::filesystem::path(filename));
}
bool JsonDb::Validate(TransactionHandle &transaction)
{
std::set<ValueKey> tree_keys = WalkTree(transaction);
std::set<ValueKey> db_keys = transaction->Walk();
// Item storing next id is a valid item
if(db_keys.find(next_id_key) != db_keys.end())
tree_keys.insert(next_id_key);
std::set<ValueKey> db_missing_keys;
std::set_difference(
tree_keys.begin(), tree_keys.end(),
db_keys.begin(), db_keys.end(),
std::inserter(db_missing_keys, db_missing_keys.begin()));
std::set<ValueKey> db_stale_keys;
std::set_difference(
db_keys.begin(), db_keys.end(),
tree_keys.begin(), tree_keys.end(),
std::inserter(db_stale_keys, db_stale_keys.begin()));
std::cout << "Keys found in tree: " << std::endl;
for(std::set<ValueKey>::const_iterator i = tree_keys.begin(); i != tree_keys.end(); ++i)
std::cout << (i != tree_keys.begin() ? ", " : "") << *i;
std::cout << std::endl;
std::cout << "Keys found in database: " << std::endl;
for(std::set<ValueKey>::const_iterator i = db_keys.begin(); i != db_keys.end(); ++i)
std::cout << (i != db_keys.begin() ? ", " : "") << *i;
std::cout << std::endl;
if(!db_missing_keys.empty())
{
std::cout << "Following keys are missing from the database: " << std::endl;
for(std::set<ValueKey>::const_iterator i = db_missing_keys.begin(); i != db_missing_keys.end(); ++i)
std::cout << (i != db_missing_keys.begin() ? ", " : "") << *i;
std::cout << std::endl;
}
if(!db_stale_keys.empty())
{
std::cout << "Following stale keys where found in the database: " << std::endl;
for(std::set<ValueKey>::const_iterator i = db_stale_keys.begin(); i != db_stale_keys.end(); ++i)
std::cout << (i != db_stale_keys.begin() ? ", " : "") << *i;
std::cout << std::endl;
}
return (db_missing_keys.empty() && db_stale_keys.empty());
}