Yet more work on record match.
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.28  1995-11-21 09:20:30  adam
8  * Yet more work on record match.
9  *
10  * Revision 1.27  1995/11/20  16:59:45  adam
11  * New update method: the 'old' keys are saved for each records.
12  *
13  * Revision 1.26  1995/11/20  11:56:24  adam
14  * Work on new traversal.
15  *
16  * Revision 1.25  1995/11/16  15:34:54  adam
17  * Uses new record management system in both indexer and server.
18  *
19  * Revision 1.24  1995/11/15  19:13:08  adam
20  * Work on record management.
21  *
22  * Revision 1.23  1995/10/27  14:00:10  adam
23  * Implemented detection of database availability.
24  *
25  * Revision 1.22  1995/10/17  18:02:07  adam
26  * New feature: databases. Implemented as prefix to words in dictionary.
27  *
28  * Revision 1.21  1995/10/10  12:24:38  adam
29  * Temporary sort files are compressed.
30  *
31  * Revision 1.20  1995/10/06  13:52:05  adam
32  * Bug fixes. Handler may abort further scanning.
33  *
34  * Revision 1.19  1995/10/04  12:55:16  adam
35  * Bug fix in ranked search. Use=Any keys inserted.
36  *
37  * Revision 1.18  1995/10/04  09:37:08  quinn
38  * Fixed bug.
39  *
40  * Revision 1.17  1995/10/03  14:28:57  adam
41  * Buffered read in extract works.
42  *
43  * Revision 1.16  1995/10/03  14:28:45  adam
44  * Work on more effecient read handler in extract.
45  *
46  * Revision 1.15  1995/10/02  15:42:53  adam
47  * Extract uses file descriptors instead of FILE pointers.
48  *
49  * Revision 1.14  1995/10/02  15:29:13  adam
50  * More logging in file_extract.
51  *
52  * Revision 1.13  1995/09/29  14:01:39  adam
53  * Bug fixes.
54  *
55  * Revision 1.12  1995/09/28  14:22:56  adam
56  * Sort uses smaller temporary files.
57  *
58  * Revision 1.11  1995/09/28  12:10:31  adam
59  * Bug fixes. Field prefix used in queries.
60  *
61  * Revision 1.10  1995/09/28  09:19:41  adam
62  * xfree/xmalloc used everywhere.
63  * Extract/retrieve method seems to work for text records.
64  *
65  * Revision 1.9  1995/09/27  12:22:28  adam
66  * More work on extract in record control.
67  * Field name is not in isam keys but in prefix in dictionary words.
68  *
69  * Revision 1.8  1995/09/14  07:48:22  adam
70  * Record control management.
71  *
72  * Revision 1.7  1995/09/11  13:09:32  adam
73  * More work on relevance feedback.
74  *
75  * Revision 1.6  1995/09/08  14:52:27  adam
76  * Minor changes. Dictionary is lower case now.
77  *
78  * Revision 1.5  1995/09/06  16:11:16  adam
79  * Option: only one word key per file.
80  *
81  * Revision 1.4  1995/09/05  15:28:39  adam
82  * More work on search engine.
83  *
84  * Revision 1.3  1995/09/04  12:33:41  adam
85  * Various cleanup. YAZ util used instead.
86  *
87  * Revision 1.2  1995/09/04  09:10:34  adam
88  * More work on index add/del/update.
89  * Merge sort implemented.
90  * Initial work on z39 server.
91  *
92  * Revision 1.1  1995/09/01  14:06:35  adam
93  * Split of work into more files.
94  *
95  */
96 #include <stdio.h>
97 #include <assert.h>
98 #include <unistd.h>
99 #include <fcntl.h>
100 #include <ctype.h>
101
102 #include <alexutil.h>
103 #include <recctrl.h>
104 #include "index.h"
105
106 #include "recindex.h"
107
108 static Dict file_idx;
109
110 static Records records = NULL;
111
112 static char **key_buf;
113 static size_t ptr_top;
114 static size_t ptr_i;
115 static size_t key_buf_used;
116 static int key_file_no;
117
118 void key_open (int mem)
119 {
120     if (mem < 50000)
121         mem = 50000;
122     key_buf = xmalloc (mem);
123     ptr_top = mem/sizeof(char*);
124     ptr_i = 0;
125
126     key_buf_used = 0;
127     key_file_no = 0;
128
129     if (!(file_idx = dict_open (FNAME_FILE_DICT, 40, 1)))
130     {
131         logf (LOG_FATAL, "dict_open fail of %s", "fileidx");
132         exit (1);
133     }
134     assert (!records);
135     records = rec_open (1);
136 }
137
138 struct encode_info {
139     int  sysno;
140     int  seqno;
141     char buf[512];
142 };
143
144 void encode_key_init (struct encode_info *i)
145 {
146     i->sysno = 0;
147     i->seqno = 0;
148 }
149
150 char *encode_key_int (int d, char *bp)
151 {
152     if (d <= 63)
153         *bp++ = d;
154     else if (d <= 16383)
155     {
156         *bp++ = 64 + (d>>8);
157         *bp++ = d  & 255;
158     }
159     else if (d <= 4194303)
160     {
161         *bp++ = 128 + (d>>16);
162         *bp++ = (d>>8) & 255;
163         *bp++ = d & 255;
164     }
165     else
166     {
167         *bp++ = 192 + (d>>24);
168         *bp++ = (d>>16) & 255;
169         *bp++ = (d>>8) & 255;
170         *bp++ = d & 255;
171     }
172     return bp;
173 }
174
175 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
176 {
177     struct it_key key;
178     char *bp = i->buf;
179
180     while ((*bp++ = *k++))
181         ;
182     memcpy (&key, k+1, sizeof(struct it_key));
183     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
184     if (i->sysno != key.sysno)
185     {
186         i->sysno = key.sysno;
187         i->seqno = 0;
188     }
189     bp = encode_key_int (key.seqno - i->seqno, bp);
190     i->seqno = key.seqno;
191     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
192     {
193         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
194         exit (1);
195     }
196 }
197
198 void key_flush (void)
199 {
200     FILE *outf;
201     char out_fname[200];
202     char *prevcp, *cp;
203     struct encode_info encode_info;
204     
205     if (ptr_i <= 0)
206         return;
207
208     key_file_no++;
209     logf (LOG_LOG, "sorting section %d", key_file_no);
210     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
211     sprintf (out_fname, TEMP_FNAME, key_file_no);
212
213     if (!(outf = fopen (out_fname, "w")))
214     {
215         logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
216         exit (1);
217     }
218     logf (LOG_LOG, "writing section %d", key_file_no);
219     prevcp = cp = key_buf[ptr_top-ptr_i];
220     
221     encode_key_init (&encode_info);
222     encode_key_write (cp, &encode_info, outf);
223     while (--ptr_i > 0)
224     {
225         cp = key_buf[ptr_top-ptr_i];
226         if (strcmp (cp, prevcp))
227         {
228             encode_key_init (&encode_info);
229             encode_key_write (cp, &encode_info, outf);
230             prevcp = cp;
231         }
232         else
233             encode_key_write (cp + strlen(cp), &encode_info, outf);
234     }
235     if (fclose (outf))
236     {
237         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
238         exit (1);
239     }
240     logf (LOG_LOG, "finished section %d", key_file_no);
241     ptr_i = 0;
242     key_buf_used = 0;
243 }
244
245 int key_close (void)
246 {
247     key_flush ();
248     xfree (key_buf);
249     rec_close (&records);
250     dict_close (file_idx);
251
252     return key_file_no;
253 }
254
255 static void wordInit (RecWord *p)
256 {
257     p->attrSet = 1;
258     p->attrUse = 1016;
259     p->which = Word_String;
260 }
261
262 struct recKeys {
263     int buf_used;
264     int buf_max;
265     char *buf;
266 } reckeys;
267
268 static void addRecordKey (const RecWord *p)
269 {
270     char *dst;
271     char attrSet;
272     short attrUse;
273     size_t i;
274
275     if (reckeys.buf_used+1024 > reckeys.buf_max)
276     {
277         char *b;
278
279         b = malloc (reckeys.buf_max += 65000);
280         if (reckeys.buf_used > 0)
281             memcpy (b, reckeys.buf, reckeys.buf_used);
282         free (reckeys.buf);
283         reckeys.buf = b;
284     }
285     dst = reckeys.buf + reckeys.buf_used;
286     switch (p->which)
287     {
288     case Word_String:
289         attrSet = p->attrSet;
290         memcpy (dst, &attrSet, sizeof(attrSet));
291         dst += sizeof(attrSet);
292
293         attrUse = p->attrUse;
294         memcpy (dst, &attrUse, sizeof(attrUse));
295         dst += sizeof(attrUse);
296         
297         for (i = 0; p->u.string[i]; i++)
298             *dst++ = p->u.string[i];
299         *dst++ = '\0';
300
301         memcpy (dst, &p->seqno, sizeof(p->seqno));
302         dst += sizeof(p->seqno);
303
304         break;
305     default:
306         return;
307     }
308     reckeys.buf_used = dst - reckeys.buf;
309 }
310
311 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
312                              const char *databaseName)
313 {
314     int off = 0;
315     while (off < reckeys->buf_used)
316     {
317         const char *src = reckeys->buf + off;
318         char attrSet;
319         short attrUse;
320         struct it_key key;
321         
322         memcpy (&attrSet, src, sizeof(attrSet));
323         src += sizeof(attrSet);
324
325         memcpy (&attrUse, src, sizeof(attrUse));
326         src += sizeof(attrUse);
327
328         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
329             key_flush ();
330         ++ptr_i;
331         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
332         key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
333                                            attrSet, attrUse, databaseName);
334         while (*src)
335             ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
336         ((char*)key_buf) [key_buf_used++] = '\0';
337         
338         ((char*) key_buf)[key_buf_used++] = cmd;
339
340         memcpy (&key.seqno, src, sizeof(key.seqno));
341         src += sizeof(key.seqno);
342         key.sysno = sysno;
343         memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
344         key_buf_used += sizeof(key);
345         off = src - reckeys->buf;
346     }
347     assert (off = reckeys->buf_used);
348 }
349
350 #if 0
351 static int key_cmd;
352 static int key_sysno;
353 static const char *key_databaseName;
354 static int key_del_max;
355 static int key_del_used;
356 static char *key_del_buf;
357
358 static void wordAdd (const RecWord *p)
359 {
360     struct it_key key;
361     size_t i;
362
363     if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
364         key_flush ();
365     ++ptr_i;
366     key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
367     key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
368                                 p->attrSet, p->attrUse,
369                                 key_databaseName);
370     switch (p->which)
371     {
372     case Word_String:
373         for (i = 0; p->u.string[i]; i++)
374             ((char*)key_buf) [key_buf_used++] =
375                 index_char_cvt (p->u.string[i]);
376         ((char*)key_buf) [key_buf_used++] = '\0';
377         break;
378     default:
379         return ;
380     }
381     ((char*) key_buf)[key_buf_used++] = ((key_cmd == 'a') ? 1 : 0);
382     key.sysno = key_sysno;
383     key.seqno = p->seqno;
384     memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
385     key_buf_used += sizeof(key);
386
387     if (key_cmd == 'a' && key_del_used >= 0)
388     {
389         char attrSet;
390         short attrUse;
391         if (key_del_used + 1024 > key_del_max)
392         {
393             char *kbn;
394             
395             if (!(kbn = malloc (key_del_max += 64000)))
396             {
397                 logf (LOG_FATAL, "malloc");
398                 exit (1);
399             }
400             if (key_del_buf)
401                 memcpy (kbn, key_del_buf, key_del_used);
402             free (key_del_buf);
403             key_del_buf = kbn;
404         }
405         switch (p->which)
406         {
407         case Word_String:
408             for (i = 0; p->u.string[i]; i++)
409                 ((char*)key_del_buf) [key_del_used++] = p->u.string[i];
410             ((char*)key_del_buf) [key_del_used++] = '\0';
411             break;
412         default:
413             return ;
414         }
415         attrSet = p->attrSet;
416         memcpy (key_del_buf + key_del_used, &attrSet, sizeof(attrSet));
417         key_del_used += sizeof(attrSet);
418
419         attrUse = p->attrUse;
420         memcpy (key_del_buf + key_del_used, &attrUse, sizeof(attrUse));
421         key_del_used += sizeof(attrUse);
422
423         memcpy (key_del_buf + key_del_used, &p->seqno, sizeof(p->seqno));
424         key_del_used += sizeof(p->seqno);
425     }
426 }
427
428 #endif
429
430 static void addRecordKeyAny (const RecWord *p)
431 {
432     if (p->attrSet != 1 || p->attrUse != 1016)
433     {
434         RecWord w;
435
436         memcpy (&w, p, sizeof(w));
437         w.attrSet = 1;
438         w.attrUse = 1016;
439         addRecordKey (&w);
440     }
441     addRecordKey (p);
442 }
443
444 static char *file_buf;
445 static int file_offset;
446 static int file_bufsize;
447
448 static void file_read_start (int fd)
449 {
450     file_offset = 0;
451     file_buf = xmalloc (4096);
452     file_bufsize = read (fd, file_buf, 4096);
453 }
454
455 static void file_read_stop (int fd)
456 {
457     xfree (file_buf);
458 }
459
460 static int file_read (int fd, char *buf, size_t count)
461 {
462     int l = file_bufsize - file_offset;
463
464     if (count > l)
465     {
466         int r;
467         if (l > 0)
468             memcpy (buf, file_buf + file_offset, l);
469         count = count-l;
470         if (count > file_bufsize)
471         {
472             if ((r = read (fd, buf + l, count)) == -1)
473             {
474                 logf (LOG_FATAL|LOG_ERRNO, "read");
475                 exit (1);
476             }
477             file_bufsize = 0;
478             file_offset = 0;
479             return r;
480         }
481         file_bufsize = r = read (fd, file_buf, 4096);
482         if (r == -1)
483         {
484             logf (LOG_FATAL|LOG_ERRNO, "read");
485             exit (1);
486         }
487         else if (r <= count)
488         {
489             file_offset = r;
490             memcpy (buf + l, file_buf, r);
491             return l + r;
492         }
493         else
494         {
495             file_offset = count;
496             memcpy (buf + l, file_buf, count - l);
497             return count;
498         }
499     }
500     memcpy (buf, file_buf + file_offset, count);
501     file_offset += count;
502     return count;
503 }
504
505 int fileExtract (SYSNO *sysno, const char *fname, const char *databaseName,
506                  int deleteFlag)
507 {
508     int i, r;
509     char ext[128];
510     char ext_res[128];
511     const char *file_type;
512     struct recExtractCtrl extractCtrl;
513     RecType rt;
514     Record rec;
515
516     logf (LOG_DEBUG, "fileExtractAdd %s", fname);
517
518     for (i = strlen(fname); --i >= 0; )
519         if (fname[i] == '/')
520         {
521             strcpy (ext, "");
522             break;
523         }
524         else if (fname[i] == '.')
525         {
526             strcpy (ext, fname+i+1);
527             break;
528         }
529     sprintf (ext_res, "fileExtension.%s", ext);
530     if (!(file_type = res_get (common_resource, ext_res)))
531         return 0;
532     if (!(rt = recType_byName (file_type)))
533         return 0;
534
535     if ((extractCtrl.fd = open (fname, O_RDONLY)) == -1)
536     {
537         logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
538         return 0;
539     }
540
541     extractCtrl.subType = "";
542     extractCtrl.init = wordInit;
543     extractCtrl.add = addRecordKeyAny;
544
545     reckeys.buf_used = 0;
546     file_read_start (extractCtrl.fd);
547     extractCtrl.readf = file_read;
548     r = (*rt->extract)(&extractCtrl);
549     file_read_stop (extractCtrl.fd);
550     close (extractCtrl.fd);
551   
552     if (r)      
553     {
554         logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
555         return 0;
556     }
557     if (! *sysno)  /* match criteria */
558     {
559         logf (LOG_LOG, "add record %s", fname);
560         rec = rec_new (records);
561         *sysno = rec->sysno;
562
563         flushRecordKeys (*sysno, 1, &reckeys, databaseName);
564     }
565     else
566     {
567         struct recKeys delkeys;
568         
569         rec = rec_get (records, *sysno);
570
571         delkeys.buf_used = rec->size[2];
572         delkeys.buf = rec->info[2];
573         flushRecordKeys (*sysno, 0, &delkeys, rec->info[3]);
574         flushRecordKeys (*sysno, 1, &reckeys, databaseName); 
575     }
576     free (rec->info[0]);
577     rec->info[0] = rec_strdup (file_type, &rec->size[0]);
578
579     free (rec->info[1]);
580     rec->info[1] = rec_strdup (fname, &rec->size[1]);
581
582     free (rec->info[2]);
583     if (reckeys.buf_used > 0)
584     {
585         rec->info[2] = malloc (reckeys.buf_used);
586         rec->size[2] = reckeys.buf_used;
587         memcpy (rec->info[2], reckeys.buf, rec->size[2]);
588     }
589     else
590     {
591         rec->info[2] = NULL;
592         rec->size[2] = 0;
593     }
594     free (rec->info[3]);
595     rec->info[3] = rec_strdup (databaseName, &rec->size[3]); 
596
597     rec_put (records, &rec);
598     return 1;
599 }