#47
authoradam <adamansky@gmail.com>
Tue, 12 Feb 2013 09:34:07 +0000 (16:34 +0700)
committeradam <adamansky@gmail.com>
Tue, 12 Feb 2013 09:34:07 +0000 (16:34 +0700)
pyejdb/pyejdb/__init__.py
pyejdb/src/EJDB.c
pyejdb/test/test_one.py

index df22c80..e021b5a 100644 (file)
@@ -1,6 +1,8 @@
 import _pyejdb
 from pprint import pprint
+from collections import OrderedDict as odict
 from pyejdb import bson
+from pyejdb.typecheck import *
 import re
 
 __all__ = [
@@ -32,10 +34,6 @@ JBOLCKNB = _pyejdb.JBOLCKNB
 JBOTSYNC = _pyejdb.JBOTSYNC
 DEFAULT_OPEN_MODE = JBOWRITER | JBOCREAT | JBOTSYNC
 
-def check_collname(cname):
-    if not isinstance(cname, str):
-        raise TypeError("Collection name must be an instance of %s" % str.__name__)
-
 
 def check_oid(oid):
     if not isinstance(oid, str) or _oidRE.match(oid) is None:
@@ -58,26 +56,34 @@ class EJDB(object):
     def sync(self):
         return self.__ejdb.sync()
 
-    def save(self, cname, *jsarr, **kwargs):
-        check_collname(cname)
+    @typecheck
+    def save(self, cname : str, *jsarr, **kwargs):
         for doc in jsarr:
             _oid = self.__ejdb.save(cname, bson.serialize_to_bytes(doc), **kwargs)
             if "_id" not in doc:
                 doc["_id"] = _oid
 
-    def load(self, cname, oid):
-        check_collname(cname)
+    @typecheck
+    def load(self, cname : str, oid : str):
         check_oid(oid)
         docbytes = self.__ejdb.load(cname, oid)
         if docbytes is None:
             return None
         return bson.parse_bytes(docbytes)
 
-    def remove(self, cname, oid):
-        check_collname(cname)
+    @typecheck
+    def remove(self, cname : str, oid):
         check_oid(oid)
         return self.__ejdb.remove(cname, oid)
 
+    @typecheck
+    def find(self, cname : str, qobj : optional(dict)=None, *args, **kwargs):
+        if not qobj: qobj = {}
+        qobj = bson.serialize_to_bytes(qobj)
+        hints = bson.serialize_to_bytes(kwargs["hints"] if "hints" in kwargs else {})
+        orarr = [bson.serialize_to_bytes(x) for x in args]
+        return self.__ejdb.find(cname, qobj, orarr, hints)
+
 
 
 
index 023bfb3..23531a3 100644 (file)
@@ -116,7 +116,10 @@ static PyObject* EJDB_load(PEJDB *self, PyObject *args) {
     }
     bson_oid_t oid;
     bson_oid_from_string(&oid, soid);
-    bson *doc = ejdbloadbson(coll, &oid);
+    bson *doc;
+    Py_BEGIN_ALLOW_THREADS
+    doc = ejdbloadbson(coll, &oid);
+    Py_END_ALLOW_THREADS
     if (!doc) {
         if (ejdbecode(self->ejdb) != TCESUCCESS) {
             return set_ejdb_error(self->ejdb);
@@ -132,6 +135,7 @@ static PyObject* EJDB_load(PEJDB *self, PyObject *args) {
 static PyObject* EJDB_remove(PEJDB *self, PyObject *args) {
     const char *cname;
     const char *soid;
+    bool bret = false;
     if (!PyArg_ParseTuple(args, "ss:EJDB_remove", &cname, &soid)) {
         return NULL;
     }
@@ -144,12 +148,47 @@ static PyObject* EJDB_remove(PEJDB *self, PyObject *args) {
     }
     bson_oid_t oid;
     bson_oid_from_string(&oid, soid);
-    if (!ejdbrmbson(coll, &oid)) {
+    Py_BEGIN_ALLOW_THREADS
+    bret = ejdbrmbson(coll, &oid);
+    Py_END_ALLOW_THREADS
+    if (!bret) {
         return set_ejdb_error(self->ejdb);
     }
     Py_RETURN_NONE;
 }
 
+static PyObject* EJDB_find(PEJDB *self, PyObject *args) {
+    //return self.__ejdb.find(cname, qobj, qobj, orarr, hints)
+    const char *cname;
+    PyObject *qbsbufpy, *orlistpy, *hintsbufpy;
+    void *qbsbuf, *hintsbsbuf;
+    int qbsbufsz, hintsbsbufsz;
+    bool err = false;
+
+    if (!PyArg_ParseTuple(args, "sOOO:EJDB_find", &cname, &qbsbufpy, &orlistpy, &hintsbufpy)) {
+        return NULL;
+    }
+    if (bytes_to_void(qbsbufpy, &qbsbuf, &qbsbufsz)) {
+        return NULL;
+    }
+    if (bytes_to_void(hintsbufpy, &hintsbsbuf, &hintsbsbufsz)) {
+        return NULL;
+    }
+    if (!PyList_Check(orlistpy)) {
+        return set_error(PyExc_TypeError, "'orarr' must be an list object");
+    }
+    Py_ssize_t orlen =  PyList_GET_SIZE(orlistpy);
+    
+
+
+finish:
+    if (err) {
+        return NULL;
+    } else {
+        Py_RETURN_NONE;
+    }
+}
+
 /* EJDBType.tp_methods */
 static PyMethodDef EJDB_tp_methods[] = {
     {"open", (PyCFunction) EJDB_open, METH_VARARGS, NULL},
@@ -159,6 +198,7 @@ static PyMethodDef EJDB_tp_methods[] = {
     {"load", (PyCFunction) EJDB_load, METH_VARARGS, NULL},
     {"remove", (PyCFunction) EJDB_remove, METH_VARARGS, NULL},
     {"sync", (PyCFunction) EJDB_sync, METH_NOARGS, NULL},
+    {"find", (PyCFunction) EJDB_find, METH_VARARGS, NULL},
     {NULL}
 };
 
index 63da1a4..3820335 100644 (file)
@@ -27,6 +27,12 @@ class TestOne(unittest.TestCase):
         self.assertTrue(ldoc is None)
         ejdb.sync()
 
+    def test_find(self):
+        ejdb = TestOne._ejdb
+        self.assertEqual(ejdb.isopen, True)
+        ejdb.find("foocoll")
+
+
     @classmethod
     def tearDownClass(cls):
         if cls._ejdb: