~juan457/+junk/zorba

« back to all changes in this revision

Viewing changes to src/store/naive/json_items.h

  • Committer: Markos Zaharioudakis
  • Date: 2012-07-11 15:38:39 UTC
  • mfrom: (10924 zorba)
  • mto: This revision was merged to the branch mainline in revision 10932.
  • Revision ID: markos_za@yahoo.com-20120711153839-0mkh15cg2ubknchd
work in progress

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#ifndef ZORBA_STORE_JSON_ITEMS_H
18
18
#define ZORBA_STORE_JSON_ITEMS_H
19
19
 
 
20
#include <set>
20
21
#include <vector>
21
 
#include <set>
22
22
 
23
23
#include <zorba/config.h>
 
24
#include <zorbautils/hashmap_zstring.h>
 
25
 
 
26
#include "store/api/item_handle.h"
 
27
#include "store/api/iterator.h"
 
28
 
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"
28
31
 
29
32
 
30
33
namespace zorba
59
62
 
60
63
  zstring getStringValue() const { return "null"; }
61
64
 
62
 
  void
63
 
  getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
 
65
  void getStringValue2(zstring& val) const { val = "null"; }
 
66
 
 
67
  void appendStringValue(zstring& buf) const { buf += "null"; }
 
68
 
 
69
  void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
64
70
 
65
71
  store::SchemaTypeCode getTypeCode() const { return store::JDM_NULL; }
66
72
 
67
73
  store::Item* getType() const; 
68
74
 
69
 
  bool 
70
 
  equals(
 
75
  bool equals(
71
76
      const store::Item* other,
72
77
      long timezone = 0,
73
78
      const XQPCollator* collation = 0) const;
74
79
 
75
 
  uint32_t 
76
 
  hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
 
80
  uint32_t hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
77
81
 
78
 
  bool
79
 
  getEBV() const { return false; }
 
82
  bool getEBV() const { return false; }
80
83
};
81
84
 
82
85
 
88
91
{
89
92
protected:
90
93
  SYNC_CODE(mutable RCLock  theRCLock;)
 
94
 
91
95
public:
92
96
  SYNC_CODE(RCLock* getRCLock() const { return &theRCLock; })
93
97
 
95
99
 
96
100
  virtual ~JSONItem() {}
97
101
 
98
 
  bool 
99
 
  equals(
 
102
  // store API
 
103
 
 
104
  virtual bool equals(
100
105
      const store::Item* other,
101
106
      long timezone = 0,
102
107
      const XQPCollator* aCollation = 0) const
103
108
  {
104
109
    return this == other;
105
110
  }
 
111
  
 
112
  // store methods
 
113
  // store methods
 
114
 
 
115
  virtual const JSONItem* getRoot() = 0;
106
116
};
107
117
 
108
118
 
115
125
public:
116
126
  virtual ~JSONObject() {}
117
127
 
118
 
  store::StoreConsts::JSONItemKind getJSONItemKind() const 
 
128
  // store API
 
129
 
 
130
  virtual store::StoreConsts::JSONItemKind getJSONItemKind() const 
119
131
  {
120
132
    return store::StoreConsts::jsonObject;
121
133
  }
122
134
 
123
 
  bool isJSONObject() const { return true; }
124
 
 
125
 
  virtual bool add(const JSONObjectPair_t& aPair, bool accumulate) = 0;
126
 
 
127
 
  virtual JSONObjectPair_t remove(const store::Item_t& aName) = 0;
128
 
 
129
 
  virtual xs_integer getSize() const = 0;
130
 
 
131
 
  bool getBooleanValue() const { return true; }
 
135
  virtual bool isJSONObject() const { return true; }
 
136
 
 
137
  virtual store::Iterator_t getObjectKeys() const = 0;
 
138
 
 
139
  virtual store::Item_t getObjectValue(const store::Item_t& aKey) const = 0;
132
140
 
133
141
  virtual Item* getType() const;
134
142
 
135
 
  virtual SimpleCollection* getCollection() const = 0;
136
 
 
 
143
  // updates
 
144
  
 
145
  virtual bool add(
 
146
      const store::Item_t& aName,
 
147
      const store::Item_t& aValue,
 
148
      bool accumulate) = 0;
 
149
 
 
150
  virtual store::Item_t remove(const store::Item_t& aName) = 0;
 
151
 
 
152
  virtual store::Item_t setValue(
 
153
    const store::Item_t& aName,
 
154
    const store::Item_t& aValue) = 0;
 
155
    
 
156
  virtual bool rename(
 
157
    const store::Item_t& aName,
 
158
    const store::Item_t& aNewName) = 0;
 
159
    
137
160
  virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
138
 
 
139
 
#if 0
140
 
  uint32_t hash(long timezone = 0, const XQPCollator* aCollation = 0) const;
141
 
 
142
 
  virtual store::Item* copy(store::Item* parent, const store::CopyMode&) const;
143
 
#endif
144
161
};
145
162
 
