From 7747a25871d415cd44a46beef3aee77adf3341cc Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 1 Nov 2012 18:07:50 +0700 Subject: [PATCH] #1 --- node/ejdb_native.cc | 86 +++++++++++++++++++++++++++++++++++++++++------------ tcejdb/ejdb.c | 1 + 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/node/ejdb_native.cc b/node/ejdb_native.cc index ac1f982..da3fb64 100644 --- a/node/ejdb_native.cc +++ b/node/ejdb_native.cc @@ -547,8 +547,16 @@ namespace ejdb { goto finish; } res = ejdbqrysearch(coll, q, &cmdata->count, cmdata->qflags, NULL); - - + if (ejdbecode(m_jb) != TCESUCCESS) { + if (res) { + tclistdel(res); + res = NULL; + } + task->cmd_ret = CMD_RET_ERROR; + task->cmd_ret_msg = _jb_error_msg(); + goto finish; + } + cmdata->res = res; finish: if (q) { ejdbquerydel(q); @@ -558,10 +566,7 @@ finish: } } - void query_after(BSONQCmdTask *task) { - HandleScope scope; - - } + void query_after(BSONQCmdTask *task); bool open(const char* dbpath, int mode) { m_jb = ejdbnew(); @@ -654,14 +659,16 @@ finish: static Persistent constructor_template; + NodeEJDB *m_nejdb; TCLIST *m_rs; //result set bsons int m_pos; //current cursor position static Handle s_new_object(const Arguments& args) { HandleScope scope; - REQ_ARGS(1); - REQ_EXT_ARG(0, rs); - NodeEJDBCursor *cursor = new NodeEJDBCursor((TCLIST*) rs->Value()); + REQ_ARGS(2); + REQ_EXT_ARG(0, nejedb); + REQ_EXT_ARG(1, rs); + NodeEJDBCursor *cursor = new NodeEJDBCursor((NodeEJDB*) nejedb->Value(), (TCLIST*) rs->Value()); cursor->Wrap(args.This()); return scope.Close(args.This()); } @@ -670,10 +677,7 @@ finish: HandleScope scope; NodeEJDBCursor *c = ObjectWrap::Unwrap< NodeEJDBCursor > (args.This()); assert(c); - if (c->m_rs) { - tclistdel(c->m_rs); - c->m_rs = NULL; - } + c->close(); return scope.Close(args.This()); } @@ -727,15 +731,26 @@ finish: c->m_pos = nval; } - NodeEJDBCursor(TCLIST *rs) : m_rs(rs), m_pos(0) { - } - - virtual ~NodeEJDBCursor() { + void close() { + if (m_nejdb) { + m_nejdb->Unref(); + m_nejdb = NULL; + } if (m_rs) { tclistdel(m_rs); + m_rs = NULL; } } + NodeEJDBCursor(NodeEJDB *_nejedb, TCLIST *_rs) : m_nejdb(_nejedb), m_rs(_rs), m_pos(0) { + assert(m_rs && m_nejdb); + this->m_nejdb->Ref(); + } + + virtual ~NodeEJDBCursor() { + close(); + } + public: static void Init(Handle target) { @@ -764,16 +779,49 @@ finish: } }; - Persistent NodeEJDB::constructor_template; Persistent NodeEJDBCursor::constructor_template; + /////////////////////////////////////////////////////////////////////////// + // rest // + /////////////////////////////////////////////////////////////////////////// + + void NodeEJDB::query_after(BSONQCmdTask *task) { + HandleScope scope; + Local argv[3]; + if (task->cmd_ret != 0) { + argv[0] = Exception::Error(String::New(task->cmd_ret_msg.c_str())); + TryCatch try_catch; + task->cb->Call(Context::GetCurrent()->Global(), 1, argv); + if (try_catch.HasCaught()) { + FatalException(try_catch); + } + return; + } + BSONQCmdData *cmdata = task->cmd_data; + assert(cmdata); + TCLIST *res = cmdata->res; + cmdata->res = NULL; //res will be freed by NodeEJDBCursor instead of ~BSONQCmdData() + argv[0] = Local::New(Null()); + Local cursorArgv[2]; + cursorArgv[0] = External::New(task->wrapped); + cursorArgv[1] = External::New(res); + Local cursor(NodeEJDBCursor::constructor_template->GetFunction()->NewInstance(2, cursorArgv)); + argv[1] = Local::New(cursor); + argv[2] = Integer::New(cmdata->count); + TryCatch try_catch; + task->cb->Call(Context::GetCurrent()->Global(), 3, argv); + if (try_catch.HasCaught()) { + FatalException(try_catch); + } + } + void Init(v8::Handle target) { #ifdef __unix setlocale(LC_ALL, "en_US.UTF-8"); //todo review it #endif ejdb::NodeEJDB::Init(target); - ejdb::NodeEJDB::Init(target); + ejdb::NodeEJDBCursor::Init(target); } } diff --git a/tcejdb/ejdb.c b/tcejdb/ejdb.c index d196c13..f0ca1d8 100644 --- a/tcejdb/ejdb.c +++ b/tcejdb/ejdb.c @@ -570,6 +570,7 @@ EJDB_EXPORT TCLIST* ejdbqrysearch(EJCOLL *jcoll, const EJQ *q, uint32_t *count, } JBCLOCKMETHOD(jcoll, false); TCLIST *res; + _ejdbsetecode(jcoll->jb, TCESUCCESS, __FILE__, __LINE__, __func__); res = _qrysearch(jcoll, q, count, qflags, log); JBCUNLOCKMETHOD(jcoll); return res; -- 2.7.4