Merge lp:~zorba-coders/zorba/jsoniq-static-casts into lp:zorba

Proposed by Ghislain Fourny on 2012-08-09
Status: Merged
Approved by: Markos Zaharioudakis on 2012-08-29
Approved revision: 10983
Merged at revision: 11008
Proposed branch: lp:~zorba-coders/zorba/jsoniq-static-casts
Merge into: lp:zorba
Diff against target: 1264 lines (+400/-438)
5 files modified
src/store/naive/json_items.cpp (+183/-286)
src/store/naive/json_items.h (+105/-69)
src/store/naive/pul_primitives.cpp (+22/-4)
src/store/naive/simple_collection.cpp (+26/-57)
src/store/naive/simple_pul.cpp (+64/-22)
To merge this branch: bzr merge lp:~zorba-coders/zorba/jsoniq-static-casts
Reviewer Review Type Date Requested Status
Markos Zaharioudakis 2012-08-09 Approve on 2012-08-29
Matthias Brantner 2012-08-09 Approve on 2012-08-27
Review via email: mp+118961@code.launchpad.net

Commit message

Simplified JSON items class and fixed some Xml Node static casts to handle JSON items as well.

Description of the change

Simplified JSON items class and fixed some Xml Node static casts to handle JSON items as well.

To post a comment you must log in.
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job jsoniq-static-casts-2012-08-09T13-50-04.515Z is finished. The final status was:

All tests succeeded!

Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 3 Pending.

Markos Zaharioudakis (markos-za) wrote :

Ghislain, has this been scheduled for a release? If so, which one?

Ghislain Fourny (gislenius) wrote :

I would say Zorba 2.7. I opened a but and marked it accordingly.

The static casts are important as they could cause crashes.

Chris Hillery (ceejatec) wrote :

If it's done and working, no reason not to merge it immediately. Releases sometimes have to wait for features, but the reverse is not usually true unless the feature includes a backwards-incompatible change.

JSONItem::unfix contains a "return" statement before an assertion. Not sure if that's on purpose.

review: Approve
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job jsoniq-static-casts-2012-08-27T16-42-57.41Z is finished. The final status was:

All tests succeeded!

Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 1 Approve, 2 Pending.

Ghislain Fourny (gislenius) wrote :

Thanks Matthias, I missed this one!

Markos Zaharioudakis (markos-za) wrote :

Ghislain, it seems that the jsoniq tree id is not currently used anywhere. Do you envision that it will ever be needed somewhere? If not, let's remove it.

Also, I don't like the "fix" "unfix" names. What about addInCollection and removeFromCollection? (but this is up to you, I will approve anyway).

Ghislain Fourny (gislenius) wrote :

Thanks Markos, I renamed the functions to attachToCollection/detachFromCollection. Tree IDs are needed in Sausalito, and I thought it would make sense in Zorba too even if it is not used yet. This keeps the diff between Zorba and Sausalito small...

Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job jsoniq-static-casts-2012-08-29T09-15-57.932Z is finished. The final status was:

All tests succeeded!

Zorba Build Bot (zorba-buildbot) wrote :

Voting does not meet specified criteria. Required: Approve > 1, Disapprove < 1, Needs Fixing < 1, Pending < 1. Got: 1 Approve, 2 Pending.

review: Approve
Zorba Build Bot (zorba-buildbot) wrote :

Validation queue job jsoniq-static-casts-2012-08-29T10-14-02.99Z is finished. The final status was:

