forked from fumiyas/hyperestraier-encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
estmtdb.c
826 lines (674 loc) · 20 KB
/
estmtdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
/*************************************************************************************************
* Implementation of the MT-safe API
* Copyright (C) 2004-2007 Mikio Hirabayashi
* This file is part of Hyper Estraier.
* Hyper Estraier is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Hyper Estraier is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Hyper
* Estraier; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "estraier.h"
#include "estmtdb.h"
#include "myconf.h"
#define ESTMINIBNUM 31 /* bucket number of map for attributes */
/* private function prototypes */
static int est_global_lock(void);
static void est_global_unlock(void);
static int est_mtdb_lock(ESTMTDB *db);
static void est_mtdb_unlock(ESTMTDB *db);
/*************************************************************************************************
* API for MT-safe database
*************************************************************************************************/
/* Global mutex. */
pthread_mutex_t est_global_mutex = PTHREAD_MUTEX_INITIALIZER;
CBMAP *est_global_db_names = NULL;
/* Open a database. */
ESTMTDB *est_mtdb_open(const char *name, int omode, int *ecp){
ESTMTDB *mtdb;
ESTDB *db;
char *path;
assert(name && ecp);
if(!est_global_lock()){
*ecp = ESTELOCK;
return NULL;
}
if(!est_global_db_names){
est_global_db_names = cbmapopenex(ESTMINIBNUM);
cbglobalgc(est_global_db_names, (void (*)(void *))cbmapclose);
}
path = est_realpath(name);
if(cbmapget(est_global_db_names, path, -1, NULL)){
free(path);
*ecp = ESTEACCES;
est_global_unlock();
return NULL;
}
mtdb = cbmalloc(sizeof(ESTMTDB));
if(!(db = est_db_open(name, omode, ecp))){
free(mtdb);
free(path);
est_global_unlock();
return NULL;
}
free(path);
path = est_realpath(name);
cbmapput(est_global_db_names, path, -1, "", 0, FALSE);
mtdb->db = db;
mtdb->path = path;
pthread_mutex_init(&(mtdb->mutex), NULL);
est_global_unlock();
return mtdb;
}
/* Close a database. */
int est_mtdb_close(ESTMTDB *db, int *ecp){
int err;
assert(db && ecp);
if(!est_global_lock()){
*ecp = ESTELOCK;
return FALSE;
}
err = FALSE;
cbmapout(est_global_db_names, db->path, -1);
pthread_mutex_destroy(&(db->mutex));
free(db->path);
if(!est_db_close(db->db, ecp)) err = TRUE;
free(db);
est_global_unlock();
return err ? FALSE : TRUE;
}
/* Get the last happended error code of a database. */
int est_mtdb_error(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return ESTELOCK;
rv = est_db_error(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Check whether a database has a fatal error. */
int est_mtdb_fatal(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_fatal(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Add an index for narrowing or sorting with document attributes. */
int est_mtdb_add_attr_index(ESTMTDB *db, const char *name, int type){
int rv;
assert(db && name);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_add_attr_index(db->db, name, type);
est_mtdb_unlock(db);
return rv;
}
/* Flush index words in the cache of a database. */
int est_mtdb_flush(ESTMTDB *db, int max){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_flush(db->db, max);
est_mtdb_unlock(db);
return rv;
}
/* Synchronize updating contents of a database. */
int est_mtdb_sync(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_sync(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Optimize a database. */
int est_mtdb_optimize(ESTMTDB *db, int options){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_optimize(db->db, options);
est_mtdb_unlock(db);
return rv;
}
/* Merge another database. */
int est_mtdb_merge(ESTMTDB *db, const char *name, int options){
int rv;
assert(db && name);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_merge(db->db, name, options);
est_mtdb_unlock(db);
return rv;
}
/* Add a document to a database. */
int est_mtdb_put_doc(ESTMTDB *db, ESTDOC *doc, int options){
int rv;
assert(db && doc);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_put_doc(db->db, doc, options);
est_mtdb_unlock(db);
return rv;
}
/* Remove a document from a database. */
int est_mtdb_out_doc(ESTMTDB *db, int id, int options){
int rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_out_doc(db->db, id, options);
est_mtdb_unlock(db);
return rv;
}
/* Edit attributes of a document object in a database. */
int est_mtdb_edit_doc(ESTMTDB *db, ESTDOC *doc){
int rv;
assert(db && doc);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_edit_doc(db->db, doc);
est_mtdb_unlock(db);
return rv;
}
/* Retrieve a document in a database. */
ESTDOC *est_mtdb_get_doc(ESTMTDB *db, int id, int options){
ESTDOC *rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_get_doc(db->db, id, options);
est_mtdb_unlock(db);
return rv;
}
/* Retrieve the value of an attribute of a document in a database. */
char *est_mtdb_get_doc_attr(ESTMTDB *db, int id, const char *name){
char *rv;
assert(db && id > 0 && name);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_get_doc_attr(db->db, id, name);
est_mtdb_unlock(db);
return rv;
}
/* Get the ID of a document spacified by URI. */
int est_mtdb_uri_to_id(ESTMTDB *db, const char *uri){
int rv;
assert(db && uri);
if(!est_mtdb_lock(db)) return -1;
rv = est_db_uri_to_id(db->db, uri);
est_mtdb_unlock(db);
return rv;
}
/* Get the name of a database. */
const char *est_mtdb_name(ESTMTDB *db){
const char *rv;
assert(db);
if(!est_mtdb_lock(db)) return "";
rv = est_db_name(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the number of documents in a database. */
int est_mtdb_doc_num(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_doc_num(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the number of unique words in a database. */
int est_mtdb_word_num(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_word_num(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the size of a database. */
double est_mtdb_size(ESTMTDB *db){
double rv;
assert(db);
if(!est_mtdb_lock(db)) return 0.0;
rv = est_db_size(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Search documents corresponding a condition for a database. */
int *est_mtdb_search(ESTMTDB *db, ESTCOND *cond, int *nump, CBMAP *hints){
int *rv;
assert(db && cond && nump);
if(!est_mtdb_lock(db)){
est_db_set_ecode(db->db, ESTELOCK);
cbmapput(hints, "", 0, "0", -1, TRUE);
*nump = 0;
return cbmalloc(1);
}
rv = est_db_search(db->db, cond, nump, hints);
est_mtdb_unlock(db);
return rv;
}
/* Search plural databases for documents corresponding a condition. */
int *est_mtdb_search_meta(ESTMTDB **dbs, int dbnum, ESTCOND *cond, int *nump, CBMAP *hints){
ESTDB **cdbs;
CBMAP *umap;
const char *kbuf;
int i, *rv;
assert(dbs && dbnum >= 0 && cond && nump);
cdbs = cbmalloc(dbnum * sizeof(ESTDB *) + 1);
if(dpisreentrant){
umap = cbmapopenex(dbnum + 1);
for(i = 0; i < dbnum; i++){
if(!cbmapget(umap, (char *)&(dbs[i]), sizeof(ESTDB *), NULL) && !est_mtdb_lock(dbs[i])){
while(--i >= 0){
est_mtdb_unlock(dbs[i]);
}
free(cdbs);
est_db_set_ecode(cdbs[i], ESTELOCK);
cbmapput(hints, "", 0, "0", -1, TRUE);
*nump = 0;
return cbmalloc(1);
}
cbmapput(umap, (char *)&(dbs[i]), sizeof(ESTDB *), "", 0, FALSE);
cdbs[i] = dbs[i]->db;
}
rv = est_db_search_meta(cdbs, dbnum, cond, nump, hints);
cbmapiterinit(umap);
while((kbuf = cbmapiternext(umap, NULL)) != NULL){
est_mtdb_unlock(*(ESTMTDB **)kbuf);
}
cbmapclose(umap);
} else {
if(pthread_mutex_lock(&est_global_mutex) != 0){
*nump = 0;
return cbmalloc(1);
}
for(i = 0; i < dbnum; i++){
cdbs[i] = dbs[i]->db;
}
rv = est_db_search_meta(cdbs, dbnum, cond, nump, hints);
pthread_mutex_unlock(&est_global_mutex);
}
free(cdbs);
return rv;
}
/* Check whether a document object matches the phrase of a search condition object definitely. */
int est_mtdb_scan_doc(ESTMTDB *db, ESTDOC *doc, ESTCOND *cond){
int rv;
assert(db && doc && cond);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_scan_doc(db->db, doc, cond);
est_mtdb_unlock(db);
return rv;
}
/* Set the maximum size of the cache memory of a database. */
void est_mtdb_set_cache_size(ESTMTDB *db, size_t size, int anum, int tnum, int rnum){
assert(db);
if(!est_mtdb_lock(db)) return;
est_db_set_cache_size(db->db, size, anum, tnum, rnum);
est_mtdb_unlock(db);
}
/* Add a pseudo index directory to a database. */
int est_mtdb_add_pseudo_index(ESTMTDB *db, const char *path){
int rv;
assert(db && path);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_add_pseudo_index(db->db, path);
est_mtdb_unlock(db);
return rv;
}
/*************************************************************************************************
* features for experts
*************************************************************************************************/
/* Set the error code of a database. */
void est_mtdb_set_ecode(ESTMTDB *db, int ecode){
assert(db);
if(!est_mtdb_lock(db)) return;
est_db_set_ecode(db->db, ecode);
est_mtdb_unlock(db);
}
/* Check whether an option is set. */
int est_mtdb_check_option(ESTMTDB *db, int option){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return -1;
rv = est_db_check_option(db->db, option);
est_mtdb_unlock(db);
return rv;
}
/* Get the inode number of a database. */
int est_mtdb_inode(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return -1;
rv = est_db_inode(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Set the entity data of a document in a database. */
int est_mtdb_set_doc_entity(ESTMTDB *db, int id, const char *ptr, int size){
int rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_set_doc_entity(db->db, id, ptr, size);
est_mtdb_unlock(db);
return rv;
}
/* Get the entity data of a document in a database. */
char *est_mtdb_get_doc_entity(ESTMTDB *db, int id, int *sp){
char *rv;
assert(db && id > 0 && sp);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_get_doc_entity(db->db, id, sp);
est_mtdb_unlock(db);
return rv;
}
/* Set the maximum number of extension of wild cards. */
void est_mtdb_set_wildmax(ESTMTDB *db, int num){
assert(db && num >= 0);
if(!est_mtdb_lock(db)) return;
est_db_set_wildmax(db->db, num);
est_mtdb_unlock(db);
}
/* Add a piece of meta data to a database. */
void est_mtdb_add_meta(ESTMTDB *db, const char *name, const char *value){
assert(db && name);
if(!est_mtdb_lock(db)) return;
est_db_add_meta(db->db, name, value);
est_mtdb_unlock(db);
}
/* Get a list of names of meta data of a database. */
CBLIST *est_mtdb_meta_names(ESTMTDB *db){
CBLIST *rv;
assert(db);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_meta_names(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the value of a piece of meta data of a database. */
char *est_mtdb_meta(ESTMTDB *db, const char *name){
char *rv;
assert(db && name);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_meta(db->db, name);
est_mtdb_unlock(db);
return rv;
}
/* Extract keywords of a document object. */
CBMAP *est_mtdb_etch_doc(ESTMTDB *db, ESTDOC *doc, int max){
CBMAP *rv;
assert(doc && max >= 0);
if(!db) return est_db_etch_doc(NULL, doc, max);
if(!est_mtdb_lock(db)) return cbmapopenex(1);
rv = est_db_etch_doc(db->db, doc, max);
est_mtdb_unlock(db);
return rv;
}
/* Store a map object of keywords. */
int est_mtdb_put_keywords(ESTMTDB *db, int id, CBMAP *kwords, double weight){
int rv;
assert(db && id > 0 && kwords);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_put_keywords(db->db, id, kwords, weight);
est_mtdb_unlock(db);
return rv;
}
/* Remove keywords of a document. */
int est_mtdb_out_keywords(ESTMTDB *db, int id){
int rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_out_keywords(db->db, id);
est_mtdb_unlock(db);
return rv;
}
/* Retrieve a map object of keywords. */
CBMAP *est_mtdb_get_keywords(ESTMTDB *db, int id){
CBMAP *rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_get_keywords(db->db, id);
est_mtdb_unlock(db);
return rv;
}
/* Mesure the total size of each inner records of a stored document. */
int est_mtdb_measure_doc(ESTMTDB *db, int id, int parts){
int rv;
assert(db && id > 0);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_measure_doc(db->db, id, parts);
est_mtdb_unlock(db);
return rv;
}
/* Initialize the iterator of a database. */
int est_mtdb_iter_init(ESTMTDB *db, const char *prev){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_iter_init(db->db, prev);
est_mtdb_unlock(db);
return rv;
}
/* Get the next ID of the iterator of a database. */
int est_mtdb_iter_next(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return -1;
rv = est_db_iter_next(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Initialize the word iterator of a database. */
int est_mtdb_word_iter_init(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_word_iter_init(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the next word of the word iterator of a database. */
char *est_mtdb_word_iter_next(ESTMTDB *db){
char *rv;
assert(db);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_word_iter_next(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the size of the record of a word. */
int est_mtdb_word_rec_size(ESTMTDB *db, const char *word){
int rv;
assert(db && word);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_word_rec_size(db->db, word);
est_mtdb_unlock(db);
return rv;
}
/* Get the number of unique keywords in a database. */
int est_mtdb_keyword_num(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_keyword_num(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Initialize the keyword iterator of a database. */
int est_mtdb_keyword_iter_init(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_keyword_iter_init(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the next keyword of the word iterator of a database. */
char *est_mtdb_keyword_iter_next(ESTMTDB *db){
char *rv;
assert(db);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_keyword_iter_next(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the size of the record of a keyword. */
int est_mtdb_keyword_rec_size(ESTMTDB *db, const char *word){
int rv;
assert(db && word);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_keyword_rec_size(db->db, word);
est_mtdb_unlock(db);
return rv;
}
/* Search documents corresponding a keyword for a database. */
int *est_mtdb_keyword_search(ESTMTDB *db, const char *word, int *nump){
int *rv;
assert(db && word && nump);
if(!est_mtdb_lock(db)) return NULL;
rv = est_db_keyword_search(db->db, word, nump);
est_mtdb_unlock(db);
return rv;
}
/* Get the number of records in the cache memory of a database. */
int est_mtdb_cache_num(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_cache_num(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the size of used cache region. */
int est_mtdb_used_cache_size(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return FALSE;
rv = est_db_used_cache_size(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Set the special cache for narrowing and sorting with document attributes. */
void est_mtdb_set_special_cache(ESTMTDB *db, const char *name, int num){
assert(db && name && num >= 0);
if(!est_mtdb_lock(db)) return;
est_db_set_special_cache(db->db, name, num);
est_mtdb_unlock(db);
}
/* Set the callback function to inform of database events. */
void est_mtdb_set_informer(ESTMTDB *db, void (*func)(const char *, void *), void *opaque){
assert(db && func);
if(!est_mtdb_lock(db)) return;
est_db_set_informer(db->db, func, opaque);
est_mtdb_unlock(db);
}
/* Fill the cache for keys for TF-IDF. */
void est_mtdb_fill_key_cache(ESTMTDB *db){
assert(db);
if(!est_mtdb_lock(db)) return;
est_db_fill_key_cache(db->db);
est_mtdb_unlock(db);
}
/* Set the database of document frequency. */
void est_mtdb_set_dfdb(ESTMTDB *db, DEPOT *dfdb){
assert(db && dfdb);
if(!est_mtdb_lock(db)) return;
est_db_set_dfdb(db->db, dfdb);
est_mtdb_unlock(db);
}
/* Clear the result cache. */
void est_mtdb_refresh_rescc(ESTMTDB *db){
assert(db);
if(!est_mtdb_lock(db)) return;
est_db_refresh_rescc(db->db);
est_mtdb_unlock(db);
}
/* Charge the result cache. */
void est_mtdb_charge_rescc(ESTMTDB *db, int max){
assert(db);
if(!est_mtdb_lock(db)) return;
est_db_charge_rescc(db->db, max);
est_mtdb_unlock(db);
}
/* Get a list of words in the result cache. */
CBLIST *est_mtdb_list_rescc(ESTMTDB *db){
CBLIST *rv;
assert(db);
if(!est_mtdb_lock(db)) return cblistopen();
rv = est_db_list_rescc(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get the number of pseudo documents in a database. */
int est_mtdb_pseudo_doc_num(ESTMTDB *db){
int rv;
assert(db);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_pseudo_doc_num(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Get a list of expressions of attribute indexes of a database. */
CBLIST *est_mtdb_attr_index_exprs(ESTMTDB *db){
CBLIST *rv;
assert(db);
if(!est_mtdb_lock(db)) return 0;
rv = est_db_attr_index_exprs(db->db);
est_mtdb_unlock(db);
return rv;
}
/* Interrupt long time processing. */
void est_mtdb_interrupt(ESTMTDB *db){
assert(db);
est_db_interrupt(db->db);
}
/*************************************************************************************************
* features for hyper experts
*************************************************************************************************/
/* Get the usage ratio of the cache. */
double est_mtdb_cache_usage(ESTMTDB *db){
if(db->db->flsflag) return 1.0;
return (double)db->db->icsiz / db->db->icmax;
}
/*************************************************************************************************
* private objects
*************************************************************************************************/
/* Lock the global environment.
The return value is true if success, else it is false. */
static int est_global_lock(void){
return pthread_mutex_lock(&est_global_mutex) == 0;
}
/* Unlock the global environment. */
static void est_global_unlock(void){
pthread_mutex_unlock(&est_global_mutex);
}
/* Lock a database object.
`db' specifies a database object.
The return value is true if success, else it is false. */
static int est_mtdb_lock(ESTMTDB *db){
assert(db);
if(dpisreentrant){
if(pthread_mutex_lock(&(db->mutex)) != 0){
est_db_set_ecode(db->db, ESTELOCK);
return FALSE;
}
return TRUE;
}
if(pthread_mutex_lock(&est_global_mutex) != 0){
est_db_set_ecode(db->db, ESTELOCK);
return FALSE;
}
return TRUE;
}
/* Unlock a database object.
`db' specifies a database object. */
static void est_mtdb_unlock(ESTMTDB *db){
assert(db);
if(dpisreentrant){
pthread_mutex_unlock(&(db->mutex));
} else {
pthread_mutex_unlock(&est_global_mutex);
}
}
/* END OF FILE */