From: adam Date: Sun, 17 Mar 2013 07:34:07 +0000 (+0700) Subject: #51 X-Git-Tag: v1.2.12~296^2~74^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=986be724cffca368d6c95b733373545978364c19;p=platform%2Fupstream%2Fejdb.git #51 --- diff --git a/luaejdb/Makefile b/luaejdb/Makefile index 4e9db33..bda0421 100644 --- a/luaejdb/Makefile +++ b/luaejdb/Makefile @@ -1,12 +1,14 @@ -CFLAGS=-g -O0 -fPIC -std=c99 -Wall -D_GNU_SOURCE build: - luarocks --pack-binary-rock CFLAGS='$(CFLAGS)' make + luarocks --pack-binary-rock make -check: build +build-dbg: + luarocks --pack-binary-rock CFLAGS='-g -O0 -fPIC -std=c99 -Wall' make + +check: build-dbg make -C ./test -check-valgrind: build +check-valgrind: build-dbg make RUNCMD="valgrind --tool=memcheck --leak-check=full --error-exitcode=1" check -C ./test clean: @@ -14,4 +16,4 @@ clean: make -C ./test clean -.PHONY: build test clean +.PHONY: build build-dbg check check-valgrind clean diff --git a/luaejdb/ejdb.lua b/luaejdb/ejdb.lua index ca08940..e782d74 100644 --- a/luaejdb/ejdb.lua +++ b/luaejdb/ejdb.lua @@ -129,20 +129,33 @@ end function DB:find(cname, q, ...) assert(getmetatable(q) == mtBObj, "Query object must be instance of 'luaejdb.B' class `q = luaejdb.B()`") - local flags = ... - if (type(flags) ~= "number") then - flags = 0 - end + local sflags = ... local orBsons = {} local ors = q:getJoinedORs() if ors then for _, o in ipairs(ors) do - table.insert(orBsons, o.toBSON()) + table.insert(orBsons, o:toBSON()) end end - return self:_find(cname, q:toBSON(), orBsons, q:toHintsBSON(), flags) + return self:_find(cname, q:toBSON(), orBsons, q:toHintsBSON(), sflags) +end + + +function DB:findOne(cname, q, ...) + +end + +function DB:count(cname, q, sflags, ...) + if type(sflags) ~= "string" then + sflags = "c" + else + sflags = "c" .. sflags --append count flag + end + local _, count, log = self:find(cname, q, sflags, ...) + return count, log end + -- ------- EJDB Query ------------- function B:_init(fname, ...) @@ -291,7 +304,7 @@ function B:Do(val) return self:_rootOp("$do", val) end function B:Or(...) self._or = self._or or {} for i, v in ipairs({ ... }) do - assert(getmetatable(v) == mtBObj, "Each 'or' argument must be instance of 'luaejdb.B' class") + assert(getmetatable(v) == mtBObj, "Each 'Or()' argument must be instance of 'luaejdb.B' class") table.insert(self._or, v) end return self diff --git a/luaejdb/luabson.c b/luaejdb/luabson.c index 2c0f7e8..1052143 100644 --- a/luaejdb/luabson.c +++ b/luaejdb/luabson.c @@ -144,7 +144,8 @@ void lua_push_bson_table(lua_State *L, bson_iterator *it) { void lua_push_bson_array(lua_State *L, bson_iterator *it) { bson_type bt; lua_push_bsontype_table(L, BSON_ARRAY); - for (int i = 1; (bt = bson_iterator_next(it)) != BSON_EOO; ++i) { + int i; + for (i = 1; (bt = bson_iterator_next(it)) != BSON_EOO; ++i) { lua_push_bson_value(L, it); lua_rawseti(L, -2, i); } @@ -233,7 +234,8 @@ static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, i } if (array) { if (key) bson_append_start_array(bs, key); - for (int i = 1; i <= len; ++i, lua_pop(L, 1)) { + int i; + for (i = 1; i <= len; ++i, lua_pop(L, 1)) { lua_rawgeti(L, vpos, i); bson_numstrn(nbuf, TCNUMBUFSIZ, (int64_t) i); lua_val_to_bson(L, nbuf, -1, bs, tref); @@ -255,7 +257,8 @@ static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, i int ipos = lua_gettop(L); size_t ilen = lua_objlen(L, ipos); lua_checkstack(L, 2); - for (size_t i = 1; i <= ilen; ++i, lua_pop(L, 1)) { + size_t i; + for (i = 1; i <= ilen; ++i, lua_pop(L, 1)) { lua_rawgeti(L, ipos, i); //gettop == 3 if (!lua_istable(L, -1)) continue; @@ -264,7 +267,8 @@ static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, i size_t jlen = lua_objlen(L, jpos); lua_checkstack(L, 3); bool wrapped = false; - for (size_t j = 1; j <= jlen; ++j, lua_pop(L, 1)) { + size_t j; + for (j = 1; j <= jlen; ++j, lua_pop(L, 1)) { lua_rawgeti(L, jpos, j); if (j == 1) { fname = strdup(lua_tostring(L, -1)); @@ -320,7 +324,8 @@ static void lua_val_to_bson(lua_State *L, const char *key, int vpos, bson *bs, i } } tclistsort(keys); - for (int i = 0; i < TCLISTNUM(keys); ++i) { + int i; + for (i = 0; i < TCLISTNUM(keys); ++i) { int vkeysz = TCLISTVALSIZ(keys, i); const char *vkey = TCLISTVALPTR(keys, i); lua_pushlstring(L, vkey, vkeysz); diff --git a/luaejdb/luaejdb-1.0-1.rockspec b/luaejdb/luaejdb-1.0-1.rockspec index 8646fab..043a6a9 100644 --- a/luaejdb/luaejdb-1.0-1.rockspec +++ b/luaejdb/luaejdb-1.0-1.rockspec @@ -30,7 +30,8 @@ build = { sources = {"luaejdb.c", "luabson.c"}, libraries = {"tcejdb"} , incdirs = {"$(LIBTCEJDB_INCDIR)"}, - libdirs = {"$(LIBTCEJDB_LIBDIR)"} + libdirs = {"$(LIBTCEJDB_LIBDIR)"}, + defines = {"_GNU_SOURCE"} } } } diff --git a/luaejdb/luaejdb.c b/luaejdb/luaejdb.c index 9e4e200..3295221 100644 --- a/luaejdb/luaejdb.c +++ b/luaejdb/luaejdb.c @@ -383,8 +383,23 @@ static int db_find(lua_State *L) { const char *qbsbuf = luaL_checkstring(L, 3); //Query bson luaL_checktype(L, 4, LUA_TTABLE); //or joined const char *hbsbuf = luaL_checkstring(L, 5); //Hints bson - int qflags = luaL_checkinteger(L, 6); + int qflags = 0; + if (lua_isstring(L, 6)) { + const char *sflags = lua_tostring(L, 6); + int i; + for (i = 0; sflags[i] != '\0'; ++i) { + switch (sflags[i]) { + case 'c': + qflags |= JBQRYCOUNT; + break; + case 'l': + qflags |= JBQRYLOG; + break; + } + } + } + size_t i; bson oqarrstack[8]; //max 8 $or bsons on stack bson *oqarr = NULL; bson qbson = {NULL}; @@ -399,7 +414,8 @@ static int db_find(lua_State *L) { size_t orsz = lua_objlen(L, 4); oqarr = ((orsz <= 8) ? oqarrstack : (bson*) tcmalloc(orsz * sizeof (bson))); - for (size_t i = 0; i < orsz; ++i) { + + for (i = 0; i < orsz; ++i) { const void *bsdata; size_t bsdatasz; lua_rawgeti(L, 4, i + 1); @@ -468,7 +484,7 @@ static int db_find(lua_State *L) { finish: - for (size_t i = 0; i < orsz; ++i) { + for (i = 0; i < orsz; ++i) { bson_destroy(&oqarr[i]); } if (oqarr && oqarr != oqarrstack) { @@ -498,7 +514,8 @@ static int db_open(lua_State *L) { } else if (lua_isstring(L, 2)) { mode = 0; const char* om = lua_tostring(L, 2); - for (int i = 0; om[i] != '\0'; ++i) { + int i; + for (i = 0; om[i] != '\0'; ++i) { mode |= JBOREADER; switch (om[i]) { case 'w': diff --git a/luaejdb/test/t1.lua b/luaejdb/test/t1.lua index f43d790..765ea1b 100644 --- a/luaejdb/test/t1.lua +++ b/luaejdb/test/t1.lua @@ -139,6 +139,29 @@ local r,err = pcall(qres.object, qres, 1); assert(err == "Cursor closed") +local qres, count, log = db:find("mycoll", Q("foo", "bar"), "l") +assert(type(log) == "string") +assert(qres ~= nil) +assert(#qres == 1) +assert(count == 1) +assert(log:find("COUNT ONLY: NO")) + +local qres, count, log = db:find("mycoll", Q("foo", "bar"), "lc") --COUNT only +assert(type(log) == "string") +assert(qres == nil) +assert(count == 1) +assert(log:find("COUNT ONLY: YES")) + +--db:save("mycoll", { foo = "bar6", k2 = "v2" }); +local count, log = db:count("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6"))); +assert(count == 2) +assert(log == nil) + +local count, log = db:count("mycoll", Q():Or(Q("foo", "bar"), Q("foo", "bar6")), "l"); +assert(count == 2) +assert(log:find("COUNT ONLY: YES")) + + db:close() collectgarbage()