[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/TextIndexNG/src - indexsupport.c:1.1.2.7

Andreas Jung andreas@digicool.com
Mon, 18 Mar 2002 20:48:05 -0500


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/TextIndexNG/src
In directory cvs.zope.org:/tmp/cvs-serv2821/src

Modified Files:
      Tag: ajung-textindexng-branch
	indexsupport.c 
Log Message:
reimplemented main insert loop for WordDocStorage in C


=== Zope/lib/python/Products/PluginIndexes/TextIndexNG/src/indexsupport.c 1.1.2.6 => 1.1.2.7 ===
 PyObject *IIBTreeModule, *IISet, *IIBucket;
 
+
+int importIIBTreeModule(void)
+{
+    PyObject  *mod_dict;
+
+    IIBTreeModule = PyImport_ImportModule("BTrees.IIBTree");
+    if (! IIBTreeModule)
+        return 0;
+
+    mod_dict = PyModule_GetDict(IIBTreeModule);
+
+    IISet = PyDict_GetItemString(mod_dict, "IISet");
+    if (! IISet)
+        return 0;
+
+    IIBucket = PyDict_GetItemString(mod_dict, "IIBucket");
+    if (! IIBucket)
+        return 0;
+
+    return 1;
+}
+
+PyObject *IISetFactory(void)
+{
+    PyObject *iiset;
+    iiset = PyObject_CallFunction(IISet,NULL);
+    return iiset;
+}
+
+PyObject *IIBucketFactory(void)
+{
+    PyObject *iibucket;
+    iibucket = PyObject_CallFunction(IIBucket,NULL);
+    return iibucket;
+}
+
+
+
 static PyObject *
 stopwordfilter(PyObject *modinfo, PyObject *args)
 {
@@ -71,6 +109,79 @@
  
     - fwdIdx  --  forward index (OIBTree)
     - reverseIdx -- reverse index (IOBtree)
+    - wordIds -- sequence of wordIds to be inserted 
+    - docId --  documentId
+*/
+
+PyObject *storageBatchInsert(PyObject *modinfo,PyObject *args)
+{
+
+    PyObject *fwdIdx,*revIdx, *wids, *wid, *docId, *insert, *update,*iiset;
+    int i;
+
+    if (! PyArg_ParseTuple(args, "OOOO", &fwdIdx,&revIdx,&wids,&docId))
+        return NULL;
+
+    if (! PyMapping_Check(fwdIdx)) {
+        PyErr_SetString(PyExc_TypeError, "1st argument must be IOBTree instance");
+        return NULL;
+    }
+
+    if (! PyMapping_Check(revIdx)) {
+        PyErr_SetString(PyExc_TypeError, "2nd argument must be IOTree instance");
+        return NULL;
+    }
+
+    if (! PySequence_Check(wids)) {
+        PyErr_SetString(PyExc_TypeError, "3rd argument must be sequence");
+        return NULL;
+    }
+
+    if (! PyInt_Check(docId)) {
+        PyErr_SetString(PyExc_TypeError, "4th argument must be integer");
+        return NULL;
+    }
+
+
+    // Forward index
+
+    for (i=0; i<PySequence_Length(wids);i++) {
+        PyObject *docIds;
+
+        wid = PySequence_GetItem(wids,i);
+
+        if (! PyMapping_HasKey(fwdIdx,wid)) {
+            PyObject *iiset;
+            iiset = IISetFactory();
+            PyObject_SetItem(fwdIdx, wid, iiset);
+        }
+
+        docIds = PyObject_GetItem(fwdIdx, wid);
+        insert = PyObject_GetAttrString(docIds, "insert");
+        PyObject_CallFunction(insert,"O", docId);
+    }
+
+    // Reverse index
+
+    if (PyMapping_HasKey(revIdx, docId)) {
+        iiset = PyObject_GetItem(revIdx, docId);
+    } else {
+        iiset = IISetFactory();
+    }
+
+    update = PyObject_GetAttrString(iiset, "update");
+    PyObject_CallFunction(update,"O", wids);
+    PyObject_SetItem(revIdx, docId, iiset);
+
+    return Py_None;
+}
+
+
+/*
+    Arguments:
+ 
+    - fwdIdx  --  forward index (OIBTree)
+    - reverseIdx -- reverse index (IOBtree)
     - words -- sequence of words to be inserted 
 */
 
@@ -124,48 +235,18 @@
     return wids;
 }
 
-int importIIBTreeModule(void)
-{
-    PyObject  *mod_dict;
-
-    IIBTreeModule = PyImport_ImportModule("BTrees.IIBTree");
-    if (! IIBTreeModule)
-        return 0;
-
-    mod_dict = PyModule_GetDict(IIBTreeModule);
-
-    IISet = PyDict_GetItemString(mod_dict, "IISet");
-    if (! IISet) return 0;
-
-    IIBucket = PyDict_GetItemString(mod_dict, "IIBucket");
-    if (! IIBucket) return 0;
-
-    return 1;
-}
-
-PyObject *IISetFactory()
-{
-    PyObject *iiset;
-    iiset = PyObject_CallFunction(IISet,NULL);
-    return iiset;
-}
-
-PyObject *IIBucketFactory()
-{
-    PyObject *iibucket;
-    iibucket = PyObject_CallFunction(IIBucket,NULL);
-    return iibucket;
-}
-
 
 static struct PyMethodDef indexsupport_module_methods[] =
     {
         { "stopwordfilter", (PyCFunction) stopwordfilter, METH_VARARGS,
-          "stopwordfilter(wordslist,stopword dict') " "-- filters words from wordslist that are stopwords"
+            "stopwordfilter(wordslist,stopword dict') " "-- filters words from wordslist that are stopwords"
         },
         { "vocabBatchInsert", (PyCFunction) vocabularyBatchInsert, METH_VARARGS,
           "vocabBatchInsert(fwdIdx,revIdx,wordLst)" "-- inserts forward and backward entries for vocabularies"
         },
+        { "storageBatchInsert", (PyCFunction) storageBatchInsert, METH_VARARGS,
+          "storageBatchInsert(fwdIdx,revIdx,wordLst,docId)" "-- inserts forward and backward entries for WordDocStorage"
+        },
         { "listIndexes", (PyCFunction) listIndexes, METH_VARARGS,
           "listIndexes(list of words',word) " "-- find all positions of word in a list of words"
         },
@@ -194,7 +275,7 @@
     PyDict_SetItemString(d, "__version__",
                          PyString_FromStringAndSize(rev+11,strlen(rev+11)-2));
     if (! importIIBTreeModule()) {
-        Py_FatalError("can't initialize module indexsupport");
+        Py_FatalError("importing IIBTree failed");
     }
     if (PyErr_Occurred())
         Py_FatalError("can't initialize module indexsupport");