146
163
 
151
168
class SimpleJSONObject : public JSONObject
152
169
{
153
170
protected:
154
 
  struct JSONObjectPairComparator
155
 
  {
156
 
    bool operator() (const store::Item* lhs, const store::Item* rhs) const;
157
 
  };
158
 
 
159
 
  typedef std::map<store::Item*, size_t, JSONObjectPairComparator> PairMap;
160
 
  typedef PairMap::const_iterator PairMapConstIter;
161
 
  typedef PairMap::iterator PairMapIter;
162
 
 
163
 
  typedef std::vector<JSONObjectPair*>  Pairs;
164
 
  typedef Pairs::const_iterator         PairsConstIter;
165
 
  typedef Pairs::iterator               PairsIter;
166
 
 
167
 
  PairMap thePairMap;
168
 
  Pairs   thePairs;
169
 
 
170
 
  SimpleCollection* theCollection;
171
 
 
172
 
  class PairIterator : public store::Iterator
 
171
  ZSTRING_HASH_MAP(csize, Keys);
 
172
  typedef std::vector<std::pair<store::Item*, store::Item*> > Pairs;
 
173
 
 
174
  class KeyIterator : public store::Iterator
173
175
  {
174
176
    protected:
175
 
      SimpleJSONObject_t theObject;
176
 
      PairsConstIter     theIter;
 
177
      SimpleJSONObject_t    theObject;
 
178
      Pairs::iterator theIter;
177
179
 
178
180
    public:
179
 
      PairIterator(const SimpleJSONObject_t& aObject) : theObject(aObject) {}
 
181
      KeyIterator(const SimpleJSONObject_t& aObject) : theObject(aObject) {}
180
182
 
181
 
      virtual ~PairIterator();
 
183
      virtual ~KeyIterator();
182
184
 
183
185
      virtual void open();
184
186
 
189
191
      virtual void close();
190
192
  };
191
193
 
 
194
protected:
 
195
 
 
196
  Keys                 theKeys;
 
197
  Pairs                thePairs;
 
198
  store::Collection  * theCollection;
 
199
  const JSONItem     * theRoot;
 
200
 
192
201
public:
193
 
  SimpleJSONObject() : theCollection(0) {}
 
202
  SimpleJSONObject()
 
203
    :
 
204
    theKeys(64, false),
 
205
    theCollection(NULL),
 
206
    theRoot(NULL)
 
207
  {
 
208
  }
194
209
 
195
210
  virtual ~SimpleJSONObject();
196
211
 
197
 
  bool add(const JSONObjectPair_t& aPair, bool accumulate);
198
 
 
199
 
  JSONObjectPair_t remove(const store::Item_t& aName);
200
 
 
201
 
  xs_integer getSize() const { return xs_integer(thePairs.size()); }
202
 
 
203
 
  store::Iterator_t getPairs() const;
204
 
 
205
 
  store::Item* getPair(const store::Item_t& name) const;
206
 
 
207
 
  store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
208
 
 
209
 
  zstring getStringValue() const;
210
 
 
211
 
  void getStringValue2(zstring& val) const;
212
 
 
213
 
  void appendStringValue(zstring& buf) const;
214
 
 
215
 
  void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
216
 
 
217
 
  virtual SimpleCollection* getCollection() const { return theCollection; }
218
 
 
219
 
  void setCollection(SimpleCollection* collection, xs_integer pos);
 
212
  // store API
 
213
 
 
214
  virtual store::Iterator_t getObjectKeys() const;
 
215
 
 
216
  virtual store::Item_t getObjectValue(const store::Item_t& aKey) const;
 
217
 
 
218
  virtual store::Item* copy(
 
219
      store::Item* parent,
 
220
      const store::CopyMode& copymode) const;
 
221
 
 
222
  virtual zstring getStringValue() const;
 
223
 
 
224
  virtual void getStringValue2(zstring& val) const;
 
225
 
 
226
  virtual void appendStringValue(zstring& buf) const;
 
227
 
 
228
  virtual void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
 
229
 
 
230
  virtual const store::Collection* getCollection() const;
 
231
 
 
232
  // updates
 
233
  
 
234
  virtual bool add(
 
235
      const store::Item_t& aName,
 
236
      const store::Item_t& aValue,
 
237
      bool accumulate);
 
238
 
 
239
  virtual store::Item_t remove(const store::Item_t& aName);
 
240
 
 
241
  virtual store::Item_t setValue(
 
242
      const store::Item_t& aName,
 
243
      const store::Item_t& aValue);
 
244
    
 
245
  virtual bool rename(
 
246
      const store::Item_t& aName,
 
247
      const store::Item_t& aNewName);
 
248
 
 
249
  virtual void setCollection(SimpleCollection* collection, xs_integer pos);
 
250
  
 
251
  // root management
 
252
  
 
253
protected:
 
254
  friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
 
255
 
 
256
  void setRoot(const JSONItem* aRoot);
 
257
 
 
258
  const JSONItem* getRoot() { return theRoot; }
 
259
  
 
260
  // Invariant handling
 
261
protected:
 
262
  friend class SimpleJSONArray;
 
263
 
 
264
#ifndef NDEBUG
 
265
  void assertInvariant() const;
 
266
 
 
267
  bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
 
268
 
 
269
  bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
 
270
#endif
220
271
};
221
272
 
