forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdf_db.c
9135 lines (7295 loc) · 213 KB
/
rdf_db.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
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2002-2013, University of Amsterdam
VU University Amsterdam
This library 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 (at your option) any later version.
This library 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 this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define WITH_PL_MUTEX 1
#ifdef __WINDOWS__
#include <malloc.h>
#define inline __inline
#ifndef SIZEOF_LONG
#define SIZEOF_LONG 4
#endif
#endif
#include "rdf_db.h"
#include <wctype.h>
#include <ctype.h>
#include "murmur.h"
#include "memory.h"
#include "buffer.h"
#ifdef WITH_MD5
#include "md5.h"
#undef ERROR /* also in wingdi.h; we do not care */
#define ERROR -1
static void md5_triple(triple *t, md5_byte_t *digest);
static void sum_digest(md5_byte_t *digest, md5_byte_t *add);
static void dec_digest(md5_byte_t *digest, md5_byte_t *add);
static int md5_unify_digest(term_t t, md5_byte_t digest[16]);
#endif
void *
rdf_malloc(rdf_db *db, size_t size)
{ return malloc(size);
}
void
rdf_free(rdf_db *db, void *ptr, size_t size)
{ free(ptr);
}
static functor_t FUNCTOR_literal1;
static functor_t FUNCTOR_literal2;
static functor_t FUNCTOR_colon2;
static functor_t FUNCTOR_plus2;
static functor_t FUNCTOR_triples1;
static functor_t FUNCTOR_triples2;
static functor_t FUNCTOR_resources1;
static functor_t FUNCTOR_predicates1;
static functor_t FUNCTOR_duplicates1;
static functor_t FUNCTOR_literals1;
static functor_t FUNCTOR_subject1;
static functor_t FUNCTOR_predicate1;
static functor_t FUNCTOR_object1;
static functor_t FUNCTOR_graph1;
static functor_t FUNCTOR_indexed16;
static functor_t FUNCTOR_hash_quality1;
static functor_t FUNCTOR_hash3;
static functor_t FUNCTOR_hash4;
static functor_t FUNCTOR_exact1;
static functor_t FUNCTOR_plain1;
static functor_t FUNCTOR_substring1;
static functor_t FUNCTOR_word1;
static functor_t FUNCTOR_prefix1;
static functor_t FUNCTOR_like1;
static functor_t FUNCTOR_le1;
static functor_t FUNCTOR_between2;
static functor_t FUNCTOR_ge1;
static functor_t FUNCTOR_symmetric1;
static functor_t FUNCTOR_inverse_of1;
static functor_t FUNCTOR_transitive1;
static functor_t FUNCTOR_rdf_subject_branch_factor1; /* S --> BF*O */
static functor_t FUNCTOR_rdf_object_branch_factor1; /* O --> BF*S */
static functor_t FUNCTOR_rdfs_subject_branch_factor1; /* S --> BF*O */
static functor_t FUNCTOR_rdfs_object_branch_factor1; /* O --> BF*S */
static functor_t FUNCTOR_searched_nodes1;
static functor_t FUNCTOR_lang2;
static functor_t FUNCTOR_type2;
static functor_t FUNCTOR_gc4;
static functor_t FUNCTOR_graphs1;
static functor_t FUNCTOR_assert4;
static functor_t FUNCTOR_retract4;
static functor_t FUNCTOR_update5;
static functor_t FUNCTOR_new_literal1;
static functor_t FUNCTOR_old_literal1;
static functor_t FUNCTOR_transaction2;
static functor_t FUNCTOR_load2;
static functor_t FUNCTOR_begin1;
static functor_t FUNCTOR_end1;
static functor_t FUNCTOR_create_graph1;
static atom_t ATOM_user;
static atom_t ATOM_exact;
static atom_t ATOM_plain;
static atom_t ATOM_prefix;
static atom_t ATOM_substring;
static atom_t ATOM_word;
static atom_t ATOM_like;
static atom_t ATOM_error;
static atom_t ATOM_begin;
static atom_t ATOM_end;
static atom_t ATOM_error;
static atom_t ATOM_infinite;
static atom_t ATOM_snapshot;
static atom_t ATOM_true;
static atom_t ATOM_size;
static atom_t ATOM_optimize_threshold;
static atom_t ATOM_average_chain_len;
static atom_t ATOM_subPropertyOf;
static predicate_t PRED_call1;
#define MATCH_EXACT 0x01 /* exact triple match */
#define MATCH_SUBPROPERTY 0x02 /* Use subPropertyOf relations */
#define MATCH_SRC 0x04 /* Match graph location */
#define MATCH_INVERSE 0x08 /* use symmetric match too */
#define MATCH_QUAL 0x10 /* Match qualifiers too */
#define MATCH_DUPLICATE (MATCH_EXACT|MATCH_QUAL)
static int match_triples(rdf_db *db, triple *t, triple *p,
query *q, unsigned flags);
static void unlock_atoms(rdf_db *db, triple *t);
static void lock_atoms(rdf_db *db, triple *t);
static void unlock_atoms_literal(literal *lit);
static size_t triple_hash_key(triple *t, int which);
static size_t object_hash(triple *t);
static void mark_duplicate(rdf_db *db, triple *t, query *q);
static void link_triple_hash(rdf_db *db, triple *t);
static void free_triple(rdf_db *db, triple *t, int linger);
static sub_p_matrix *create_reachability_matrix(rdf_db *db,
predicate_cloud *cloud,
query *q);
static void free_reachability_matrix(rdf_db *db, sub_p_matrix *rm);
static void gc_is_leaf(rdf_db *db, predicate *p, gen_t gen);
static int get_predicate(rdf_db *db, term_t t, predicate **p, query *q);
static int get_existing_predicate(rdf_db *db, term_t t, predicate **p);
static void free_bitmatrix(rdf_db *db, bitmatrix *bm);
static predicate_cloud *new_predicate_cloud(rdf_db *db,
predicate **p, size_t count);
static int unify_literal(term_t lit, literal *l);
static int check_predicate_cloud(predicate_cloud *c);
static void invalidate_is_leaf(predicate *p, query *q, int add);
static void create_triple_hashes(rdf_db *db, int count, int *ic);
/*******************************
* LOCKING *
*******************************/
static void
INIT_LOCK(rdf_db *db)
{ simpleMutexInit(&db->locks.literal);
simpleMutexInit(&db->locks.misc);
simpleMutexInit(&db->locks.gc);
simpleMutexInit(&db->locks.duplicates);
}
static simpleMutex rdf_lock;
/*******************************
* DEBUG SUPPORT *
*******************************/
#ifdef O_DEBUG
#define PRT_SRC 0x1 /* print source */
#define PRT_NL 0x2 /* add newline */
#define PRT_GEN 0x4 /* print generation info */
#define PRT_ADR 0x8 /* print address */
static void
print_literal(literal *lit)
{ switch(lit->objtype)
{ case OBJ_STRING:
switch(lit->qualifier)
{ case Q_TYPE:
Sdprintf("%s^^\"%s\"",
PL_atom_chars(lit->value.string),
PL_atom_chars(lit->type_or_lang));
break;
case Q_LANG:
Sdprintf("%s@\"%s\"",
PL_atom_chars(lit->value.string),
PL_atom_chars(lit->type_or_lang));
break;
default:
{ size_t len;
const char *s;
const wchar_t *w;
if ( (s = PL_atom_nchars(lit->value.string, &len)) )
{ if ( strlen(s) == len )
Sdprintf("\"%s\"", s);
else
Sdprintf("\"%s\" (len=%d)", s, len);
} else if ( (w = PL_atom_wchars(lit->value.string, &len)) )
{ unsigned int i;
Sputc('L', Serror);
Sputc('"', Serror);
for(i=0; i<len; i++)
{ if ( w[i] < 0x7f )
Sputc(w[i], Serror);
else
Sfprintf(Serror, "\\\\u%04x", w[i]);
}
Sputc('"', Serror);
}
break;
}
}
break;
case OBJ_INTEGER:
Sdprintf("%ld", lit->value.integer);
break;
case OBJ_DOUBLE:
Sdprintf("%f", lit->value.real);
break;
case OBJ_TERM:
{ fid_t fid = PL_open_foreign_frame();
term_t term = PL_new_term_ref();
PL_recorded_external(lit->value.term.record, term);
PL_write_term(Serror, term, 1200,
PL_WRT_QUOTED|PL_WRT_NUMBERVARS|PL_WRT_PORTRAY);
PL_discard_foreign_frame(fid);
break;
}
default:
assert(0);
}
}
static void
print_object(triple *t)
{ if ( t->object_is_literal )
{ print_literal(t->object.literal);
} else
{ Sdprintf("%s", t->object.resource ? PL_atom_chars(t->object.resource) : "?o");
}
}
static void
print_src(triple *t)
{ if ( t->graph_id )
{ if ( t->line == NO_LINE )
Sdprintf(" [%s]", PL_atom_chars(ID_ATOM(t->graph_id)));
else
Sdprintf(" [%s:%ld]", PL_atom_chars(ID_ATOM(t->graph_id)), t->line);
} else
{ Sdprintf(" ?g");
}
}
static char *
triple_status_flags(triple *t, char *buf)
{ char *o = buf;
*o++ = ' ';
if ( t->atoms_locked )
*o++ = 'L';
if ( t->is_duplicate )
*o++ = 'D';
if ( o > buf+1 )
*o = '\0';
else
buf[0] = '\0';
return buf;
}
static void
print_gen(triple *t)
{ char buf[3][24];
Sdprintf(" (%s..%s%s)",
gen_name(t->lifespan.born, buf[0]),
gen_name(t->lifespan.died, buf[1]),
triple_status_flags(t, buf[2]));
}
static void
print_triple(triple *t, int flags)
{ Sdprintf("<%s %s ",
t->subject_id ? PL_atom_chars(ID_ATOM(t->subject_id)) : "?s",
t->predicate.r->name ? PL_atom_chars(t->predicate.r->name) : "?p");
print_object(t);
if ( (flags & PRT_SRC) )
print_src(t);
if ( (flags & PRT_GEN) )
print_gen(t);
if ( (flags & PRT_ADR) )
Sdprintf(" &%p", t);
Sdprintf((flags & PRT_NL) ? ">\n" : ">");
}
#endif
/*******************************
* STORAGE *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Tables that allow finding the hash-chains for a particular index. They
are currently crafted by hand, such that the compiler knowns the mapping
is constant. check_index_tables() verifies that the tables are
consistent. To add an index:
* Increment INDEX_TABLES in rdf_db.h
* Add the index to col_index[]
* Assign it a (consistent) position in index_col[]
* If decide wich unindexed queries are best mapped
to the new index and add them to alt_index[]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define ICOL(i) (index_col[i])
static const int index_col[16] =
{ 0, /* BY_NONE */
1, /* BY_S */
2, /* BY_P */
3, /* BY_SP */
4, /* BY_O */
~0, /* BY_SO */
5, /* BY_PO */
6, /* BY_SPO */
7, /* BY_G */
8, /* BY_SG */
9, /* BY_PG */
~0, /* BY_SPG */
~0, /* BY_OG */
~0, /* BY_SOG */
~0, /* BY_POG */
~0 /* BY_SPOG */
};
static int col_index[INDEX_TABLES] =
{ BY_NONE,
BY_S,
BY_P,
BY_SP,
BY_O,
BY_PO,
BY_SPO,
BY_G,
BY_SG,
BY_PG
};
static const char *col_name[INDEX_TABLES] =
{ "-",
"s",
"p",
"sp",
"o",
"po",
"spo",
"g",
"sg",
"pg"
};
static const int col_avg_len[INDEX_TABLES] =
{ 0, /*BY_NONE*/
2, /*BY_S*/
2, /*BY_P*/
2, /*BY_SP*/
4, /*BY_O*/
2, /*BY_PO*/
2, /*BY_SPO*/
1, /*BY_G*/
2, /*BY_SG*/
2 /*BY_PG*/
};
static const int col_opt_threshold[INDEX_TABLES] =
{ 0, /*BY_NONE*/
2, /*BY_S*/
2, /*BY_P*/
2, /*BY_SP*/
2, /*BY_O*/
2, /*BY_PO*/
2, /*BY_SPO*/
2, /*BY_G*/
2, /*BY_SG*/
2 /*BY_PG*/
};
static const int alt_index[16] =
{ BY_NONE, /* BY_NONE */
BY_S, /* BY_S */
BY_P, /* BY_P */
BY_SP, /* BY_SP */
BY_O, /* BY_O */
BY_S, /* BY_SO */
BY_PO, /* BY_PO */
BY_SPO, /* BY_SPO */
BY_G, /* BY_G */
BY_SG, /* BY_SG */
BY_PG, /* BY_PG */
BY_SP, /* BY_SPG */
BY_O, /* BY_OG */
BY_S, /* BY_SOG */
BY_PO, /* BY_POG */
BY_SPO /* BY_SPOG */
};
static void
check_index_tables(void)
{ int i, ic;
for(i=0; i<16; i++)
{ if ( (ic=index_col[i]) != ~0 )
{ assert(col_index[ic] == i);
}
}
for(i=0; i<16; i++)
{ int ai = alt_index[i];
assert(index_col[ai] != ~0);
}
for(i=0; i<INDEX_TABLES; i++)
{ ic = col_index[i];
assert(alt_index[ic] == ic);
}
}
/*******************************
* LISTS *
*******************************/
static int
add_list(rdf_db *db, list *list, void *value)
{ cell *c;
for(c=list->head; c; c=c->next)
{ if ( c->value == value )
return FALSE; /* already a member */
}
c = rdf_malloc(db, sizeof(*c));
c->value = value;
c->next = NULL;
if ( list->tail )
list->tail->next = c;
else
list->head = c;
list->tail = c;
return TRUE;
}
static int
del_list(rdf_db *db, list *list, void *value)
{ cell *c, *p = NULL;
for(c=list->head; c; p=c, c=c->next)
{ if ( c->value == value )
{ if ( p )
p->next = c->next;
else
list->head = c->next;
if ( !c->next )
list->tail = p;
rdf_free(db, c, sizeof(*c));
return TRUE;
}
}
return FALSE; /* not a member */
}
static void
free_list(rdf_db *db, list *list)
{ cell *c, *n;
for(c=list->head; c; c=n)
{ n = c->next;
rdf_free(db, c, sizeof(*c));
}
list->head = list->tail = NULL;
}
/*******************************
* TMP STORE *
*******************************/
static void
init_tmp_store(tmp_store *s)
{ s->chunks = &s->store0;
s->chunks->next = NULL;
s->chunks->used = 0;
}
static void *
alloc_tmp_store(tmp_store *s, size_t size)
{ void *p;
assert(size < CHUNKSIZE);
if ( s->chunks->used + size > CHUNKSIZE )
{ mchunk *ch = malloc(sizeof(mchunk));
ch->used = 0;
ch->next = s->chunks;
s->chunks = ch;
}
p = &s->chunks->buf[s->chunks->used];
s->chunks->used += size;
return p;
}
static void
destroy_tmp_store(tmp_store *s)
{ mchunk *ch, *next;
for(ch=s->chunks; ch != &s->store0; ch = next)
{ next = ch->next;
free(ch);
}
}
/*******************************
* ATOM SETS *
*******************************/
#define ATOMSET_INITIAL_ENTRIES 16
typedef struct atom_cell
{ struct atom_cell *next;
atom_t atom;
} atom_cell;
typedef struct
{ atom_cell **entries; /* Hash entries */
size_t size; /* Hash-table size */
size_t count; /* # atoms stored */
tmp_store store; /* Temporary storage */
atom_cell *entries0[ATOMSET_INITIAL_ENTRIES];
} atomset;
static void *
alloc_atomset(atomset *as, size_t size)
{ return alloc_tmp_store(&as->store, size);
}
static void
init_atomset(atomset *as)
{ init_tmp_store(&as->store);
memset(as->entries0, 0, sizeof(as->entries0));
as->entries = as->entries0;
as->size = ATOMSET_INITIAL_ENTRIES;
as->count = 0;
}
static void
destroy_atomset(atomset *as)
{ destroy_tmp_store(&as->store);
if ( as->entries != as->entries0 )
free(as->entries);
}
static void
rehash_atom_set(atomset *as)
{ size_t newsize = as->size*2;
atom_cell **new = malloc(newsize*sizeof(atom_cell*));
int i;
memset(new, 0, newsize*sizeof(atom_cell*));
for(i=0; i<as->size; i++)
{ atom_cell *c, *n;
for(c=as->entries[i]; c; c=n)
{ size_t inew = atom_hash(c->atom, MURMUR_SEED)&(newsize-1);
n = c->next;
c->next = new[inew];
new[inew] = c;
}
}
if ( as->entries == as->entries0 )
{ as->entries = new;
} else
{ atom_cell **old = as->entries;
as->entries = new;
free(old);
}
as->size = newsize;
}
static int
add_atomset(atomset *as, atom_t atom)
{ size_t i = atom_hash(atom, MURMUR_SEED)&(as->size-1);
atom_cell *c;
for(c=as->entries[i]; c; c=c->next)
{ if ( c->atom == atom )
return 0;
}
if ( ++as->count > 2*as->size )
{ rehash_atom_set(as);
i = atom_hash(atom, MURMUR_SEED)&(as->size-1);
}
c = alloc_atomset(as, sizeof(*c));
c->atom = atom;
c->next = as->entries[i];
as->entries[i] = c;
return 1;
}
static int
for_atomset(atomset *as,
int (*func)(atom_t a, void *closure),
void *closure)
{ int key;
for(key=0; key < as->size; key++)
{ atom_cell *c;
for(c=as->entries[key]; c; c=c->next)
{ if ( !(*func)(c->atom, closure) )
return FALSE;
}
}
return TRUE;
}
/*******************************
* TRIPLE SETS *
*******************************/
/* Note that only ->entries need to be NULL to consider the set empty.
The remainder of the initialization is done lazily.
*/
static void *
alloc_tripleset(void *ptr, size_t size)
{ tripleset *ts = ptr;
return alloc_tmp_store(&ts->store, size);
}
static void
init_tripleset(tripleset *ts)
{ init_tmp_store(&ts->store);
memset(ts->entries0, 0, sizeof(ts->entries0));
ts->entries = ts->entries0;
ts->size = TRIPLESET_INITIAL_ENTRIES;
ts->count = 0;
}
static void
destroy_tripleset(tripleset *ts)
{ if ( ts->entries )
{ destroy_tmp_store(&ts->store);
if ( ts->entries != ts->entries0 )
free(ts->entries);
}
}
static void
rehash_triple_set(tripleset *ts)
{ size_t newsize = ts->size*2;
triple_cell **new = malloc(newsize*sizeof(triple_cell*));
int i;
memset(new, 0, newsize*sizeof(triple_cell*));
for(i=0; i<ts->size; i++)
{ triple_cell *c, *n;
for(c=ts->entries[i]; c; c=n)
{ size_t inew = triple_hash_key(c->triple, BY_SPO)&(newsize-1);
n = c->next;
c->next = new[inew];
new[inew] = c;
}
}
if ( ts->entries == ts->entries0 )
{ ts->entries = new;
} else
{ triple_cell **old = ts->entries;
ts->entries = new;
free(old);
}
ts->size = newsize;
}
static int
add_tripleset(search_state *state, tripleset *ts, triple *triple)
{ size_t i;
triple_cell *c;
if ( !ts->entries )
init_tripleset(ts);
i = triple_hash_key(triple, BY_SPO)&(ts->size-1);
for(c=ts->entries[i]; c; c=c->next)
{ if ( match_triples(state->db,
triple, c->triple,
state->query, MATCH_DUPLICATE) )
return 0;
}
if ( ++ts->count > 2*ts->size )
{ rehash_triple_set(ts);
i = triple_hash_key(triple, BY_SPO)&(ts->size-1);
}
c = alloc_tripleset(ts, sizeof(*c));
c->triple = triple;
c->next = ts->entries[i];
ts->entries[i] = c;
return 1;
}
#ifdef COMPACT
/*******************************
* TRIPLE ARRAY *
*******************************/
static triple_element *
alloc_array_slice(size_t count, triple_element **last)
{ size_t bytes = count*sizeof(triple_element);
triple_element *slice = malloc(bytes);
if ( slice )
{ triple_element *end = slice+count-1;
triple_element *e, *n;
for(e=slice; e<end; e=n)
{ n = e+1;
e->fnext = n;
}
e->fnext = NULL;
if ( last )
*last = e;
}
return slice;
}
static void
free_array_slice(triple_array *a, triple_element *list, triple_element *last)
{ triple_element *o;
do
{ o = a->freelist;
last->fnext = o;
} while ( !__sync_bool_compare_and_swap(&a->freelist, o, list) );
}
static int
init_triple_array(rdf_db *db)
{ triple_array *a = &db->triple_array;
triple_element *slice = alloc_array_slice(TRIPLE_ARRAY_PREINIT, NULL);
int i;
for(i=0; i<MSB(TRIPLE_ARRAY_PREINIT); i++)
a->blocks[i] = slice;
a->freelist = slice->fnext; /* simply ignore the first for id>0 */
a->preinit = TRIPLE_ARRAY_PREINIT;
a->size = TRIPLE_ARRAY_PREINIT;
return TRUE;
}
static void
destroy_triple_array(rdf_db *db)
{ triple_array *a = &db->triple_array;
int i;
free(a->blocks[0]);
for(i=MSB(a->preinit); i<MSB(a->size); i++)
{ triple_element *e = a->blocks[i];
e += 1<<(i-1);
free(e);
}
memset(a, 0, sizeof(*a));
}
static void
reset_triple_array(rdf_db *db)
{ destroy_triple_array(db);
init_triple_array(db);
}
static void
resize_triple_array(rdf_db *db)
{ triple_array *a = &db->triple_array;
int i = MSB(a->size);
triple_element *last;
triple_element *slice = alloc_array_slice(a->size, &last);
if ( slice )
{ a->blocks[i] = slice - a->size;
a->size *= 2;
free_array_slice(a, slice, last);
}
}
static triple_element *
fetch_triple_element(rdf_db *db, triple_id id)
{ return &db->triple_array.blocks[MSB(id)][id];
}
/* assign a new triple a place in the triple array
*/
static triple_id
register_triple(rdf_db *db, triple *t)
{ triple_array *a = &db->triple_array;
triple_element *e;
size_t slice_size;
int i;
do
{ if ( !(e=a->freelist) )
{ simpleMutexLock(&db->locks.misc);
while ( !(e=a->freelist) )
resize_triple_array(db);
simpleMutexUnlock(&db->locks.misc);
}
} while ( !__sync_bool_compare_and_swap(&a->freelist, e, e->fnext) );
e->triple = t;
for(i=1,slice_size=1; i<MAX_TBLOCKS; i++,slice_size*=2)
{ if ( e >= a->blocks[i]+slice_size &&
e < a->blocks[i]+slice_size*2 )
{ t->id = e - a->blocks[i];
assert(fetch_triple(db, t->id) == t);
return t->id;
}
}
assert(0);
return 0;
}
static void
unregister_triple(rdf_db *db, triple *t)
{ if ( t->id != TRIPLE_NO_ID )
{ triple_element *e = fetch_triple_element(db, t->id);
t->id = TRIPLE_NO_ID;
free_array_slice(&db->triple_array, e, e);
}
}
static void
finalize_triple(void *data, void *client)
{ unregister_triple(client, data);
}
static triple *
triple_follow_hash(rdf_db *db, triple *t, int icol)
{ triple_id nid = t->tp.next[icol];
return fetch_triple(db, nid);
}
#define T_ID(t) ((t) ? (t)->id : 0)
#else /*COMPACT*/
#define init_triple_array(db) (void)0
#define reset_triple_array(db) (void)0
#define register_triple(db, t) (void)0
#define unregister_triple(db, t) (void)0
#define triple_follow_hash(db, t, icol) ((t)->tp.next[icol])
#define T_ID(t) (t)
#endif /*COMPACT*/
/*******************************
* TRIPLE WALKER *
*******************************/
/* init_triple_walker() and next_triple() are the primitives to walk indexed
triples. The pattern is:
triple_walker tw;
init_triple_walker(&tw, db, pattern, index);
while((t=next_triple(tw)))
<do your job>
TBD: Get the generation into this story. Most likely it is better to
deal with this in this low-level loop then outside. We will handle
this in the next cycle.
*/
static void
init_triple_walker(triple_walker *tw, rdf_db *db, triple *pattern, int which)
{ tw->unbounded_hash = triple_hash_key(pattern, which);
tw->current = NULL;
tw->icol = ICOL(which);
tw->db = db;
if ( !tw->db->hash[tw->icol].created )
create_triple_hashes(db, 1, &tw->icol);
tw->bcount = tw->db->hash[tw->icol].bucket_count_epoch;
}
static void
init_triple_literal_walker(triple_walker *tw, rdf_db *db,
triple *pattern, int which, unsigned int hash)
{ tw->unbounded_hash = hash;