17
17
#ifndef ZORBA_STORE_JSON_ITEMS_H
18
18
#define ZORBA_STORE_JSON_ITEMS_H
23
23
#include <zorba/config.h>
24
#include <zorbautils/hashmap_zstring.h>
26
#include "store/api/item_handle.h"
27
#include "store/api/iterator.h"
24
29
#include "atomic_items.h"
25
30
#include "simple_collection.h"
26
#include "store/api/item_handle.h"
27
#include "store/api/iterator.h"
60
63
zstring getStringValue() const { return "null"; }
63
getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
65
void getStringValue2(zstring& val) const { val = "null"; }
67
void appendStringValue(zstring& buf) const { buf += "null"; }
69
void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
65
71
store::SchemaTypeCode getTypeCode() const { return store::JDM_NULL; }
67
73
store::Item* getType() const;
71
76
const store::Item* other,
73
78
const XQPCollator* collation = 0) const;
76
hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
80
uint32_t hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
79
getEBV() const { return false; }
82
bool getEBV() const { return false; }
116
126
virtual ~JSONObject() {}
118
store::StoreConsts::JSONItemKind getJSONItemKind() const
130
virtual store::StoreConsts::JSONItemKind getJSONItemKind() const
120
132
return store::StoreConsts::jsonObject;
123
bool isJSONObject() const { return true; }
125
virtual bool add(const JSONObjectPair_t& aPair, bool accumulate) = 0;
127
virtual JSONObjectPair_t remove(const store::Item_t& aName) = 0;
129
virtual xs_integer getSize() const = 0;
131
bool getBooleanValue() const { return true; }
135
virtual bool isJSONObject() const { return true; }
137
virtual store::Iterator_t getObjectKeys() const = 0;
139
virtual store::Item_t getObjectValue(const store::Item_t& aKey) const = 0;
133
141
virtual Item* getType() const;
135
virtual SimpleCollection* getCollection() const = 0;
146
const store::Item_t& aName,
147
const store::Item_t& aValue,
148
bool accumulate) = 0;
150
virtual store::Item_t remove(const store::Item_t& aName) = 0;
152
virtual store::Item_t setValue(
153
const store::Item_t& aName,
154
const store::Item_t& aValue) = 0;
157
const store::Item_t& aName,
158
const store::Item_t& aNewName) = 0;
137
160
virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
140
uint32_t hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
142
virtual store::Item* copy(store::Item* parent, const store::CopyMode&) const;
151
168
class SimpleJSONObject : public JSONObject
154
struct JSONObjectPairComparator
156
bool operator() (const store::Item* lhs, const store::Item* rhs) const;
159
typedef std::map<store::Item*, size_t, JSONObjectPairComparator> PairMap;
160
typedef PairMap::const_iterator PairMapConstIter;
161
typedef PairMap::iterator PairMapIter;
163
typedef std::vector<JSONObjectPair*> Pairs;
164
typedef Pairs::const_iterator PairsConstIter;
165
typedef Pairs::iterator PairsIter;
170
SimpleCollection* theCollection;
172
class PairIterator : public store::Iterator
171
ZSTRING_HASH_MAP(csize, Keys);
172
typedef std::vector<std::pair<store::Item*, store::Item*> > Pairs;
174
class KeyIterator : public store::Iterator
175
SimpleJSONObject_t theObject;
176
PairsConstIter theIter;
177
SimpleJSONObject_t theObject;
178
Pairs::iterator theIter;
179
PairIterator(const SimpleJSONObject_t& aObject) : theObject(aObject) {}
181
KeyIterator(const SimpleJSONObject_t& aObject) : theObject(aObject) {}
181
virtual ~PairIterator();
183
virtual ~KeyIterator();
183
185
virtual void open();
189
191
virtual void close();
198
store::Collection * theCollection;
199
const JSONItem * theRoot;
193
SimpleJSONObject() : theCollection(0) {}
195
210
virtual ~SimpleJSONObject();
197
bool add(const JSONObjectPair_t& aPair, bool accumulate);
199
JSONObjectPair_t remove(const store::Item_t& aName);
201
xs_integer getSize() const { return xs_integer(thePairs.size()); }
203
store::Iterator_t getPairs() const;
205
store::Item* getPair(const store::Item_t& name) const;
207
store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
209
zstring getStringValue() const;
211
void getStringValue2(zstring& val) const;
213
void appendStringValue(zstring& buf) const;
215
void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
217
virtual SimpleCollection* getCollection() const { return theCollection; }
219
void setCollection(SimpleCollection* collection, xs_integer pos);
214
virtual store::Iterator_t getObjectKeys() const;
216
virtual store::Item_t getObjectValue(const store::Item_t& aKey) const;
218
virtual store::Item* copy(
220
const store::CopyMode& copymode) const;
222
virtual zstring getStringValue() const;
224
virtual void getStringValue2(zstring& val) const;
226
virtual void appendStringValue(zstring& buf) const;
228
virtual void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
230
virtual const store::Collection* getCollection() const;
235
const store::Item_t& aName,
236
const store::Item_t& aValue,
239
virtual store::Item_t remove(const store::Item_t& aName);
241
virtual store::Item_t setValue(
242
const store::Item_t& aName,
243
const store::Item_t& aValue);
246
const store::Item_t& aName,
247
const store::Item_t& aNewName);
249
virtual void setCollection(SimpleCollection* collection, xs_integer pos);
254
friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
256
void setRoot(const JSONItem* aRoot);
258
const JSONItem* getRoot() { return theRoot; }
260
// Invariant handling
262
friend class SimpleJSONArray;
265
void assertInvariant() const;
267
bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
269
bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
285
338
typedef std::vector<store::Item*> Members;
286
typedef Members::const_iterator MembersConstIter;
287
typedef Members::iterator MembersIter;
290
SimpleCollection * theCollection;
292
340
class ValuesIterator : public store::Iterator
295
343
SimpleJSONArray_t theArray;
296
MembersConstIter theIterator;
344
Members::iterator theIter;
299
ValuesIterator(const SimpleJSONArray_t& aArray)
305
virtual ~ValuesIterator()
309
virtual void open() { theIterator = theArray->theContent.begin(); }
311
virtual bool next(store::Item_t& res)
313
if (theIterator == theArray->theContent.end())
319
res = *(theIterator++);
324
virtual void reset() { open(); }
326
virtual void close() { theIterator = theArray->theContent.end(); }
347
ValuesIterator(const SimpleJSONArray_t& anArray)
348
: theArray(anArray) {}
350
virtual ~ValuesIterator();
354
virtual bool next(store::Item_t&);
356
virtual void reset();
358
virtual void close();
329
friend class ValuesIterator;
363
const store::Collection * theCollection;
364
const JSONItem * theRoot;
332
367
SimpleJSONArray()
338
374
virtual ~SimpleJSONArray();
378
xs_integer getArraySize() const;
380
store::Item_t getArrayValue(const xs_integer& position) const;
382
store::Iterator_t getArrayValues() const;
386
const store::CopyMode& copymode) const;
388
zstring getStringValue() const;
390
void getStringValue2(zstring& val) const;
392
void appendStringValue(zstring& buf) const;
394
void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
396
virtual const store::Collection* getCollection() const;
341
401
push_back(const store::Item_t& aValue);
356
416
insert_after(const xs_integer& pos, const std::vector<store::Item_t>& members);
418
virtual store::Item_t
359
419
remove(const xs_integer& aPos);
421
virtual store::Item_t
362
422
replace(const xs_integer& aPos, const store::Item_t& value);
365
getSize() const { return xs_integer(theContent.size()); }
367
virtual const store::Item*
368
operator[](const xs_integer&) const;
370
virtual store::Iterator_t
374
getMember(const store::Item_t& position) const;
376
store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
378
zstring getStringValue() const;
380
void getStringValue2(zstring& val) const;
382
void appendStringValue(zstring& buf) const;
385
getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
387
virtual SimpleCollection* getCollection() const { return theCollection; }
389
424
void setCollection(SimpleCollection* collection, xs_integer pos);
393
add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
396
cast(const xs_integer& i);
400
/******************************************************************************
402
*******************************************************************************/
403
class JSONObjectPair : public JSONItem
406
virtual ~JSONObjectPair() {}
408
bool isJSONPair() const { return true; }
411
setName(const store::Item_t& aName) = 0;
414
setValue(const store::Item_t& aValue) = 0;
420
getValue() const = 0;
422
store::StoreConsts::JSONItemKind
423
getJSONItemKind() const { return store::StoreConsts::jsonPair; }
430
/******************************************************************************
432
*******************************************************************************/
433
class SimpleJSONObjectPair : public JSONObjectPair
436
store::Item_t theName;
437
store::Item_t theValue;
440
SimpleJSONObjectPair() {}
442
SimpleJSONObjectPair(const store::Item_t& aName, const store::Item_t& aValue)
449
virtual ~SimpleJSONObjectPair() {}
451
zstring getStringValue() const { return theValue->getStringValue(); }
453
void getStringValue2(zstring& val) const { theValue->getStringValue2(val); }
455
void appendStringValue(zstring& buf) const { theValue->appendStringValue(buf); }
458
setName(const store::Item_t& aName) { theName = aName; }
461
setValue(const store::Item_t& aValue) { theValue = aValue; }
464
getName() const { return theName.getp(); }
467
getValue() const { return theValue.getp(); }
469
store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
472
getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
428
friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
430
void setRoot(const JSONItem* aRoot);
432
const JSONItem* getRoot() { return theRoot; }
435
void add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
437
static uint64_t cast(const xs_integer& i);
439
// Invariant handling
441
friend class SimpleJSONObject;
444
void assertInvariant() const;
446
bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
448
bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
453
void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
455
#if 0 // ifndef NDEBUG
456
#define ASSERT_INVARIANT() assertInvariant()
458
#define ASSERT_INVARIANT()
475
461
} // namespace json
476
462
} // namespace simplestore