222
273
 
231
282
 
232
283
  virtual ~JSONArray() {}
233
284
 
 
285
  // store API
 
286
  
234
287
  bool isJSONArray() const { return true; }
235
288
 
236
289
  store::StoreConsts::JSONItemKind
238
291
 
239
292
  virtual store::Item*
240
293
  getType() const;
 
294
  
 
295
  virtual xs_integer getArraySize() const = 0;
 
296
 
 
297
  virtual store::Item_t getArrayValue(const xs_integer& position) const = 0;
 
298
 
 
299
  virtual store::Iterator_t getArrayValues() const = 0;
 
300
 
 
301
  // updates
241
302
 
242
303
  virtual void
243
304
  push_back(const store::Item_t& aValue) = 0;
257
318
  virtual void
258
319
  insert_after(const xs_integer& pos, const std::vector<store::Item_t>& members) = 0;
259
320
 
260
 
  virtual void
 
321
  virtual store::Item_t
261
322
  remove(const xs_integer& pos) = 0;
262
323
 
263
 
  virtual void
 
324
  virtual store::Item_t
264
325
  replace(const xs_integer& pos, const store::Item_t& value) = 0;
265
326
 
266
 
  virtual xs_integer
267
 
  getSize() const = 0;
268
 
 
269
 
  virtual const store::Item*
270
 
  operator[](const xs_integer&) const = 0;
271
 
 
272
 
  virtual SimpleCollection* getCollection() const = 0;
273
 
 
274
327
  virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
275
328
};
276
329
 
