/[MITgcm]/mitgcm.org/devel/buildweb/pkg/swish-e/src/db_berkeley_db.c
ViewVC logotype

Contents of /mitgcm.org/devel/buildweb/pkg/swish-e/src/db_berkeley_db.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Fri Sep 20 19:47:29 2002 UTC (22 years, 10 months ago) by adcroft
Branch: Import, MAIN
CVS Tags: baseline, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
Importing web-site building process.

1 /*
2 **
3 ** This program and library is free software; you can redistribute it and/or
4 ** modify it under the terms of the GNU (Library) General Public License
5 ** as published by the Free Software Foundation; either version 2
6 ** of the License, or any later version.
7 **
8 ** This program is distributed in the hope that it will be useful,
9 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ** GNU (Library) General Public License for more details.
12 **
13 ** You should have received a copy of the GNU (Library) General Public License
14 ** along with this program; if not, write to the Free Software
15 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 **
17 ** db_berkeley_db module
18 **
19 ** 2001-07 jose ruiz initial coding
20 **
21 ** Tested and Built with db-3.29 (See www.sleepycat.com)
22 **
23 ** Must be linked with the correspondant libdb.a
24 **
25 */
26
27 #include "swish.h"
28 #include "file.h"
29 #include "error.h"
30 #include "string.h"
31 #include "mem.h"
32 #include "compress.h"
33 #include "hash.h"
34 #include "db.h"
35 #include "db_berkeley_db.h"
36
37
38 /*
39 -- init structures for this module
40 */
41
42 void initModule_DB_db(SWISH * sw)
43 {
44 struct MOD_DB *Db;
45
46 Db = (struct MOD_DB *) emalloc(sizeof(struct MOD_DB));
47
48 Db->DB_name = (char *) estrdup("gdbm");
49
50 Db->DB_Create = DB_Create_db;
51 Db->DB_Open = DB_Open_db;
52 Db->DB_Close = DB_Close_db;
53 Db->DB_Remove = DB_Remove_db;
54
55 Db->DB_InitWriteHeader = DB_InitWriteHeader_db;
56 Db->DB_WriteHeaderData = DB_WriteHeaderData_db;
57 Db->DB_EndWriteHeader = DB_EndWriteHeader_db;
58
59 Db->DB_InitReadHeader = DB_InitReadHeader_db;
60 Db->DB_ReadHeaderData = DB_ReadHeaderData_db;
61 Db->DB_EndReadHeader = DB_EndReadHeader_db;
62
63 Db->DB_InitWriteWords = DB_InitWriteWords_db;
64 Db->DB_GetWordID = DB_GetWordID_db;
65 Db->DB_WriteWord = DB_WriteWord_db;
66 Db->DB_WriteWordHash = DB_WriteWordHash_db;
67 Db->DB_WriteWordData = DB_WriteWordData_db;
68 Db->DB_EndWriteWords = DB_EndWriteWords_db;
69
70 Db->DB_InitReadWords = DB_InitReadWords_db;
71 Db->DB_ReadWordHash = DB_ReadWordHash_db;
72 Db->DB_ReadFirstWordInvertedIndex = DB_ReadFirstWordInvertedIndex_db;
73 Db->DB_ReadNextWordInvertedIndex = DB_ReadNextWordInvertedIndex_db;
74 Db->DB_ReadWordData = DB_ReadWordData_db;
75 Db->DB_EndReadWords = DB_EndReadWords_db;
76
77 Db->DB_InitWriteFiles = DB_InitWriteFiles_db;
78 Db->DB_WriteFile = DB_WriteFile_db;
79 Db->DB_EndWriteFiles = DB_EndWriteFiles_db;
80
81 Db->DB_InitReadFiles = DB_InitReadFiles_db;
82 Db->DB_ReadFile = DB_ReadFile_db;
83 Db->DB_EndReadFiles = DB_EndReadFiles_db;
84
85 Db->DB_InitWriteSortedIndex = DB_InitWriteSortedIndex_db;
86 Db->DB_WriteSortedIndex = DB_WriteSortedIndex_db;
87 Db->DB_EndWriteSortedIndex = DB_EndWriteSortedIndex_db;
88
89 Db->DB_InitReadSortedIndex = DB_InitReadSortedIndex_db;
90 Db->DB_ReadSortedIndex = DB_ReadSortedIndex_db;
91 Db->DB_EndReadSortedIndex = DB_EndReadSortedIndex_db;
92
93
94 #ifdef PROPFILE
95 Db->DB_WriteProperty = DB_WriteProperty_db;
96 Db->DB_ReadProperty = DB_ReadProperty_db;
97 Db->DB_Reopen_PropertiesForRead = DB_Reopen_PropertiesForRead_db;
98 #endif
99
100 sw->Db = Db;
101
102 return;
103 }
104
105
106 /*
107 -- release all wired memory for this module
108 */
109
110 void freeModule_DB_db(SWISH * sw)
111 {
112 efree(sw->Db->DB_name);
113 efree(sw->Db);
114 return;
115 }
116
117
118
119 /* ---------------------------------------------- */
120
121
122
123 /*
124 -- Config Directives
125 -- Configuration directives for this Module
126 -- return: 0/1 = none/config applied
127 */
128
129 int configModule_DB_db(SWISH * sw, StringList * sl)
130 {
131 // struct MOD_DB_db *md = sw->DB_db;
132 // char *w0 = sl->word[0];
133 int retval = 1;
134
135
136 retval = 0; // tmp due to empty routine
137
138 return retval;
139 }
140
141
142
143 struct Handle_DB_db *open_db_files(char *dbname, u_int32_t flags)
144 {
145 char *tmp;
146 DB *dbp;
147 struct Handle_DB_db *DB;
148 int ret;
149
150 /* Allocate structure */
151 DB = (struct Handle_DB_db *) emalloc(sizeof(struct Handle_DB_db));
152
153 DB->wordcounter = 0;
154
155 tmp = emalloc(strlen(dbname) + 7 + 1);
156
157
158 /* Create index DB */
159
160 /* db file to store header */
161 /* we will use a hash for the pair values */
162
163 sprintf(tmp, "%s_hdr.db", dbname);
164
165
166 if ((ret = db_create(&dbp, NULL, 0)) != 0)
167 {
168 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
169 }
170
171 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
172 {
173 dbp->err(dbp, ret, "%s", tmp);
174 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
175 }
176 DB->dbf_header = dbp;
177
178
179
180 /* db file to store words */
181
182 sprintf(tmp, "%s_wds.db", dbname);
183 if ((ret = db_create(&dbp, NULL, 0)) != 0)
184 {
185 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
186 }
187 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
188 {
189 dbp->err(dbp, ret, "%s", tmp);
190 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
191 }
192
193 DB->dbf_words = dbp;
194
195
196
197 /* db file to store properties */
198
199 sprintf(tmp, "%s_prp.db", dbname);
200 if ((ret = db_create(&dbp, NULL, 0)) != 0)
201 {
202 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
203 }
204 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
205 {
206 dbp->err(dbp, ret, "%s", tmp);
207 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
208 }
209
210 DB->dbf_properties = dbp;
211
212
213
214 /* db file to store word's data */
215
216 sprintf(tmp, "%s_wdd.db", dbname);
217 if ((ret = db_create(&dbp, NULL, 0)) != 0)
218 {
219 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
220 }
221 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
222 {
223 dbp->err(dbp, ret, "%s", tmp);
224 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
225 }
226 DB->dbf_worddata = dbp;
227
228
229
230 /* db file to store word's inverted index */
231
232 sprintf(tmp, "%s_wii.db", dbname);
233 if ((ret = db_create(&dbp, NULL, 0)) != 0)
234 {
235 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
236 }
237 if ((ret = dbp->open(dbp, tmp, NULL, DB_BTREE, flags, 0664)) != 0)
238 {
239 dbp->err(dbp, ret, "%s", tmp);
240 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
241 }
242 DB->dbf_invertedindex = dbp;
243
244
245
246 /* db file to store docs's data */
247
248 sprintf(tmp, "%s_doc.db", dbname);
249 if ((ret = db_create(&dbp, NULL, 0)) != 0)
250 {
251 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
252 }
253 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
254 {
255 dbp->err(dbp, ret, "%s", tmp);
256 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
257 }
258 DB->dbf_docs = dbp;
259
260
261
262 /* db file to store sorted indexes */
263
264 sprintf(tmp, "%s_srt.db", dbname);
265 if ((ret = db_create(&dbp, NULL, 0)) != 0)
266 {
267 progerr("db_berkeley_db.c error in db_create: %s\n", db_strerror(ret));
268 }
269 if ((ret = dbp->open(dbp, tmp, NULL, DB_HASH, flags, 0664)) != 0)
270 {
271 dbp->err(dbp, ret, "%s", tmp);
272 progerr("db_berkeley_db.c error in db->open: %s\n", db_strerror(ret));
273 }
274 DB->dbf_sorted_indexes = dbp;
275
276 efree(tmp);
277
278 return DB;
279 }
280
281 void *DB_Create_db(SWISH *sw, char *dbname)
282 {
283 return (void *) open_db_files(dbname, DB_CREATE);
284 }
285
286 void *DB_Open_db(SWISH *sw, char *dbname)
287 {
288 return (void *) open_db_files(dbname, DB_RDONLY);
289 }
290
291 void DB_Close_db(void *db)
292 {
293 DB *dbp;
294 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
295 int ret;
296
297 dbp = DB->dbf_header;
298 if ((ret = dbp->close(dbp, 0)) != 0)
299 {
300 progerr("db_berkeley_db.c error in db->close\n");
301 }
302
303 dbp = DB->dbf_words;
304 if ((ret = dbp->close(dbp, 0)) != 0)
305 {
306 progerr("db_berkeley_db.c error in db->close\n");
307 }
308
309 dbp = DB->dbf_worddata;
310 if ((ret = dbp->close(dbp, 0)) != 0)
311 {
312 progerr("db_berkeley_db.c error in db->close\n");
313 }
314
315 dbp = DB->dbf_invertedindex;
316 if ((ret = dbp->close(dbp, 0)) != 0)
317 {
318 progerr("db_berkeley_db.c error in db->close\n");
319 }
320
321 dbp = DB->dbf_docs;
322 if ((ret = dbp->close(dbp, 0)) != 0)
323 {
324 progerr("db_berkeley_db.c error in db->close\n");
325 }
326
327 dbp = DB->dbf_sorted_indexes;
328 if ((ret = dbp->close(dbp, 0)) != 0)
329 {
330 progerr("db_berkeley_db.c error in db->close\n");
331 }
332
333
334 dbp = DB->dbf_properties;
335 if ((ret = dbp->close(dbp, 0)) != 0)
336 {
337 progerr("db_berkeley_db.c error in db->close\n");
338 }
339
340
341 efree(DB);
342 }
343
344 void DB_Remove_db(void *db)
345 {
346 DB *dbp;
347 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
348
349 dbp = DB->dbf_header;
350 dbp = DB->dbf_words;
351 dbp = DB->dbf_worddata;
352 dbp = DB->dbf_invertedindex;
353 dbp = DB->dbf_docs;
354 dbp = DB->dbf_sorted_indexes;
355 dbp = DB->dbf_properties;
356
357 efree(DB);
358 }
359
360
361 /*--------------------------------------------*/
362 /*--------------------------------------------*/
363 /* Header stuff */
364 /*--------------------------------------------*/
365 /*--------------------------------------------*/
366
367 int DB_InitWriteHeader_db(void *db)
368 {
369 return 0;
370 }
371
372
373 int DB_EndWriteHeader_db(void *db)
374 {
375 return 0;
376 }
377
378 int DB_WriteHeaderData_db(int id, unsigned char *s, int len, void *db)
379 {
380 DB *dbp;
381 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
382
383 DBT key,
384 content;
385 int ret;
386
387 memset(&key, 0, sizeof(key));
388 memset(&content, 0, sizeof(content));
389
390 key.size = sizeof(int);
391
392 key.data = (char *) &id;
393
394 content.size = len;
395 content.data = s;
396
397 dbp = DB->dbf_header;
398 ret = dbp->put(dbp, NULL, &key, &content, 0);
399
400 if (ret)
401 progerr("Berkeley DB error writing header");
402
403 return 0;
404 }
405
406
407 int DB_InitReadHeader_db(void *db)
408 {
409 DB *dbp;
410 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
411
412 int ret;
413
414 dbp = DB->dbf_header;
415
416 /* Acquire a cursor for the database. */
417 if ((ret = dbp->cursor(dbp, NULL, &DB->cursor_header, 0)) != 0)
418 {
419 DB->cursor_header = NULL;
420 }
421
422 return 0;
423 }
424
425 int DB_ReadHeaderData_db(int *id, unsigned char **s, int *len, void *db)
426 {
427 DB *dbp;
428 DBC *dbcp;
429 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
430 DBT key,
431 content;
432 int ret;
433
434 dbp = DB->dbf_header;
435 dbcp = DB->cursor_header;
436
437
438 if (!dbcp)
439 {
440 *id = 0;
441 *len = 0;
442 *s = NULL;
443 }
444 else
445 {
446 /* Initialize the key/data pair so the flags aren't set. */
447 memset(&key, 0, sizeof(key));
448 memset(&content, 0, sizeof(content));
449
450 ret = dbcp->c_get(dbcp, &key, &content, DB_NEXT);
451 if (!ret)
452 {
453 *id = *(int *) key.data;
454 *len = content.size;
455 *s = emalloc(content.size);
456 memcpy(*s, content.data, content.size);
457 //*s = content.data;
458 }
459 else
460 {
461 *id = 0;
462 *len = 0;
463 *s = NULL;
464 }
465 }
466 return 0;
467 }
468
469
470 int DB_EndReadHeader_db(void *db)
471 {
472 DB *dbp;
473 DBC *dbcp;
474 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
475
476 dbp = DB->dbf_header;
477 dbcp = DB->cursor_header;
478
479 if (dbcp)
480 dbcp->c_close(dbcp);
481
482 return 0;
483 }
484
485 /*--------------------------------------------*/
486 /*--------------------------------------------*/
487 /* Word Stuff */
488 /*--------------------------------------------*/
489 /*--------------------------------------------*/
490
491 int DB_InitWriteWords_db(void *db)
492 {
493 return 0;
494 }
495
496
497 int DB_EndWriteWords_db(void *db)
498 {
499 return 0;
500 }
501
502 long DB_GetWordID_db(void *db)
503 {
504 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
505
506 return (long) ++(DB->wordcounter);
507 }
508
509 int DB_WriteWord_db(char *word, long wordID, void *db)
510 {
511 DB *dbp;
512 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
513
514 DBT key,
515 content;
516 int ret;
517
518 dbp = DB->dbf_invertedindex;
519
520 memset(&key, 0, sizeof(key));
521 memset(&content, 0, sizeof(content));
522
523 key.size = strlen(word) + 1;
524 key.data = (char *) word;
525
526 content.size = sizeof(int);
527
528 content.data = (char *) &wordID;
529
530 ret = dbp->put(dbp, NULL, &key, &content, 0);
531
532 if (ret)
533 progerr("DB error writing word");
534
535 return 0;
536 }
537
538
539 long DB_WriteWordData_db(long wordID, unsigned char *worddata, int lendata, void *db)
540 {
541 DB *dbp;
542 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
543
544 DBT key,
545 content;
546 int ret;
547
548 dbp = DB->dbf_worddata;
549
550 memset(&key, 0, sizeof(key));
551 memset(&content, 0, sizeof(content));
552
553 key.size = sizeof(long);
554
555 key.data = (char *) &wordID;
556
557 content.size = lendata;
558 content.data = worddata;
559
560 ret = dbp->put(dbp, NULL, &key, &content, 0);
561
562 if (ret)
563 progerr("DB error writing word's data");
564
565 return 0;
566 }
567
568
569
570 int DB_WriteWordHash_db(char *word, long wordID, void *db)
571 {
572 DB *dbp;
573 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
574
575 DBT key,
576 content;
577 int ret;
578
579 dbp = DB->dbf_words;
580
581 memset(&key, 0, sizeof(key));
582 memset(&content, 0, sizeof(content));
583
584 key.size = strlen(word) + 1;
585 key.data = (char *) word;
586
587 content.size = sizeof(int);
588
589 content.data = (char *) &wordID;
590
591 ret = dbp->put(dbp, NULL, &key, &content, 0);
592
593 if (ret)
594 progerr("DB error writing word");
595
596 return 0;
597 }
598
599 int DB_InitReadWords_db(void *db)
600 {
601 return 0;
602 }
603
604 int DB_EndReadWords_db(void *db)
605 {
606 return 0;
607 }
608
609
610 int DB_ReadWordHash_db(char *word, long *wordID, void *db)
611 {
612 DB *dbp;
613 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
614
615 DBT key,
616 content;
617 int ret;
618
619 dbp = DB->dbf_words;
620
621 memset(&key, 0, sizeof(key));
622 memset(&content, 0, sizeof(content));
623
624 key.size = strlen(word) + 1;
625 key.data = (char *) word;
626
627 ret = dbp->get(dbp, NULL, &key, &content, 0);
628
629 if (ret)
630 {
631 *wordID = 0;
632 }
633 else
634 {
635 *wordID = (long) *(int *) content.data;
636 }
637
638 return 0;
639 }
640
641
642
643 int DB_ReadFirstWordInvertedIndex_db(char *word, char **resultword, long *wordID, void *db)
644 {
645 DB *dbp;
646 DBC *dbcp;
647 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
648 DBT key,
649 content;
650 int ret;
651 int len;
652
653 len = strlen(word);
654
655 dbp = DB->dbf_invertedindex;
656 dbcp = NULL;
657
658 DB->cursor_inverted = NULL;
659
660 /* Acquire a cursor for the database. */
661 if ((ret = dbp->cursor(dbp, NULL, &DB->cursor_inverted, 0)) != 0)
662 {
663 DB->cursor_inverted = NULL;
664 }
665 dbcp = DB->cursor_inverted;
666
667 /* Initialize the key/data pair so the flags aren't set. */
668 memset(&key, 0, sizeof(key));
669 memset(&content, 0, sizeof(content));
670
671 key.size = strlen(word);
672 key.data = word;
673
674 ret = dbcp->c_get(dbcp, &key, &content, DB_SET_RANGE);
675 if (!ret)
676 {
677 if (strncmp(word, key.data, len) == 0)
678 {
679 *resultword = emalloc(key.size + 1);
680 memcpy(*resultword, key.data, key.size);
681 (*resultword)[key.size] = '\0';
682 *wordID = *(long *) content.data;
683 return 0;
684 }
685 }
686 *resultword = NULL;
687 *wordID = 0;
688 dbcp->c_close(dbcp);
689 DB->cursor_inverted = NULL;
690
691 return 0;
692 }
693
694 int DB_ReadNextWordInvertedIndex_db(char *word, char **resultword, long *wordID, void *db)
695 {
696 DB *dbp;
697 DBC *dbcp;
698 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
699 DBT key,
700 content;
701 int ret;
702 int len;
703
704 len = strlen(word);
705
706 dbp = DB->dbf_invertedindex;
707 dbcp = DB->cursor_inverted;
708
709
710 if (!dbcp)
711 {
712 *resultword = NULL;
713 *wordID = 0;
714 }
715 else
716 {
717 /* Initialize the key/data pair so the flags aren't set. */
718 memset(&key, 0, sizeof(key));
719 memset(&content, 0, sizeof(content));
720
721 ret = dbcp->c_get(dbcp, &key, &content, DB_NEXT);
722 if (!ret)
723 {
724 if (strncmp(word, key.data, len) == 0)
725 {
726
727 *resultword = emalloc(key.size + 1);
728 memcpy(*resultword, key.data, key.size);
729 (*resultword)[key.size] = '\0';
730 *wordID = *(long *) content.data;
731 return 0;
732 }
733 }
734 *resultword = NULL;
735 *wordID = 0;
736 dbcp->c_close(dbcp);
737 DB->cursor_inverted = NULL;
738 }
739 return 0;
740 }
741
742
743 long DB_ReadWordData_db(long wordID, unsigned char **worddata, int *lendata, void *db)
744 {
745 DB *dbp;
746 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
747
748 DBT key,
749 content;
750 int ret;
751
752 dbp = DB->dbf_worddata;
753
754 memset(&key, 0, sizeof(key));
755 memset(&content, 0, sizeof(content));
756
757 key.size = sizeof(long);
758
759 key.data = (char *) &wordID;
760
761 ret = dbp->get(dbp, NULL, &key, &content, 0);
762
763 if (ret)
764 {
765 *lendata = 0;
766 *worddata = NULL;
767 }
768 else
769 {
770 *lendata = content.size;
771 *worddata = emalloc(content.size);
772 memcpy(*worddata, content.data, content.size);
773 //*worddata = content.data;
774 }
775
776 return 0;
777 }
778
779 /*--------------------------------------------*/
780 /*--------------------------------------------*/
781 /* FileList Stuff */
782 /*--------------------------------------------*/
783 /*--------------------------------------------*/
784
785 int DB_InitWriteFiles_db(void *db)
786 {
787 return 0;
788 }
789
790
791 int DB_EndWriteFiles_db(void *db)
792 {
793 return 0;
794 }
795
796 int DB_WriteFile_db(int filenum, unsigned char *filedata, int sz_filedata, void *db)
797 {
798 DB *dbp;
799 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
800
801 DBT key,
802 content;
803 int ret;
804
805 dbp = DB->dbf_docs;
806
807 memset(&key, 0, sizeof(key));
808 memset(&content, 0, sizeof(content));
809
810 key.size = sizeof(int);
811
812 key.data = (char *) &filenum;
813
814 content.size = sz_filedata;
815 content.data = (char *) filedata;
816
817 ret = dbp->put(dbp, NULL, &key, &content, 0);
818
819 if (ret)
820 progerr("DB error writing docs");
821
822 return 0;
823 }
824
825 int DB_InitReadFiles_db(void *db)
826 {
827 return 0;
828 }
829
830 int DB_ReadFile_db(int filenum, unsigned char **filedata, int *sz_filedata, void *db)
831 {
832 DB *dbp;
833 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
834
835 DBT key,
836 content;
837 int ret;
838
839 dbp = DB->dbf_docs;
840
841 memset(&key, 0, sizeof(key));
842 memset(&content, 0, sizeof(content));
843
844 // This needs to be fixed --> The old compress problem with zeroes
845 filenum--;
846
847 key.size = sizeof(int);
848
849 key.data = (char *) &filenum;
850
851 ret = dbp->get(dbp, NULL, &key, &content, 0);
852
853 if (ret)
854 {
855 *sz_filedata = 0;
856 *filedata = NULL;
857 }
858 else
859 {
860 *sz_filedata = content.size;
861 *filedata = emalloc(content.size);
862 memcpy(*filedata, content.data, content.size);
863 //*filedata = content.data;
864 }
865 return 0;
866 }
867
868
869 int DB_EndReadFiles_db(void *db)
870 {
871 return 0;
872 }
873
874
875 /*--------------------------------------------*/
876 /*--------------------------------------------*/
877 /* Sorted data Stuff */
878 /*--------------------------------------------*/
879 /*--------------------------------------------*/
880
881
882
883
884
885 int DB_InitWriteSortedIndex_db(void *db)
886 {
887 return 0;
888 }
889
890 int DB_WriteSortedIndex_db(int propID, unsigned char *data, int sz_data, void *db)
891 {
892 DB *dbp;
893 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
894
895 DBT key,
896 content;
897 int ret;
898
899 dbp = DB->dbf_sorted_indexes;
900
901 memset(&key, 0, sizeof(key));
902 memset(&content, 0, sizeof(content));
903
904 key.size = sizeof(int);
905
906 key.data = (char *) &propID;
907
908 content.size = sz_data;
909 content.data = (char *) data;
910
911 ret = dbp->put(dbp, NULL, &key, &content, 0);
912
913 if (ret)
914 progerr("DB error writing sorted indexes");
915
916 return 0;
917 }
918
919 int DB_EndWriteSortedIndex_db(void *db)
920 {
921 return 0;
922 }
923
924
925 int DB_InitReadSortedIndex_db(void *db)
926 {
927 return 0;
928 }
929
930 int DB_ReadSortedIndex_db(int propID, unsigned char **data, int *sz_data, void *db)
931 {
932 DB *dbp;
933 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
934
935 DBT key,
936 content;
937 int ret;
938
939 dbp = DB->dbf_sorted_indexes;
940
941 memset(&key, 0, sizeof(key));
942 memset(&content, 0, sizeof(content));
943
944 key.size = sizeof(int);
945
946 key.data = (char *) &propID;
947
948 ret = dbp->get(dbp, NULL, &key, &content, 0);
949
950 if (ret)
951 {
952 *sz_data = 0;
953 *data = NULL;
954 }
955 else
956 {
957 *sz_data = content.size;
958 *data = emalloc(content.size);
959 memcpy(*data, content.data, content.size);
960 //*data = content.data;
961 }
962
963 return 0;
964 }
965
966 int DB_EndReadSortedIndex_db(void *db)
967 {
968 return 0;
969 }
970
971
972 #ifdef PROPFILE
973 /*--------------------------------------------*/
974 /*--------------------------------------------*/
975 /* Properties */
976 /*--------------------------------------------*/
977 /*--------------------------------------------*/
978
979
980 typedef union
981 {
982 char skey;
983 struct {
984 int filenum;
985 int propID;
986 } key;
987 } PropKEY;
988
989
990 void DB_WriteProperty_db( FileRec *fi, int propID, char *buffer, int datalen, void *db )
991 {
992 DB *dbp;
993 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
994 DBT key,
995 content;
996 PropKEY propkey;
997 int ret;
998
999
1000 /*** Just until I can get a flag to see if this data is needed ***/
1001 /*** create empty space using arrays! ***/
1002
1003 /* Create places to store the seek positions and lengths if first time */
1004 if ( !fi->propSize )
1005 {
1006 int i;
1007
1008 fi->propLocationsCount = fi->docProperties->n;
1009 fi->propLocations = (long *) emalloc( fi->propLocationsCount * sizeof( int *) );
1010 fi->propSize = (long *) emalloc( fi->propLocationsCount * sizeof( int *) );
1011
1012 /* Zero array */
1013 for( i = 0; i < fi->propLocationsCount; i++ )
1014 {
1015 fi->propSize[ i ] = 0; // here's the flag!
1016 fi->propLocations[ i ] = 0; // not here!
1017 }
1018 }
1019
1020
1021
1022 dbp = DB->dbf_properties;
1023
1024 memset(&key, 0, sizeof(key));
1025 memset(&content, 0, sizeof(content));
1026
1027 propkey.key.filenum = fi->filenum;
1028 propkey.key.propID = propID;
1029 key.size = sizeof(propkey);
1030 key.data = (char *) &propkey;
1031
1032 content.size = datalen;
1033 content.data = (char *) buffer;
1034
1035 ret = dbp->put(dbp, NULL, &key, &content, 0);
1036
1037 /* error message? */
1038 if (ret)
1039 progerr("DB error writing properties filenum: %d property %d", fi->filenum, propID);
1040 }
1041
1042
1043 char * DB_ReadProperty_db( FileRec *fi, int propID, void *db )
1044 {
1045 DB *dbp;
1046 struct Handle_DB_db *DB = (struct Handle_DB_db *) db;
1047 DBT key,
1048 content;
1049 PropKEY propkey;
1050 char *buffer;
1051 int ret;
1052
1053
1054 dbp = DB->dbf_properties;
1055
1056 memset(&key, 0, sizeof(key));
1057 memset(&content, 0, sizeof(content));
1058
1059 propkey.key.filenum = fi->filenum;
1060 propkey.key.propID = propID;
1061 key.size = sizeof(propkey);
1062 key.data = (char *) &propkey;
1063
1064 ret = dbp->get(dbp, NULL, &key, &content, 0);
1065
1066 /* No property for this file:id? */
1067 if (ret)
1068 return NULL;
1069
1070 buffer = (char *)emalloc(content.size);
1071 memcpy(buffer, content.data, content.size);
1072
1073 return buffer;
1074 }
1075
1076
1077
1078
1079 void DB_Reopen_PropertiesForRead_db(void *db)
1080 {
1081 return;
1082 }
1083 #endif

  ViewVC Help
Powered by ViewVC 1.1.22