Don't dump core, when there were no recods indexed due to unknown type
[idzebra-moved-to-github.git] / index / extract.c
index cd3369d..fb6e228 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: extract.c,v 1.130 2002-11-07 09:07:07 adam Exp $
+/* $Id: extract.c,v 1.137 2003-02-26 12:30:54 pop Exp $
    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
    Index Data Aps
 
@@ -361,8 +361,9 @@ static char *fileMatchStr (ZebraHandle zh,
                 spec_src = rGroup->groupName;
             else if (!strcmp (special, "database"))
                 spec_src = rGroup->databaseName;
-            else if (!strcmp (special, "filename"))
+            else if (!strcmp (special, "filename")) {
                 spec_src = fname;
+           }
             else if (!strcmp (special, "type"))
                 spec_src = rGroup->recordType;
             else 
@@ -890,8 +891,6 @@ int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
         close (fd);
     return r;
 }
-
-
 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
                         const char *buf, size_t buf_size,
                         const char *databaseName, int delete_flag,
@@ -899,14 +898,53 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
                         int store_keys, int store_data,
                         const char *match_criteria)
 {
+    struct recordGroup rGroup;
+    rGroup.groupName = NULL;
+    rGroup.databaseName = (char *)databaseName;
+    rGroup.path = NULL;
+    rGroup.recordId = NULL;
+    rGroup.recordType = (char *)recordType;
+    rGroup.flagStoreData = store_data;
+    rGroup.flagStoreKeys = store_keys;
+    rGroup.flagRw = 1;
+    rGroup.databaseNamePath = 0;
+    rGroup.explainDatabase = 0;
+    rGroup.fileVerboseLimit = 100000;
+    rGroup.followLinks = -1;
+    return (bufferExtractRecord (zh,
+                                buf, buf_size,
+                                &rGroup,
+                                delete_flag,
+                                test_mode,
+                                recordType,
+                                sysno,
+                                match_criteria,
+                                "<no file>"));
+}
+/*
+  If sysno is provided, then it's used to identify the reocord.
+  If not, and match_criteria is provided, then sysno is guessed
+  If not, and a record is provided, then sysno is got from there
+  
+ */
+int bufferExtractRecord (ZebraHandle zh, 
+                        const char *buf, size_t buf_size,
+                        struct recordGroup *rGroup, 
+                        int delete_flag,
+                        int test_mode, 
+                        const char *recordType,
+                        int *sysno,
+                        const char *match_criteria,
+                        const char *fname)
+
+{
     RecordAttr *recordAttr;
     struct recExtractCtrl extractCtrl;
     int i, r;
     char *matchStr = 0;
-    RecType recType;
+    RecType recType = NULL;
     char subType[1024];
     void *clientData;
-    const char *fname = "<no file>";
     Record rec;
     long recordOffset = 0;
     struct zebra_fetch_control fc;
@@ -926,17 +964,10 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
     extractCtrl.fh = &fc;
 
     /* announce database */
-    if (zebraExplain_curDatabase (zh->reg->zei, databaseName))
-    {
-        if (zebraExplain_newDatabase (zh->reg->zei, databaseName, 0))
-           return 0;
-    }
-    if (!(recType =
-         recType_byName (zh->reg->recTypes, recordType, subType,
-                         &clientData)))
+    if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
     {
-        logf (LOG_WARN, "No such record type: %s", recordType);
-        return 0;
+      if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName, 0))
+       return 0;
     }
 
     zh->reg->keys.buf_used = 0;
@@ -947,6 +978,26 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
     zh->reg->sortKeys.buf_max = 0;
     zh->reg->sortKeys.buf = 0;
 
+    if (*recordType) {
+      logf (LOG_DEBUG, "Record type explicitly specified: %s", recordType);
+      recType = recType_byName (zh->reg->recTypes, recordType, subType,
+                               &clientData);
+    } else {
+      if (!(rGroup->recordType)) {
+        logf (LOG_WARN, "No such record type defined");
+        return 0;
+      }
+      logf (LOG_DEBUG, "Get record type from rgroup: %s",rGroup->recordType);
+      recType = recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
+                               &clientData);
+      recordType = rGroup->recordType;
+    }
+
+    if (!recType) {
+      logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
+      return 0;
+    }
+
     extractCtrl.subType = subType;
     extractCtrl.init = extract_init;
     extractCtrl.tokenAdd = extract_token_add;
