Markos Zaharioudakis has proposed merging lp:~zorba-coders/zorba/markos-scratch 
into lp:zorba.

Commit message:
duplicate elimination for the jn:keys() function

Requested reviews:
  Markos Zaharioudakis (markos-za)

For more details, see:
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/170167

duplicate elimination for the jn:keys() function
-- 
https://code.launchpad.net/~zorba-coders/zorba/markos-scratch/+merge/170167
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'modules/ExternalModules.conf'
--- modules/ExternalModules.conf	2013-06-18 11:03:29 +0000
+++ modules/ExternalModules.conf	2013-06-18 18:57:39 +0000
@@ -52,4 +52,4 @@
 stack           bzr  lp:zorba/stack-module            zorba-2.9
 queue           bzr  lp:zorba/queue-module            zorba-2.9
 couchbase       bzr  lp:zorba/couchbase-module
-#sqlite          bzr  lp:zorba/sqlite-module
+sqlite          bzr  lp:zorba/sqlite-module

=== modified file 'modules/org/jsoniq/www/functions.xq'
--- modules/org/jsoniq/www/functions.xq	2013-06-15 02:57:08 +0000
+++ modules/org/jsoniq/www/functions.xq	2013-06-18 18:57:39 +0000
@@ -184,11 +184,13 @@
 
 
 (:~
- : Returns the names used in the object. 
- : The names will be returned in an implementation-defined order
+ : Returns the set of keys belonging to the objects found inside a given
+ : sequence of items. The keys are returned in an implementation-defined
+ : order. Duplicate keys are eliminated.
  :
- : @param $o A JSON Object.
- : @return The names of pairs in the object.
+ : @param $o A sequence of items. Only object items are actually processed;
+ :           items of any other kind are simply skipped.
+ : @return The distinct keys of the objects in the input sequence.
  :)
 declare function jn:keys($o as item()*) as xs:string* external;
 
@@ -235,17 +237,19 @@
  : @param $p The position in the array.
  : @return The member at the specified position, or empty sequence.
  :)
-(: obsolete - use $a($p) instead :)
+(: obsolete - use $a($p) or $a[[$p]] instead :)
 declare function jn:member($a as item(), $p as item()?) as item()? external;
 
 
 (:~
- : Returns the members of an Array.
+ : Returns the items belonging to the arrays found inside a given sequence
+ : of items. The items are returned in an implementation-defined order.
  :
- : @param $a A JSON Array.
- : @return The members of the specified array.
+ : @param $a A sequence of items. Only array items are actually processed;
+ :           items of any other kind are simply skipped.
+ : @return The members of the arrays in the input sequence.
  :)
-declare function jn:members($o as item()*) as item()* external;
+declare function jn:members($a as item()*) as item()* external;
 
 
 (:~

=== modified file 'src/runtime/json/jsoniq_functions_impl.cpp'
--- src/runtime/json/jsoniq_functions_impl.cpp	2013-06-15 02:57:08 +0000
+++ src/runtime/json/jsoniq_functions_impl.cpp	2013-06-18 18:57:39 +0000
@@ -48,11 +48,11 @@
 
 #include <runtime/util/doc_uri_heuristics.h>
 
-#include <store/api/pul.h>
-#include <store/api/item.h>
-#include <store/api/item_factory.h>
-#include <store/api/store.h>
-#include <store/api/copymode.h>
+#include "store/api/pul.h"
+#include "store/api/item.h"
+#include "store/api/item_factory.h"
+#include "store/api/store.h"
+#include "store/api/copymode.h"
 
 #include "util/uri_util.h"
 #include "util/stream_util.h"
@@ -1126,11 +1126,26 @@
 /*******************************************************************************
   jn:names($o as item()*) as xs:string*
 ********************************************************************************/
+
+void JSONObjectNamesIteratorState::init(PlanState& planState)
+{
+  theNamesSet.reset(new HashSet<zstring, HashMapZStringCmp>(64, false));
+}
+
+
+void JSONObjectNamesIteratorState::reset(PlanState& planState)
+{
+  PlanIteratorState::reset(planState);
+  theNamesSet->clear();
+}
+
+
 bool JSONObjectNamesIterator::nextImpl(
     store::Item_t& result,
     PlanState& planState) const
 {
   store::Item_t input;
+  zstring name;
 
   JSONObjectNamesIteratorState* state;
   DEFAULT_STACK_INIT(JSONObjectNamesIteratorState, state, planState);
@@ -1144,7 +1159,12 @@
 
       while (state->theNames->next(result))
       {
-        STACK_PUSH (true, state);
+        name = result->getStringValue();
+        if (!state->theNamesSet->exists(name))
+        {
+          state->theNamesSet->insert(name);
+          STACK_PUSH(true, state);
+        }
       }
       state->theNames = NULL;
     }
@@ -1154,29 +1174,6 @@
 }
 
 
