-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonDbValues.cpp
executable file
·346 lines (277 loc) · 9.77 KB
/
JsonDbValues.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
/*
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"
void ValueNull::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << "null";
}
void ValueNull::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_NULL;
output.write((char *)&type, sizeof(unsigned char));
}
void ValueNumberInteger::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_NUMBER_INTEGER;
output.write((char *)&type, sizeof(unsigned char));
output.write((char *)&value, sizeof(Type));
}
void ValueNumberInteger::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << value;
}
void ValueNumberReal::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_NUMBER_REAL;
output.write((char *)&type, sizeof(unsigned char));
output.write((char *)&value, sizeof(Type));
}
void ValueNumberReal::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << (boost::format("%.1f") % value);
}
void ValueNumberBoolean::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_NUMBER_BOOL;
output.write((char *)&type, sizeof(unsigned char));
output.write((char *)&value, sizeof(Type));
}
void ValueNumberBoolean::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << (value ? "true" : "false");
}
void ValueString::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_STRING;
output.write((char *)&type, sizeof(unsigned char));
size_t len = value.size();
output.write((char *)&len, sizeof(size_t));
output.write(value.c_str(), len);
}
void ValueString::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << "\"" << value << "\"";
}
void ValueArray::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_ARRAY;
output.write((char *)&type, sizeof(unsigned char));
unsigned int entries = values.size();
output.write((char *)&entries, sizeof(unsigned int));
if(entries > 0)
output.write((char *)&values[0], sizeof(ValueKey) * entries);
}
void ValueArray::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << "[";
for(Type::const_iterator i = values.begin(); i != values.end(); ++i)
{
if(i != values.begin())
output << ",";
transaction->Retrieve(*i)->Print(transaction, output, indent_level);
}
output << "]";
}
ValuePointer ValueArray::Get(JsonDb::TransactionHandle &transaction, size_t index)
{
if(index >= values.size())
throw std::runtime_error((boost::format("Index out of bound: %d") % index).str());
return transaction->Retrieve(values[index]);
}
void ValueArray::Append(JsonDb::TransactionHandle &transaction, ValueKey key)
{
values.push_back(key);
transaction->Store(GetKey(), shared_from_this());
}
void ValueArray::Delete(JsonDb::TransactionHandle &transaction)
{
// Delete this element
transaction->Delete(GetKey());
// Delete all sub elements
for(Type::const_iterator i = values.begin(); i != values.end(); ++i)
transaction->Retrieve(*i)->Delete(transaction);
}
void ValueArray::Delete(JsonDb::TransactionHandle &transaction, ValuePointer const &element)
{
// Delete all sub elements
for(Type::iterator i = values.begin(); i != values.end(); ++i)
{
if(*i == element->GetKey())
{
// Delete the element
transaction->Retrieve(*i)->Delete(transaction);
// Remove element from list
values.erase(i);
// Store the current element
transaction->Store(GetKey(), shared_from_this());
// Done
return;
}
}
}
void ValueArray::Walk(JsonDb::TransactionHandle &transaction, std::set<ValueKey> &keys)
{
keys.insert(GetKey());
for(Type::const_iterator i = values.begin(); i != values.end(); ++i)
transaction->Retrieve(*i)->Walk(transaction, keys);
}
void ValueObject::Serialize(std::ostream &output) const
{
unsigned char type = VALUE_OBJECT;
output.write((char *)&type, sizeof(unsigned char));
unsigned int entries = values.size();
output.write((char *)&entries, sizeof(unsigned int));
for(std::map<std::string, ValueKey>::const_iterator i = values.begin(); i != values.end(); ++i)
{
unsigned int name_length = i->first.size();
output.write((char *)&name_length, sizeof(name_length));
output.write(&i->first[0], i->first.size());
output.write((char *)&i->second, sizeof(ValueKey));
}
}
void ValueObject::Print(JsonDb::TransactionHandle &transaction, std::ostream &output, unsigned int indent_level) const
{
output << std::endl << Indent(indent_level - 1) << "{";
for(std::map<std::string, ValueKey>::const_iterator i = values.begin(); i != values.end(); ++i)
{
if(i != values.begin())
output << "," << std::endl;
else
output << std::endl;
output << Indent(indent_level) << "\"" << i->first << "\" = ";
transaction->Retrieve(i->second)->Print(transaction, output, indent_level + 1);
}
output << std::endl << Indent(indent_level - 1) << "}";
}
ValuePointer ValueObject::Get(JsonDb::TransactionHandle &transaction, std::string const &path, NotExistsResolution not_exists_resolution)
{
ValuePointer element_pointer;
Type::const_iterator i = values.find(path);
if(i != values.end())
return transaction->Retrieve(i->second);
if(not_exists_resolution == throw_exception)
{
throw std::runtime_error((boost::format("Element not found in object: %s") % path).str());
} else if(not_exists_resolution == return_null)
{
return ValuePointer();
}
ValueKey key = transaction->GenerateKey();
element_pointer = ValuePointer(new ValueObject(key));
values[path] = element_pointer->GetKey();
transaction->Store(element_pointer->GetKey(), element_pointer);
transaction->Store(GetKey(), shared_from_this());
return element_pointer;
}
void ValueObject::Delete(JsonDb::TransactionHandle &transaction)
{
// Delete this element
transaction->Delete(GetKey());
// Delete all sub elements
for(Type::const_iterator i = values.begin(); i != values.end(); ++i)
transaction->Retrieve(i->second)->Delete(transaction);
}
void ValueObject::Delete(JsonDb::TransactionHandle &transaction, ValuePointer const &element)
{
// Delete all sub elements
for(Type::iterator i = values.begin(); i != values.end(); ++i)
{
if(i->second == element->GetKey())
{
// Delete the element
transaction->Retrieve(i->second)->Delete(transaction);
// Remove element from list
values.erase(i);
// Store the current element
transaction->Store(GetKey(), shared_from_this());
// Done
return;
}
}
}
void ValueObject::Walk(JsonDb::TransactionHandle &transaction, std::set<ValueKey> &keys)
{
keys.insert(GetKey());
for(Type::const_iterator i = values.begin(); i != values.end(); ++i)
transaction->Retrieve(i->second)->Walk(transaction, keys);
}
ValuePointer Value::Unserialize(ValueKey key, std::istream &input)
{
unsigned char type;
input.read((char *)&type, sizeof(unsigned char));
// std::cout << "Type: " << (unsigned int)type << ", object: " << Value::VALUE_OBJECT << std::endl;
switch(type)
{
case Value::VALUE_NUMBER_INTEGER:
{
ValueNumberInteger::Type value;
input.read((char *)&value, sizeof(value));
return ValuePointer(new ValueNumberInteger(key, value));
}
case Value::VALUE_NUMBER_REAL:
{
ValueNumberReal::Type value;
input.read((char *)&value, sizeof(value));
return ValuePointer(new ValueNumberReal(key, value));
}
case Value::VALUE_NUMBER_BOOL:
{
ValueNumberBoolean::Type value;
input.read((char *)&value, sizeof(value));
return ValuePointer(new ValueNumberBoolean(key, value));
}
case Value::VALUE_STRING:
{
size_t len;
input.read((char *)&len, sizeof(size_t));
ValueString::Type value(len, (char)0);
if(len > 0)
input.read((char *)&value[0], len);
return ValuePointer(new ValueString(key, value));
}
case Value::VALUE_NULL:
{
return ValuePointer(new ValueNull(key));
}
case Value::VALUE_ARRAY:
{
unsigned int entries;
input.read((char *)&entries, sizeof(entries));
ValueArray::Type values(entries);
if(entries > 0)
input.read((char *)&values[0], sizeof(ValueKey) * entries);
return ValuePointer(new ValueArray(key, values));
}
case Value::VALUE_OBJECT:
{
unsigned int entries;
input.read((char *)&entries, sizeof(entries));
ValueObject::Type values;
for(unsigned int i = 0; i < entries; ++i)
{
unsigned int name_length;
input.read((char *)&name_length, sizeof(name_length));
std::string name((size_t)name_length, (char)0);
input.read(&name[0], name_length);
ValueKey value;
input.read((char *)&value, sizeof(value));
values[name] = value;
}
return ValuePointer(new ValueObject(key, values));
}
};
throw std::runtime_error("Failed to unserialize database entry");
}