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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1999
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char sccsid[] = "@(#)qam.c 11.23 (Sleepycat) 10/26/99";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#endif
#include "db_int.h"
#include "db_page.h"
#include "db_shash.h"
#include "db_am.h"
#include "lock.h"
#include "qam.h"
#include "mp.h"
static int CDB___qam_c_close __P((DBC *));
static int CDB___qam_c_del __P((DBC *, u_int32_t));
static int CDB___qam_c_get __P((DBC *, DBT *, DBT *, u_int32_t));
static int CDB___qam_c_put __P((DBC *, DBT *, DBT *, u_int32_t));
static int CDB___qam_getno __P((DB *, const DBT *, db_recno_t *));
static int CDB___qam_i_delete __P((DBC *));
static int CDB___qam_i_put __P((DBC *, DBT *, u_int32_t));
static int CDB___qam_nrecs __P((DBC *, db_recno_t *, db_recno_t *));
static int CDB___qam_position
__P((DBC *, db_recno_t *, db_lockmode_t, db_recno_t, int *));
static int CDB___qam_c_destroy __P((DBC *));
/*
* CDB___qam_nrecs --
* Return the record number for the head and tail of the queue.
*/
static int
CDB___qam_nrecs(dbc, rep, start)
DBC *dbc;
db_recno_t *rep, *start;
{
DB *dbp;
DB_LOCK lock;
QMETA *meta;
db_pgno_t pg;
int ret;
dbp = dbc->dbp;
pg = ((QUEUE *)dbp->q_internal)->q_meta;
if ((ret = CDB___db_lget(dbc, 0, pg, DB_LOCK_READ, 0, &lock)) != 0)
return (ret);
if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &meta)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, lock);
return (ret);
}
*rep = meta->cur_recno;
*start = meta->start;
if ((ret = CDB_memp_fput(dbp->mpf, meta, 0)) != 0)
return (ret);
/* Don't hold the meta page long term. */
if ((ret = __LPUT(dbc, lock)) != 0)
return (ret);
return (0);
}
/*
* CDB___qam_position --
* Position a queued access method cursor at a record. This returns
* the page locked. *exactp will be set if the record is valid.
*/
static int
CDB___qam_position(dbc, recnop, lock_mode, start, exactp)
DBC *dbc; /* open cursor */
db_recno_t *recnop; /* pointer to recno to find */
db_lockmode_t lock_mode;/* locking: read or write */
db_recno_t start; /* meta.start */
int *exactp; /* indicate if it was found */
{
QUEUE_CURSOR *cp;
DB *dbp;
QAMDATA *qp;
db_pgno_t pg;
int ret;
dbp = dbc->dbp;
cp = (QUEUE_CURSOR *) dbc->internal;
/* Fetch the page for this recno. */
pg = QAM_RECNO_PAGE(dbp, start, *recnop);
if ((ret = CDB___db_lget(dbc, 0, pg, lock_mode, 0, &cp->lock)) != 0)
return (ret);
if ((ret = CDB_memp_fget(dbp->mpf, &pg,
lock_mode == DB_LOCK_WRITE ? DB_MPOOL_CREATE : 0,
&cp->page)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, cp->lock);
cp->lock.off = LOCK_INVALID;
return (ret);
}
cp->pgno = pg;
cp->indx = QAM_RECNO_INDEX(dbp, pg, start, *recnop);
if (cp->page->pgno == 0) {
if (F_ISSET(dbp, DB_AM_RDONLY)) {
*exactp = 0;
return (0);
}
cp->page->pgno = pg;
cp->page->type = P_QAMDATA;
}
qp = QAM_GET_RECORD(dbp, cp->page, cp->indx);
*exactp = F_ISSET(qp, QAM_VALID);
return (ret);
}
/*
* CDB___qam_pitem --
* Put an item on a queue page. Copy the data to the page and set the
* VALID and SET bits. If logging and the record was previously set,
* log that data, otherwise just log the new data.
*
* pagep must be write locked
*
* PUBLIC: int CDB___qam_pitem
* PUBLIC: __P((DBC *, QPAGE *, u_int32_t, db_recno_t, DBT *));
*/
int
CDB___qam_pitem(dbc, pagep, indx, recno, data)
DBC *dbc;
QPAGE *pagep;
u_int32_t indx;
db_recno_t recno;
DBT *data;
{
DB *dbp;
DBT olddata, pdata, *datap;
QAMDATA *qp;
QUEUE *t;
u_int32_t size;
u_int8_t *dest, *p;
int alloced, ret;
alloced = ret = 0;
dbp = dbc->dbp;
t = (QUEUE *)dbp->q_internal;
if (data->size > t->re_len)
return (EINVAL);
qp = QAM_GET_RECORD(dbp, pagep, indx);
p = qp->data;
size = data->size;
datap = data;
if (F_ISSET(data, DB_DBT_PARTIAL)) {
if (data->size != data->dlen)
return (EINVAL);
if (data->doff + data->dlen > t->re_len)
return (EINVAL);
if (data->size == t->re_len)
goto no_partial;
/*
* If we are logging, then we have to build the record
* first, otherwise, we can simply drop the change
* directly on the page. After this clause, make
* sure that datap and p are set up correctly so that
* copying datap into p does the right thing.
*
* Note, I am changing this so that if the existing
* record is not valid, we create a complete record
* to log so that both this and the recovery code is simpler.
*/
if (DB_LOGGING(dbc) || !F_ISSET(qp, QAM_VALID)) {
datap = &pdata;
memset(datap, 0, sizeof(*datap));
if ((ret =
CDB___os_malloc(t->re_len, NULL, &datap->data)) != 0)
return (ret);
alloced = 1;
datap->size = t->re_len;
/*
* Construct the record if it's valid, otherwise set it
* all to the pad character.
*/
dest = datap->data;
if (F_ISSET(qp, QAM_VALID))
memcpy(dest, p, t->re_len);
else
memset(dest, t->re_pad, t->re_len);
dest += data->doff;
memcpy(dest, data->data, data->size);
} else {
datap = data;
p += data->doff;
}
}
no_partial:
if (DB_LOGGING(dbc)) {
olddata.size = 0;
if (F_ISSET(qp, QAM_SET)) {
olddata.data = qp->data;
olddata.size = t->re_len;
}
if ((ret = CDB___qam_add_log(dbp->dbenv, dbc->txn, &LSN(pagep),
0, dbp->log_fileid, &LSN(pagep), pagep->pgno,
indx, recno, datap, qp->flags,
olddata.size == 0 ? NULL : &olddata)) != 0)
goto err;
}
F_SET(qp, QAM_VALID | QAM_SET);
memcpy(p, datap->data, datap->size);
if (!F_ISSET(data, DB_DBT_PARTIAL))
memset(p + datap->size, t->re_pad, t->re_len - datap->size);
err: if (alloced)
CDB___os_free(datap->data, t->re_len);
return (ret);
}
/*
* CDB___qam_c_put
* Cursor put for queued access method.
* BEFORE and AFTER cannot be specified.
*/
static int
CDB___qam_c_put(dbc, key, data, flags)
DBC *dbc;
DBT *key, *data;
u_int32_t flags;
{
QUEUE_CURSOR *cp;
DB_LOCK lock;
int ret;
/* Check for invalid flags. */
if ((ret = CDB___db_cputchk(dbc->dbp, key, data, flags,
F_ISSET(dbc->dbp, DB_AM_RDONLY),
((QUEUE_CURSOR *)dbc->internal)->recno != RECNO_OOB)) != 0)
return (ret);
DEBUG_LWRITE(dbc, dbc->txn, "qam_c_put", NULL, data, flags);
cp = (QUEUE_CURSOR *)dbc->internal;
lock = cp->lock;
ret = CDB___qam_i_put(dbc, data, flags);
/* Release any previous lock, if not in a transaction. */
if (ret == 0 && lock.off != LOCK_INVALID) {
if ((ret = __TLPUT(dbc, lock)) != 0)
return (ret);
cp->lock.off = LOCK_INVALID;
}
return (0);
}
/*
* CDB___qam_i_put
* Internal cursor put for queued access method.
*/
static int
CDB___qam_i_put(dbc, data, flags)
DBC *dbc;
DBT *data;
u_int32_t flags;
{
QUEUE_CURSOR *cp;
DB *dbp;
DB_LOCK lock;
QMETA *meta;
db_pgno_t pg;
db_recno_t new_cur, new_first;
u_int32_t opcode;
int exact, ret, t_ret;
dbp = dbc->dbp;
PANIC_CHECK(dbp->dbenv);
/*
* If we are running CDB, this had better be either a write
* cursor or an immediate writer. If it's a regular writer,
* that means we have an IWRITE lock and we need to upgrade
* it to a write lock.
*/
if (F_ISSET(dbp->dbenv, DB_ENV_CDB)) {
if (!F_ISSET(dbc, DBC_WRITECURSOR | DBC_WRITER))
return (EINVAL);
if (F_ISSET(dbc, DBC_WRITECURSOR) &&
(ret = CDB_lock_get(dbp->dbenv, dbc->locker, DB_LOCK_UPGRADE,
&dbc->lock_dbt, DB_LOCK_WRITE, &dbc->mylock)) != 0)
return (ret);
}
cp = (QUEUE_CURSOR *)dbc->internal;
/* Write lock the record. */
if ((ret = CDB___db_lget(dbc,
0, cp->recno, DB_LOCK_WRITE, DB_LOCK_RECORD, &lock)) != 0)
return (ret);
if ((ret = CDB___qam_position(dbc,
&cp->recno, DB_LOCK_WRITE, cp->start, &exact)) != 0) {
/* We could not get the page, we can release the record lock. */
__LPUT(dbc, lock);
return (ret);
}
if (exact && flags == DB_NOOVERWRITE) {
ret = __TLPUT(dbc, lock);
/* Doing record locking, release the page lock */
if ((t_ret = __LPUT(dbc, cp->lock)) == 0)
cp->lock.off = LOCK_INVALID;
else
if (ret == 0)
ret = t_ret;
if ((t_ret = CDB_memp_fput(dbp->mpf, cp->page, 0)) != 0 && ret == 0)
ret = t_ret;
return (ret == 0 ? DB_KEYEXIST : ret);
}
/* Put the item on the page. */
ret = CDB___qam_pitem(dbc, (QPAGE *)cp->page, cp->indx, cp->recno, data);
/* Doing record locking, release the page lock */
if ((t_ret = __LPUT(dbc, cp->lock)) != 0 && ret == 0)
ret = t_ret;
if ((t_ret = CDB_memp_fput(dbp->mpf, cp->page, DB_MPOOL_DIRTY)) && ret == 0)
ret = t_ret;
cp->lock = lock;
cp->lock_mode = DB_LOCK_WRITE;
if (ret != 0)
return (ret);
/* We may need to reset the head or tail of the queue. */
pg = ((QUEUE *)dbp->q_internal)->q_meta;
if ((ret = CDB___db_lget(dbc, 0, pg, DB_LOCK_WRITE, 0, &lock)) != 0)
return (ret);
if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &meta)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, lock);
return (ret);
}
opcode = 0;
new_cur = new_first = 0;
if (cp->recno > meta->cur_recno) {
new_cur = cp->recno;
opcode |= QAM_SETCUR;
}
if (cp->recno < meta->first_recno || meta->first_recno < meta->start) {
new_first = cp->recno;
opcode |= QAM_SETFIRST;
}
if (opcode != 0 && DB_LOGGING(dbc)) {
ret = CDB___qam_mvptr_log(dbp->dbenv, dbc->txn, &meta->dbmeta.lsn,
0, opcode, dbp->log_fileid, meta->first_recno, new_first,
meta->cur_recno, new_cur, &meta->dbmeta.lsn);
}
if (opcode & QAM_SETCUR)
meta->cur_recno = cp->recno;
if (opcode & QAM_SETFIRST)
meta->first_recno = cp->recno;
if ((t_ret =
CDB_memp_fput(dbp->mpf, meta, opcode != 0 ? DB_MPOOL_DIRTY : 0)) != 0 &&
ret == 0)
ret = t_ret;
/* Don't hold the meta page long term. */
if ((t_ret = __LPUT(dbc, lock)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
/*
* CDB___qam_put --
* Add a record to the queue.
* If we are doing anything but appending, just call qam_c_put to do the
* work. Otherwise we fast path things here.
*
* PUBLIC: int CDB___qam_put __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
*/
int
CDB___qam_put(dbp, txn, key, data, flags)
DB *dbp;
DB_TXN *txn;
DBT *key, *data;
u_int32_t flags;
{
QUEUE_CURSOR *cp;
DBC *dbc;
DB_LOCK lock;
QMETA *meta;
QPAGE *page;
db_pgno_t pg;
db_recno_t recno, start, total;
int ret, t_ret;
PANIC_CHECK(dbp->dbenv);
/* Allocate a cursor. */
if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
return (ret);
DEBUG_LWRITE(dbc, txn, "qam_put", key, data, flags);
cp = dbc->internal;
/* Check for invalid flags. */
if ((ret = CDB___db_putchk(dbp,
key, data, flags, F_ISSET(dbp, DB_AM_RDONLY), 0)) != 0)
goto done;
/* If not appending, then just call the cursor routine */
if (flags != DB_APPEND) {
if ((ret = CDB___qam_getno(dbp, key, &cp->recno)) != 0)
goto done;
CDB___qam_nrecs(dbc, &total, &cp->start);
ret = CDB___qam_i_put(dbc, data, flags);
goto done;
}
/* Write lock the meta page. */
pg = ((QUEUE *)dbp->q_internal)->q_meta;
if ((ret = CDB___db_lget(dbc, 0, pg, DB_LOCK_WRITE, 0, &lock)) != 0)
goto done;
if ((ret = CDB_memp_fget(dbp->mpf, &pg, 0, &meta)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, lock);
goto done;
}
/* Record that we are going to allocate a record. */
if (DB_LOGGING(dbc)) {
CDB___qam_inc_log(dbp->dbenv,
txn, &meta->dbmeta.lsn,
0, dbp->log_fileid, &meta->dbmeta.lsn);
}
/* Get the next record number. */
recno = ++meta->cur_recno;
start = meta->start;
if (meta->first_recno < meta->start || meta->first_recno > recno)
meta->first_recno = recno;
/* Release the meta page. */
if ((ret = CDB_memp_fput(dbp->mpf, meta, DB_MPOOL_DIRTY)) != 0)
return (ret);
/* Lock the record and release meta page lock. */
if ((ret = CDB___db_lget(dbc,
1, recno, DB_LOCK_WRITE, DB_LOCK_RECORD, &lock)) != 0)
goto done;
cp->lock = lock;
cp->lock_mode = DB_LOCK_WRITE;
pg = QAM_RECNO_PAGE(dbp, start, recno);
/* Fetch and write lock the data page. */
if ((ret = CDB___db_lget(dbc, 0, pg, DB_LOCK_WRITE, 0, &lock)) != 0)
goto done;
if ((ret = CDB_memp_fget(dbp->mpf, &pg, DB_MPOOL_CREATE, &page)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, lock);
goto done;
}
/* See if this is a new page. */
if (page->pgno == 0) {
page->pgno = pg;
page->type = P_QAMDATA;
}
/* Put the item on the page and log it. */
if ((ret = CDB___qam_pitem(dbc, page,
QAM_RECNO_INDEX(dbp, pg, start, recno), recno, data)) != 0) {
/* The put failed. We did not change the row. */
(void)__LPUT(dbc, cp->lock);
(void)CDB_memp_fput(dbp->mpf, cp->page, DB_MPOOL_DIRTY);
return (ret);
}
/* Doing record locking, release the page lock */
if ((ret = __LPUT(dbc, lock)) != 0)
return (ret);
if ((ret = CDB_memp_fput(dbp->mpf, page, DB_MPOOL_DIRTY)) != 0)
return (ret);
/* Return the record number to the user. */
*(db_recno_t *)key->data = recno;
done:
/* Discard the cursor. */
if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
/*
* CDB___qam_i_delete --
* Internal version of recno delete, called by CDB___qam_delete and
* CDB___qam_c_del.
*/
static int
CDB___qam_i_delete(dbc)
DBC *dbc;
{
QUEUE_CURSOR *cp;
DB *dbp;
DBT data;
DB_LOCK lock;
PAGE *pagep;
QAMDATA *qp;
db_recno_t start;
int exact, ret, t_ret;
dbp = dbc->dbp;
cp = dbc->internal;
start = cp->start;
ret = 0;
/*
* If this is CDB and this isn't a write cursor, then it's an error.
* If it is a write cursor, but we don't yet hold the write lock, then
* we need to upgrade to the write lock.
*/
if (F_ISSET(dbp->dbenv, DB_ENV_CDB)) {
/* Make sure it's a valid update cursor. */
if (!F_ISSET(dbc, DBC_WRITECURSOR | DBC_WRITER))
return (EINVAL);
if (F_ISSET(dbc, DBC_WRITECURSOR) &&
(ret = CDB_lock_get(dbp->dbenv, dbc->locker,
DB_LOCK_UPGRADE, &dbc->lock_dbt, DB_LOCK_WRITE,
&dbc->mylock)) != 0)
return (EAGAIN);
}
if ((ret = CDB___db_lget(dbc,
0, cp->recno, DB_LOCK_WRITE, DB_LOCK_RECORD, &lock)) != 0)
goto err;
cp->lock_mode = DB_LOCK_WRITE;
/* Find the record ; delete only deletes exact matches. */
if ((ret = CDB___qam_position(dbc,
&cp->recno, DB_LOCK_WRITE, start, &exact)) != 0) {
cp->lock = lock;
goto err;
}
if (!exact) {
ret = DB_NOTFOUND;
goto err1;
}
pagep = cp->page;
qp = QAM_GET_RECORD(dbp, pagep, cp->indx);
if (DB_LOGGING(dbc)) {
data.size = ((QUEUE *)dbp->q_internal)->re_len;
data.data = qp->data;
if ((ret = CDB___qam_del_log(dbp->dbenv, dbc->txn,
&LSN(pagep), 0, dbp->log_fileid, &LSN(pagep),
pagep->pgno, cp->indx, cp->recno)) != 0)
goto err1;
}
F_CLR(qp, QAM_VALID);
err1:
if ((t_ret = CDB_memp_fput(
dbp->mpf, cp->page, ret == 0 ? DB_MPOOL_DIRTY : 0)) != 0)
return (ret ? ret : t_ret);
/* Doing record locking, release the page lock */
if ((t_ret = __LPUT(dbc, cp->lock)) != 0) {
cp->lock = lock;
return (ret ? ret : t_ret);
}
cp->lock = lock;
return (ret);
err: if (F_ISSET(dbp->dbenv, DB_ENV_CDB) && F_ISSET(dbc, DBC_WRITECURSOR))
(void)CDB___lock_downgrade(
dbp->dbenv, &dbc->mylock, DB_LOCK_IWRITE, 0);
return (ret);
}
/*
* CDB___qam_delete --
* Queue db->del function.
*
* PUBLIC: int CDB___qam_delete __P((DB *, DB_TXN *, DBT *, u_int32_t));
*/
int
CDB___qam_delete(dbp, txn, key, flags)
DB *dbp;
DB_TXN *txn;
DBT *key;
u_int32_t flags;
{
QUEUE_CURSOR *cp;
DBC *dbc;
db_recno_t total;
int ret, t_ret;
PANIC_CHECK(dbp->dbenv);
/* Check for invalid flags. */
if ((ret =
CDB___db_delchk(dbp, key, flags, F_ISSET(dbp, DB_AM_RDONLY))) != 0)
return (ret);
/* Acquire a cursor. */
if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
return (ret);
DEBUG_LWRITE(dbc, txn, "qam_delete", key, NULL, flags);
cp = dbc->internal;
if ((ret = CDB___qam_getno(dbp, key, &cp->recno)) != 0)
goto err;
CDB___qam_nrecs(dbc, &total, &cp->start);
/* Do the delete. */
if (cp->recno > total) {
ret = DB_NOTFOUND;
goto err;
}
ret = CDB___qam_i_delete(dbc);
/* Release the cursor. */
err: if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
ret = t_ret;
return (ret);
}
/*
* CDB___qam_c_del --
* Qam cursor->c_del function
*/
static int
CDB___qam_c_del(dbc, flags)
DBC *dbc;
u_int32_t flags;
{
QUEUE_CURSOR *cp;
DB *dbp;
db_recno_t total;
int ret;
dbp = dbc->dbp;
cp = dbc->internal;
PANIC_CHECK(dbp->dbenv);
/* Check for invalid flags. */
if ((ret = CDB___db_cdelchk(dbp, flags,
F_ISSET(dbp, DB_AM_RDONLY), cp->recno != RECNO_OOB)) != 0)
return (ret);
DEBUG_LWRITE(dbc, dbc->txn, "qam_c_del", NULL, NULL, flags);
if ((ret = CDB___qam_nrecs(dbc, &total, &cp->start)) != 0)
return (ret);
return (CDB___qam_i_delete(dbc));
}
/*
* CDB___qam_c_get --
* Queue cursor->c_get function.
*/
static int
CDB___qam_c_get(dbc_orig, key, data, flags)
DBC *dbc_orig;
DBT *key, *data;
u_int32_t flags;
{
QUEUE_CURSOR *cp, *orig;
DB *dbp;
DB_LOCK lock, pglock, metalock, save_lock;
DBC *dbc;
PAGE *pg;
QAMDATA *qp;
QMETA *meta;
db_indx_t save_indx;
db_lockmode_t lock_mode;
db_pgno_t metapno, save_page;
db_recno_t start, first, skipped, save_recno;
int exact, is_first, locked, ret, t_ret, with_delete;
int put_mode, meta_dirty;
orig = dbc_orig->internal;
dbp = dbc_orig->dbp;
PANIC_CHECK(dbp->dbenv);
with_delete = 0;
lock_mode = DB_LOCK_READ;
put_mode = 0;
t_ret = 0;
/* Check for invalid flags. */
if ((ret = CDB___db_cgetchk(dbp,
key, data, flags, orig->recno != RECNO_OOB)) != 0)
return (ret);
/* Clear OR'd in additional bits so we can check for flag equality. */
if (LF_ISSET(DB_RMW)) {
lock_mode = DB_LOCK_WRITE;
LF_CLR(DB_RMW);
}
if (flags == DB_CONSUME) {
with_delete = 1;
flags = DB_FIRST;
lock_mode = DB_LOCK_WRITE;
}
DEBUG_LREAD(dbc_orig, dbc_orig->txn, "qam_c_get",
flags == DB_SET || flags == DB_SET_RANGE ? key : NULL, NULL, flags);
/* Get a copy of the original cursor, including position. */
if ((ret = dbc_orig->c_dup(dbc_orig, &dbc, DB_POSITIONI)) != 0)
return (ret);
cp = dbc->internal;
is_first = 0;
/* get the meta page */
metapno = ((QUEUE *)dbp->q_internal)->q_meta;
if ((ret = CDB___db_lget(dbc, 0, metapno, lock_mode, 0, &metalock)) != 0)
return (ret);
locked = 1;
if ((ret = CDB_memp_fget(dbp->mpf, &metapno, 0, &meta)) != 0) {
/* We did not fetch it, we can release the lock. */
(void)__LPUT(dbc, metalock);
return (ret);
}
skipped = 0;
/* Make lint and friends happy. */
first = 0;
meta_dirty = 0;
/* Release any previous lock if not in a transaction. */
if (cp->lock.off != LOCK_INVALID) {
(void)__TLPUT(dbc, cp->lock);
cp->lock.off = LOCK_INVALID;
}
retry: /* Update the record number. */
cp->start = start = meta->start;
switch (flags) {
case DB_CURRENT:
break;
case DB_NEXT:
if (cp->recno != RECNO_OOB) {
++cp->recno;
break;
}
/* FALLTHROUGH */
case DB_FIRST:
flags = DB_NEXT;
is_first = 1;
/* get the first record number */
cp->recno = first = meta->first_recno;
/* if we will delete it, then increment */
if (with_delete && first < meta->cur_recno) {
if (DB_LOGGING(dbc))
CDB___qam_incfirst_log(dbp->dbenv, dbc->txn,
&LSN(meta), 0,
dbp->log_fileid, first);
meta->first_recno++;
meta_dirty = 1;
}
break;
case DB_PREV:
if (cp->recno != RECNO_OOB) {
if (cp->recno <= meta->first_recno) {
ret = DB_NOTFOUND;
goto err;
}
--cp->recno;
break;
}
/* FALLTHROUGH */
case DB_LAST:
cp->recno = meta->cur_recno;
if (cp->recno == 0) {
ret = DB_NOTFOUND;
goto err;
}
break;
case DB_SET:
case DB_SET_RANGE:
if ((ret = CDB___qam_getno(dbp, key, &cp->recno)) != 0)
goto err;
break;
}
if (cp->recno > meta->cur_recno || cp->recno < start) {
ret = DB_NOTFOUND;
pg = NULL;
if (skipped)
goto undo_meta;
goto err;
}
/* Don't hold the meta page long term. */
if (locked) {
if ((ret = __LPUT(dbc, metalock)) != 0)
goto err;
locked = 0;
}
/* Lock the record. */
if ((ret = CDB___db_lget(dbc, 0, cp->recno, lock_mode,
with_delete ? DB_LOCK_NOWAIT | DB_LOCK_RECORD : DB_LOCK_RECORD,
&lock)) == DB_LOCK_NOTGRANTED) {
/*
* In the DB_CONSUME case we skip the locked
* record, someone else will pick it up.
*
*/
is_first = 0;
if (skipped == 0)
skipped = cp->recno;
goto retry;
}
if (ret != 0)
goto err;
/*
* In the DB_FIRST or DB_LAST cases we must wait and then start over
* since the first/last may have moved while we slept.
* We release our locks and try again.
*/
if ((!with_delete && is_first) || flags == DB_LAST) {
if ((ret =
CDB___db_lget(dbc, 0, metapno, lock_mode, 0, &metalock)) != 0)
goto err;
if (cp->recno !=
(is_first ? meta->first_recno : meta->cur_recno)) {
__LPUT(dbc, lock);
if (is_first)
flags = DB_FIRST;
locked = 1;
goto retry;
}
/* Don't hold the meta page long term. */
if ((ret = __LPUT(dbc, metalock)) != 0)
goto err;
}
/* Position the cursor on the record. */
if ((ret =
CDB___qam_position(dbc, &cp->recno, lock_mode, start, &exact)) != 0) {
/* We cannot get the page, release the record lock. */
(void)__LPUT(dbc, lock);
goto err;
}
pg = cp->page;
pglock = cp->lock;
cp->lock = lock;
cp->lock_mode = lock_mode;
if (!exact) {
if (flags == DB_NEXT || flags == DB_PREV || flags == DB_LAST) {
/* Release locks and try again. */
(void)CDB_memp_fput(dbp->mpf, cp->page, 0);
(void)__LPUT(dbc, pglock);
(void)__LPUT(dbc, cp->lock);
cp->lock.off = LOCK_INVALID;
if (flags == DB_LAST)
flags = DB_PREV;
if (!with_delete)
is_first = 0;
goto retry;
}
/* this is for the SET and SET_RANGE cases */
ret = DB_KEYEMPTY;
goto err1;
}
/* Return the key if the user didn't give us one. */
if (flags != DB_SET &&
(ret = CDB___db_retcopy(dbp, key, &cp->recno, sizeof(cp->recno),
&dbc->rkey.data, &dbc->rkey.ulen)) != 0) {
if (with_delete)
goto undo_meta;
else
goto err1;
}
qp = QAM_GET_RECORD(dbp, pg, cp->indx);
/* Return the data item. */
if ((ret = CDB___db_retcopy(dbp, data, qp->data,
((QUEUE *)dbp->q_internal)->re_len,
&dbc->rdata.data, &dbc->rdata.ulen)) != 0) {
if (with_delete)
goto undo_meta;
else
goto err1;
}
/* Finally, if we are doing DB_CONSUME mark the record. */
if (with_delete) {
if (DB_LOGGING(dbc))
if ((ret = CDB___qam_del_log(dbp->dbenv, dbc->txn,
&LSN(pg), 0, dbp->log_fileid, &LSN(pg),
pg->pgno, cp->indx, cp->recno)) != 0)
goto undo_meta;
F_CLR(qp, QAM_VALID);
put_mode = DB_MPOOL_DIRTY;
/*
* This code is responsible for correcting metadata.
* There are 3 cases.
* 1) We moved ahead more than one record.
* 2) We did not actually delete cp->recno.
* 3) We encountered at least one locked.
* record and skipped them.
*/
if (cp->recno != first) {
if (0) {
undo_meta: is_first = 0;
}
if (locked == 0 && (t_ret = CDB___db_lget(
dbc, 0, metapno, lock_mode, 0, &metalock)) != 0)
goto err1;
if (is_first) {
/*
* Check to see if we moved past the first record,
* if so update meta so others can start past the
* deleted records.
*/
if (meta->first_recno > first) {
meta->first_recno = cp->recno;
meta_dirty = 1;
}
}
else if (skipped == 0) {
/*
* Error case: we did not actually delete the
* record, restore meta_first so that it is at
* least at or before cp->recno
*/
if (meta->first_recno > cp->recno) {
meta->first_recno = cp->recno;
meta_dirty = 1;
}
}
else if (meta->first_recno > skipped) {
/*
* We skipped some records because they were
* locked. If the meta-data page reflects a
* starting pointer after the skipped records
* we need to move it back to the first record
* that is not deleted or is sill locked.
* Release locks as we go, we are only
* reading to optimize future fetches.
*/
first = meta->first_recno;
/* Don't hold the meta page long term. */
__LPUT(dbc, metalock);
locked = 0;
/* reverify the skipped record */
save_page = cp->pgno;
save_indx = cp->indx;
save_recno = cp->recno;
save_lock = cp->lock;
do {
t_ret = CDB___db_lget(dbc, 0, skipped,
DB_LOCK_READ,
DB_LOCK_NOWAIT | DB_LOCK_RECORD,
&lock);
if (t_ret == DB_LOCK_NOTGRANTED)
break;
if (t_ret != 0)
goto err1;
if ((t_ret =
CDB___qam_position(dbc, &skipped,
DB_LOCK_READ,
start, &exact)) != 0) {
(void)__LPUT(dbc, lock);
goto err1;
}
if ((t_ret = CDB_memp_fput(dbp->mpf,
cp->page, put_mode)) != 0)
goto err1;
if ((t_ret =__LPUT(dbc, lock)) != 0)
goto err1;
if ((t_ret =
__LPUT(dbc, cp->lock)) != 0)
goto err1;
if (exact)
break;
} while (++skipped <= first);
t_ret = 0;
if ((t_ret = CDB___db_lget(
dbc, 0, metapno,
lock_mode, 0, &metalock)) != 0)
goto err1;
if (meta->first_recno > skipped) {
meta->first_recno = skipped;
meta_dirty = 1;
}
cp->pgno = save_page;
cp->indx = save_indx;
cp->recno = save_recno;
cp->lock = save_lock;
}
locked = 1;
}
}
/*
* Swap the cursors so we are left with the new position inside of
* the original DBCs structure, and close the dup'd cursor once it
* references the old position.
*
* The close can fail, but we only expect DB_LOCK_DEADLOCK failures.
* This violates our "the cursor is unchanged on error" semantics,
* but since all you can do with a DB_LOCK_DEADLOCK failure is close
* the cursor, I believe that's OK.
*/
orig = dbc_orig->internal;
dbc_orig->internal = dbc->internal;
dbc->internal = orig;
err1:
if (pg != NULL) {
if (!ret)
ret = t_ret;
t_ret = CDB_memp_fput(dbp->mpf, pg, put_mode);
if (!ret)
ret = t_ret;
/* Doing record locking, release the page lock */
t_ret = __LPUT(dbc, pglock);
}
err:
if (meta) {
if (!ret)
ret = t_ret;
/* release the meta page */
t_ret = CDB_memp_fput(
dbp->mpf, meta, meta_dirty ? DB_MPOOL_DIRTY : 0);
if (!ret)
ret = t_ret;
/* Don't hold the meta page long term. */
if (locked)
t_ret = __LPUT(dbc, metalock);
}
if (!ret)
ret = t_ret;
t_ret = dbc->c_close(dbc);
return (ret ? ret : t_ret);
}
/*
* CDB___qam_c_close --
* Close down the cursor from a single use.
*/
static int
CDB___qam_c_close(dbc)
DBC *dbc;
{
QUEUE_CURSOR *cp;
cp = dbc->internal;
/* Discard any locks not acquired inside of a transaction. */
if (cp->lock.off != LOCK_INVALID) {
(void)__TLPUT(dbc, cp->lock);
cp->lock.off = LOCK_INVALID;
}
cp->page = NULL;
cp->pgno = PGNO_INVALID;
cp->indx = 0;
cp->lock.off = LOCK_INVALID;
cp->lock_mode = DB_LOCK_NG;
cp->recno = RECNO_OOB;
cp->flags = 0;
return (0);
}
/*
* CDB___qam_c_dup --
* Duplicate a queue cursor, such that the new one holds appropriate
* locks for the position of the original.
*
* PUBLIC: int CDB___qam_c_dup __P((DBC *, DBC *));
*/
int
CDB___qam_c_dup(orig_dbc, new_dbc)
DBC *orig_dbc, *new_dbc;
{
QUEUE_CURSOR *orig, *new;
orig = orig_dbc->internal;
new = new_dbc->internal;
new->pgno = orig->pgno;
new->indx = orig->indx;
new->recno = orig->recno;
new->start = orig->start;
new->lock_mode = orig->lock_mode;
/* reget the long term lock if we are not in a xact */
if (orig_dbc->txn != NULL || orig->lock.off == LOCK_INVALID)
return (0);
return (CDB___db_lget(new_dbc,
0, new->recno, new->lock_mode, DB_LOCK_RECORD, &new->lock));
}
/*
* CDB___qam_c_init
*
* PUBLIC: int CDB___qam_c_init __P((DBC *));
*/
int
CDB___qam_c_init(dbc)
DBC *dbc;
{
QUEUE_CURSOR *cp;
DB *dbp;
int ret;
dbp = dbc->dbp;
/* Allocate the internal structure. */
if ((ret = CDB___os_calloc(1, sizeof(QUEUE_CURSOR), &cp)) != 0)
return (ret);
/*
* Logical record numbers are always the same size, and we don't want
* to have to check for space every time we return one. Allocate it
* in advance.
*/
if ((ret =
CDB___os_malloc(sizeof(db_recno_t), NULL, &dbc->rkey.data)) != 0) {
CDB___os_free(cp, sizeof(QUEUE_CURSOR));
return (ret);
}
dbc->rkey.ulen = sizeof(db_recno_t);
/* Initialize methods. */
dbc->internal = cp;
dbc->c_del = CDB___qam_c_del;
dbc->c_get = CDB___qam_c_get;
dbc->c_put = CDB___qam_c_put;
dbc->c_am_close = CDB___qam_c_close;
dbc->c_am_destroy = CDB___qam_c_destroy;
return (0);
}
/*
* CDB___qam_c_destroy --
* Close a single cursor -- internal version.
*/
static int
CDB___qam_c_destroy(dbc)
DBC *dbc;
{
/* Discard the structures. */
CDB___os_free(dbc->internal, sizeof(QUEUE_CURSOR));
return (0);
}
/*
* CDB___qam_getno --
* Check the user's record number.
*/
static int
CDB___qam_getno(dbp, key, rep)
DB *dbp;
const DBT *key;
db_recno_t *rep;
{
if ((*rep = *(db_recno_t *)key->data) == 0) {
CDB___db_err(dbp->dbenv, "illegal record number of 0");
return (EINVAL);
}
return (0);
}
|