-bool JSONObjectNamesIterator::count(
-  store::Item_t& result,
-  PlanState& planState) const
-{
-  store::Item_t obj;
-  xs_integer count(0);
-
-  JSONObjectNamesIteratorState* state;
-  DEFAULT_STACK_INIT(JSONObjectNamesIteratorState, state, planState);
-
-  while (consumeNext(obj, theChild.getp(), planState))
-  {
-    if (obj->isObject())
-    {
-      count += obj->getNumObjectPairs();
-    }
-  }
-
-  STACK_PUSH(GENV_ITEMFACTORY->createInteger(result, count), state);
-  STACK_END(state);
-}
-
-
 /*******************************************************************************
   json:value($o as item(), $name as item()?) as item()?
 

=== modified file 'src/runtime/json/pregenerated/jsoniq_functions.cpp'
--- src/runtime/json/pregenerated/jsoniq_functions.cpp	2013-06-08 05:33:57 +0000
+++ src/runtime/json/pregenerated/jsoniq_functions.cpp	2013-06-18 18:57:39 +0000
@@ -239,14 +239,6 @@
 
 JSONObjectNamesIteratorState::~JSONObjectNamesIteratorState() {}
 
-
-void JSONObjectNamesIteratorState::init(PlanState& planState) {
-  PlanIteratorState::init(planState);
-}
-
-void JSONObjectNamesIteratorState::reset(PlanState& planState) {
-  PlanIteratorState::reset(planState);
-}
 // </JSONObjectNamesIterator>
 
 

=== modified file 'src/runtime/json/pregenerated/jsoniq_functions.h'
--- src/runtime/json/pregenerated/jsoniq_functions.h	2013-06-08 05:33:57 +0000
+++ src/runtime/json/pregenerated/jsoniq_functions.h	2013-06-18 18:57:39 +0000
@@ -31,6 +31,8 @@
 #include "runtime/base/narybase.h"
 #include <context/uri_resolver.h>
 #include "runtime/json/json_loader.h"
+#include "zorbautils/hashset.h"
+#include "zorbautils/hashmap_zstring.h"
 
 
 namespace zorba {
@@ -286,6 +288,7 @@
 {
 public:
   store::Iterator_t theNames; //
+  std::auto_ptr<HashSet<zstring, HashMapZStringCmp> > theNamesSet; //
 
   JSONObjectNamesIteratorState();
 
@@ -315,8 +318,6 @@
 
   virtual ~JSONObjectNamesIterator();
 
-public:
-  bool count(store::Item_t& result, PlanState& planState) const;
   void accept(PlanIterVisitor& v) const;
 
   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;

=== modified file 'src/runtime/sequences/sequences_impl.cpp'
--- src/runtime/sequences/sequences_impl.cpp	2013-06-15 23:20:00 +0000
+++ src/runtime/sequences/sequences_impl.cpp	2013-06-18 18:57:39 +0000
@@ -268,7 +268,7 @@
 {
   PlanIteratorState::reset(planState);
   theHasNaN = false;
-  if (theAlreadySeenMap.get () != NULL)
+  if (theAlreadySeenMap.get() != NULL)
     theAlreadySeenMap->clear();
 }
 

=== modified file 'src/runtime/spec/json/jsoniq_functions.xml'
--- src/runtime/spec/json/jsoniq_functions.xml	2013-06-08 05:33:57 +0000
+++ src/runtime/spec/json/jsoniq_functions.xml	2013-06-18 18:57:39 +0000
@@ -12,6 +12,8 @@
 <zorba:header>
   <zorba:include form="Angle-bracket">context/uri_resolver.h</zorba:include>
   <zorba:include form="Quoted">runtime/json/json_loader.h</zorba:include>
+  <zorba:include form="Quoted">zorbautils/hashset.h</zorba:include>
+  <zorba:include form="Quoted">zorbautils/hashmap_zstring.h</zorba:include>
 </zorba:header>
   
 <!--
@@ -256,7 +258,7 @@
   </zorba:function>
 
   <zorba:state>
-    <zorba:member type="store::Iterator_t" name="theIterator" brief=""/>
+    <zorba:member type="store::Iterator_t" name="theIterator"/>
   </zorba:state>
 
 </zorba:iterator>
@@ -285,15 +287,14 @@
 
   </zorba:function>
 
-  <zorba:state>
-    <zorba:member type="store::Iterator_t" name="theNames" brief=""/>
+  <zorba:state generateInit="false" generateReset="false">
+
+    <zorba:member type="store::Iterator_t" name="theNames"/>
+
+    <zorba:member type="std::auto_ptr&lt;HashSet&lt;zstring, HashMapZStringCmp> >"
+                  name="theNamesSet"/>
   </zorba:state>
 
-  <zorba:method name="count" const="true" return="bool">
-     <zorba:param name="result" type="store::Item_t&amp;"/>
-     <zorba:param name="planState" type="PlanState&amp;"/>
-  </zorba:method>
-
 </zorba:iterator>
 
 

=== added file 'test/rbkt/ExpQueryResults/zorba/jsoniq/keys_05.xml.res'
--- test/rbkt/ExpQueryResults/zorba/jsoniq/keys_05.xml.res	1970-01-01 00:00:00 +0000
+++ test/rbkt/ExpQueryResults/zorba/jsoniq/keys_05.xml.res	2013-06-18 18:57:39 +0000
@@ -0,0 +1,1 @@
+foo1 foo2

=== added file 'test/rbkt/Queries/zorba/jsoniq/keys_05.xq'
--- test/rbkt/Queries/zorba/jsoniq/keys_05.xq	1970-01-01 00:00:00 +0000
+++ test/rbkt/Queries/zorba/jsoniq/keys_05.xq	2013-06-18 18:57:39 +0000
@@ -0,0 +1,4 @@
+let $a :=
+  for $i in 1 to 10
+  return { if ($i mod 2 eq 0) then "foo2" else "foo1" : "bar" || $i }
+return jn:keys($a)

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to