#1
authoradam <anton@adamansky.com>
Sun, 4 Nov 2012 15:21:54 +0000 (22:21 +0700)
committeradam <anton@adamansky.com>
Sun, 4 Nov 2012 15:21:54 +0000 (22:21 +0700)
.gitignore
binding.gyp
node/ejdb.js
node/ejdb_native.cc
node/tests/t2.js

index 03d8419..992179d 100644 (file)
@@ -1,6 +1,7 @@
 *.o
 *.vlog
 
+/npm-debug.log
 /.idea/workspace.xml
 
 /tcejdb/Makefile
index ca5a1c0..4801c14 100644 (file)
@@ -19,6 +19,7 @@
                  '_LARGEFILE_SOURCE',
                  '_FILE_OFFSET_BITS=64',
                  '_GNU_SOURCE',
+                 '_GLIBCXX_PERMIT_BACKWARD_HASH',
                ],
             }],
             [ 'OS=="linux" or OS=="freebsd" or OS=="openbsd" or OS=="solaris"', {
index 451306f..72d61ab 100644 (file)
@@ -91,8 +91,8 @@ EJDB.prototype.remove = function(cname, oid, cb) {
  */
 EJDB.prototype.query = function(cname, qobj, orarr, hints, cb) {
     if (arguments.length == 4) {
-        hints = orarr;
         cb = hints;
+        hints = orarr;
         orarr = [];
     } else if (arguments.length == 3) {
         cb = orarr;
index 3a66e77..7a1f7b2 100644 (file)
 
 #include <vector>
 #include <sstream>
+#include <hash_set>
 #include <locale.h>
-#include <stdio.h>
+
 
 using namespace node;
 using namespace v8;
 
+#ifdef __GNUC__
+using namespace __gnu_cxx;
+#endif
+
 static const int CMD_RET_ERROR = 1;
 
 #define DEFINE_INT64_CONSTANT(target, constant)                       \
@@ -106,9 +111,25 @@ namespace ejdb {
         return sobj->ToNumber()->NumberValue();
     }
 
-    typedef struct {
-        Handle<Object> traversed;
-    } TBSONCTX;
+    struct V8ObjHash {
+
+        size_t operator()(const Handle<Object>& obj) const {
+            return (size_t) obj->GetIdentityHash();
+        }
+    };
+
+    struct V8ObjEq {
+
+        bool operator()(const Handle<Object>& o1, const Handle<Object>& o2) const {
+            return (o1 == o2);
+        }
+    };
+
+    typedef hash_set<Handle<Object>, V8ObjHash, V8ObjEq> V8ObjSet;
+
+    struct TBSONCTX {
+        V8ObjSet tset; //traversed objects set
+    };
 
     static Handle<Object> toV8Object(bson_iterator *it, bson_type obt = BSON_OBJECT);
     static Handle<Value> toV8Value(bson_iterator *it);
@@ -322,12 +343,13 @@ namespace ejdb {
     static void toBSON0(Handle<Object> obj, bson *bs, TBSONCTX *ctx) {
         HandleScope scope;
         assert(ctx && obj->IsObject());
-        if (ctx->traversed->Get(obj)->IsObject()) {
+        V8ObjSet::iterator it = ctx->tset.find(obj);
+        if (it != ctx->tset.end()) {
             bs->err = BSON_ERROR_ANY;
             bs->errstr = strdup("Circular object reference");
             return;
         }
-        ctx->traversed->Set(obj, obj);
+        ctx->tset.insert(obj);
         Local<Array> pnames = obj->GetOwnPropertyNames();
         for (uint32_t i = 0; i < pnames->Length(); ++i) {
             Local<Value> pn = pnames->Get(i);
@@ -389,7 +411,6 @@ namespace ejdb {
     static void toBSON(Handle<Object> obj, bson *bs) {
         HandleScope scope;
         TBSONCTX ctx;
-        ctx.traversed = Object::New();
         toBSON0(obj, bs, &ctx);
     }
 
@@ -596,7 +617,7 @@ namespace ejdb {
                             ));
                 }
                 bson *bs = bson_create();
-                bson_init(bs);
+                bson_init_as_query(bs);
                 toBSON(Local<Object>::Cast(qv), bs);
                 bson_finish(bs);
                 if (bs->err) {
index 5b9874a..959b1ed 100644 (file)
@@ -49,6 +49,7 @@ module.exports.testSaveLoad = function(test) {
     });
 };
 
+/*
 
 module.exports.testQuery1 = function(test) {
     test.ok(jb);
@@ -79,8 +80,20 @@ module.exports.testQuery1 = function(test) {
     });
 };
 
+*/
+
 module.exports.testQuery2 = function(test) {
-    test.done();
+    test.ok(jb);
+    test.ok(jb.isOpen());
+    jb.query("parrots",
+            {name : /(grenny|bounty)/ig},
+            {$orderby : {name : 1}},
+            function(err, cursor, count) {
+                test.ifError(err);
+                console.log("count=" + count);
+                test.done();
+            });
+
 };
 
 module.exports.testClose = function(test) {