From bfa218e0174dae5b6ede12b2e386fa76ccdc85c0 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 17 Mar 2013 23:36:03 +0700 Subject: [PATCH] #51 --- luaejdb/luaejdb.c | 149 ++++++++++++++++++++++++++++++++++++++++++++++++---- luaejdb/test/t1.lua | 17 ++++-- 2 files changed, 153 insertions(+), 13 deletions(-) diff --git a/luaejdb/luaejdb.c b/luaejdb/luaejdb.c index 3295221..0eb6ae3 100644 --- a/luaejdb/luaejdb.c +++ b/luaejdb/luaejdb.c @@ -244,8 +244,6 @@ static int cursor_iter_bsons_next(lua_State *L) { } }; -//return cursor iterator - static int cursor_call(lua_State *L) { const char *flags = ""; CURSORDATA *cdata = luaL_checkudata(L, 1, EJDBCURSORMT); @@ -264,9 +262,9 @@ static int cursor_call(lua_State *L) { static int db_del(lua_State *L) { EJDBDATA *data = luaL_checkudata(L, 1, EJDBUDATAMT); - EJDB *db = data->db; - if (db) { - ejdbdel(db); + EJDB *jb = data->db; + if (jb) { + ejdbdel(jb); data->db = NULL; } return 0; @@ -276,15 +274,130 @@ static int db_close(lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); //self lua_getfield(L, 1, EJDBUDATAKEY); EJDBDATA *data = luaL_checkudata(L, -1, EJDBUDATAMT); - EJDB *db = data->db; - if (db) { - if (!ejdbclose(db)) { - EJDBERR(L, db); + EJDB *jb = data->db; + if (jb) { + if (!ejdbclose(jb)) { + EJDBERR(L, jb); + } + } + return 0; +} + +static int db_is_open(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); //self + lua_getfield(L, 1, EJDBUDATAKEY); + lua_pushboolean(L, ejdbisopen(((EJDBDATA*) luaL_checkudata(L, -1, EJDBUDATAMT))->db)); + return 1; +} + +static int db_ensure_collection(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); //self + lua_getfield(L, 1, EJDBUDATAKEY); + EJDBDATA *data = luaL_checkudata(L, -1, EJDBUDATAMT); + EJDB *jb = data->db; + const char *cname = luaL_checkstring(L, 2); + EJCOLLOPTS opts = {NULL}; + if (lua_istable(L, 3)) { + lua_getfield(L, 3, "cachedrecords"); + if (lua_isnumber(L, -1)) { + opts.cachedrecords = lua_tointeger(L, -1); + } + lua_pop(L, 1); + + lua_getfield(L, 3, "records"); + if (lua_isnumber(L, -1)) { + opts.records = lua_tointeger(L, -1); + } + lua_pop(L, 1); + + lua_getfield(L, 3, "large"); + opts.large = lua_toboolean(L, -1); + lua_pop(L, 1); + + lua_getfield(L, 3, "compressed"); + opts.compressed = lua_toboolean(L, -1); + lua_pop(L, 1); //-field + } + if (!ejdbcreatecoll(jb, cname, &opts)) { + return set_ejdb_error(L, jb); + } + return 0; +} + +static int db_drop_collection(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); //self + lua_getfield(L, 1, EJDBUDATAKEY); + EJDBDATA *data = luaL_checkudata(L, -1, EJDBUDATAMT); + EJDB *jb = data->db; + const char *cname = luaL_checkstring(L, 2); + bool unlink = lua_toboolean(L, 3); + if (!ejdbrmcoll(jb, cname, unlink)) { + return set_ejdb_error(L, jb); + } + return 0; +} + +static int db_remove(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); //self + lua_getfield(L, 1, EJDBUDATAKEY); + EJDBDATA *data = luaL_checkudata(L, -1, EJDBUDATAMT); + EJDB *jb = data->db; + bson_oid_t oid; + memset(&oid, 0, sizeof (oid)); + const char *cname = luaL_checkstring(L, 2); //collections name + if (lua_isstring(L, 3)) { + const char *soid = lua_tostring(L, 3); + if (ejdbisvalidoidstr(soid)) { + bson_oid_from_string(&oid, soid); + } + } else if (luaL_getmetafield(L, 3, "__bsontype") && lua_tointeger(L, -1) == BSON_OID) { + lua_pop(L, 1); + lua_rawgeti(L, 3, 1); + const char *soid = lua_tostring(L, -1); + if (ejdbisvalidoidstr(soid)) { + bson_oid_from_string(&oid, soid); + } + lua_pop(L, 1); + } + if (!oid.ints[0] && !oid.ints[1] && !oid.ints[2]) { + return luaL_error(L, "Invalid OID arg #2"); + } + EJCOLL *coll = ejdbgetcoll(jb, cname); + if (!coll) { + return 0; + } + if (!ejdbrmbson(coll, &oid)) { + return set_ejdb_error(L, jb); + } + return 0; +} + +static int db_sync(lua_State *L) { + luaL_checktype(L, 1, LUA_TTABLE); //self + lua_getfield(L, 1, EJDBUDATAKEY); + EJDBDATA *data = luaL_checkudata(L, -1, EJDBUDATAMT); + EJDB *jb = data->db; + if (lua_isstring(L, 2)) { //sync coll + const char *cname = lua_tostring(L, 2); + EJCOLL *coll = ejdbgetcoll(jb, cname); + if (coll) { + if (!ejdbsyncoll(coll)) { + return set_ejdb_error(L, jb); + } + } + } else { //sync db + if (!ejdbsyncdb(jb)) { + return set_ejdb_error(L, jb); } } return 0; } +static int db_set_index(lua_State *L) { + //todo + return 0; +} + static int db_save(lua_State *L) { int argc = lua_gettop(L); luaL_checktype(L, 1, LUA_TTABLE); //self @@ -569,6 +682,24 @@ static int db_open(lua_State *L) { lua_pushcfunction(L, db_find); lua_setfield(L, -2, "_find"); + lua_pushcfunction(L, db_is_open); + lua_setfield(L, -2, "isOpen"); + + lua_pushcfunction(L, db_ensure_collection); + lua_setfield(L, -2, "ensureCollection"); + + lua_pushcfunction(L, db_drop_collection); + lua_setfield(L, -2, "dropCollection"); + + lua_pushcfunction(L, db_remove); + lua_setfield(L, -2, "remove"); + + lua_pushcfunction(L, db_sync); + lua_setfield(L, -2, "sync"); + + lua_pushcfunction(L, db_set_index); + lua_setfield(L, -2, "_setIndex"); + if (lua_gettop(L) - argc != 1) { ejdbdel(db); udb->db = NULL; diff --git a/luaejdb/test/t1.lua b/luaejdb/test/t1.lua index a74cb2b..6bc3acd 100644 --- a/luaejdb/test/t1.lua +++ b/luaejdb/test/t1.lua @@ -13,6 +13,7 @@ local Q = ejdb.Q local B = ejdb.B local db = ejdb:open("testdb", "rwct"); +assert(db:isOpen() == true) local q = Q("name", "Andy"):F("_id"):Eq("510f7fa91ad6270a00000000"):F("age"):Gt(20):Lt(40):F("score"):In({ 11, 22.12333, 1362835380447, db.toNull() }):Max(232); assert([[.name(2)=Andy @@ -75,6 +76,7 @@ assert([[._id(7)=510f7fa91ad6270a00000000 -- Test save -- local oid = db:save("mycoll", { foo = "bar", k1 = "v1" }); +local firstOid = oid; ejdb.check_valid_oid_string(oid) oid = db:save("mycoll", B("foo2", "bar2"):KV("g", "d"):KV("e", 1):KV("a", "g")); @@ -138,7 +140,6 @@ assert(#qres == 0) 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) @@ -169,12 +170,20 @@ local vobj, count, log = db:findOne("mycoll", Q():Or(Q("foo", "bar"), Q("foo", " assert(count == 1) assert(vobj["foo"] == "bar") +db:ensureCollection("ecoll", { large = true, records = 200000 }) +db:dropCollection("ecoll", true); + +assert(db:count("mycoll", Q("_id", firstOid)) == 1) +db:remove("mycoll", firstOid) +assert(db:count("mycoll", Q("_id", firstOid)) == 0) + +db:sync("mycoll") -- sync only mycoll +db:sync() -- sync whole db +assert(db:isOpen() == true) db:close() +assert(db:isOpen() == false) collectgarbage() collectgarbage() collectgarbage() - - - -- 2.7.4