-
Notifications
You must be signed in to change notification settings - Fork 0
/
anvil.cpp
1121 lines (964 loc) · 27.3 KB
/
anvil.cpp
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
/* This file is part of Anvil. Anvil is copyright 2007-2010 The Regents
* of the University of California. It is distributed under the terms of
* version 2 of the GNU GPL. See the file LICENSE for details. */
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
#include "sys_journal.h"
#include "dtable_factory.h"
#include "ctable_factory.h"
#include "dtable_cache_iter.h"
#include "ctable_cache_iter.h"
#include "journal_dtable.h"
#include "temp_journal_dtable.h"
#include "anvil.h"
static void rename_gmon_out(void)
{
struct stat gmon;
int r = stat("gmon.out", &gmon);
if(r >= 0)
{
char name[sizeof("gmon.out.") + 12];
for(uint32_t seq = 0; seq != (uint32_t) -1; seq++)
{
snprintf(name, sizeof(name), "gmon.out.%u", seq);
r = stat(name, &gmon);
if(r < 0)
{
fprintf(stderr, "%s() renaming gmon.out to %s\n", __FUNCTION__, name);
rename("gmon.out", name);
break;
}
}
}
}
int anvil_init(const char * path)
{
int r, fd = open(path, O_RDONLY);
if(fd < 0)
return fd;
rename_gmon_out();
/* make maximum log size 4MB */
r = tx_init(fd, 4194304);
if(r >= 0)
{
r = tx_start_r();
if(r >= 0)
{
sys_journal * global = sys_journal::get_global_journal();
r = sys_journal::set_unique_id_file(fd, "sys_journal_id", true);
if(r >= 0)
r = global->init(fd, "sys_journal", &journal_dtable::warehouse, &temp_journal_dtable::warehouse, true);
if(r >= 0)
{
/* maybe we should not always do this here? */
r = global->filter();
r = tx_end_r();
}
else
tx_end_r();
}
}
close(fd);
return r;
}
static inline int init_anvil_istr(anvil_istr * c, const istr & value)
{
anvil_istr_union safer(c);
/* istr is sufficiently initialized if it is zeroed out */
util::memset(c, 0, sizeof(*c));
*safer = value;
return 0;
}
int anvil_istr_new(anvil_istr * c, const char * str)
{
return init_anvil_istr(c, str);
}
int anvil_istr_copy(anvil_istr * c, const anvil_istr * src)
{
anvil_istr_union_const src_safer(src);
return init_anvil_istr(c, *src_safer);
}
void anvil_istr_kill(anvil_istr * c)
{
anvil_istr_union safer(c);
*safer = istr(NULL);
}
const char * anvil_istr_str(const anvil_istr * c)
{
anvil_istr_union_const safer(c);
return safer->str();
}
size_t anvil_istr_length(const anvil_istr * c)
{
anvil_istr_union_const safer(c);
return safer->length();
}
static inline int init_anvil_blob(anvil_blob * c, const blob & value)
{
anvil_blob_union safer(c);
/* blob is sufficiently initialized if it is zeroed out */
util::memset(c, 0, sizeof(*c));
*safer = value;
return 0;
}
int anvil_blob_new(anvil_blob * c, size_t size, const void * data)
{
return init_anvil_blob(c, blob(size, data));
}
int anvil_blob_str(anvil_blob * c, const char * str)
{
return init_anvil_blob(c, blob(str));
}
int anvil_blob_dne(anvil_blob * c)
{
return init_anvil_blob(c, blob::dne);
}
int anvil_blob_empty(anvil_blob * c)
{
return init_anvil_blob(c, blob::empty);
}
int anvil_blob_copy(anvil_blob * c, const anvil_blob * src)
{
anvil_blob_union_const src_safer(src);
return init_anvil_blob(c, *src_safer);
}
int anvil_blob_copy_buffer(anvil_blob * c, const anvil_blob_buffer * src)
{
anvil_blob_buffer_union_const src_safer(src);
return init_anvil_blob(c, blob(*src_safer));
}
void anvil_blob_kill(anvil_blob * c)
{
anvil_blob_union safer(c);
*safer = blob();
}
bool anvil_blob_exists(const anvil_blob * c)
{
anvil_blob_union_const safer(c);
return safer->exists();
}
size_t anvil_blob_size(const anvil_blob * c)
{
anvil_blob_union_const safer(c);
return safer->size();
}
const void * anvil_blob_data(const anvil_blob * c)
{
anvil_blob_union_const safer(c);
return safer->data();
}
int anvil_blob_overwrite(anvil_blob * c, size_t offset, const void * data, size_t length)
{
anvil_blob_union safer(c);
blob_buffer buffer(*safer);
int r = buffer.overwrite(offset, data, length);
if(r < 0)
return r;
*safer = blob(buffer);
return 0;
}
int anvil_blob_append(anvil_blob * c, const void * data, size_t length)
{
anvil_blob_union safer(c);
blob_buffer buffer(*safer);
int r = buffer.append(data, length);
if(r < 0)
return r;
*safer = blob(buffer);
return 0;
}
static inline void init_anvil_metablob(anvil_metablob * c, const metablob & value)
{
c->_size = value.size();
c->_exists = value.exists();
}
int anvil_metablob_copy(anvil_metablob * c, const anvil_metablob * src)
{
*c = *src;
return 0;
}
void anvil_metablob_kill(anvil_metablob * c)
{
/* nothing to do */
}
bool anvil_metablob_exists(const anvil_metablob * c)
{
return c->_exists;
}
size_t anvil_metablob_size(const anvil_metablob * c)
{
return c->_size;
}
static inline int init_anvil_blob_buffer(anvil_blob_buffer * c, const blob_buffer & value)
{
anvil_blob_buffer_union safer(c);
/* blob_buffer is sufficiently initialized if it is zeroed out */
util::memset(c, 0, sizeof(*c));
*safer = value;
return 0;
}
int anvil_blob_buffer_new(anvil_blob_buffer * c)
{
return init_anvil_blob_buffer(c, blob_buffer());
}
int anvil_blob_buffer_new_capacity(anvil_blob_buffer * c, size_t capacity)
{
return init_anvil_blob_buffer(c, blob_buffer(capacity));
}
int anvil_blob_buffer_new_data(anvil_blob_buffer * c, size_t size, const void * data)
{
return init_anvil_blob_buffer(c, blob_buffer(size, data));
}
int anvil_blob_buffer_copy(anvil_blob_buffer * c, const anvil_blob_buffer * src)
{
anvil_blob_buffer_union_const src_safer(src);
return init_anvil_blob_buffer(c, *src_safer);
}
int anvil_blob_buffer_copy_blob(anvil_blob_buffer * c, const anvil_blob * src)
{
anvil_blob_union_const src_safer(src);
return init_anvil_blob_buffer(c, blob_buffer(*src_safer));
}
void anvil_blob_buffer_kill(anvil_blob_buffer * c)
{
anvil_blob_buffer_union safer(c);
*safer = blob_buffer();
}
bool anvil_blob_buffer_exists(const anvil_blob_buffer * c)
{
anvil_blob_buffer_union_const safer(c);
return safer->exists();
}
size_t anvil_blob_buffer_size(const anvil_blob_buffer * c)
{
anvil_blob_buffer_union_const safer(c);
return safer->size();
}
size_t anvil_blob_buffer_capacity(const anvil_blob_buffer * c)
{
anvil_blob_buffer_union_const safer(c);
return safer->capacity();
}
const void * anvil_blob_buffer_data(const anvil_blob_buffer * c)
{
anvil_blob_buffer_union_const safer(c);
return safer->data();
}
int anvil_blob_buffer_set_size(anvil_blob_buffer * c, size_t size)
{
anvil_blob_buffer_union safer(c);
return safer->set_size(size);
}
int anvil_blob_buffer_set_capacity(anvil_blob_buffer * c, size_t capacity)
{
anvil_blob_buffer_union safer(c);
return safer->set_capacity(capacity);
}
int anvil_blob_buffer_overwrite(anvil_blob_buffer * c, size_t offset, const void * data, size_t length)
{
anvil_blob_buffer_union safer(c);
return safer->overwrite(offset, data, length);
}
int anvil_blob_buffer_append(anvil_blob_buffer * c, const void * data, size_t length)
{
anvil_blob_buffer_union safer(c);
return safer->append(data, length);
}
anvil_params * anvil_params_new(void)
{
return anvil_params_union(new params);
}
int anvil_params_parse(anvil_params * c, const char * string)
{
anvil_params_union safer(c);
return params::parse(string, safer);
}
void anvil_params_kill(anvil_params * c)
{
anvil_params_union safer(c);
delete safer.cpp;
}
static inline int init_anvil_dtype(anvil_dtype * c, const dtype & value)
{
anvil_dtype_union safer(c);
/* dtype is sufficiently initialized if it is zeroed out */
util::memset(c, 0, sizeof(*c));
*safer = value;
return 0;
}
int anvil_dtype_int(anvil_dtype * c, uint32_t value)
{
return init_anvil_dtype(c, dtype(value));
}
int anvil_dtype_dbl(anvil_dtype * c, double value)
{
return init_anvil_dtype(c, dtype(value));
}
int anvil_dtype_str(anvil_dtype * c, const char * value)
{
return init_anvil_dtype(c, dtype(value));
}
int anvil_dtype_istr(anvil_dtype * c, const anvil_istr * value)
{
anvil_istr_union_const value_safer(value);
return init_anvil_dtype(c, dtype(*value_safer));
}
int anvil_dtype_blb(anvil_dtype * c, const anvil_blob * value)
{
anvil_blob_union_const value_safer(value);
return init_anvil_dtype(c, dtype(*value_safer));
}
int anvil_dtype_copy(anvil_dtype * c, const anvil_dtype * src)
{
anvil_dtype_union_const src_safer(src);
return init_anvil_dtype(c, *src_safer);
}
void anvil_dtype_kill(anvil_dtype * c)
{
anvil_dtype_union safer(c);
*safer = dtype(0u);
}
anvil_dtype_type anvil_dtype_get_type(const anvil_dtype * c)
{
anvil_dtype_union_const safer(c);
/* same values; see dtype.h */
return (anvil_dtype_type) safer->type;
}
int anvil_dtype_get_int(const anvil_dtype * c, uint32_t * value)
{
anvil_dtype_union_const safer(c);
assert(safer->type == dtype::UINT32);
*value = safer->u32;
return 0;
}
int anvil_dtype_get_dbl(const anvil_dtype * c, double * value)
{
anvil_dtype_union_const safer(c);
assert(safer->type == dtype::DOUBLE);
*value = safer->dbl;
return 0;
}
int anvil_dtype_get_str(const anvil_dtype * c, anvil_istr * value)
{
anvil_dtype_union_const safer(c);
assert(safer->type == dtype::STRING);
return init_anvil_istr(value, safer->str);
}
int anvil_dtype_get_blb(const anvil_dtype * c, anvil_blob * value)
{
anvil_dtype_union_const safer(c);
assert(safer->type == dtype::BLOB);
return init_anvil_blob(value, safer->blb);
}
int anvil_dtype_compare(const anvil_dtype * a, const anvil_dtype * b)
{
anvil_dtype_union_const a_safer(a);
anvil_dtype_union_const b_safer(b);
return a_safer->compare(*b_safer);
}
int anvil_dtype_compare_blobcmp(const anvil_dtype * a, const anvil_dtype * b, const anvil_blobcmp * cmp)
{
anvil_dtype_union_const a_safer(a);
anvil_dtype_union_const b_safer(b);
return a_safer->compare(*b_safer, cmp);
}
const char * anvil_dtype_type_name(anvil_dtype_type type)
{
/* same values; see dtype.h */
return dtype::name((dtype::ctype) type);
}
int anvil_dtable_create(const char * type, int dfd, const char * name, const anvil_params * config, const anvil_dtable * source, const anvil_dtable * shadow)
{
anvil_params_union_const config_safer(config);
anvil_dtable_union_const source_safer(source);
anvil_dtable_union_const shadow_safer(shadow);
return dtable_factory::setup(type, dfd, name, *config_safer, source_safer, shadow_safer);
}
int anvil_dtable_create_empty(const char * type, int dfd, const char * name, const anvil_params * config, anvil_dtype_type key_type)
{
anvil_params_union_const config_safer(config);
/* same values; see dtype.h */
return dtable_factory::setup(type, dfd, name, *config_safer, (dtype::ctype) key_type);
}
anvil_dtable * anvil_dtable_open(const char * type, int dfd, const char * name, const anvil_params * config)
{
anvil_params_union_const config_safer(config);
dtable * cpp = dtable_factory::load(type, dfd, name, *config_safer, sys_journal::get_global_journal());
return anvil_dtable_union(cpp);
}
void anvil_dtable_kill(anvil_dtable * c)
{
anvil_dtable_union safer(c);
safer.cpp->destroy();
}
bool anvil_dtable_contains(const anvil_dtable * c, const anvil_dtype * key)
{
return anvil_dtable_contains_tx(c, key, NO_ABORTABLE_TX);
}
bool anvil_dtable_contains_tx(const anvil_dtable * c, const anvil_dtype * key, abortable_tx atx)
{
anvil_dtable_union_const safer(c);
anvil_dtype_union_const key_safer(key);
return safer->contains(*key_safer, atx);
}
int anvil_dtable_find(const anvil_dtable * c, const anvil_dtype * key, anvil_blob * value)
{
return anvil_dtable_find_tx(c, key, value, NO_ABORTABLE_TX);
}
int anvil_dtable_find_tx(const anvil_dtable * c, const anvil_dtype * key, anvil_blob * value, abortable_tx atx)
{
anvil_dtable_union_const safer(c);
anvil_dtype_union_const key_safer(key);
return init_anvil_blob(value, safer->find(*key_safer, atx));
}
bool anvil_dtable_writable(const anvil_dtable * c)
{
anvil_dtable_union_const safer(c);
return safer->writable();
}
int anvil_dtable_insert(anvil_dtable * c, const anvil_dtype * key, const anvil_blob * value, bool append)
{
return anvil_dtable_insert_tx(c, key, value, append, NO_ABORTABLE_TX);
}
int anvil_dtable_insert_tx(anvil_dtable * c, const anvil_dtype * key, const anvil_blob * value, bool append, abortable_tx atx)
{
anvil_dtable_union safer(c);
anvil_dtype_union_const key_safer(key);
anvil_blob_union_const value_safer(value);
return safer->insert(*key_safer, *value_safer, append, atx);
}
int anvil_dtable_remove(anvil_dtable * c, const anvil_dtype * key)
{
return anvil_dtable_remove_tx(c, key, NO_ABORTABLE_TX);
}
int anvil_dtable_remove_tx(anvil_dtable * c, const anvil_dtype * key, abortable_tx atx)
{
anvil_dtable_union safer(c);
anvil_dtype_union_const key_safer(key);
return safer->remove(*key_safer, atx);
}
anvil_dtype_type anvil_dtable_key_type(const anvil_dtable * c)
{
anvil_dtable_union_const safer(c);
/* same values; see dtype.h */
return (anvil_dtype_type) safer->key_type();
}
int anvil_dtable_set_blob_cmp(anvil_dtable * c, const anvil_blobcmp * cmp)
{
anvil_dtable_union safer(c);
return safer->set_blob_cmp(cmp);
}
const char * anvil_dtable_get_cmp_name(const anvil_dtable * c)
{
anvil_dtable_union_const safer(c);
return safer->get_cmp_name();
}
int anvil_dtable_maintain(anvil_dtable * c)
{
anvil_dtable_union safer(c);
return safer->maintain();
}
abortable_tx anvil_dtable_create_atx(anvil_dtable * c)
{
anvil_dtable_union safer(c);
return safer->create_tx();
}
int anvil_dtable_check_atx(const anvil_dtable * c, abortable_tx atx)
{
anvil_dtable_union_const safer(c);
return safer->check_tx(atx);
}
int anvil_dtable_commit_atx(anvil_dtable * c, abortable_tx atx)
{
anvil_dtable_union safer(c);
return safer->commit_tx(atx);
}
void anvil_dtable_abort_atx(anvil_dtable * c, abortable_tx atx)
{
anvil_dtable_union safer(c);
safer->abort_tx(atx);
}
anvil_dtable_key_iter * anvil_dtable_keys(const anvil_dtable * c)
{
return anvil_dtable_keys_tx(c, NO_ABORTABLE_TX);
}
anvil_dtable_key_iter * anvil_dtable_keys_tx(const anvil_dtable * c, abortable_tx atx)
{
anvil_dtable_union_const safer(c);
dtable::key_iter * iter = safer->iterator(atx);
dtable::key_iter * cache = iter ? new dtable_cache_key_iter(iter) : NULL;
if(!cache && iter)
delete iter;
return anvil_dtable_key_iter_union(cache);
}
bool anvil_dtable_key_iter_valid(const anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union_const safer(c);
return safer->valid();
}
bool anvil_dtable_key_iter_next(anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union safer(c);
return safer->next();
}
bool anvil_dtable_key_iter_prev(anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union safer(c);
return safer->prev();
}
bool anvil_dtable_key_iter_first(anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union safer(c);
return safer->first();
}
bool anvil_dtable_key_iter_last(anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union safer(c);
return safer->last();
}
int anvil_dtable_key_iter_key(const anvil_dtable_key_iter * c, anvil_dtype * key)
{
anvil_dtable_key_iter_union_const safer(c);
return init_anvil_dtype(key, safer->key());
}
bool anvil_dtable_key_iter_seek(anvil_dtable_key_iter * c, const anvil_dtype * key)
{
anvil_dtable_key_iter_union safer(c);
anvil_dtype_union_const key_safer(key);
return safer->seek(*key_safer);
}
bool anvil_dtable_key_iter_seek_test(anvil_dtable_key_iter * c, blob_test test, void * user)
{
anvil_dtable_key_iter_union safer(c);
return safer->seek(anvil_dtype_test(test, user));
}
void anvil_dtable_key_iter_kill(anvil_dtable_key_iter * c)
{
anvil_dtable_key_iter_union safer(c);
delete safer.cpp;
}
anvil_dtable_iter * anvil_dtable_iterator(const anvil_dtable * c)
{
return anvil_dtable_iterator_tx(c, NO_ABORTABLE_TX);
}
anvil_dtable_iter * anvil_dtable_iterator_tx(const anvil_dtable * c, abortable_tx atx)
{
anvil_dtable_union_const safer(c);
return anvil_dtable_iter_union(wrap_and_claim<dtable_cache_iter>(safer->iterator(atx)));
}
bool anvil_dtable_iter_valid(const anvil_dtable_iter * c)
{
anvil_dtable_iter_union_const safer(c);
return safer->valid();
}
bool anvil_dtable_iter_next(anvil_dtable_iter * c)
{
anvil_dtable_iter_union safer(c);
return safer->next();
}
bool anvil_dtable_iter_prev(anvil_dtable_iter * c)
{
anvil_dtable_iter_union safer(c);
return safer->prev();
}
bool anvil_dtable_iter_first(anvil_dtable_iter * c)
{
anvil_dtable_iter_union safer(c);
return safer->first();
}
bool anvil_dtable_iter_last(anvil_dtable_iter * c)
{
anvil_dtable_iter_union safer(c);
return safer->last();
}
int anvil_dtable_iter_key(const anvil_dtable_iter * c, anvil_dtype * key)
{
anvil_dtable_iter_union_const safer(c);
return init_anvil_dtype(key, safer->key());
}
bool anvil_dtable_iter_seek(anvil_dtable_iter * c, const anvil_dtype * key)
{
anvil_dtable_iter_union safer(c);
anvil_dtype_union_const key_safer(key);
return safer->seek(*key_safer);
}
bool anvil_dtable_iter_seek_test(anvil_dtable_iter * c, blob_test test, void * user)
{
anvil_dtable_iter_union safer(c);
return safer->seek(anvil_dtype_test(test, user));
}
void anvil_dtable_iter_meta(const anvil_dtable_iter * c, anvil_metablob * meta)
{
anvil_dtable_iter_union_const safer(c);
init_anvil_metablob(meta, safer->meta());
}
int anvil_dtable_iter_value(const anvil_dtable_iter * c, anvil_blob * value)
{
anvil_dtable_iter_union_const safer(c);
return init_anvil_blob(value, safer->value());
}
void anvil_dtable_iter_kill(anvil_dtable_iter * c)
{
anvil_dtable_iter_union_const safer(c);
delete safer.cpp;
}
int anvil_ctable_create(const char * type, int dfd, const char * name, const anvil_params * config, anvil_dtype_type key_type)
{
anvil_params_union_const config_safer(config);
/* same values; see dtype.h */
return ctable_factory::setup(type, dfd, name, *config_safer, (dtype::ctype) key_type);
}
anvil_ctable * anvil_ctable_open(const char * type, int dfd, const char * name, const anvil_params * config)
{
anvil_params_union_const config_safer(config);
ctable * cpp = ctable_factory::load(type, dfd, name, *config_safer, sys_journal::get_global_journal());
return anvil_ctable_union(cpp);
}
void anvil_ctable_kill(anvil_ctable * c)
{
anvil_ctable_union safer(c);
delete safer.cpp;
}
int anvil_ctable_find(const anvil_ctable * c, const anvil_dtype * key, const anvil_istr * column, anvil_blob * value)
{
anvil_ctable_union_const safer(c);
anvil_dtype_union_const key_safer(key);
anvil_istr_union_const column_safer(column);
return init_anvil_blob(value, safer->find(*key_safer, *column_safer));
}
bool anvil_ctable_writable(const anvil_ctable * c)
{
anvil_ctable_union_const safer(c);
return safer->writable();
}
int anvil_ctable_insert(anvil_ctable * c, const anvil_dtype * key, const anvil_istr * column, const anvil_blob * value, bool append)
{
anvil_ctable_union safer(c);
anvil_dtype_union_const key_safer(key);
anvil_istr_union_const column_safer(column);
anvil_blob_union_const value_safer(value);
return safer->insert(*key_safer, *column_safer, *value_safer, append);
}
int anvil_ctable_remove(anvil_ctable * c, const anvil_dtype * key, const anvil_istr * column)
{
anvil_ctable_union safer(c);
anvil_dtype_union_const key_safer(key);
anvil_istr_union_const column_safer(column);
return safer->remove(*key_safer, *column_safer);
}
int anvil_ctable_remove_row(anvil_ctable * c, const anvil_dtype * key)
{
anvil_ctable_union safer(c);
anvil_dtype_union_const key_safer(key);
return safer->remove(*key_safer);
}
anvil_dtype_type anvil_ctable_key_type(const anvil_ctable * c)
{
anvil_ctable_union_const safer(c);
/* same values; see dtype.h */
return (anvil_dtype_type) safer->key_type();
}
int anvil_ctable_set_blob_cmp(anvil_ctable * c, const anvil_blobcmp * cmp)
{
anvil_ctable_union safer(c);
return safer->set_blob_cmp(cmp);
}
const char * anvil_ctable_get_cmp_name(const anvil_ctable * c)
{
anvil_ctable_union_const safer(c);
return safer->get_cmp_name();
}
int anvil_ctable_maintain(anvil_ctable * c)
{
anvil_ctable_union safer(c);
return safer->maintain();
}
anvil_dtable_key_iter * anvil_ctable_keys(const anvil_ctable * c)
{
anvil_ctable_union_const safer(c);
dtable::key_iter * iter = safer->keys();
dtable::key_iter * cache = iter ? new dtable_cache_key_iter(iter) : NULL;
if(!cache && iter)
delete iter;
return anvil_dtable_key_iter_union(cache);
}
anvil_ctable_iter * anvil_ctable_iterator(const anvil_ctable * c)
{
anvil_ctable_union_const safer(c);
ctable::iter * iter = safer->iterator();
ctable::iter * cache = iter ? new ctable_cache_iter(iter) : NULL;
if(!cache && iter)
delete iter;
return anvil_ctable_iter_union(cache);
}
bool anvil_ctable_iter_valid(const anvil_ctable_iter * c)
{
anvil_ctable_iter_union_const safer(c);
return safer->valid();
}
bool anvil_ctable_iter_next(anvil_ctable_iter * c)
{
anvil_ctable_iter_union safer(c);
return safer->next();
}
bool anvil_ctable_iter_prev(anvil_ctable_iter * c)
{
anvil_ctable_iter_union safer(c);
return safer->prev();
}
bool anvil_ctable_iter_first(anvil_ctable_iter * c)
{
anvil_ctable_iter_union safer(c);
return safer->first();
}
bool anvil_ctable_iter_last(anvil_ctable_iter * c)
{
anvil_ctable_iter_union safer(c);
return safer->last();
}
int anvil_ctable_iter_key(const anvil_ctable_iter * c, anvil_dtype * key)
{
anvil_ctable_iter_union_const safer(c);
return init_anvil_dtype(key, safer->key());
}
bool anvil_ctable_iter_seek(anvil_ctable_iter * c, const anvil_dtype * key)
{
anvil_ctable_iter_union safer(c);
anvil_dtype_union_const key_safer(key);
return safer->seek(*key_safer);
}
bool anvil_ctable_iter_seek_test(anvil_ctable_iter * c, blob_test test, void * user)
{
anvil_ctable_iter_union safer(c);
return safer->seek(anvil_dtype_test(test, user));
}
size_t anvil_ctable_iter_column(const anvil_ctable_iter * c)
{
anvil_ctable_iter_union_const safer(c);
return safer->column();
}
const char * anvil_ctable_iter_name(const anvil_ctable_iter * c)
{
anvil_ctable_iter_union_const safer(c);
return safer->name();
}
int anvil_ctable_iter_value(const anvil_ctable_iter * c, anvil_blob * value)
{
anvil_ctable_iter_union_const safer(c);
return init_anvil_blob(value, safer->value());
}
void anvil_ctable_iter_kill(anvil_ctable_iter * c)
{
anvil_ctable_iter_union safer(c);
delete safer.cpp;
}
anvil_blobcmp * anvil_new_blobcmp(const char * name, blobcmp_func cmp, void * user, blobcmp_free kill, bool free_user)
{
anvil_blobcmp * blobcmp = new anvil_blobcmp(name);
blobcmp->cmp = cmp;
blobcmp->kill = kill;
blobcmp->copied = free_user;
blobcmp->user = user;
return blobcmp;
}
anvil_blobcmp * anvil_new_blobcmp_copy(const char * name, blobcmp_func cmp, const void * user, size_t size, blobcmp_free kill)
{
anvil_blobcmp * blobcmp = new anvil_blobcmp(name);
blobcmp->cmp = cmp;
blobcmp->kill = kill;
blobcmp->copied = true;
blobcmp->user = malloc(size);
util::memcpy(blobcmp->user, user, size);
return blobcmp;
}
const char * anvil_blobcmp_name(const anvil_blobcmp * blobcmp)
{
return blobcmp->name;
}
void anvil_blobcmp_retain(anvil_blobcmp * blobcmp)
{
blobcmp->retain();
}
void anvil_blobcmp_release(anvil_blobcmp ** blobcmp)
{
(*blobcmp)->release();
*blobcmp = NULL;
}
anvil_dtable * anvil_dtable_cache::open(int index, const anvil_params * config)
{
char number[24];
anvil_dtable * dtable;
idx_map::iterator iter = index_map.find(index);
if(iter != index_map.end())
{
iter->second.count++;
return iter->second.dtable;
}
snprintf(number, sizeof(number), "%d", index);
dtable = anvil_dtable_open(type, dir_fd, number, config);
if(!dtable)
return NULL;
index_map[index].init(dtable);
dtable_map[dtable] = index;
return dtable;
}
void anvil_dtable_cache::close(anvil_dtable * dtable)
{
dt_map::iterator index = dtable_map.find(dtable);
assert(index != dtable_map.end());
idx_map::iterator iter = index_map.find(index->second);
assert(iter != index_map.end());
if(!--(iter->second.count))
{
int i;
for(i = 0; i < RECENT_OPEN_DTABLES; i++)
if(recent[i] == dtable)
{
/* it's being closed from the recent list */
dtable_map.erase(index);
for(int j = 0; j < OPEN_DTABLE_ITERS; j++)
if(iter->second.iters[j])
{
iter_map.erase(iter->second.iters[j]);
anvil_dtable_iter_kill(iter->second.iters[j]);
iter->second.iters[j] = NULL;
}
index_map.erase(iter);
anvil_dtable_kill(dtable);
recent[i] = NULL;
return;
}
/* give it a second chance in the recent list */
for(i = 0; i < RECENT_OPEN_DTABLES; i++)
if(!recent[i])
break;
if(i == RECENT_OPEN_DTABLES)
close(recent[--i]);
for(; i; i--)
recent[i] = recent[i - 1];
recent[0] = dtable;
iter->second.count++;
}
}
bool anvil_dtable_cache::can_maintain(int index)
{
idx_map::iterator iter = index_map.find(index);
if(iter == index_map.end())
return false;
return (ticks - iter->second.last_tick) > DELTA_TICKS;
}
int anvil_dtable_cache::maintain(int index)
{
int r = -1;
idx_map::iterator iter = index_map.find(index);