forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursor_ll.c
1751 lines (1508 loc) · 55.3 KB
/
cursor_ll.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
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
*
* ABSTRACTION FOR A BERKDB BTREE
*
* The berkdb can be a persistent/real file or a temp/shadow file.
* (i.e. calling persistent files * real, as opposed to shadow).
* The operations are cursor based:
* - open (creator function) (X)
* - close (X)
* - first (X)
* - last (X)
* - prev (X)
* - next (X)
* - find (X)
* - insert (X)
* - delete (X)
*
* Abstracts:
* - all DBT management
* - bulk processing
* - able to use a persistent btree or a temp table as backed
*
* There is NO knowledge of what btree-s contains, this abstraction provides
* a uniform way to use temp_tables together with persistent files under the
* same api.
* There is no knowledge of stripes, indexes vs data, genid, dup keys or
*simple.
* There is no effort to keep the cursor pointed to something if a lookup
*fails.
* Each operation updates only the piece of info that has knowledge of, instead
* of updating the whole berkdb state.
* All the extra semantic that used to be meshed in this code is now handled
* in the upper layer cursor.c (cough bdb_sql.c).
*
* Game Goal:
* - Merging temp table DBT management here and maybe unify the api
*
* Dreaming:
* - Would be nice to merge this with index path, but for now I need this to
*work
* first
*
* NOTE on return codes
* - there is no PASTEOF return from these functions; a move operation that
*fails to
* find a target returns IX_NOTFND (matchin DB_NOTFOUND); it is up to the
*cursor.c
* abstraction to convert this to a IX_PASTEOF in those cases when we do a
*NEXT/PREV
* that fail to retrieve a new record!
* - accepted return codes:
* IX_FND : found what was asked for, row cached in berkdb object
* IX_NOTFND : not found what was looked for; if berkdb was pointing
*somewhere, it gets
* to keep the position
* IX_EMPTY : returned for a first/last move that does not find a row
* (TODO: WE COULD HANDLE IX_EMPTY IN SAME WAY PASTEOF IS
*HANDLED)
* <0 & bdberr: errors
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <string.h>
#include <strings.h>
#include <assert.h>
#include <thread_malloc.h>
#include <build/db.h>
#include "bdb_cursor.h"
#include "bdb_int.h"
#include "cursor_ll.h"
#include "bdb_osqlcur.h"
#include "bdb_osqllog.h"
#include "logmsg.h"
#include "tohex.h"
static unsigned int berkdb_counter = 0;
static int bdb_berkdb_unlock(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_lock(bdb_berkdb_t *pberkdb, cursor_tran_t *curtran,
int *bdberr);
static int bdb_berkdb_move(bdb_berkdb_t *pberkdb, int dir, int *bdberr);
static int bdb_berkdb_dta(bdb_berkdb_t *pberkdb, char **dta, int *bdberr);
static int bdb_berkdb_dtasize(bdb_berkdb_t *pberkdb, int *dtasize, int *bdberr);
static int bdb_berkdb_key(bdb_berkdb_t *pberkdb, char **key, int *bdberr);
static int bdb_berkdb_keysize(bdb_berkdb_t *pberkdb, int *keysize, int *bdberr);
static int bdb_berkdb_ver(bdb_berkdb_t *pberkdb, uint8_t *ver, int *bdberr);
static int bdb_berkdb_insert(bdb_berkdb_t *pberkdb, char *key, int keylen,
char *dta, int dtalen, int *bdberr);
static int bdb_berkdb_delete(bdb_berkdb_t *pberkdb, int *bdberr);
static void bdb_berkdb_trak(bdb_berkdb_t *pberkdb, int status);
static int bdb_berkdb_get_everything(struct bdb_berkdb *, char **dta,
int *dtasize, char **key, int *keysize,
uint8_t *ver, int *bdberr);
static int bdb_berkdb_is_at_eof(struct bdb_berkdb *pberkdb);
static int bdb_berkdb_prevent_optimized(struct bdb_berkdb *);
static int bdb_berkdb_allow_optimized(struct bdb_berkdb *);
static int bdb_berkdb_close_real(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_first_real(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_next_real(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_prev_real(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_last_real(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_find_real(bdb_berkdb_t *pberkdb, void *key, int keylen,
int how, int *bdberr);
static int bdb_berkdb_get_skip_stat(bdb_berkdb_t *pberkdb, u_int64_t *nextcount,
u_int64_t *skipcount);
static int bdb_berkdb_close_shad(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_first_shad(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_next_shad(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_prev_shad(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_last_shad(bdb_berkdb_t *pberkdb, int *bdberr);
static int bdb_berkdb_find_shad(bdb_berkdb_t *pberkdb, void *key, int keylen,
int how, int *bdberr);
static int bdb_berkdb_cget(bdb_berkdb_impl_t *berkdb, int use_bulk, int how,
int *bdberr);
static int bdb_berkdb_get_pagelsn(bdb_berkdb_t *pberkdb, DB_LSN *lsn,
int *bdberr);
static int bdb_berkdb_get_fileid(bdb_berkdb_t *pberkdb, void *pfileid,
int *bdberr);
static int bdb_berkdb_handle_optimized(bdb_berkdb_t *pberkdb, int *bdberr);
static int prevent_optimized(bdb_berkdb_t *pberkdb);
static int allow_optimized(bdb_berkdb_t *pberkdb);
static int outoforder_set(bdb_berkdb_t *pberkdb, int status);
static int outoforder_get(bdb_berkdb_t *pberkdb);
static int bdb_berkdb_get_pageindex(bdb_berkdb_t *pberkdb, int *page,
int *index, int *bdberr);
static int bdb_berkdb_defer_update_shadows(bdb_berkdb_t *pberkdb);
/* These routines are in cursor_rowlocks.c */
int bdb_berkdb_rowlocks_unlock(struct bdb_berkdb *pberkdb, int *bdberr);
int bdb_berkdb_rowlocks_lock(struct bdb_berkdb *pberkdb,
struct cursor_tran *curtran, int *bdberr);
int bdb_berkdb_rowlocks_move(struct bdb_berkdb *berkdb, int dir, int *bdberr);
int bdb_berkdb_rowlocks_first(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_next(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_prev(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_last(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_close(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_get_pagelsn(struct bdb_berkdb *berkdb, DB_LSN *lsn,
int *bdberr);
int bdb_berkdb_rowlocks_dta(struct bdb_berkdb *berkdb, char **dta, int *bdberr);
int bdb_berkdb_rowlocks_dtasize(struct bdb_berkdb *berkdb, int *dtasize,
int *bdberr);
int bdb_berkdb_rowlocks_key(struct bdb_berkdb *berkdb, char **key, int *bdberr);
int bdb_berkdb_rowlocks_keysize(struct bdb_berkdb *berkdb, int *keysize,
int *bdberr);
int bdb_berkdb_rowlocks_find(struct bdb_berkdb *berkdb, void *key, int keysize,
int how, int *bdberr);
int bdb_berkdb_rowlocks_insert(struct bdb_berkdb *berkdb, char *key, int keylen,
char *dta, int dtalen, int *bdberr);
int bdb_berkdb_rowlocks_delete(struct bdb_berkdb *berkdb, int *bdberr);
int bdb_berkdb_rowlocks_get_pageindex(bdb_berkdb_t *pberkdb, int *page,
int *index, int *bdberr);
int bdb_berkdb_rowlocks_get_fileid(bdb_berkdb_t *pberkdb, void *pfileid,
int *bdberr);
int bdb_berkdb_rowlocks_pause(bdb_berkdb_t *pberkdb, int *bdberr);
int bdb_berkdb_rowlocks_init(struct bdb_berkdb *berkdb, DB *db, int *bdberr);
int bdb_berkdb_rowlocks_prevent_optimized(struct bdb_berkdb *berkdb);
int bdb_berkdb_rowlocks_allow_optimized(struct bdb_berkdb *berkdb);
int bdb_berkdb_rowlocks_get_skip_stat(struct bdb_berkdb *berkdb,
u_int64_t *nextcount,
u_int64_t *skipcount);
int bdb_berkdb_rowlocks_defer_update_shadows(struct bdb_berkdb *berkdb);
/* factory method */
bdb_berkdb_t *bdb_berkdb_open(bdb_cursor_impl_t *cur, int type, int maxdata,
int maxkey, int *bdberr)
{
bdb_berkdb_t *pberkdb;
bdb_berkdb_impl_t *berkdb;
bdb_state_type *bdb_state = cur->state;
DB *db = NULL;
DBC *dbc = NULL;
DB_LSN curlsn, lwmlsn;
int rc = 0;
int lwmrc = 0;
int cmp;
u_int32_t curflags = 0;
*bdberr = 0;
pberkdb = (bdb_berkdb_t *)thread_malloc(sizeof(bdb_berkdb_t));
if (!pberkdb) {
*bdberr = BDBERR_MALLOC;
return NULL;
}
memset(pberkdb, 0, sizeof(bdb_berkdb_t));
berkdb = pberkdb->impl =
(bdb_berkdb_impl_t *)thread_malloc(sizeof(bdb_berkdb_impl_t));
if (!berkdb) {
*bdberr = BDBERR_MALLOC;
thread_free(pberkdb);
return NULL;
}
memset(berkdb, 0, sizeof(bdb_berkdb_impl_t));
berkdb->bdb_state = bdb_state;
if (type == BERKDB_REAL) {
DBT *dbt = &berkdb->u.rl.data;
dbt->data = thread_malloc(maxdata + maxkey);
if (!dbt->data) {
*bdberr = BDBERR_MALLOC;
return NULL;
}
dbt->ulen = maxdata;
dbt->flags = DB_DBT_USERMEM;
DBT *dbk = &berkdb->u.rl.key;
dbk->data = ((char *)berkdb->u.rl.data.data) + maxdata;
dbk->ulen = maxkey;
dbk->flags = DB_DBT_USERMEM;
/* shadows will be set based on temp_table */
berkdb->u.rl.bulk.ulen = bdb_state->attr->sqlbulksz;
if (cur->type == BDBC_DT) {
DBT *dbth = &berkdb->u.rl.odh;
dbth->flags = DB_DBT_USERMEM;
dbth->ulen = MAXRECSZ;
/* Maximum record size. */
berkdb->u.rl.odh_tmp = thread_malloc(MAXRECSZ);
if (!berkdb->u.rl.odh_tmp) {
*bdberr = BDBERR_MALLOC;
logmsg(LOGMSG_ERROR, "%s:%d malloc error %d\n", __FILE__, __LINE__,
MAXRECSZ);
thread_free(berkdb->u.rl.data.data);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
berkdb->u.rl.use_odh = 1;
db = bdb_state->dbp_data[0][cur->idx];
} else {
if (cur->idx < -1 || cur->idx > cur->state->numix) {
logmsg(LOGMSG_ERROR,
"%s: incorrect index %d must be between -1 and %d\n",
__func__, cur->idx, bdb_state->numix);
cheap_stack_trace();
*bdberr = BDBERR_BADARGS;
thread_free(berkdb->u.rl.data.data);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
/* Indexes do not use odh. */
berkdb->u.rl.use_odh = 0;
db = cur->state->dbp_ix[cur->idx];
}
if (!db) {
*bdberr = BDBERR_BADARGS;
if (berkdb->u.rl.use_odh)
thread_free(berkdb->u.rl.odh_tmp);
thread_free(berkdb->u.rl.data.data);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
berkdb->u.rl.id = berkdb_counter++;
/* Set appropriate cursor flags. */
if (cur->pageorder) {
curflags |= DB_PAGE_ORDER;
if (cur->discardpages)
curflags |= DB_DISCARD_PAGES;
}
dbc =
get_cursor_for_cursortran_flags(cur->curtran, db, curflags, bdberr);
if (!dbc) {
if (cur->state->attr->dbgberkdbcursor)
logmsg(LOGMSG_USER, "BERKDB=%d %p cursor dbc=%p rc=%d bdberr=%d "
"OPEN [%s %d] (%p %d)n",
berkdb->u.rl.id, berkdb, dbc, rc, *bdberr,
cursortype(cur->type), cur->idx, cur->curtran,
bdb_get_lid_from_cursortran(cur->curtran));
if (berkdb->u.rl.use_odh)
thread_free(berkdb->u.rl.odh_tmp);
thread_free(berkdb->u.rl.data.data);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
if (cur->state->attr->dbgberkdbcursor)
logmsg(LOGMSG_USER, "BERKDB=%d %p cursor dbc=%p rc=%d bdberr=%d OPEN "
"[%s %d] (%p %d)\n",
berkdb->u.rl.id, berkdb, dbc, rc, *bdberr,
cursortype(cur->type), cur->idx, cur->curtran,
bdb_get_lid_from_cursortran(cur->curtran));
/*berkdb->u.rl.db = db;*/
berkdb->u.rl.dbc = dbc;
pberkdb->unlock = bdb_berkdb_unlock;
pberkdb->lock = bdb_berkdb_lock;
pberkdb->move = bdb_berkdb_move;
pberkdb->first = bdb_berkdb_first_real;
pberkdb->next = bdb_berkdb_next_real;
pberkdb->prev = bdb_berkdb_prev_real;
pberkdb->last = bdb_berkdb_last_real;
pberkdb->close = bdb_berkdb_close_real;
pberkdb->get_pagelsn = bdb_berkdb_get_pagelsn;
pberkdb->find = bdb_berkdb_find_real;
pberkdb->get_skip_stat = bdb_berkdb_get_skip_stat;
pberkdb->pageindex = bdb_berkdb_get_pageindex;
pberkdb->fileid = bdb_berkdb_get_fileid;
pberkdb->defer_update_shadows = bdb_berkdb_defer_update_shadows;
} else if (type == BERKDB_REAL_ROWLOCKS) {
if (cur->type == BDBC_DT) {
db = bdb_state->dbp_data[0][cur->idx];
} else {
if (cur->idx < -1 || cur->idx > cur->state->numix) {
logmsg(LOGMSG_ERROR,
"%s: incorrect index %d must be between -1 and %d\n",
__func__, cur->idx, bdb_state->numix);
cheap_stack_trace();
*bdberr = BDBERR_BADARGS;
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
db = cur->state->dbp_ix[cur->idx];
}
/* Set appropriate cursor flags. */
if (cur->pageorder) {
curflags |= DB_PAGE_ORDER;
if (cur->discardpages)
curflags |= DB_DISCARD_PAGES;
}
/* Rowlocks cursors must be pausible */
curflags |= DB_PAUSIBLE;
dbc =
get_cursor_for_cursortran_flags(cur->curtran, db, curflags, bdberr);
if (!dbc) {
if (cur->state->attr->dbgberkdbcursor)
logmsg(LOGMSG_USER, "BERKDB=%d %p cursor dbc=%p rc=%d bdberr=%d "
"OPEN [%s %d] (%p %d)n",
berkdb->u.rl.id, berkdb, dbc, rc, *bdberr,
cursortype(cur->type), cur->idx, cur->curtran,
bdb_get_lid_from_cursortran(cur->curtran));
if (berkdb->u.rl.use_odh)
thread_free(berkdb->u.rl.odh_tmp);
thread_free(berkdb->u.rl.data.data);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
pberkdb->impl->u.row.pagelock_cursor = dbc;
bdb_berkdb_rowlocks_get_fileid(pberkdb, pberkdb->impl->u.row.fileid,
bdberr);
/* Get the current lsn */
__log_txn_lsn(bdb_state->dbenv, &curlsn, NULL, NULL);
/* Get the lwm lsn */
lwmrc = bdb_get_lsn_lwm(bdb_state, &lwmlsn);
/* Compare to avoid racing */
if (lwmlsn.file == 0 || 0 != lwmrc ||
((cmp = log_compare(&curlsn, &lwmlsn)) < 0)) {
/* Lowest is curlsn */
pberkdb->impl->u.row.lwmlsn.file = curlsn.file;
pberkdb->impl->u.row.lwmlsn.offset = curlsn.offset;
} else {
/* Lowest is lwm */
pberkdb->impl->u.row.lwmlsn.file = lwmlsn.file;
pberkdb->impl->u.row.lwmlsn.offset = lwmlsn.offset;
}
/* lock/unlock? - what do these do */
pberkdb->lock = bdb_berkdb_rowlocks_lock;
pberkdb->unlock = bdb_berkdb_rowlocks_unlock;
pberkdb->move = bdb_berkdb_rowlocks_move;
pberkdb->first = bdb_berkdb_rowlocks_first;
pberkdb->next = bdb_berkdb_rowlocks_next;
pberkdb->prev = bdb_berkdb_rowlocks_prev;
pberkdb->last = bdb_berkdb_rowlocks_last;
pberkdb->close = bdb_berkdb_rowlocks_close;
pberkdb->get_pagelsn = bdb_berkdb_rowlocks_get_pagelsn;
pberkdb->dta = bdb_berkdb_rowlocks_dta;
pberkdb->dtasize = bdb_berkdb_rowlocks_dtasize;
pberkdb->key = bdb_berkdb_rowlocks_key;
pberkdb->keysize = bdb_berkdb_rowlocks_keysize;
pberkdb->find = bdb_berkdb_rowlocks_find;
pberkdb->get_skip_stat = bdb_berkdb_rowlocks_get_skip_stat;
pberkdb->pageindex = bdb_berkdb_rowlocks_get_pageindex;
pberkdb->fileid = bdb_berkdb_rowlocks_get_fileid;
pberkdb->pause = bdb_berkdb_rowlocks_pause;
pberkdb->defer_update_shadows =
bdb_berkdb_rowlocks_defer_update_shadows;
} else if (type == BERKDB_SHAD || type == BERKDB_SHAD_CREATE) {
berkdb->u.sd.cur = bdb_osql_open_backfilled_shadows(
cur, cur->shadow_tran->osql, type, bdberr);
if (*bdberr) {
if (*bdberr == BDBERR_TRANTOOCOMPLEX ||
*bdberr == BDBERR_TRAN_CANCELLED || *bdberr == BDBERR_NO_LOG) {
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
} else
logmsg(LOGMSG_ERROR, "%s failure to backfill shadow %d\n", __func__,
*bdberr);
}
#if 0
berkdb->u.sd.cur = bdb_tran_open_shadow( cur->state, cur->dbnum,
cur->shadow_tran, cur->idx,
cur->type, (type==BERKDB_SHAD_CREATE), bdberr);
#endif
if (!berkdb->u.sd.cur) {
/* normal case in which the shadow was not present and
there is no backfill for it */
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
/* Need odh logic for shadow-tables. */
if (cur->type == BDBC_DT) {
berkdb->u.sd.use_odh = 1;
berkdb->u.sd.odh_tmp = thread_malloc(MAXRECSZ);
if (!berkdb->u.sd.odh_tmp) {
*bdberr = BDBERR_MALLOC;
logmsg(LOGMSG_ERROR, "%s:%d malloc error %d\n", __FILE__, __LINE__,
MAXRECSZ);
thread_free(berkdb);
thread_free(pberkdb);
return NULL;
}
}
pberkdb->move = bdb_berkdb_move;
pberkdb->first = bdb_berkdb_first_shad;
pberkdb->next = bdb_berkdb_next_shad;
pberkdb->prev = bdb_berkdb_prev_shad;
pberkdb->last = bdb_berkdb_last_shad;
pberkdb->close = bdb_berkdb_close_shad;
pberkdb->find = bdb_berkdb_find_shad;
/* Normalize type to BERKDB_SHAD. */
type = BERKDB_SHAD;
} else {
logmsg(LOGMSG_ERROR, "%s: ugh?\n", __func__);
*bdberr = BDBERR_BUG_KILLME;
return NULL;
}
pberkdb->insert = bdb_berkdb_insert;
pberkdb->delete = bdb_berkdb_delete;
pberkdb->outoforder_get = outoforder_get;
pberkdb->outoforder_set = outoforder_set;
pberkdb->is_at_eof = bdb_berkdb_is_at_eof;
pberkdb->ver = bdb_berkdb_ver;
berkdb->cur = cur;
berkdb->type = type;
berkdb->trak = cur->trak;
if (type == BERKDB_REAL_ROWLOCKS) {
pberkdb->dta = bdb_berkdb_rowlocks_dta;
pberkdb->dtasize = bdb_berkdb_rowlocks_dtasize;
pberkdb->key = bdb_berkdb_rowlocks_key;
pberkdb->keysize = bdb_berkdb_rowlocks_keysize;
pberkdb->trak = bdb_berkdb_trak;
if (cur->type == BDBC_DT)
db = bdb_state->dbp_data[0][cur->idx];
else
db = bdb_state->dbp_ix[cur->idx];
rc = bdb_berkdb_rowlocks_init(pberkdb, db, bdberr);
if (rc) {
logmsg(LOGMSG_ERROR, "Can't open cursor rc %d bdberr %d\n", rc, *bdberr);
return NULL;
}
pberkdb->prevent_optimized = bdb_berkdb_rowlocks_prevent_optimized;
pberkdb->allow_optimized = bdb_berkdb_rowlocks_allow_optimized;
} else {
pberkdb->dta = bdb_berkdb_dta;
pberkdb->dtasize = bdb_berkdb_dtasize;
pberkdb->key = bdb_berkdb_key;
pberkdb->keysize = bdb_berkdb_keysize;
pberkdb->insert = bdb_berkdb_insert;
pberkdb->delete = bdb_berkdb_delete;
pberkdb->trak = bdb_berkdb_trak;
pberkdb->get_everything = bdb_berkdb_get_everything;
pberkdb->prevent_optimized = bdb_berkdb_prevent_optimized;
pberkdb->allow_optimized = bdb_berkdb_allow_optimized;
}
pberkdb->outoforder_set(pberkdb, 1);
return pberkdb;
}
static inline void reset_bulk_bt(bdb_realdb_tag_t *bt)
{
bt->bulkptr = NULL;
bt->lastdtasize = 0;
bt->lastdta = NULL;
bt->use_bulk = 0;
}
static inline int bdb_berkdb_get_pagelsn(bdb_berkdb_t *pberkdb, DB_LSN *lsn,
int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
return bt->dbc->c_get_pagelsn(bt->dbc, lsn);
}
static int bdb_berkdb_get_fileid(bdb_berkdb_t *pberkdb, void *pfileid,
int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
int rc;
if (bt->dbc) {
if ((rc = bt->dbc->c_get_fileid(bt->dbc, bt->fileid)) != 0)
return rc;
}
memcpy(pfileid, bt->fileid, DB_FILE_ID_LEN);
return 0;
}
static unsigned long long defer_count = 0;
static int bdb_berkdb_defer_update_shadows(bdb_berkdb_t *pberkdb)
{
bdb_berkdb_impl_t *cur = pberkdb->impl;
bdb_realdb_tag_t *bt = &cur->u.rl;
if (bt->need_update_shadows)
return 0;
defer_count++;
#ifdef CONFIRM_DEFER
static int lastpr = 0;
int now;
if ((now = time(NULL)) != lastpr) {
logmsg(LOGMSG_WARN, "Deferring update shadows, defer-count=%llu\n",
defer_count);
lastpr = now;
}
#endif
return 1;
}
static int bdb_berkdb_get_pageindex(bdb_berkdb_t *pberkdb, int *page,
int *index, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
if (page)
*page = bt->pagenum;
if (index)
*index = bt->pageidx;
return 0;
/*
return bt->dbc->c_get_pageindex(bt->dbc, page, index);
*/
}
/**
* Close a real berkdb
* - free buffers
* - close cursor
* - free it
*
*/
static int bdb_berkdb_close_real(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
int rc;
if (bt->use_odh)
thread_free(bt->odh_tmp);
if (bt->bulk.data)
thread_free(bt->bulk.data);
if (bt->data.data)
thread_free(bt->data.data);
/* db->dbc = NULL if bdbcursor was invalidated */
if (bt->dbc) {
rc = bt->dbc->c_close(bt->dbc);
bt->got_fileid = 0;
berkdb->cur->queue_cursor = NULL;
if (rc) {
*bdberr = (rc == DB_LOCK_DEADLOCK || rc == DB_REP_HANDLE_DEAD)
? BDBERR_DEADLOCK
: rc;
if (berkdb->cur->state->attr->dbgberkdbcursor)
logmsg(LOGMSG_USER,
"BERKDB=%d %p dbc=%p rc=%d bdberr=%d CLOSE (%p %d)\n",
berkdb->u.rl.id, berkdb, bt->dbc, rc, *bdberr,
berkdb->cur->curtran,
bdb_get_lid_from_cursortran(berkdb->cur->curtran));
return -1;
}
if (berkdb->cur->state->attr->dbgberkdbcursor)
logmsg(LOGMSG_USER,
"BERKDB=%d %p dbc=%p rc=%d bdberr=%d CLOSE (%p %d)\n",
berkdb->u.rl.id, berkdb, bt->dbc, rc, *bdberr,
berkdb->cur->curtran,
bdb_get_lid_from_cursortran(berkdb->cur->curtran));
}
thread_free(berkdb);
thread_free(pberkdb);
return 0;
}
/**
* Position cursor on the first/last row:
* - Move first; if ok, return IX_FND
* - If deadlock or error, return error
* - Return IX_EMPTY
*
* Returns: IX_FND, IX_EMPTY, -1 if error (bdberr set)
*/
static int bdb_berkdb_firstlast_real(bdb_berkdb_t *pberkdb, int pos,
int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
int rc = IX_OK;
*bdberr = 0;
if (bt->use_bulk) {
reset_bulk_bt(bt);
pberkdb->impl->num_nexts = 0;
}
rc = bdb_berkdb_cget(berkdb, 0 /*no bulk*/, pos, bdberr);
if (rc == IX_NOTFND) {
berkdb->at_eof = 1;
rc = IX_EMPTY;
} else
berkdb->at_eof = 0;
return rc;
}
static inline int bdb_berkdb_first_real(bdb_berkdb_t *pberkdb, int *bdberr)
{
return bdb_berkdb_firstlast_real(pberkdb, DB_FIRST, bdberr);
}
static inline int bdb_berkdb_last_real(bdb_berkdb_t *pberkdb, int *bdberr)
{
return bdb_berkdb_firstlast_real(pberkdb, DB_LAST, bdberr);
}
static int process_bulk_odh(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
struct odh odh;
unsigned long long *genptr;
bdb_state_type *bdb_state = berkdb->cur->state;
int rc;
rc = bdb_unpack(berkdb->cur->state, bt->lastdta, bt->lastdtasize,
bt->odh_tmp, bt->odh.ulen, &odh, NULL);
if (rc != 0) {
*bdberr = BDBERR_UNPACK;
return -1;
}
/* Copy to bt->data or vtag_to_ondisk will expand into the next record. */
if (odh.length < bdb_state->lrl) {
memcpy(bt->data.data, odh.recptr, odh.length);
bt->odh.data = bt->data.data;
} else {
bt->odh.data = odh.recptr;
}
bt->odh.size = odh.length;
bt->ver = odh.csc2vers;
if (ip_updates_enabled(berkdb->cur->state)) {
genptr = (unsigned long long *)bt->lastkey;
#ifdef _SUN_SOURCE
unsigned long long tmp;
memcpy(&tmp, genptr, sizeof(*genptr));
tmp = set_updateid(berkdb->cur->state, odh.updateid, tmp);
memcpy(genptr, &tmp, sizeof(*genptr));
#else
*genptr = set_updateid(berkdb->cur->state, odh.updateid, *genptr);
#endif
}
*bdberr = 0;
return 0;
}
/**
* Position cursor on the next row:
* - If cursor is not initialized, this is same as "first"
* - Move next
* - If deadlock or error, return error
* - If not found, and return IX_NOTFND
* - Return IX_FND
*
* Returns: IX_FND, IX_NOTFND, -1 if error (bdberr set)
*/
int bdb_berkdb_next_real(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
int rc = IX_OK;
int flags = DB_NEXT;
*bdberr = 0;
if (berkdb->at_eof)
return IX_NOTFND;
/* only enable bulk mode if we did some minimum number of nexts */
berkdb->num_nexts++;
if (!bt->use_bulk && bt->bulk.ulen > 0 &&
berkdb->bdb_state->attr->bulk_sql_mode &&
berkdb->num_nexts > berkdb->bdb_state->attr->bulk_sql_threshold) {
/*printf("switching %p to bulk mode after %d nexts\n", pberkdb,
* berkdb->num_nexts);*/
bt->use_bulk = 1;
bt->bulkptr = NULL;
if (!bt->bulk.data) {
bt->bulk.data = thread_malloc(bt->bulk.ulen);
}
if (!bt->bulk.data) {
logmsg(LOGMSG_ERROR, "%s: malloc %d\n", __func__, bt->bulk.ulen);
*bdberr = BDBERR_MALLOC;
return -1;
}
bt->bulk.flags = DB_DBT_USERMEM;
flags |= DB_MULTIPLE_KEY;
} else if (bt->use_bulk) {
flags |= DB_MULTIPLE_KEY;
}
/* try use bulk, if any */
if (bt->bulkptr)
DB_MULTIPLE_KEY_NEXT(bt->bulkptr, &bt->bulk, bt->lastkey,
bt->lastkeysize, bt->lastdta, bt->lastdtasize);
if (bt->bulkptr) {
if (bt->use_odh && process_bulk_odh(pberkdb, bdberr))
return -1;
return IX_FND;
}
rc = bdb_berkdb_cget(berkdb, 1 /*use bulk if any*/, flags, bdberr);
berkdb->at_eof = (rc != IX_FND);
if (rc)
return rc;
/* got some data, bulk or not */
if (bt->use_bulk) {
DB_MULTIPLE_INIT(bt->bulkptr, &bt->bulk);
DB_MULTIPLE_KEY_NEXT(bt->bulkptr, &bt->bulk, bt->lastkey,
bt->lastkeysize, bt->lastdta, bt->lastdtasize);
if (bt->use_odh) {
if (process_bulk_odh(pberkdb, bdberr))
return -1;
}
}
return IX_FND;
}
/**
* Position cursor on the prev row:
* - If cursor is not initialized, this is same as "last"
* - Move prev
* - If deadlock or error, return error
* - If not found, and return IX_NOTFND
* - Return IX_FND
*
* Returns: IX_FND, IX_NOTFND, -1 if error (bdberr set)
*/
int bdb_berkdb_prev_real(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_realdb_tag_t *bt = &berkdb->u.rl;
int rc = 0;
if (bt->use_bulk) {
logmsg(LOGMSG_ERROR, "Detected PREV after NEXT!\n");
cheap_stack_trace();
*bdberr = BDBERR_BADARGS;
return -1;
}
rc = bdb_berkdb_cget(berkdb, 0 /*no bulk */, DB_PREV, bdberr);
berkdb->at_eof = (rc != IX_FND);
return rc;
}
/* =============================================================================*/
/**
* Close a real berkdb
* - free buffers
* - close cursor
* - free it
*
*/
static int bdb_berkdb_close_shad(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_shaddb_tag_t *bt = &berkdb->u.sd;
int rc = 0;
if (bt->use_odh)
thread_free(bt->odh_tmp);
rc = bdb_temp_table_close_cursor(berkdb->cur->state, berkdb->u.sd.cur,
bdberr);
thread_free(berkdb);
thread_free(pberkdb);
return rc;
}
/* Unpack the shadow-table data. */
static int bdb_berkdb_unpack_shadows(bdb_berkdb_impl_t *berkdb, int *bdberr)
{
bdb_shaddb_tag_t *bt = &berkdb->u.sd;
char *dta;
int len;
int rc;
struct odh odh;
/* Skip past the header if it's there. */
dta = bdb_temp_table_data(bt->cur);
len = bdb_temp_table_datasize(bt->cur);
/* Unpack odh to get the version number. */
if (bt->use_odh) {
rc = bdb_unpack(berkdb->cur->state, dta, len, bt->odh_tmp, MAXRECSZ,
&odh, NULL);
if (rc != 0) {
*bdberr = BDBERR_UNPACK;
return -1;
}
bt->dta = odh.recptr;
bt->dtalen = odh.length;
bt->ver = odh.csc2vers;
} else {
bt->dta = dta;
bt->dtalen = len;
bt->ver = berkdb->cur->state->version;
}
return 0;
}
/**
* Position cursor on the first/last row:
* - Move first/last; if ok, and return IX_FND
* - Return IX_EMPTY
*
*/
int bdb_berkdb_firstlast_shad(bdb_berkdb_t *pberkdb,
int (*func)(bdb_state_type *, tmpcursor_t *,
int *),
int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_shaddb_tag_t *bt = &berkdb->u.sd;
int rc = IX_OK;
*bdberr = 0;
rc = func(berkdb->cur->state, bt->cur, bdberr);
berkdb->at_eof = (rc != IX_FND);
if (rc == IX_EMPTY) {
return IX_EMPTY;
}
rc = bdb_berkdb_handle_optimized(pberkdb, bdberr);
if (rc != IX_OK)
return rc;
rc = bdb_berkdb_unpack_shadows(berkdb, bdberr);
if (rc != IX_OK)
return rc;
return 0;
}
static int bdb_berkdb_first_shad(bdb_berkdb_t *pberkdb, int *bdberr)
{
return bdb_berkdb_firstlast_shad(pberkdb, bdb_temp_table_first, bdberr);
}
static int bdb_berkdb_last_shad(bdb_berkdb_t *pberkdb, int *bdberr)
{
return bdb_berkdb_firstlast_shad(pberkdb, bdb_temp_table_last, bdberr);
}
/**
* Position cursor on the next row:
* - If cursor is not initialized, this is same as "first"
* - Move next
* - If error, return error
* - If not found, and return IX_NOTFND
* - Return IX_FND
*
* Returns: IX_FND, IX_NOTFND, -1 if error (bdberr set)
*/
int bdb_berkdb_next_shad(bdb_berkdb_t *pberkdb, int *bdberr)
{
bdb_berkdb_impl_t *berkdb = pberkdb->impl;
bdb_shaddb_tag_t *bt = &berkdb->u.sd;
int rc = IX_OK;
*bdberr = 0;
/*
rc = bdb_temp_table_next_norewind( berkdb->cur->state, bt->cur, bdberr);
*/
rc = bdb_temp_table_next(berkdb->cur->state, bt->cur, bdberr);
if (rc == IX_PASTEOF) {
rc = IX_NOTFND;
}
berkdb->at_eof = (rc != IX_FND);
if (rc) {
return rc;
}
rc = bdb_berkdb_handle_optimized(pberkdb, bdberr);
if (rc != IX_OK)
return rc;
rc = bdb_berkdb_unpack_shadows(berkdb, bdberr);
if (rc != IX_OK)
return rc;
return 0;
}