@@ -990,6 +1041,28 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
        return 1;
     }
     /* match criteria */
+    matchStr = NULL;
+
+    if (! *sysno) {
+      char *rinfo;
+      if (strlen(match_criteria) > 0) {
+       matchStr = (char *)match_criteria;
+      } else {
+       if (rGroup->recordId && *rGroup->recordId) {
+         matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname, 
+                                  rGroup->recordId);
+       }
+      }
+      if (matchStr) {
+       rinfo = dict_lookup (zh->reg->matchDict, matchStr);
+       if (rinfo)
+         memcpy (sysno, rinfo+1, sizeof(*sysno));
+      } else {
+       logf (LOG_WARN, "Bad match criteria (recordID)");
+       return 0;
+      }
+
+    }
 
     if (! *sysno)
     {
@@ -1062,10 +1135,8 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
                logf (LOG_LOG, "delete %s %s %ld", recordType,
                      fname, (long) recordOffset);
                 zh->records_deleted++;
-#if 0
                 if (matchStr)
-                    dict_delete (matchDict, matchStr);
-#endif
+                    dict_delete (zh->reg->matchDict, matchStr);
                 rec_del (zh->reg->records, &rec);
             }
            rec_rm (&rec);
@@ -1103,7 +1174,7 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
 
     /* update delete keys */
     xfree (rec->info[recInfo_delKeys]);
-    if (zh->reg->keys.buf_used > 0 && store_keys == 1)
+    if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
     {
         rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
         rec->info[recInfo_delKeys] = zh->reg->keys.buf;
@@ -1143,7 +1214,7 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
 
     /* update store data */
     xfree (rec->info[recInfo_storeData]);
-    if (store_data == 1)
+    if (rGroup->flagStoreData == 1)
     {
         rec->size[recInfo_storeData] = recordAttr->recordSize;
         rec->info[recInfo_storeData] = (char *)
@@ -1174,18 +1245,19 @@ int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
     /* update database name */
     xfree (rec->info[recInfo_databaseName]);
     rec->info[recInfo_databaseName] =
-        rec_strdup (databaseName, &rec->size[recInfo_databaseName]); 
+        rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
 
     /* update offset */
     recordAttr->recordOffset = recordOffset;
     
     /* commit this record */
     rec_put (zh->reg->records, &rec);
-
     logRecord(zh);
     return 0;
 }
 
+
+
 int explain_extract (void *handle, Record rec, data1_node *n)
 {
     ZebraHandle zh = (ZebraHandle) handle;
@@ -1528,7 +1600,7 @@ static void extract_add_sort_string (RecWord *p, const char *string,
 {
     ZebraHandle zh = p->extractCtrl->handle;
     struct sortKeys *sk = &zh->reg->sortKeys;
-    size_t off = 0;
+    int off = 0;
 
     while (off < sk->buf_used)
     {
@@ -1663,6 +1735,12 @@ static void extract_add_complete_field (RecWord *p)
 void extract_token_add (RecWord *p)
 {
     WRBUF wrbuf;
+
+#if 0
+    yaz_log (LOG_LOG, "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
+             p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
+             p->string);
+#endif
     if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
                               p->string, p->length)))
     {
@@ -1685,13 +1763,13 @@ void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
                             int cmd, struct sortKeys *sk)
 {
     SortIdx sortIdx = zh->reg->sortIdx;
-    size_t off = 0;
+    int off = 0;
 
     sortIdx_sysno (sortIdx, sysno);
 
     while (off < sk->buf_used)
     {
-        int set, use, slen, l;
+        int set, use, slen;
         
         off += key_SU_decode(&set, sk->buf + off);
         off += key_SU_decode(&use, sk->buf + off);