283
336
{
284
337
protected:
285
338
  typedef std::vector<store::Item*> Members;
286
 
  typedef Members::const_iterator MembersConstIter;
287
 
  typedef Members::iterator MembersIter;
288
 
 
289
 
  Members            theContent;
290
 
  SimpleCollection * theCollection;
291
339
 
292
340
  class ValuesIterator : public store::Iterator
293
341
  {
294
342
    protected:
295
343
      SimpleJSONArray_t theArray;
296
 
      MembersConstIter    theIterator;
 
344
      Members::iterator theIter;
297
345
 
298
346
    public:
299
 
      ValuesIterator(const SimpleJSONArray_t& aArray)
300
 
        :
301
 
        theArray(aArray)
302
 
      {
303
 
      }
304
 
 
305
 
      virtual ~ValuesIterator()
306
 
      {
307
 
      }
308
 
 
309
 
      virtual void open() { theIterator = theArray->theContent.begin(); }
310
 
 
311
 
      virtual bool next(store::Item_t& res)
312
 
      {
313
 
        if (theIterator == theArray->theContent.end())
314
 
        {
315
 
          return false;
316
 
        }
317
 
        else
318
 
        {
319
 
          res = *(theIterator++);
320
 
          return true;
321
 
        }
322
 
      }
323
 
 
324
 
      virtual void reset() { open(); }
325
 
 
326
 
      virtual void close() { theIterator = theArray->theContent.end(); }
 
347
      ValuesIterator(const SimpleJSONArray_t& anArray)
 
348
        : theArray(anArray) {}
 
349
 
 
350
      virtual ~ValuesIterator();
 
351
 
 
352
      virtual void open();
 
353
 
 
354
      virtual bool next(store::Item_t&);
 
355
 
 
356
      virtual void reset();
 
357
 
 
358
      virtual void close();
327
359
  };
328
360
 
329
 
  friend class ValuesIterator;
 
361
protected:
 
362
  Members                   theContent;
 
363
  const store::Collection * theCollection;
 
364
  const JSONItem          * theRoot;
330
365
 
331
366
public:
332
367
  SimpleJSONArray()
333
368
    :
334
 
    theCollection(0)
 
369
    theCollection(NULL),
 
370
    theRoot(NULL)
335
371
  {
336
372
  }
337
373
 
338
374
  virtual ~SimpleJSONArray();
339
 
 
 
375
  
 
376
  // store API
 
377
 
 
378
  xs_integer getArraySize() const;
 
379
 
 
380
  store::Item_t getArrayValue(const xs_integer& position) const;
 
381
  
 
382
  store::Iterator_t getArrayValues() const;
 
383
 
 
384
  store::Item* copy(
 
385
      store::Item* parent,
 
386
      const store::CopyMode& copymode) const;
 
387
 
 
388
  zstring getStringValue() const;
 
389
 
 
390
  void getStringValue2(zstring& val) const;
 
391
 
 
392
  void appendStringValue(zstring& buf) const;
 
393
 
 
394
  void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
 
395
 
 
396
  virtual const store::Collection* getCollection() const;
 
397
  
 
398
  // updates
 
399
  
340
400
  virtual void
341
401
  push_back(const store::Item_t& aValue);
342
402
 
355
415
  virtual void
356
416
  insert_after(const xs_integer& pos, const std::vector<store::Item_t>& members);
357
417
 
358
 
  virtual void
 
418
  virtual store::Item_t
359
419
  remove(const xs_integer& aPos);
360
420
 
361
 
  virtual void
 
421
  virtual store::Item_t
362
422
  replace(const xs_integer& aPos, const store::Item_t& value);
363
423
 
364
 
  virtual xs_integer
365
 
  getSize() const { return xs_integer(theContent.size()); }
366
 
 
367
 
  virtual const store::Item*
368
 
  operator[](const xs_integer&) const;
369
 
 
370
 
  virtual store::Iterator_t
371
 
  getMembers() const;
372
 
 
373
 
  virtual store::Item*
374
 
  getMember(const store::Item_t& position) const;
375
 
 
376
 
  store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
377
 
 
378
 
  zstring getStringValue() const;
379
 
 
380
 
  void getStringValue2(zstring& val) const;
381
 
 
382
 
  void appendStringValue(zstring& buf) const;
383
 
 
384
 
  void
385
 
  getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
386
 
 
387
 
  virtual SimpleCollection* getCollection() const { return theCollection; }
388
 
 
389
424
  void setCollection(SimpleCollection* collection, xs_integer pos);
390
425
 
391
 
protected:
392
 
  void
393
 
  add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
394
 
 
395
 
  static uint64_t
396
 
  cast(const xs_integer& i);
397
 
};
398
 
 
399
 
 
400
 
