-
Notifications
You must be signed in to change notification settings - Fork 15
/
query.c
911 lines (717 loc) · 21.9 KB
/
query.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 2011-2017, VU University Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
RDF-DB query management. This module keeps track of running queries. We
need this for GC purposes. In particular, we need to:
* Find the oldest active generation.
* Get a signal if all currently active queries have finished.
In addition to queries, this module performs the necessary logic on
generations:
* Is an object visible in a query?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <string.h>
#include <assert.h>
#include "rdf_db.h"
#include "query.h"
#include "memory.h"
#include "mutex.h"
#include "buffer.h"
static void init_query_stack(rdf_db *db, query_stack *qs);
/*******************************
* THREAD DATA *
*******************************/
/* Return the per-thread data for the given DB. This version uses no
locks for the common case that the required datastructures are
already provided.
*/
thread_info *
rdf_thread_info(rdf_db *db, int tid)
{ query_admin *qa = &db->queries;
per_thread *td = &qa->query.per_thread;
thread_info *ti;
size_t idx = MSB(tid);
if ( !td->blocks[idx] )
{ simpleMutexLock(&qa->query.lock);
if ( !td->blocks[idx] )
{ size_t bs = BLOCKLEN(idx);
thread_info **newblock = rdf_malloc(db, bs*sizeof(thread_info*));
memset(newblock, 0, bs*sizeof(thread_info*));
td->blocks[idx] = newblock-bs;
}
simpleMutexUnlock(&qa->query.lock);
}
if ( !(ti=td->blocks[idx][tid]) )
{ simpleMutexLock(&qa->query.lock);
if ( !(ti=td->blocks[idx][tid]) )
{ ti = rdf_malloc(db, sizeof(*ti));
memset(ti, 0, sizeof(*ti));
init_query_stack(db, &ti->queries);
MEMORY_BARRIER();
td->blocks[idx][tid] = ti;
if ( tid > qa->query.thread_max )
qa->query.thread_max = tid;
}
simpleMutexUnlock(&qa->query.lock);
}
return ti;
}
gen_t
oldest_query_geneneration(rdf_db *db, gen_t *reindex_gen)
{ int tid;
gen_t gen = db->snapshots.keep;
gen_t ren = GEN_MAX;
query_admin *qa = &db->queries;
per_thread *td = &qa->query.per_thread;
DEBUG(20,
if ( db->snapshots.keep != GEN_MAX )
{ char buf[64];
Sdprintf("Oldest snapshot gen = %s\n",
gen_name(db->snapshots.keep, buf));
});
for(tid=1; tid <= qa->query.thread_max; tid++)
{ thread_info **tis;
thread_info *ti;
if ( (tis=td->blocks[MSB(tid)]) &&
(ti=tis[tid]) )
{ query_stack *qs = &ti->queries;
if ( qs->top > 0 )
{ query *q = &qs->preallocated[0];
DEBUG(10,
{ char buf[20];
Sdprintf("Thread %d: %d queries; oldest gen %s\n",
tid, qs->top, gen_name(q->rd_gen, buf));
});
if ( q->rd_gen < gen )
gen = q->rd_gen;
if ( q->reindex_gen < ren )
ren = q->reindex_gen;
} else
{ DEBUG(11, Sdprintf("Thread %d: no queries\n", tid));
}
}
}
if ( reindex_gen )
*reindex_gen = ren;
return gen;
}
/*******************************
* QUERY-STACK *
*******************************/
static void
preinit_query(rdf_db *db, query_stack *qs, query *q, query *parent, int depth)
{ q->db = db;
q->stack = qs;
q->parent = q;
q->depth = depth;
}
static void
init_query_stack(rdf_db *db, query_stack *qs)
{ int tid = PL_thread_self();
int i;
int prealloc = sizeof(qs->preallocated)/sizeof(qs->preallocated[0]);
query *parent = NULL;
memset(qs, 0, sizeof(*qs));
simpleMutexInit(&qs->lock);
qs->db = db;
qs->tr_gen_base = GEN_TBASE + tid*GEN_TNEST;
qs->tr_gen_max = qs->tr_gen_base + (GEN_TNEST-1);
for(i=0; i<MSB(prealloc); i++)
qs->blocks[i] = qs->preallocated;
for(i=0; i<prealloc; i++)
{ query *q = &qs->preallocated[i];
preinit_query(db, qs, q, parent, i);
parent = q;
}
}
static query *
alloc_query(query_stack *qs)
{ int depth = qs->top;
int b = MSB(depth);
if ( b >= MAX_QBLOCKS )
{ PL_resource_error("open_rdf_queries");
return NULL;
}
if ( qs->blocks[b] )
{ query *q = &qs->blocks[b][depth];
assert(q->stack);
return q;
}
simpleMutexLock(&qs->lock);
if ( !qs->blocks[b] )
{ size_t bytes = BLOCKLEN(b) * sizeof(query);
query *ql = rdf_malloc(qs->db, bytes);
query *parent;
int i;
if ( !ql )
{ simpleMutexUnlock(&qs->lock);
PL_resource_error("memory");
return NULL;
}
memset(ql, 0, bytes);
ql -= depth; /* rebase */
parent = &qs->blocks[b-1][depth-1];
for(i=depth; i<depth*2; i++)
{ query *q = &ql[i];
preinit_query(qs->db, qs, q, parent, i);
parent = q;
}
MEMORY_BARRIER();
qs->blocks[b] = ql;
}
simpleMutexUnlock(&qs->lock);
return &qs->blocks[b][depth];
}
static void
push_query(rdf_db *db, query *q)
{ enter_scan(&db->defer_all);
q->stack->top++;
}
static void
pop_query(rdf_db *db, query *q)
{ q->stack->top--;
exit_scan(&db->defer_all);
}
query *
open_query(rdf_db *db)
{ int tid = PL_thread_self();
thread_info *ti = rdf_thread_info(db, tid);
query *q = alloc_query(&ti->queries);
if ( !q ) return NULL;
q->type = Q_NORMAL;
q->transaction = ti->queries.transaction;
q->reindex_gen = db->reindexed;
if ( q->transaction ) /* Query inside a transaction */
{ q->rd_gen = q->transaction->rd_gen;
q->tr_gen = q->transaction->wr_gen;
q->wr_gen = q->transaction->wr_gen;
} else
{ q->rd_gen = db->queries.generation;
q->tr_gen = GEN_TBASE;
q->wr_gen = GEN_UNDEF;
}
push_query(db, q);
return q;
}
query *
open_transaction(rdf_db *db,
triple_buffer *added,
triple_buffer *deleted,
triple_buffer *updated,
snapshot *ss)
{ int tid = PL_thread_self();
thread_info *ti = rdf_thread_info(db, tid);
query *q = alloc_query(&ti->queries);
if ( !q ) return NULL;
q->type = Q_TRANSACTION;
q->transaction = ti->queries.transaction;
q->reindex_gen = GEN_MAX; /* should not get this down */
if ( ss && ss != SNAPSHOT_ANONYMOUS )
{ int ss_tid = snapshot_thread(ss);
assert(!ss_tid || ss_tid == tid);
(void)ss_tid;
q->rd_gen = ss->rd_gen;
q->tr_gen = ss->tr_gen;
} else if ( q->transaction ) /* nested transaction */
{ q->rd_gen = q->transaction->rd_gen;
q->tr_gen = q->transaction->wr_gen;
} else
{ q->rd_gen = db->queries.generation;
q->tr_gen = ti->queries.tr_gen_base;
}
q->wr_gen = q->tr_gen;
ti->queries.transaction = q;
init_triple_buffer(added);
init_triple_buffer(deleted);
init_triple_buffer(updated);
q->transaction_data.added = added;
q->transaction_data.deleted = deleted;
q->transaction_data.updated = updated;
push_query(db, q);
return q;
}
void
close_query(query *q)
{ pop_query(q->db, q);
}
int
empty_transaction(query *q)
{ return ( is_empty_buffer(q->transaction_data.added) &&
is_empty_buffer(q->transaction_data.deleted) &&
is_empty_buffer(q->transaction_data.updated) );
}
/*******************************
* ADMIN *
*******************************/
void
init_query_admin(rdf_db *db)
{ query_admin *qa = &db->queries;
memset(qa, 0, sizeof(*qa));
simpleMutexInit(&qa->query.lock);
simpleMutexInit(&qa->write.lock);
simpleMutexInit(&qa->write.generation_lock);
}
/*******************************
* GENERATIONS *
*******************************/
/* lifespan() is true if a lifespan is visible inside a query.
A lifespan is alive if the query generation is inside it,
but with transactions there are two problems:
- If the triple is deleted by a parent transaction it is dead
- If the triple is created by a parent transaction it is alive
*/
char *
gen_name(gen_t gen, char *buf)
{ static char tmp[24];
if ( !buf )
buf = tmp;
if ( gen == GEN_UNDEF ) return "GEN_UNDEF";
if ( gen == GEN_MAX ) return "GEN_MAX";
if ( gen == GEN_PREHIST ) return "GEN_PREHIST";
if ( gen >= GEN_TBASE )
{ int tid = (int)((gen-GEN_TBASE)/GEN_TNEST);
gen_t r = (gen-GEN_TBASE)%GEN_TNEST;
if ( r == GEN_TNEST-1 )
Ssprintf(buf, "T%d+GEN_TNEST", tid);
else
Ssprintf(buf, "T%d+%" PRId64, tid, (int64_t)r);
return buf;
}
Ssprintf(buf, "%" PRId64, (int64_t)gen);
return buf;
}
int
alive_lifespan(query *q, lifespan *lifespan)
{ DEBUG(2,
{ char b[4][24];
Sdprintf("q: rd_gen=%s; tr_gen=%s; t: %s..%s\n",
gen_name(q->rd_gen, b[0]),
gen_name(q->tr_gen, b[1]),
gen_name(lifespan->born, b[2]),
gen_name(lifespan->died, b[3]));
});
if ( q->rd_gen >= lifespan->born &&
q->rd_gen < lifespan->died )
{ if ( is_wr_transaction_gen(q, lifespan->died) &&
q->tr_gen >= lifespan->died )
return FALSE;
return TRUE;
} else /* created/died in transaction */
{ if ( is_wr_transaction_gen(q, lifespan->born) )
{ if ( q->tr_gen >= lifespan->born &&
q->tr_gen < lifespan->died )
return TRUE;
}
}
return FALSE;
}
int
born_lifespan(query *q, lifespan *lifespan)
{ if ( q->rd_gen >= lifespan->born )
return TRUE;
if ( is_wr_transaction_gen(q, lifespan->born) &&
q->tr_gen >= lifespan->born )
return TRUE;
return FALSE;
}
/*******************************
* TRIPLE MANIPULATION *
*******************************/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
We have three basic triple manipulations:
- Add triples
- Delete triples
- Updated triples (expressed as deleting and adding)
add_triples() adds an array of triples to the database, stepping the
database generation by 1. Calls to add triples must be synchronized with
other addition calls, but not with read nor delete operations. This
synchronization is needed because without we cannot set the generation
for new queries to a proper value.
To reduce the locked time, we perform this in multiple steps:
- prelink_triple() performs tasks that do not affect the remainder of
the database.
- In the link-phase, we add the triples in packages of ADD_CHUNK_SIZE
to the database, but addressed in the far future. No reader sees
see what we are doing.
- Next, we grab the generation_lock and update the triples to
the next generation and increment the generation to make them
visible.
- Finally, we do some post-hock work to update statistics.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define ADD_CHUNK_SIZE 50
int
add_triples(query *q, triple **triples, size_t count)
{ rdf_db *db = q->db;
gen_t gen, gen_max;
triple **ep = triples+count;
triple **tp;
/* pre-lock phase */
for(tp=triples; tp < ep; tp++)
prelink_triple(db, *tp, q);
consider_triple_rehash(db, count);
/* Add the triples in the future */
gen_max = query_max_gen(q);
for(tp=triples; tp < ep; )
{ triple **echunk = tp+50;
if ( echunk > ep )
echunk = ep;
simpleMutexLock(&db->queries.write.lock);
for(; tp<echunk; tp++)
{ triple *t = *tp;
t->lifespan.born = gen_max;
t->lifespan.died = gen_max;
link_triple(db, t, q);
}
simpleMutexUnlock(&db->queries.write.lock);
}
/* generation update */
simpleMutexLock(&db->queries.write.generation_lock);
gen = queryWriteGen(q)+1;
for(tp=triples; tp < ep; tp++)
{ triple *t = *tp;
t->lifespan.born = gen;
}
setWriteGen(q, gen);
simpleMutexUnlock(&db->queries.write.generation_lock);
if ( q->transaction )
{ for(tp=triples; tp < ep; tp++)
{ triple *t = *tp;
postlink_triple(db, t, q);
buffer_triple(q->transaction->transaction_data.added, t);
}
} else
{ for(tp=triples; tp < ep; tp++)
{ triple *t = *tp;
postlink_triple(db, t, q);
}
if ( rdf_is_broadcasting(EV_ASSERT|EV_ASSERT_LOAD) )
{ for(tp=triples; tp < ep; tp++)
{ triple *t = *tp;
broadcast_id id = t->loaded ? EV_ASSERT_LOAD : EV_ASSERT;
if ( !rdf_broadcast(id, t, NULL) )
return FALSE;
}
}
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
del_triples() deletes triples from the database. There are two actions:
- del_triple_consequences() deletes (entailment) consequences of
erasing the triple. Currently this is handling subPropertyOf
entailment. This doesn't remove the triple, but merely invalidates
the subPropertyOf reachability matrix for subsequent generations.
- erase_triple() is called on the final commit and updates statistics.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int
del_triples(query *q, triple **triples, size_t count)
{ rdf_db *db = q->db;
gen_t gen;
triple **ep = triples+count;
triple **tp;
if ( count == 0 )
return TRUE;
else
rdf_create_gc_thread(db);
simpleMutexLock(&db->queries.write.generation_lock);
simpleMutexLock(&db->queries.write.lock);
gen = queryWriteGen(q) + 1;
for(tp=triples; tp < ep; tp++)
{ triple *t = deref_triple(db, *tp);
t->lifespan.died = gen;
del_triple_consequences(db, t, q);
if ( q->transaction )
buffer_triple(q->transaction->transaction_data.deleted, t);
else
erase_triple(db, t, q);
}
setWriteGen(q, gen);
simpleMutexUnlock(&db->queries.write.lock);
simpleMutexUnlock(&db->queries.write.generation_lock);
if ( !q->transaction && rdf_is_broadcasting(EV_RETRACT) )
{ for(tp=triples; tp < ep; tp++)
{ triple *t = deref_triple(db, *tp);
if ( !rdf_broadcast(EV_RETRACT, t, NULL) )
return FALSE;
}
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
update_triples() updates an array of triples.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int
update_triples(query *q,
triple **old, triple **new,
size_t count)
{ rdf_db *db = q->db;
gen_t gen, gen_max;
triple **eo = old+count;
triple **en = new+count;
triple **to, **tn;
size_t updated = 0;
if ( count == 0 )
return TRUE;
else
rdf_create_gc_thread(db);
for(tn=new; tn < en; tn++)
{ triple *t = *tn;
if ( t )
prelink_triple(db, t, q);
}
simpleMutexLock(&db->queries.write.generation_lock);
simpleMutexLock(&db->queries.write.lock);
gen = queryWriteGen(q) + 1;
gen_max = query_max_gen(q);
for(to=old,tn=new; to < eo; to++,tn++)
{ if ( *tn )
{ triple *n = *tn; /* new, cannot be reindexed */
triple *o = deref_triple(db, *to);
o->lifespan.died = gen;
n->lifespan.born = gen;
n->lifespan.died = gen_max;
link_triple(db, *tn, q);
del_triple_consequences(db, o, q);
if ( q->transaction )
{ buffer_triple(q->transaction->transaction_data.updated, *to);
buffer_triple(q->transaction->transaction_data.updated, *tn);
} else
{ erase_triple(db, *to, q);
}
updated++;
}
}
(void)updated;
setWriteGen(q, gen);
simpleMutexUnlock(&db->queries.write.lock);
simpleMutexUnlock(&db->queries.write.generation_lock);
consider_triple_rehash(db, 1);
if ( !q->transaction && rdf_is_broadcasting(EV_UPDATE) )
{ for(to=old,tn=new; to < eo; to++,tn++)
{ triple *t = *tn;
if ( t )
{ postlink_triple(db, *tn, q);
if ( !rdf_broadcast(EV_UPDATE, *to, *tn) )
return FALSE;
}
}
} else
{ for(tn=new; tn < en; tn++)
{ triple *t = *tn;
if ( t )
postlink_triple(db, t, q);
}
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Matrices used in a transaction must be discarded because a new
transaction will use the same generation numbers, but typically for
different modifications.
TBD: Hand some statistics to GC, such that we know that there are
matrices to collect.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static void
invalidate_lifespans_transaction(query *q)
{ cell *c, *next;
for(c=q->transaction_data.lifespans.head; c; c=next)
{ lifespan *span = c->value;
next = c->next;
span->died = GEN_PREHIST;
rdf_free(q->db, c, sizeof(*c));
}
q->transaction_data.lifespans.head = NULL;
q->transaction_data.lifespans.tail = NULL;
}
void
close_transaction(query *q)
{ assert(q->type == Q_TRANSACTION);
free_triple_buffer(q->transaction_data.added);
free_triple_buffer(q->transaction_data.deleted);
free_triple_buffer(q->transaction_data.updated);
invalidate_lifespans_transaction(q);
q->stack->transaction = q->transaction;
close_query(q);
}
static void
commit_add(query *q, gen_t gen_max, gen_t gen, triple *t)
{ t = deref_triple(q->db, t);
if ( t->lifespan.died == gen_max )
{ t->lifespan.born = gen;
add_triple_consequences(q->db, t, q);
if ( q->transaction )
buffer_triple(q->transaction->transaction_data.added, t);
else
t->lifespan.died = GEN_MAX;
}
}
static void
commit_del(query *q, gen_t gen, triple *t)
{ t = deref_triple(q->db, t);
if ( is_wr_transaction_gen(q, t->lifespan.died) )
{ t->lifespan.died = gen;
if ( q->transaction )
{ del_triple_consequences(q->db, t, q);
buffer_triple(q->transaction->transaction_data.deleted, t);
} else
{ erase_triple(q->db, t, q);
}
}
}
int
commit_transaction(query *q)
{ rdf_db *db = q->db;
triple **tp;
gen_t gen, gen_max;
simpleMutexLock(&db->queries.write.generation_lock);
simpleMutexLock(&db->queries.write.lock);
gen = queryWriteGen(q) + 1;
gen_max = transaction_max_gen(q);
/* added triples */
for(tp=q->transaction_data.added->base;
tp<q->transaction_data.added->top;
tp++)
{ commit_add(q, gen_max, gen, *tp);
}
/* deleted triples */
for(tp=q->transaction_data.deleted->base;
tp<q->transaction_data.deleted->top;
tp++)
{ commit_del(q, gen, *tp);
}
/* updated triples */
for(tp=q->transaction_data.updated->base;
tp<q->transaction_data.updated->top;
tp += 2)
{ triple *to = tp[0];
triple *tn = tp[1];
commit_del(q, gen, to);
commit_add(q, gen_max, gen, tn);
}
setWriteGen(q, gen);
simpleMutexUnlock(&db->queries.write.lock);
simpleMutexUnlock(&db->queries.write.generation_lock);
q->stack->transaction = q->transaction; /* do not nest monitor calls */
/* inside the transaction */
/* Broadcast new triples */
if ( !q->transaction )
{ if ( rdf_is_broadcasting(EV_RETRACT) )
{ for(tp=q->transaction_data.deleted->base;
tp<q->transaction_data.deleted->top;
tp++)
{ triple *t = *tp;
if ( t->lifespan.died == gen )
{ if ( !rdf_broadcast(EV_RETRACT, t, NULL) )
return FALSE;
}
}
}
if ( rdf_is_broadcasting(EV_ASSERT|EV_ASSERT_LOAD) )
{ for(tp=q->transaction_data.added->base;
tp<q->transaction_data.added->top;
tp++)
{ triple *t = *tp;
if ( t->lifespan.born == gen )
{ broadcast_id id = t->loaded ? EV_ASSERT_LOAD : EV_ASSERT;
if ( !rdf_broadcast(id, t, NULL) )
return FALSE;
}
}
}
if ( rdf_is_broadcasting(EV_UPDATE) )
{ for(tp=q->transaction_data.updated->base;
tp<q->transaction_data.updated->top;
tp += 2)
{ triple *to = tp[0];
triple *tn = tp[1];
if ( to->lifespan.died == gen &&
tn->lifespan.born == gen )
{ if ( !rdf_broadcast(EV_UPDATE, to, tn) )
return FALSE;
}
}
}
}
close_transaction(q);
return TRUE;
}
/* TBD: What if someone else deleted this triple too? We can check
that by discovering multiple changes to the died generation.
*/
int
discard_transaction(query *q)
{ rdf_db *db = q->db;
triple **tp;
gen_t gen_max = transaction_max_gen(q);
for(tp=q->transaction_data.added->base;
tp<q->transaction_data.added->top;
tp++)
{ triple *t = *tp;
/* revert creation of new */
if ( is_wr_transaction_gen(q, t->lifespan.born) )
{ t = deref_triple(db, t);
t->lifespan.died = GEN_PREHIST;
erase_triple(db, t, q);
}
}
for(tp=q->transaction_data.deleted->base;
tp<q->transaction_data.deleted->top;
tp++)
{ triple *t = *tp;
/* revert deletion of old */
if ( is_wr_transaction_gen(q, t->lifespan.died) )
{ t = deref_triple(db, t);
t->lifespan.died = gen_max;
}
}
for(tp=q->transaction_data.updated->base;
tp<q->transaction_data.updated->top;
tp += 2)
{ triple *to = tp[0];
triple *tn = tp[1];
/* revert deletion of old */
if ( is_wr_transaction_gen(q, to->lifespan.died) )
{ to = deref_triple(db, to);
to->lifespan.died = gen_max;
}
/* revert creation of new */
if ( is_wr_transaction_gen(q, tn->lifespan.born) &&
tn->lifespan.died == gen_max )
{ tn = deref_triple(db, tn);
tn->lifespan.died = GEN_PREHIST;
erase_triple(db, tn, q);
}
}
close_transaction(q);
return TRUE;
}