Commits

Anonymous committed 3e1e73e

add iterator support for table type

Comments (0)

Files changed (3)

tokyocabinet/btree.c

 BTree_subscript(BTree *self, PyObject *key)
 {
     char *kbuf, *vbuf;
-    int ksiz;
-    int vsiz;
+    Py_ssize_t ksiz;
+    Py_ssize_t vsiz;
     PyObject *value;
+    int tcvsiz;
     
     if (!PyString_Check(key))
     {
     }
     
     Py_BEGIN_ALLOW_THREADS
-    vbuf = tcbdbget(self->db, kbuf, (int) ksiz, &vsiz);
+    vbuf = tcbdbget(self->db, kbuf, (int) ksiz, &tcvsiz);
     Py_END_ALLOW_THREADS
-    
+    vsiz = tcvsiz;
+
     if (!vbuf)
     {
         raise_btree_error(self->db);
 {
     bool success;
     char *kbuf, *vbuf;
-    int ksiz, vsiz;
+    Py_ssize_t ksiz, vsiz;
     
     if (!PyString_Check(key))
     {
 BTree_contains(BTree *self, PyObject *value)
 {
     char *kbuf;
-    int ksiz;
-    int vsiz;
+    Py_ssize_t ksiz;
+    Py_ssize_t vsiz;
     
     if (!PyString_Check(value))
     {

tokyocabinet/hash.c

 Hash_subscript(Hash *self, PyObject *key)
 {
     char *kbuf, *vbuf;
-    int ksiz;
-    int vsiz;
+    Py_ssize_t ksiz;
+    Py_ssize_t vsiz;
     PyObject *value;
+    int tcvsiz;
     
     if (!PyString_Check(key))
     {
     }
     
     Py_BEGIN_ALLOW_THREADS
-    vbuf = tchdbget(self->db, kbuf, (int) ksiz, &vsiz);
+    vbuf = tchdbget(self->db, kbuf, (int) ksiz, &tcvsiz);
     Py_END_ALLOW_THREADS
+    vsiz = tcvsiz;
     
     if (!vbuf)
     {
 {
     bool success;
     char *kbuf, *vbuf;
-    int ksiz, vsiz;
+    Py_ssize_t ksiz, vsiz;
     
     if (!PyString_Check(key))
     {
 Hash_contains(Hash *self, PyObject *value)
 {
     char *kbuf;
-    int ksiz;
-    int vsiz;
+    Py_ssize_t ksiz;
+    Py_ssize_t vsiz;
     
     if (!PyString_Check(value))
     {

tokyocabinet/table.c

     }
     
     PyObject *key, *value;
-    int pos = 0;
+    Py_ssize_t pos = 0;
     const char *kstr, *vstr;
     TCMAP *map;
     
     return Py_BuildValue("i", vsiz);
 }
 
+static PyObject *
+Table_iter(Table *self)
+{
+    bool result;
+    Py_BEGIN_ALLOW_THREADS
+    result = tctdbiterinit(self->db);
+    Py_END_ALLOW_THREADS
+    if(!result){
+        return NULL;
+    }
+    Py_INCREF(self);
+    return (PyObject*)self;
+}
+
+static PyObject *
+Table_iternext(Table *self)
+{
+    const char *key;
+    PyObject *pykey;
+
+    key = tctdbiternext2(self->db);
+    if (!key) {
+        if (tctdbecode(self->db) == TCENOREC) {
+            PyErr_SetNone(PyExc_StopIteration);
+            return NULL;
+        }
+        return NULL;
+    }
+    pykey = PyString_FromString(key);
+    tcfree((void *)key);
+    return pykey;
+}
 
 static PyObject *
 Table_fwmkeys(Table *self, PyObject *args, PyObject *kwargs)
 Table_subscript(Table *self, PyObject *key)
 {
     char *kbuf;
-    int ksiz;
+    Py_ssize_t ksiz;
     TCMAP *cols;
     PyObject *value;
     
 {
     bool success;
     char *kbuf;
-    int ksiz;
+    Py_ssize_t ksiz;
     TCMAP *cols;
     
     if (!PyString_Check(key))
 Table_contains(Table *self, PyObject *value)
 {
     char *kbuf;
-    int ksiz;
-    int vsiz;
+    Py_ssize_t ksiz;
+    Py_ssize_t vsiz;
     
     if (!PyString_Check(value))
     {
   0,                                           /* tp_clear */
   0,                                           /* tp_richcompare */
   0,                                           /* tp_weaklistoffset */
-  0,                                           /* tp_iter */
-  0,                                           /* tp_iternext */
+  (getiterfunc)Table_iter,                     /* tp_iter */
+  (iternextfunc)Table_iternext,                /* tp_iternext */
   Table_methods,                               /* tp_methods */
   0,                                           /* tp_members */
   0,                                           /* tp_getset */