/******************************************************************************
401
 
 
402
 
*******************************************************************************/
403
 
class JSONObjectPair : public JSONItem
404
 
{
405
 
public:
406
 
  virtual ~JSONObjectPair() {}
407
 
 
408
 
  bool isJSONPair() const { return true; }
409
 
 
410
 
  virtual void
411
 
  setName(const store::Item_t& aName) = 0;
412
 
 
413
 
  virtual void
414
 
  setValue(const store::Item_t& aValue)  = 0;
415
 
 
416
 
  virtual store::Item*
417
 
  getName() const = 0;
418
 
 
419
 
  virtual store::Item*
420
 
  getValue() const = 0;
421
 
 
422
 
  store::StoreConsts::JSONItemKind
423
 
  getJSONItemKind() const { return store::StoreConsts::jsonPair; }
424
 
 
425
 
  virtual Item*
426
 
  getType() const;
427
 
};
428
 
 
429
 
 
430
 
/******************************************************************************
431
 
 
432
 
*******************************************************************************/
433
 
class SimpleJSONObjectPair : public JSONObjectPair
434
 
{
435
 
protected:
436
 
  store::Item_t          theName;
437
 
  store::Item_t          theValue;
438
 
 
439
 
public:
440
 
  SimpleJSONObjectPair() {}
441
 
 
442
 
  SimpleJSONObjectPair(const store::Item_t& aName, const store::Item_t& aValue)
443
 
    :
444
 
    theName(aName),
445
 
    theValue(aValue)
446
 
  {
447
 
  }
448
 
 
449
 
  virtual ~SimpleJSONObjectPair() {}
450
 
 
451
 
  zstring getStringValue() const { return theValue->getStringValue(); }
452
 
 
453
 
  void getStringValue2(zstring& val) const { theValue->getStringValue2(val); }
454
 
 
455
 
  void appendStringValue(zstring& buf) const { theValue->appendStringValue(buf); }
456
 
  
457
 
  void
458
 
  setName(const store::Item_t& aName) { theName = aName; }
459
 
 
460
 
  void
461
 
  setValue(const store::Item_t& aValue) { theValue = aValue; }
462
 
 
463
 
  store::Item*
464
 
  getName() const { return theName.getp(); }
465
 
 
466
 
  store::Item*
467
 
  getValue() const { return theValue.getp(); }
468
 
 
469
 
  store::Item* copy(store::Item* parent, const store::CopyMode& copymode) const;
470
 
 
471
 
  void
472
 
  getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
473
 
};
 
426
  // root management
 
427
protected:
 
428
  friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
 
429
 
 
430
  void setRoot(const JSONItem* aRoot);
 
431
 
 
432
  const JSONItem* getRoot() { return theRoot; }
 
433
  
 
434
protected:
 
435
  void add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
 
436
 
 
437
  static uint64_t cast(const xs_integer& i);
 
438
  
 
439
  // Invariant handling
 
440
protected:
 
441
  friend class SimpleJSONObject;
 
442
 
 
443
#ifndef NDEBUG
 
444
  void assertInvariant() const;
 
445
 
 
446
  bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
 
447
 
 
448
  bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
 
449
#endif
 
450
};
 
451
 
 
452
 
 
453
void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
 
454
    
 
455
#if 0 // ifndef NDEBUG
 
456
#define ASSERT_INVARIANT() assertInvariant()
 
457
#else
 
458
#define ASSERT_INVARIANT()
 
459
#endif
474
460
 
475
461
} // namespace json
476
462
} // namespace simplestore
483
469
 * End:
484
470
 */
485
471
/* vim:set et sw=2 ts=2: */
 
472