All tests succeeded!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/store/naive/json_items.cpp'
2--- src/store/naive/json_items.cpp 2012-08-16 18:31:02 +0000
3+++ src/store/naive/json_items.cpp 2012-08-29 09:14:27 +0000
4@@ -76,6 +76,124 @@
5
6 /////////////////////////////////////////////////////////////////////////////////
7 // //
8+// JSON Item //
9+// //
10+/////////////////////////////////////////////////////////////////////////////////
11+
12+/******************************************************************************
13+
14+*******************************************************************************/
15+JSONItem::~JSONItem()
16+{
17+ delete theTree;
18+}
19+
20+
21+/******************************************************************************
22+
23+*******************************************************************************/
24+void JSONItem::free()
25+{
26+ destroy();
27+}
28+
29+
30+/******************************************************************************
31+
32+*******************************************************************************/
33+void JSONItem::destroy()
34+{
35+ delete this;
36+}
37+
38+
39+/******************************************************************************
40+
41+*******************************************************************************/
42+const simplestore::Collection* JSONItem::getCollection() const
43+{
44+ if (theTree == NULL)
45+ {
46+ return NULL;
47+ }
48+ return theTree->getCollection();
49+}
50+
51+
52+/******************************************************************************
53+ Should only to be called if item is in a collection.
54+*******************************************************************************/
55+const TreeId& JSONItem::getTreeId() const
56+{
57+ ZORBA_ASSERT(theTree);
58+ return theTree->getTreeId();
59+}
60+
61+
62+/******************************************************************************
63+ Should only to be called if item is in a collection.
64+*******************************************************************************/
65+JSONItem* JSONItem::getRoot() const
66+{
67+ ZORBA_ASSERT(theTree);
68+ return theTree->getRoot();
69+}
70+
71+
72+/*******************************************************************************
73+
74+********************************************************************************/
75+void JSONItem::attachToCollection(Collection* aCollection, const TreeId& aTreeId)
76+{
77+ ASSERT_INVARIANT();
78+
79+ assert(aCollection);
80+
81+ // Attach
82+ assert(getTree() == NULL);
83+ setTree(new JSONTree());
84+ getTree()->setRoot(this);
85+ getTree()->setCollection(aCollection);
86+ getTree()->setTreeId(aTreeId);
87+
88+ ASSERT_INVARIANT();
89+}
90+
91+/*******************************************************************************
92+
93+********************************************************************************/
94+void JSONItem::detachFromCollection()
95+{
96+ ASSERT_INVARIANT();
97+
98+ JSONTree* lTree = getTree();
99+ // Detach
100+ assert(lTree);
101+ delete lTree;
102+ setTree(NULL);
103+
104+ ASSERT_INVARIANT();
105+}
106+
107+#ifndef NDEBUG
108+/******************************************************************************
109+
110+*******************************************************************************/
111+void JSONItem::assertInvariant() const
112+{
113+ if (theTree != NULL)
114+ {
115+ assert(theTree->getCollection() != NULL);
116+ assert(theTree->getRoot() != NULL);
117+ assert(isThisTreeOfAllDescendants(theTree));
118+ assert(theTree->getRoot()->isThisJSONItemInDescendance(this));
119+ }
120+}
121+#endif
122+
123+
124+/////////////////////////////////////////////////////////////////////////////////
125+// //
126 // Object //
127 // //
128 /////////////////////////////////////////////////////////////////////////////////
129@@ -93,36 +211,6 @@
130 /******************************************************************************
131
132 *******************************************************************************/
133-void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot)
134-{
135- if (aJSONItem->isJSONObject())
136- {
137- assert(dynamic_cast<SimpleJSONObject*>(aJSONItem));
138- SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(aJSONItem);
139-
140- // Only attach or detach allowed - no direct reattach.
141- assert(aRoot == NULL || lObject->theRoot == NULL);
142- lObject->setRoot(aRoot);
143- }
144- else if (aJSONItem->isJSONArray())
145- {
146- assert(dynamic_cast<SimpleJSONArray*>(aJSONItem));
147- SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(aJSONItem);
148-
149- // Only attach or detach allowed - no direct reattach.
150- assert(aRoot == NULL || lArray->theRoot == NULL);
151- lArray->setRoot(aRoot);
152- }
153- else
154- {
155- assert(false);
156- }
157-}
158-
159-
160-/******************************************************************************
161-
162-*******************************************************************************/
163 SimpleJSONObject::~SimpleJSONObject()
164 {
165 ASSERT_INVARIANT();
166@@ -134,7 +222,9 @@
167 store::Item* lChild = lIter->second;
168 if (getCollection() != NULL && lChild->isJSONItem())
169 {
170- setJSONRoot(lChild, NULL);
171+ assert(dynamic_cast<JSONItem*>(lChild));
172+ JSONItem* lJSONItem = static_cast<JSONItem*>(lChild);
173+ lJSONItem->setTree(NULL);
174 }
175 lName->removeReference();
176 lChild->removeReference();
177@@ -212,7 +302,9 @@
178
179 if (getCollection() != NULL && aValue->isJSONItem())
180 {
181- setJSONRoot(lValue, theRoot);
182+ assert(dynamic_cast<JSONItem*>(aValue.getp()));
183+ JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
184+ lJSONItem->setTree(getTree());
185 }
186
187 csize lPosition = thePairs.size();
188@@ -244,7 +336,7 @@
189
190 if (getCollection() != NULL)
191 {
192- setJSONRoot(array.getp(), theRoot);
193+ array->setTree(getTree());
194 }
195
196 lValue->removeReference();
197@@ -277,7 +369,6 @@
198 return 0;
199 }
200
201-#if 1
202 store::Item* lKey;
203
204 lKey = thePairs[lPosition].first;
205@@ -285,7 +376,9 @@
206
207 if (getCollection() != NULL && lValue->isJSONItem())
208 {
209- setJSONRoot(lValue.getp(), NULL);
210+ assert(dynamic_cast<JSONItem*>(lValue.getp()));
211+ JSONItem* lJSONItem = static_cast<JSONItem*>(lValue.getp());
212+ lJSONItem->setTree(NULL);
213 }
214
215 lKey->removeReference();
216@@ -308,49 +401,6 @@
217 }
218 }
219
220-#else
221-
222- Pairs::iterator lIterator;
223- csize lIteratorPosition;
224- for (lIterator = thePairs.begin(), lIteratorPosition = 0;
225- lIterator != thePairs.end();
226- ++lIterator, ++lIteratorPosition)
227- {
228- if (lIteratorPosition < lPosition)
229- {
230- continue;
231- }
232-
233- // This is the position we are looking for.
234- else if (lIteratorPosition == lPosition)
235- {
236- // Preparing the returned item.
237- assert(lIterator->first->getStringValue() == lName);
238-
239- lValue = lIterator->second;
240-
241- if (getCollection() != NULL && lValue->isJSONItem())
242- {
243- setJSONRoot(lValue.getp(), NULL);
244- }
245-
246- // Erasing the corresponding entries.
247- lIterator->first->removeReference();
248- lIterator->second->removeReference();
249- lIterator = thePairs.erase(lIterator);
250- theKeys.erase(lName);
251- }
252-
253- // Rebuilding the key positions after this removed pair.
254- assert(lIterator->first != NULL);
255- assert(lIterator->second != NULL);
256- Keys::iterator lKeyIterator = theKeys.find(lIterator->first->getStringValue());
257- assert(lKeyIterator != theKeys.end());
258- assert(lKeyIterator.getValue() == lPosition + 1);
259- lKeyIterator.setValue(lPosition);
260- }
261-#endif
262-
263 ASSERT_INVARIANT();
264 return lValue;
265 }
266@@ -381,12 +431,16 @@
267 {
268 if (lOldValue->isJSONItem())
269 {
270- setJSONRoot(lOldValue.getp(), NULL);
271+ assert(dynamic_cast<JSONItem*>(lOldValue.getp()));
272+ JSONItem* lJSONItem = static_cast<JSONItem*>(lOldValue.getp());
273+ lJSONItem->setTree(NULL);
274 }
275
276 if (aValue->isJSONItem())
277 {
278- setJSONRoot(aValue.getp(), theRoot);
279+ assert(dynamic_cast<JSONItem*>(aValue.getp()));
280+ JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
281+ lJSONItem->setTree(getTree());
282 }
283 }
284
285@@ -441,75 +495,21 @@
286 /******************************************************************************
287
288 *******************************************************************************/
289-void SimpleJSONObject::setRoot(const JSONItem* aRoot)
290+void SimpleJSONObject::setTree(JSONTree* aTree)
291 {
292- theRoot = aRoot;
293+ theTree = aTree;
294
295 for (Pairs::iterator lIter = thePairs.begin();
296 lIter != thePairs.end();
297 ++lIter)
298 {
299 store::Item* lValue = lIter->second;
300- if (lValue->isJSONObject())
301- {
302- assert(dynamic_cast<SimpleJSONObject*>(lValue));
303- SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(lValue);
304-
305- lObject->setRoot(aRoot);
306- }
307- else if (lValue->isJSONArray())
308- {
309- assert(dynamic_cast<SimpleJSONArray*>(lValue));
310- SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(lValue);
311-
312- lArray->setRoot(aRoot);
313- }
314- }
315-}
316-
317-
318-/*******************************************************************************
319-
320-********************************************************************************/
321-void SimpleJSONObject::setCollection(SimpleCollection* collection, xs_integer /*pos*/)
322-{
323- ASSERT_INVARIANT();
324- // Ensures one either detaches or attaches.
325- assert(collection == NULL || theCollection == NULL);
326-
327- theCollection = collection;
328-
329- if (theCollection != NULL)
330- {
331- // Attach
332- setRoot(this);
333- }
334- else
335- {
336- // Detach
337- setRoot(NULL);
338- }
339-
340- ASSERT_INVARIANT();
341-}
342-
343-
344-/******************************************************************************
345-
346-*******************************************************************************/
347-const store::Collection* SimpleJSONObject::getCollection() const
348-{
349- if (theRoot == this)
350- {
351- return theCollection;
352- }
353- else if (theRoot != NULL)
354- {
355- return theRoot->getCollection();
356- }
357- else
358- {
359- return NULL;
360+ if (lValue->isJSONItem())
361+ {
362+ assert(dynamic_cast<JSONItem*>(lValue));
363+ JSONItem* lJSONItem = static_cast<JSONItem*>(lValue);
364+ lJSONItem->setTree(aTree);
365+ }
366 }
367 }
368
369@@ -590,34 +590,7 @@
370 *******************************************************************************/
371 void SimpleJSONObject::assertInvariant() const
372 {
373- // Note: only root objects may point to a collection, so if theCollection ==
374- // NULL, it doesn't mean that the object does not belong to a collection.
375- assert(theCollection == NULL || theRoot == this);
376-
377- if (theRoot != NULL)
378- {
379- const store::Collection* lCollection = getCollection();
380- assert(lCollection != NULL);
381-
382- const SimpleJSONObject* lObject =
383- dynamic_cast<const SimpleJSONObject*>(theRoot);
384- const SimpleJSONArray* lArray =
385- dynamic_cast<const SimpleJSONArray*>(theRoot);
386-
387- assert(lObject != NULL || lArray != NULL);
388-
389- if (lObject != NULL)
390- {
391- assert(lObject->isThisRootOfAllDescendants(theRoot));
392- assert(lObject->isThisJSONItemInDescendance(this));
393- }
394- else
395- {
396- assert(lArray->isThisRootOfAllDescendants(theRoot));
397- assert(lArray->isThisJSONItemInDescendance(this));
398- }
399- }
400-
401+ JSONItem::assertInvariant();
402 assert(theKeys.size() == thePairs.size());
403
404 for(Keys::iterator lIter = theKeys.begin();
405@@ -637,9 +610,9 @@
406 /******************************************************************************
407
408 *******************************************************************************/
409-bool SimpleJSONObject::isThisRootOfAllDescendants(const store::Item* aRoot) const
410+bool SimpleJSONObject::isThisTreeOfAllDescendants(const JSONTree* aTree) const
411 {
412- if (theRoot != aRoot)
413+ if (theTree != aTree)
414 {
415 return false;
416 }
417@@ -649,16 +622,8 @@
418 ++lIter)
419 {
420 store::Item* lValue = lIter->second;
421- const SimpleJSONObject* lObject =
422- dynamic_cast<const SimpleJSONObject*>(lValue);
423- const SimpleJSONArray* lArray =
424- dynamic_cast<const SimpleJSONArray*>(lValue);
425-
426- if (lObject != NULL && (!lObject->isThisRootOfAllDescendants(aRoot)))
427- {
428- return false;
429- }
430- else if (lArray != NULL && (!lArray->isThisRootOfAllDescendants(aRoot)))
431+ const JSONItem* lJSONItem = dynamic_cast<const JSONItem*>(lValue);
432+ if (lJSONItem != NULL && (!lJSONItem->isThisTreeOfAllDescendants(aTree)))
433 {
434 return false;
435 }
436@@ -783,7 +748,9 @@
437 {
438 if (getCollection() != NULL && (*lIter)->isJSONItem())
439 {
440- setJSONRoot(*lIter, NULL);
441+ assert(dynamic_cast<JSONItem*>(*lIter));
442+ JSONItem* lJSONItem = static_cast<JSONItem*>(*lIter);
443+ lJSONItem->setTree(NULL);
444 }
445 (*lIter)->removeReference();
446 }
447@@ -799,7 +766,9 @@
448
449 if (getCollection() != NULL && aValue->isJSONItem())
450 {
451- setJSONRoot(aValue.getp(), theRoot);
452+ assert(dynamic_cast<JSONItem*>(aValue.getp()));
453+ JSONItem* lJSONItem = static_cast<JSONItem*>(aValue.getp());
454+ lJSONItem->setTree(getTree());
455 }
456
457 aValue->addReference();
458@@ -844,7 +813,9 @@
459
460 if (getCollection() != NULL && member->isJSONItem())
461 {
462- setJSONRoot(member.getp(), theRoot);
463+ assert(dynamic_cast<JSONItem*>(member.getp()));
464+ JSONItem* lJSONItem = static_cast<JSONItem*>(member.getp());
465+ lJSONItem->setTree(getTree());
466 }
467
468 member->addReference();
469@@ -903,7 +874,9 @@
470
471 if (getCollection() != NULL && lItem->isJSONItem())
472 {
473- setJSONRoot(lItem, theRoot);
474+ assert(dynamic_cast<JSONItem*>(lItem));
475+ JSONItem* lJSONItem = static_cast<JSONItem*>(lItem);
476+ lJSONItem->setTree(getTree());
477 }
478
479 lItem->addReference();
480@@ -924,7 +897,9 @@
481
482 if (getCollection() != NULL && lItem->isJSONItem())
483 {
484- setJSONRoot(lItem.getp(), NULL);
485+ assert(dynamic_cast<JSONItem*>(lItem.getp()));
486+ JSONItem* lJSONItem = static_cast<JSONItem*>(lItem.getp());
487+ lJSONItem->setTree(NULL);
488 }
489
490 lItem->removeReference();
491@@ -948,14 +923,18 @@
492
493 if (getCollection() != NULL && lItem->isJSONItem())
494 {
495- setJSONRoot(lItem.getp(), NULL);
496+ assert(dynamic_cast<JSONItem*>(lItem.getp()));
497+ JSONItem* lJSONItem = static_cast<JSONItem*>(lItem.getp());
498+ lJSONItem->setTree(NULL);
499 }
500
501 uint64_t pos = cast(aPos) - 1;
502
503 if (getCollection() != NULL && value->isJSONItem())
504 {
505- setJSONRoot(value.getp(), theRoot);
506+ assert(dynamic_cast<JSONItem*>(value.getp()));
507+ JSONItem* lJSONItem = static_cast<JSONItem*>(value.getp());
508+ lJSONItem->setTree(getTree());
509 }
510
511 theContent[pos]->removeReference();
512@@ -970,27 +949,20 @@
513 /******************************************************************************
514
515 *******************************************************************************/
516-void SimpleJSONArray::setRoot(const JSONItem* aRoot)
517+void SimpleJSONArray::setTree(JSONTree* aTree)
518 {
519- theRoot = aRoot;
520+ theTree = aTree;
521
522 for (Members::const_iterator lIter = theContent.begin();
523 lIter != theContent.end();
524 ++lIter)
525 {
526- if ((*lIter)->isJSONObject())
527- {
528- assert(dynamic_cast<SimpleJSONObject*>(*lIter));
529- SimpleJSONObject* lObject = static_cast<SimpleJSONObject*>(*lIter);
530-
531- lObject->setRoot(aRoot);
532- }
533- else if ((*lIter)->isJSONArray())
534- {
535- assert(dynamic_cast<SimpleJSONArray*>(*lIter));
536- SimpleJSONArray* lArray = static_cast<SimpleJSONArray*>(*lIter);
537-
538- lArray->setRoot(aRoot);
539+ store::Item* lValue = *lIter;
540+ if (lValue->isJSONItem())
541+ {
542+ assert(dynamic_cast<JSONItem*>(lValue));
543+ JSONItem* lJSONItem = static_cast<JSONItem*>(lValue);
544+ lJSONItem->setTree(aTree);
545 }
546 }
547 }
548@@ -1137,100 +1109,25 @@
549 }
550
551
552-/*******************************************************************************
553-
554-********************************************************************************/
555-void SimpleJSONArray::setCollection(SimpleCollection* collection, xs_integer /*pos*/)
556-{
557- ASSERT_INVARIANT();
558- // Ensures one either detaches or attaches.
559- ZORBA_ASSERT(collection == NULL || theCollection == NULL);
560-
561- theCollection = collection;
562-
563- if (theCollection != NULL)
564- {
565- // Attach
566- setRoot(this);
567- }
568- else
569- {
570- // Detach
571- setRoot(NULL);
572- }
573-
574- ASSERT_INVARIANT();
575-}
576-
577-
578-/******************************************************************************
579-
580-*******************************************************************************/
581-const store::Collection* SimpleJSONArray::getCollection() const
582-{
583- if (theRoot == this)
584- {
585- return theCollection;
586- }
587- else if (theRoot != NULL)
588- {
589- return theRoot->getCollection();
590- }
591- else
592- {
593- return NULL;
594- }
595-}
596-
597-
598 #ifndef NDEBUG
599
600 /******************************************************************************
601
602 *******************************************************************************/
603-void SimpleJSONArray::assertInvariant() const
604-{
605- assert(theCollection == NULL || theRoot == this);
606- if (theRoot != NULL)
607- {
608- const store::Collection* lCollection = getCollection();
609- assert(lCollection != NULL);
610- const SimpleJSONObject* lObject = dynamic_cast<const SimpleJSONObject*>(theRoot);
611- const SimpleJSONArray* lArray = dynamic_cast<const SimpleJSONArray*>(theRoot);
612- assert(lObject != NULL || lArray != NULL);
613- if (lObject != NULL) {
614- assert(lObject->isThisRootOfAllDescendants(theRoot));
615- assert(lObject->isThisJSONItemInDescendance(this));
616- } else {
617- assert(lArray->isThisRootOfAllDescendants(theRoot));
618- assert(lArray->isThisJSONItemInDescendance(this));
619- }
620- }
621-}
622-
623-
624-/******************************************************************************
625-
626-*******************************************************************************/
627-bool SimpleJSONArray::isThisRootOfAllDescendants(const store::Item* aRoot) const
628-{
629- if(theRoot != aRoot)
630+bool SimpleJSONArray::isThisTreeOfAllDescendants(const JSONTree* aTree) const
631+{
632+ if (getTree() != aTree)
633 {
634 return false;
635 }
636+
637 for (Members::const_iterator lIter = theContent.begin();
638 lIter != theContent.end();
639 ++lIter)
640 {
641- const SimpleJSONObject* lObject =
642- dynamic_cast<const SimpleJSONObject*>(*lIter);
643- const SimpleJSONArray* lArray =
644- dynamic_cast<const SimpleJSONArray*>(*lIter);
645- if (lObject != NULL && (!lObject->isThisRootOfAllDescendants(aRoot)))
646- {
647- return false;
648- }
649- else if (lArray != NULL && (!lArray->isThisRootOfAllDescendants(aRoot)))
650+ store::Item* lValue = (*lIter);
651+ const JSONItem* lJSONItem = dynamic_cast<const JSONItem*>(lValue);
652+ if (lJSONItem != NULL && (!lJSONItem->isThisTreeOfAllDescendants(aTree)))
653 {
654 return false;
655 }
656
657=== modified file 'src/store/naive/json_items.h'
658--- src/store/naive/json_items.h 2012-08-16 18:31:02 +0000
659+++ src/store/naive/json_items.h 2012-08-29 09:14:27 +0000
660@@ -87,18 +87,89 @@
661
662 *******************************************************************************/
663
664+class JSONTree
665+{
666+private:
667+ simplestore::Collection * theCollection;
668+ TreeId theId;
669+ JSONItem * theRoot;
670+
671+public:
672+ JSONTree() : theCollection(NULL), theId(), theRoot(NULL)
673+ {}
674+
675+ simplestore::Collection* getCollection() const
676+ {
677+ return theCollection;
678+ }
679+
680+ void setCollection(simplestore::Collection* aCollection)
681+ {
682+ theCollection = aCollection;
683+ }
684+
685+ const TreeId& getTreeId() const
686+ {
687+ return theId;
688+ }
689+
690+ void setTreeId(const TreeId& aId)
691+ {
692+ theId = aId;
693+ }
694+
695+ JSONItem* getRoot() const
696+ {
697+ return theRoot;
698+ }
699+
700+ void setRoot(JSONItem* aRoot)
701+ {
702+ theRoot = aRoot;
703+ }
704+};
705+
706+
707+/******************************************************************************
708+
709+*******************************************************************************/
710+
711 class JSONItem : public store::Item
712 {
713 protected:
714 SYNC_CODE(mutable RCLock theRCLock;)
715
716+ JSONTree * theTree;
717+
718 public:
719 SYNC_CODE(RCLock* getRCLock() const { return &theRCLock; })
720
721- JSONItem() : store::Item(JSONIQ) {}
722-
723- virtual ~JSONItem() {}
724-
725+ JSONItem() : store::Item(JSONIQ), theTree(NULL) {}
726+
727+ virtual ~JSONItem();
728+
729+ virtual void free();
730+
731+ virtual void destroy();
732+
733+ const simplestore::Collection* getCollection() const;
734+
735+ virtual void setTree(JSONTree* aTree) = 0;
736+
737+ JSONTree* getTree() const
738+ {
739+ return theTree;
740+ }
741+
742+ // These two functions are only to be called if in a collection.
743+ const TreeId& getTreeId() const;
744+
745+ JSONItem* getRoot() const;
746+
747+ void attachToCollection(Collection* aCollection, const TreeId& aTreeId);
748+
749+ void detachFromCollection();
750+
751 // store API
752
753 virtual bool equals(
754@@ -108,11 +179,15 @@
755 {
756 return this == other;
757 }
758-
759- // store methods
760- // store methods
761-
762- virtual const JSONItem* getRoot() = 0;
763+
764+public:
765+#ifndef NDEBUG
766+ virtual void assertInvariant() const;
767+
768+ virtual bool isThisTreeOfAllDescendants(const JSONTree* aTree) const = 0;
769+
770+ virtual bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const = 0;
771+#endif
772 };
773
774
775@@ -156,8 +231,6 @@
776 virtual bool rename(
777 const store::Item_t& aName,
778 const store::Item_t& aNewName) = 0;
779-
780- virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
781 };
782
783
784@@ -174,8 +247,8 @@
785 class KeyIterator : public store::Iterator
786 {
787 protected:
788- SimpleJSONObject_t theObject;
789- Pairs::iterator theIter;
790+ SimpleJSONObject_t theObject;
791+ Pairs::iterator theIter;
792
793 public:
794 KeyIterator(const SimpleJSONObject_t& aObject) : theObject(aObject) {}
795@@ -191,19 +264,14 @@
796 virtual void close();
797 };
798
799-protected:
800-
801- Keys theKeys;
802- Pairs thePairs;
803- store::Collection * theCollection;
804- const JSONItem * theRoot;
805+private:
806+ Keys theKeys;
807+ Pairs thePairs;
808
809 public:
810 SimpleJSONObject()
811 :
812- theKeys(64, false),
813- theCollection(NULL),
814- theRoot(NULL)
815+ theKeys(64, false)
816 {
817 }
818
819@@ -227,8 +295,6 @@
820
821 virtual void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
822
823- virtual const store::Collection* getCollection() const;
824-
825 // updates
826
827 virtual bool add(
828@@ -246,25 +312,17 @@
829 const store::Item_t& aName,
830 const store::Item_t& aNewName);
831
832- virtual void setCollection(SimpleCollection* collection, xs_integer pos);
833-
834 // root management
835
836 protected:
837- friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
838-
839- void setRoot(const JSONItem* aRoot);
840-
841- const JSONItem* getRoot() { return theRoot; }
842-
843+ void setTree(JSONTree* aTree);
844+
845 // Invariant handling
846-protected:
847- friend class SimpleJSONArray;
848-
849+public:
850 #ifndef NDEBUG
851 void assertInvariant() const;
852-
853- bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
854+
855+ bool isThisTreeOfAllDescendants(const JSONTree* aTree) const;
856
857 bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
858 #endif
859@@ -323,8 +381,6 @@
860
861 virtual store::Item_t
862 replace(const xs_integer& pos, const store::Item_t& value) = 0;
863-
864- virtual void setCollection(SimpleCollection* collection, xs_integer pos) = 0;
865 };
866
867
868@@ -358,18 +414,12 @@
869 virtual void close();
870 };
871
872-protected:
873- Members theContent;
874- const store::Collection * theCollection;
875- const JSONItem * theRoot;
876+private:
877+ Members theContent;
878
879 public:
880 SimpleJSONArray()
881- :
882- theCollection(NULL),
883- theRoot(NULL)
884- {
885- }
886+ {}
887
888 virtual ~SimpleJSONArray();
889
890@@ -393,8 +443,6 @@
891
892 void getTypedValue(store::Item_t& val, store::Iterator_t& iter) const;
893
894- virtual const store::Collection* getCollection() const;
895-
896 // updates
897
898 virtual void
899@@ -421,38 +469,26 @@
900 virtual store::Item_t
901 replace(const xs_integer& aPos, const store::Item_t& value);
902
903- void setCollection(SimpleCollection* collection, xs_integer pos);
904-
905 // root management
906-protected:
907- friend void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
908-
909- void setRoot(const JSONItem* aRoot);
910-
911- const JSONItem* getRoot() { return theRoot; }
912-
913+public:
914+ void setTree(JSONTree* aTree);
915+
916 protected:
917 void add(uint64_t pos, const std::vector<store::Item_t>& aNewMembers);
918
919 static uint64_t cast(const xs_integer& i);
920-
921+
922 // Invariant handling
923-protected:
924- friend class SimpleJSONObject;
925-
926+public:
927 #ifndef NDEBUG
928- void assertInvariant() const;
929-
930- bool isThisRootOfAllDescendants(const store::Item* aRoot) const;
931+ bool isThisTreeOfAllDescendants(const JSONTree* aTree) const;
932
933 bool isThisJSONItemInDescendance(const store::Item* aJSONItem) const;
934 #endif
935 };
936
937
938-void setJSONRoot(store::Item* aJSONItem, const JSONItem* aRoot);
939-
940-#if 0 // ifndef NDEBUG
941+#ifndef NDEBUG
942 #define ASSERT_INVARIANT() assertInvariant()
943 #else
944 #define ASSERT_INVARIANT()
945
946=== modified file 'src/store/naive/pul_primitives.cpp'
947--- src/store/naive/pul_primitives.cpp 2012-08-16 18:31:02 +0000
948+++ src/store/naive/pul_primitives.cpp 2012-08-29 09:14:27 +0000
949@@ -164,8 +164,11 @@
950 theParent->deleteChild(*this);
951 }
952
953- static_cast<zorba::simplestore::XmlNode*>(theTarget.getp())
954+ if (theTarget->isNode())
955+ {
956+ static_cast<zorba::simplestore::XmlNode*>(theTarget.getp())
957 ->unregisterReferencesToDeletedSubtree();
958+ }
959 }
960
961
962@@ -993,9 +996,24 @@
963
964 for (uint64_t i = 0; i < size; ++i)
965 {
966- XmlNode* root = static_cast<XmlNode*>(collection->nodeAt(xs_integer(i)).getp());
967- XmlTree* tree = root->getTree();
968- if (tree->getRefCount() > 1)
969+ long lRefCount = 0;
970+ store::Item* lItem = collection->nodeAt(xs_integer(i)).getp();
971+ if (lItem->isNode())
972+ {
973+ assert(dynamic_cast<XmlNode*>(lItem));
974+ XmlNode* lNode = static_cast<XmlNode*>(lItem);
975+ lRefCount = lNode->getTree()->getRefCount();
976+#ifdef ZORBA_WITH_JSON
977+ }
978+ else if (lItem->isJSONItem())
979+ {
980+ assert(dynamic_cast<json::JSONItem*>(lItem));
981+ json::JSONItem* lJSONItem = static_cast<json::JSONItem*>(lItem);
982+ lRefCount = lJSONItem->getRefCount();
983+#endif
984+ }
985+
986+ if (lRefCount > 1)
987 {
988 RAISE_ERROR(zerr::ZDDY0015_COLLECTION_BAD_DESTROY_NODES, theLoc,
989 ERROR_PARAMS(collection->getName()->getStringValue()));
990
991=== modified file 'src/store/naive/simple_collection.cpp'
992--- src/store/naive/simple_collection.cpp 2012-08-16 18:31:02 +0000
993+++ src/store/naive/simple_collection.cpp 2012-08-29 09:14:27 +0000
994@@ -96,8 +96,7 @@
995 {
996 XmlNode* node = NULL;
997 #ifdef ZORBA_WITH_JSON
998- json::SimpleJSONArray* array = NULL;
999- json::SimpleJSONObject* object = NULL;
1000+ json::JSONItem* lJSONItem = NULL;
1001 #endif
1002
1003 if (item->isNode())
1004@@ -111,13 +110,9 @@
1005 }
1006 }
1007 #ifdef ZORBA_WITH_JSON
1008- else if (item->isJSONObject())
1009- {
1010- object = static_cast<json::SimpleJSONObject*>(item);
1011- }
1012- else if (item->isJSONArray())
1013- {
1014- array = static_cast<json::SimpleJSONArray*>(item);
1015+ else if (item->isJSONItem())
1016+ {
1017+ lJSONItem = static_cast<json::JSONItem*>(item);
1018 }
1019 else
1020 {
1021@@ -154,10 +149,8 @@
1022 }
1023
1024 #ifdef ZORBA_WITH_JSON
1025- if (object)
1026- object->setCollection(this, pos);
1027- else if (array)
1028- array->setCollection(this, pos);
1029+ if (lJSONItem)
1030+ lJSONItem->attachToCollection(this, createTreeId());
1031 else
1032 #endif
1033 node->setCollection(this, pos);
1034@@ -202,8 +195,7 @@
1035
1036 XmlNode* node = NULL;
1037 #ifdef ZORBA_WITH_JSON
1038- json::SimpleJSONArray* array = NULL;
1039- json::SimpleJSONObject* object = NULL;
1040+ json::JSONItem* lJSONItem = NULL;
1041 #endif
1042
1043 if (item->isNode())
1044@@ -217,13 +209,9 @@
1045 }
1046 }
1047 #ifdef ZORBA_WITH_JSON
1048- else if (item->isJSONObject())
1049- {
1050- object = static_cast<json::SimpleJSONObject*>(item);
1051- }
1052- else if (item->isJSONArray())
1053- {
1054- array = static_cast<json::SimpleJSONArray*>(item);
1055+ else if (item->isJSONItem())
1056+ {
1057+ lJSONItem = static_cast<json::JSONItem*>(item);
1058 }
1059 else
1060 {
1061@@ -248,10 +236,8 @@
1062 pos = targetPos + i;
1063
1064 #ifdef ZORBA_WITH_JSON
1065- if (object)
1066- object->setCollection(this, pos);
1067- else if (array)
1068- array->setCollection(this, pos);
1069+ if (lJSONItem)
1070+ lJSONItem->attachToCollection(this, createTreeId());
1071 else
1072 #endif
1073 node->setCollection(this, pos);
1074@@ -289,8 +275,7 @@
1075 {
1076 XmlNode* node = NULL;
1077 #ifdef ZORBA_WITH_JSON
1078- json::SimpleJSONArray* array = NULL;
1079- json::SimpleJSONObject* object = NULL;
1080+ json::JSONItem* lJSONItem = NULL;
1081 #endif
1082
1083 if (item->isNode())
1084@@ -298,13 +283,9 @@
1085 node = static_cast<XmlNode*>(item);
1086 }
1087 #ifdef ZORBA_WITH_JSON
1088- else if (item->isJSONObject())
1089- {
1090- object = static_cast<json::SimpleJSONObject*>(item);
1091- }
1092- else if (item->isJSONArray())
1093- {
1094- array = static_cast<json::SimpleJSONArray*>(item);
1095+ else if (item->isJSONItem())
1096+ {
1097+ lJSONItem = static_cast<json::JSONItem*>(item);
1098 }
1099 else
1100 {
1101@@ -330,10 +311,8 @@
1102 xs_integer const &zero = xs_integer::zero();
1103
1104 #ifdef ZORBA_WITH_JSON
1105- if (object)
1106- object->setCollection(NULL, zero);
1107- else if (array)
1108- array->setCollection(NULL, zero);
1109+ if (lJSONItem)
1110+ lJSONItem->detachFromCollection();
1111 else
1112 #endif
1113 node->setCollection(NULL, zero);
1114@@ -378,15 +357,10 @@
1115 node->setCollection(NULL, zero);
1116 }
1117 #ifdef ZORBA_WITH_JSON
1118- else if (item->isJSONObject())
1119- {
1120- json::SimpleJSONObject* object = static_cast<json::SimpleJSONObject*>(item);
1121- object->setCollection(NULL, zero);
1122- }
1123- else if (item->isJSONArray())
1124- {
1125- json::SimpleJSONArray* array = static_cast<json::SimpleJSONArray*>(item);
1126- array->setCollection(NULL, zero);
1127+ else if (item->isJSONItem())
1128+ {
1129+ json::JSONItem* lJSONItem = static_cast<json::JSONItem*>(item);
1130+ lJSONItem->detachFromCollection();
1131 }
1132 #endif
1133 else
1134@@ -439,15 +413,10 @@
1135 node->setCollection(NULL, zero);
1136 }
1137 #ifdef ZORBA_WITH_JSON
1138- else if (item->isJSONObject())
1139- {
1140- json::SimpleJSONObject* object = static_cast<json::SimpleJSONObject*>(item);
1141- object->setCollection(NULL, zero);
1142- }
1143- else if (item->isJSONArray())
1144- {
1145- json::SimpleJSONArray* array = static_cast<json::SimpleJSONArray*>(item);
1146- array->setCollection(NULL, zero);
1147+ else if (item->isJSONItem())
1148+ {
1149+ json::JSONItem* lJSONItem = static_cast<json::JSONItem*>(item);
1150+ lJSONItem->detachFromCollection();
1151 }
1152 #endif
1153 else
1154
1155=== modified file 'src/store/naive/simple_pul.cpp'
1156--- src/store/naive/simple_pul.cpp 2012-08-16 18:31:02 +0000
1157+++ src/store/naive/simple_pul.cpp 2012-08-29 09:14:27 +0000
1158@@ -176,18 +176,31 @@
1159 assert(target->isNode());
1160 #endif
1161
1162- const XmlNode* n = static_cast<const XmlNode*>(target);
1163-
1164- const store::Collection* collection = n->getCollection();
1165-
1166- if (collection != NULL)
1167- {
1168- collName = static_cast<const QNameItem*>(collection->getName())->getNormalized();
1169+ const store::Collection* lCollection;
1170+
1171+ if (target->isNode())
1172+ {
1173+ assert(dynamic_cast<const XmlNode*>(target));
1174+ const XmlNode* lNode = static_cast<const XmlNode*>(target);
1175+ lCollection = lNode->getCollection();
1176+#ifdef ZORBA_WITH_JSON
1177+ }
1178+ else if (target->isJSONItem())
1179+ {
1180+ assert(dynamic_cast<const json::JSONItem*>(target));
1181+ const json::JSONItem* lJSONItem = static_cast<const json::JSONItem*>(target);
1182+ lCollection = lJSONItem->getCollection();
1183+#endif
1184+ }
1185+
1186+ if (lCollection != NULL)
1187+ {
1188+ collName = static_cast<const QNameItem*>(lCollection->getName())->getNormalized();
1189
1190 if (collName == theLastCollection)
1191 return theLastPul;
1192
1193- return getCollectionPulByName(collName, collection->isDynamic());
1194+ return getCollectionPulByName(collName, lCollection->isDynamic());
1195 }
1196 else if (theNoCollectionPul != NULL)
1197 {
1198@@ -2230,23 +2243,52 @@
1199 NodeToUpdatesMap::iterator it = pul->theNodeToUpdatesMap.begin();
1200 NodeToUpdatesMap::iterator end = pul->theNodeToUpdatesMap.end();
1201
1202+ bool found = false;
1203+
1204 for (; it != end; ++it)
1205 {
1206- const XmlNode* targetNode = static_cast<XmlNode*>((*it).first);
1207-
1208- bool found = false;
1209-
1210- for (csize i = 0; i < numRoots; i++)
1211- {
1212- XmlNode* rootNode = static_cast<XmlNode*>(rootNodes[i]);
1213+ zorba::store::Item* lItem = (*it).first;
1214+ if (lItem->isNode())
1215+ {
1216+ assert(dynamic_cast<const XmlNode*>(lItem));
1217+ const XmlNode* lNode = static_cast<const XmlNode*>(lItem);
1218+ for (csize i = 0; i < numRoots; i++)
1219+ {
1220+ if (rootNodes[i]->isNode())
1221+ {
1222+ assert(dynamic_cast<const XmlNode*>(rootNodes[i]));
1223+ XmlNode* lRootNode = static_cast<XmlNode*>(rootNodes[i]);
1224+
1225+ if (lNode->getTree() == lRootNode->getTree())
1226+ {
1227+ found = true;
1228+ break;
1229+ }
1230+ }
1231+ }
1232+#ifdef ZORBA_WITH_JSON
1233+ }
1234+ else if (lItem->isJSONItem())
1235+ {
1236+ assert(dynamic_cast<const json::JSONItem*>(lItem));
1237+ const json::JSONItem* lJSONItem = static_cast<const json::JSONItem*>(lItem);
1238+ for (csize i = 0; i < numRoots; i++)
1239+ {
1240+ if (rootNodes[i]->isJSONItem())
1241+ {
1242+ assert(dynamic_cast<const json::JSONItem*>(rootNodes[i]));
1243+ json::JSONItem* lRootJSONItem = static_cast<json::JSONItem*>(rootNodes[i]);
1244+
1245+ if (lJSONItem->getTree() == lRootJSONItem->getTree())
1246+ {
1247+ found = true;
1248+ break;
1249+ }
1250+ }
1251+ }
1252+#endif
1253+ }
1254
1255- if (targetNode->getTree() == rootNode->getTree())
1256- {
1257- found = true;
1258- break;
1259- }
1260- }
1261-
1262 if (!found)
1263 throw XQUERY_EXCEPTION(err::XUDY0014);
1264 }

Subscribers

People subscribed via source and target branches