/
bt_cursor.c
2374 lines (2095 loc) · 81.3 KB
/
bt_cursor.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 (c) 2014-present MongoDB, Inc.
* Copyright (c) 2008-2014 WiredTiger, Inc.
* All rights reserved.
*
* See the file LICENSE for redistribution information.
*/
#include "wt_internal.h"
/*
* When returning an error, we need to restore the cursor to a valid state, the upper-level cursor
* code is likely to retry. This structure and the associated functions are used save and restore
* the cursor state.
*/
typedef struct {
WT_ITEM key;
WT_ITEM value;
uint64_t recno;
uint64_t flags;
} WT_CURFILE_STATE;
/*
* __btcur_bounds_contains_key --
* Determine if a given key is within the bounds set on a cursor.
*/
static int
__btcur_bounds_contains_key(WT_SESSION_IMPL *session, WT_CURSOR *cursor, WT_ITEM *key,
uint64_t recno, bool *key_out_of_boundsp, bool *upperp)
{
*key_out_of_boundsp = false;
if (!WT_CURSOR_BOUNDS_SET(cursor))
return (0);
if (upperp != NULL)
*upperp = false;
WT_ASSERT_ALWAYS(session, key != NULL || recno != WT_RECNO_OOB,
"A valid key or recno must be provided when comparing bounds.");
if (F_ISSET(cursor, WT_CURSTD_BOUND_LOWER))
WT_RET(__wt_compare_bounds(session, cursor, key, recno, false, key_out_of_boundsp));
if (!(*key_out_of_boundsp) && F_ISSET(cursor, WT_CURSTD_BOUND_UPPER)) {
WT_RET(__wt_compare_bounds(session, cursor, key, recno, true, key_out_of_boundsp));
if (*key_out_of_boundsp && upperp != NULL)
*upperp = true;
}
return (0);
}
/*
* __btcur_bounds_search_near_reposition --
* This function validates whether a given key is within the provided cursor bounds. If not the
* search near key is updated to the nearest bound.
*/
static int
__btcur_bounds_search_near_reposition(WT_SESSION_IMPL *session, WT_CURSOR_BTREE *cbt)
{
WT_CURSOR *cursor;
bool key_out_of_bounds, upper;
cursor = &cbt->iface;
key_out_of_bounds = upper = false;
if (!WT_CURSOR_BOUNDS_SET(cursor))
return (0);
/*
* Suppose a caller calls with the search key set to the lower bound but also specifies that the
* lower bound isn't inclusive. We cannot know which key to set the lower bound to so we would
* set it to the lower bound. The same is true for the upper bound. We can optimize for this
* scenario by either disabling and re-enabling the flags or passing a boolean to the key within
* bounds calls. However for the time being we will leave it as is as it is unlikely to present
* a performance issue.
*/
WT_RET(__btcur_bounds_contains_key(
session, cursor, &cursor->key, cursor->recno, &key_out_of_bounds, &upper));
if (key_out_of_bounds) {
__wt_cursor_set_raw_key(cursor, upper ? &cursor->upper_bound : &cursor->lower_bound);
WT_STAT_CONN_DATA_INCR(session, cursor_bounds_search_near_repositioned_cursor);
}
return (0);
}
/*
* __cursor_state_save --
* Save the cursor's external state.
*/
static inline void
__cursor_state_save(WT_CURSOR *cursor, WT_CURFILE_STATE *state)
{
WT_ITEM_SET(state->key, cursor->key);
WT_ITEM_SET(state->value, cursor->value);
state->recno = cursor->recno;
state->flags = cursor->flags;
}
/*
* __cursor_state_restore --
* Restore the cursor's external state.
*/
static inline void
__cursor_state_restore(WT_CURSOR *cursor, WT_CURFILE_STATE *state)
{
if (F_ISSET(state, WT_CURSTD_KEY_EXT))
WT_ITEM_SET(cursor->key, state->key);
if (F_ISSET(state, WT_CURSTD_VALUE_EXT))
WT_ITEM_SET(cursor->value, state->value);
cursor->recno = state->recno;
F_CLR(cursor, WT_CURSTD_KEY_INT | WT_CURSTD_VALUE_INT);
F_SET(cursor, F_MASK(state, WT_CURSTD_KEY_EXT | WT_CURSTD_VALUE_EXT));
}
/*
* __cursor_page_pinned --
* Return if we have a page pinned.
*/
static inline bool
__cursor_page_pinned(WT_CURSOR_BTREE *cbt, bool search_operation)
{
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
cursor = &cbt->iface;
session = CUR2S(cbt);
/*
* Check the page active flag, asserting the page reference with any external key.
*/
if (!F_ISSET(cbt, WT_CBT_ACTIVE)) {
WT_ASSERT(session, cbt->ref == NULL && !F_ISSET(cursor, WT_CURSTD_KEY_INT));
return (false);
}
/*
* Check if the key references an item on a page. When returning from search, the page is pinned
* and the key is internal. After the application sets a key, the key becomes external. For the
* search and search-near operations, we assume locality and check any pinned page first on each
* new search operation. For operations other than search and search-near, check if we have an
* internal key. If the page is pinned and we're pointing into the page, we don't need to search
* at all, we can proceed with the operation. However, if the key has been set, that is, it's an
* external key, we're going to have to do a full search.
*/
if (!search_operation && !F_ISSET(cursor, WT_CURSTD_KEY_INT))
return (false);
/*
* XXX No fast-path searches at read-committed isolation. Underlying transactional functions
* called by the fast and slow path search code handle transaction IDs differently, resulting in
* different search results at read-committed isolation. This makes no difference for the update
* functions, but in the case of a search, we will see different results based on the cursor's
* initial location. See WT-5134 for the details.
*/
if (search_operation && session->txn->isolation == WT_ISO_READ_COMMITTED)
return (false);
/*
* Fail if the page is flagged for forced eviction (so we periodically release pages grown too
* large).
*/
if (cbt->ref->page->read_gen == WT_READGEN_OLDEST)
return (false);
return (true);
}
/*
* __cursor_size_chk --
* Return if an inserted item is too large.
*/
static inline int
__cursor_size_chk(WT_SESSION_IMPL *session, WT_ITEM *kv)
{
WT_BM *bm;
WT_BTREE *btree;
WT_DECL_RET;
size_t size;
btree = S2BT(session);
bm = btree->bm;
if (btree->type == BTREE_COL_FIX) {
/* Fixed-size column-stores take a single byte. */
if (kv->size != 1)
WT_RET_MSG(session, EINVAL,
"item size of %" WT_SIZET_FMT
" does not match fixed-length file requirement of 1 byte",
kv->size);
return (0);
}
/* Don't waste effort, 1GB is always cool. */
if (kv->size <= WT_GIGABYTE)
return (0);
/* Check what we are willing to store in the tree. */
if (kv->size > WT_BTREE_MAX_OBJECT_SIZE)
WT_RET_MSG(session, EINVAL,
"item size of %" WT_SIZET_FMT
" exceeds the maximum supported WiredTiger size of %" PRIu32,
kv->size, WT_BTREE_MAX_OBJECT_SIZE);
/* Check what the block manager can actually write. */
size = kv->size;
if ((ret = bm->write_size(bm, session, &size)) != 0)
WT_RET_MSG(
session, ret, "item size of %" WT_SIZET_FMT " refused by block manager", kv->size);
return (0);
}
/*
* __cursor_fix_implicit --
* Return if search went past the end of the tree.
*/
static inline bool
__cursor_fix_implicit(WT_BTREE *btree, WT_CURSOR_BTREE *cbt)
{
/*
* When there's no exact match, column-store search returns the key nearest the searched-for key
* (continuing past keys smaller than the searched-for key to return the next-largest key).
* Therefore, if the returned comparison is -1, the searched-for key was larger than any row on
* the page's standard information or column-store insert list.
*
* If the returned comparison is NOT -1, there's a row equal to or larger than the searched-for
* key, and we implicitly create missing rows. The "equal to" is important, this function does
* not do anything special for exact matches, and where behavior for exact matches differs from
* behavior for implicitly created records, our caller is responsible to handling it.
*/
return (btree->type == BTREE_COL_FIX && cbt->compare != -1);
}
/*
* __cursor_valid_insert --
* Check the insert list for a valid update.
*/
static int
__cursor_valid_insert(WT_CURSOR_BTREE *cbt, WT_ITEM *key, bool *valid, bool check_bounds)
{
WT_ITEM tmp_key;
WT_SESSION_IMPL *session;
bool key_out_of_bounds;
key_out_of_bounds = false;
session = CUR2S(cbt);
if (cbt->ins == NULL)
return (0);
if (check_bounds && WT_CURSOR_BOUNDS_SET(&cbt->iface)) {
/* Get the insert list key. */
if (key == NULL && CUR2BT(cbt)->type == BTREE_ROW) {
tmp_key.data = WT_INSERT_KEY(cbt->ins);
tmp_key.size = WT_INSERT_KEY_SIZE(cbt->ins);
WT_RET(__btcur_bounds_contains_key(
session, &cbt->iface, &tmp_key, WT_RECNO_OOB, &key_out_of_bounds, NULL));
} else
WT_RET(__btcur_bounds_contains_key(
session, &cbt->iface, key, cbt->recno, &key_out_of_bounds, NULL));
/* The key we found is out of bounds. */
if (key_out_of_bounds)
return (0);
}
WT_RET(__wt_txn_read_upd_list(session, cbt, cbt->ins->upd));
*valid =
cbt->upd_value->type != WT_UPDATE_INVALID && cbt->upd_value->type != WT_UPDATE_TOMBSTONE;
return (0);
}
/*
* __cursor_valid_ondisk --
* Check the on-disk value or history store for a valid update.
*/
static int
__cursor_valid_ondisk(WT_CURSOR_BTREE *cbt, WT_ITEM *key, WT_UPDATE *upd, bool *valid)
{
WT_SESSION_IMPL *session;
session = CUR2S(cbt);
/*
* Check for an on-disk value or in the history store, passing in any update.
*
* Potentially checking an update chain twice (both here, and above if the insert list is set in
* the case of column-store), isn't a mistake. In a modify chain, if rollback-to-stable recovers
* a history store record into the update list, the base update may be in the update list and we
* must use it rather than falling back to the on-disk value as the base update.
*/
WT_RET(__wt_txn_read(session, cbt, key, cbt->recno, upd));
*valid =
cbt->upd_value->type != WT_UPDATE_INVALID && cbt->upd_value->type != WT_UPDATE_TOMBSTONE;
return (0);
}
/*
* __cursor_valid_row --
* Determine if a valid update is being referenced by the cursor for row-store.
*/
static int
__cursor_valid_row(WT_CURSOR_BTREE *cbt, bool *valid, bool check_bounds)
{
WT_DECL_ITEM(key);
WT_PAGE *page;
WT_SESSION_IMPL *session;
WT_UPDATE *upd;
bool key_out_of_bounds;
*valid = false;
session = CUR2S(cbt);
upd = NULL;
page = cbt->ref->page;
key_out_of_bounds = false;
/* For all row-store cases we can extract the key from cbt->tmp if the compare value is zero. */
if (cbt->compare == 0 && cbt->tmp != NULL)
key = cbt->tmp;
/*
* In the case of row-store, an insert object implies ignoring any page objects, no insert
* object can have the same key as an on-page object.
*
* For row-store:
* if there's an insert object:
* if there's a visible update:
* exact match
* else
* no exact match
* else
* use the on-page object (which may have an associated update object that may or may
* not be visible to us). If there are no entries, exit.
*/
WT_RET(__cursor_valid_insert(cbt, key, valid, check_bounds));
if (*valid || cbt->ins != NULL || page->entries == 0)
return (0);
/*
* In case of prepare conflict, the slot might not have a valid value, if the update in the
* insert list of a new page scanned is in prepared state.
*/
WT_ASSERT(session, cbt->slot == UINT32_MAX || cbt->slot < page->entries);
/*
* The key can be NULL only when we didn't find an exact match, copy the search found key into
* the temporary buffer for further use.
*/
if (key == NULL) {
WT_RET(__wt_row_leaf_key(session, page, &page->pg_row[cbt->slot], cbt->tmp, true));
key = cbt->tmp;
}
if (check_bounds) {
WT_RET(__btcur_bounds_contains_key(
session, &cbt->iface, key, WT_RECNO_OOB, &key_out_of_bounds, NULL));
/* The key we found is out of bounds. */
if (key_out_of_bounds)
return (0);
}
/* Check for an update. */
upd = (page->modify != NULL && page->modify->mod_row_update != NULL) ?
page->modify->mod_row_update[cbt->slot] :
NULL;
return (__cursor_valid_ondisk(cbt, key, upd, valid));
}
/*
* __cursor_valid_col --
* Determine if a valid update is being referenced by the cursor for col-store.
*/
static int
__cursor_valid_col(WT_CURSOR_BTREE *cbt, bool *valid, bool check_bounds)
{
WT_BTREE *btree;
WT_CELL *cell;
WT_COL *cip;
WT_PAGE *page;
WT_SESSION_IMPL *session;
WT_UPDATE *upd;
bool key_out_of_bounds;
*valid = false;
key_out_of_bounds = false;
btree = CUR2BT(cbt);
session = CUR2S(cbt);
page = cbt->ref->page;
upd = NULL;
/*
* Column-store is more complicated than row-store because an insert object can have the same
* key as an on-page object: updates to column-store rows are insert/object pairs, and an
* invisible update isn't the end as there may be an on-page object that is visible.
*
* For column-store:
* if there's an insert object:
* if there's a visible update:
* exact match
* else if the on-page object's key matches the insert key
* use the on-page object
* else
* use the on-page object
*/
WT_RET(__cursor_valid_insert(cbt, NULL, valid, check_bounds));
if (*valid)
return (0);
/*
* In the case of column-store, there's an insert object but no update was visible to us and the
* key on the page is the same as the insert object's key, and the slot as set by the search
* function is valid, we can use the original page information.
*/
if (btree->type == BTREE_COL_FIX) {
/*
* If search returned an insert, we might be past the end of page in the append list, so
* there's no on-disk value.
*/
if (cbt->recno >= cbt->ref->ref_recno + page->entries)
return (0);
/*
* Check for an update. For column store, modifications are handled with insert lists, so an
* insert can have the same key as an on-page or history store object.
*
* Note: we do not want to replace tombstones with zero here; it skips cases in other code
* below that expect to handle it themselves, and then doesn't work.
*/
upd = cbt->ins ? cbt->ins->upd : NULL;
} else {
/* The search function doesn't check for empty pages. */
if (page->entries == 0)
return (0);
/*
* In case of prepare conflict, the slot might not have a valid value, if the update in the
* insert list of a new page scanned is in prepared state.
*/
WT_ASSERT(session, cbt->slot == UINT32_MAX || cbt->slot < page->entries);
/*
* Column-store updates are stored as "insert" objects. If search returned an insert object
* we can't return, the returned on-page object must be checked for a match. The flag tells
* us whether the insert was actually an append to allow skipping the on-disk check. Note
* that appends can't have history store content. This is true both for "real" appends at
* the end of the tree and also for appends that are filling in truncated gaps in the middle
* of the tree -- the gap only appears after the truncation becomes globally visible and at
* that point by definition nothing older can be accessible.
*/
if (cbt->ins != NULL && !F_ISSET(cbt, WT_CBT_VAR_ONPAGE_MATCH))
return (0);
/*
* Although updates would have appeared as an "insert" objects, variable-length column store
* deletes are written into the backing store; check the cell for a record already deleted
* when read.
*/
cip = &page->pg_var[cbt->slot];
cell = WT_COL_PTR(page, cip);
if (__wt_cell_type(cell) == WT_CELL_DEL)
return (0);
if (check_bounds) {
WT_RET(__btcur_bounds_contains_key(
session, &cbt->iface, NULL, cbt->recno, &key_out_of_bounds, NULL));
/* The key value pair we were trying to return weren't within the given bounds. */
if (key_out_of_bounds)
return (0);
}
/*
* Check for an update. For column store, modifications are handled with insert lists, so an
* insert can have the same key as an on-page or history store object. Setting update here,
* even after checking the insert list above, is correct, see the comment below for details.
*/
upd = cbt->ins ? cbt->ins->upd : NULL;
}
return (__cursor_valid_ondisk(cbt, NULL, upd, valid));
}
/*
* __wt_cursor_valid --
* Return if the cursor references a valid key/value pair.
*/
int
__wt_cursor_valid(WT_CURSOR_BTREE *cbt, bool *valid, bool check_bounds)
{
*valid = false;
/* If the cursor is operating in key only mode we don't need to do any more work. */
if (F_ISSET(&cbt->iface, WT_CURSTD_KEY_ONLY)) {
*valid = true;
return (0);
}
/*
* We may be pointing to an insert object, and we may have a page with existing entries. Insert
* objects always have associated update objects (the value). Any update object may be deleted,
* or invisible to us. In the case of an on-page entry, it may also be invisible to us.
*
* Unfortunately, the objects we might have and their relationships are different for the
* underlying page types.
*/
switch (CUR2BT(cbt)->type) {
case BTREE_COL_VAR:
case BTREE_COL_FIX:
WT_RET(__cursor_valid_col(cbt, valid, check_bounds));
break;
case BTREE_ROW:
WT_RET(__cursor_valid_row(cbt, valid, check_bounds));
break;
}
return (0);
}
/*
* __cursor_col_search --
* Column-store search from a cursor.
*/
static inline int
__cursor_col_search(WT_CURSOR_BTREE *cbt, WT_REF *leaf, bool *leaf_foundp)
{
WT_DECL_RET;
WT_SESSION_IMPL *session;
session = CUR2S(cbt);
#ifdef HAVE_DIAGNOSTIC
/*
* Turn off cursor-order checks in all cases on search. The search/search-near functions turn
* them back on after a successful search.
*/
__wt_cursor_key_order_reset(cbt);
#endif
WT_WITH_PAGE_INDEX(
session, ret = __wt_col_search(cbt, cbt->iface.recno, leaf, false, leaf_foundp));
return (ret);
}
/*
* __cursor_row_search --
* Row-store search from a cursor.
*/
static inline int
__cursor_row_search(WT_CURSOR_BTREE *cbt, bool insert, WT_REF *leaf, bool *leaf_foundp)
{
WT_DECL_RET;
WT_SESSION_IMPL *session;
session = CUR2S(cbt);
#ifdef HAVE_DIAGNOSTIC
/*
* Turn off cursor-order checks in all cases on search. The search/search-near functions turn
* them back on after a successful search.
*/
__wt_cursor_key_order_reset(cbt);
#endif
WT_WITH_PAGE_INDEX(
session, ret = __wt_row_search(cbt, &cbt->iface.key, insert, leaf, false, leaf_foundp));
return (ret);
}
/*
* __cursor_col_modify --
* Column-store modify from a cursor.
*/
static inline int
__cursor_col_modify(WT_CURSOR_BTREE *cbt, const WT_ITEM *value, u_int modify_type)
{
return (__wt_col_modify(cbt, cbt->iface.recno, value, NULL, modify_type, false, false));
}
/*
* __cursor_row_modify --
* Row-store modify from a cursor.
*/
static inline int
__cursor_row_modify(WT_CURSOR_BTREE *cbt, const WT_ITEM *value, u_int modify_type)
{
return (__wt_row_modify(cbt, &cbt->iface.key, value, NULL, modify_type, false, false));
}
/*
* __cursor_restart --
* Common cursor restart handling.
*/
static void
__cursor_restart(WT_SESSION_IMPL *session, uint64_t *yield_count, uint64_t *sleep_usecs)
{
__wt_spin_backoff(yield_count, sleep_usecs);
WT_STAT_CONN_DATA_INCR(session, cursor_restart);
}
/*
* __wt_btcur_reset --
* Invalidate the cursor position.
*/
int
__wt_btcur_reset(WT_CURSOR_BTREE *cbt)
{
WT_CURSOR *cursor;
WT_SESSION_IMPL *session;
cursor = &cbt->iface;
session = CUR2S(cbt);
WT_STAT_CONN_DATA_INCR(session, cursor_reset);
F_CLR(cursor, WT_CURSTD_KEY_SET | WT_CURSTD_VALUE_SET);
/* Initialize the update value as we are not pointing to any value. */
cbt->upd_value->type = WT_UPDATE_INVALID;
WT_TIME_WINDOW_INIT(&cbt->upd_value->tw);
return (__cursor_reset(cbt));
}
/*
* __wt_btcur_search_prepared --
* Search and return exact matching records only.
*/
int
__wt_btcur_search_prepared(WT_CURSOR *cursor, WT_UPDATE **updp)
{
WT_BTREE *btree;
WT_CURSOR_BTREE *cbt;
WT_UPDATE *upd;
*updp = NULL;
cbt = (WT_CURSOR_BTREE *)cursor;
btree = CUR2BT(cbt);
upd = NULL; /* -Wuninitialized */
/*
* Not calling the cursor initialization functions, we don't want to be tapped for eviction nor
* do we want other standard cursor semantics like snapshots, just discard the hazard pointer
* from the last operation. This also depends on the fact we're not setting the cursor's active
* flag, this is really a special chunk of code and not to be modified without careful thought.
*/
WT_RET(__cursor_reset(cbt));
WT_RET(btree->type == BTREE_ROW ? __cursor_row_search(cbt, false, NULL, NULL) :
__cursor_col_search(cbt, NULL, NULL));
/*
* Ideally an exact match will be found, as this transaction is searching for updates done by
* itself. But, we cannot be sure of finding one, as pre-processing of the updates could have
* happened as part of resolving earlier transaction operations.
*/
if (cbt->compare != 0)
return (0);
/* Get any uncommitted update from the in-memory page. */
switch (btree->type) {
case BTREE_ROW:
/*
* Any update must be either in the insert list, in which case search will have returned a
* pointer for us, or as an update in a particular key's update list, in which case the slot
* will be returned to us. In either case, we want the most recent update (any update
* attempted after the prepare would have failed).
*/
if (cbt->ins != NULL)
upd = cbt->ins->upd;
else if (cbt->ref->page->modify != NULL && cbt->ref->page->modify->mod_row_update != NULL)
upd = cbt->ref->page->modify->mod_row_update[cbt->slot];
break;
case BTREE_COL_FIX:
case BTREE_COL_VAR:
/*
* Any update must be in the insert list and we want the most recent update (any update
* attempted after the prepare would have failed).
*/
if (cbt->ins != NULL)
upd = cbt->ins->upd;
break;
}
*updp = upd;
return (0);
}
/*
* __cursor_reposition_timing_stress --
* Optionally reposition the cursor 10% of times
*/
static inline bool
__cursor_reposition_timing_stress(WT_SESSION_IMPL *session)
{
WT_CONNECTION_IMPL *conn;
conn = S2C(session);
if (FLD_ISSET(conn->timing_stress_flags, WT_TIMING_STRESS_EVICT_REPOSITION) &&
__wt_random(&session->rnd) % 10 == 0)
return (true);
return (false);
}
/*
* __wt_btcur_evict_reposition --
* Try to evict the page and reposition the cursor on the saved key.
*/
int
__wt_btcur_evict_reposition(WT_CURSOR_BTREE *cbt)
{
WT_CURSOR *cursor;
WT_DECL_RET;
WT_SESSION_IMPL *session;
cursor = &cbt->iface;
session = CUR2S(cbt);
/* It's not always OK to release the cursor position */
if (!F_ISSET(cursor, WT_CURSTD_EVICT_REPOSITION))
return (0);
/*
* Try to evict the page and then reposition the cursor back to the page for operations with
* snapshot isolation and for pages that require urgent eviction. Snapshot isolation level
* maintains a snapshot allowing the cursor to point at the correct value after a reposition
* unlike read committed isolation level.
*/
if (session->txn->isolation == WT_ISO_SNAPSHOT && F_ISSET(session->txn, WT_TXN_RUNNING) &&
(__wt_page_evict_soon_check(session, cbt->ref, NULL) ||
__cursor_reposition_timing_stress(session))) {
WT_STAT_CONN_DATA_INCR(session, cursor_reposition);
WT_ERR(__wt_cursor_localkey(cursor));
WT_ERR(__cursor_reset(cbt));
WT_WITHOUT_EVICT_REPOSITION(ret = __wt_btcur_search(cbt));
WT_ERR(ret);
}
err:
if (ret != 0) {
WT_STAT_CONN_DATA_INCR(session, cursor_reposition_failed);
/*
* If we got a WT_ROLLBACK it is because there is a lot of cache pressure and the
* transaction is being killed - don't panic in that case.
*/
if (ret != WT_ROLLBACK)
WT_RET_PANIC(session, ret, "failed to reposition the cursor");
}
return (ret);
}
/*
* __wt_btcur_search --
* Search for a matching record in the tree.
*/
int
__wt_btcur_search(WT_CURSOR_BTREE *cbt)
{
WT_BTREE *btree;
WT_CURFILE_STATE state;
WT_CURSOR *cursor;
WT_DECL_RET;
WT_SESSION_IMPL *session;
bool key_out_of_bounds, leaf_found, valid;
btree = CUR2BT(cbt);
cursor = &cbt->iface;
key_out_of_bounds = false;
session = CUR2S(cbt);
valid = false;
WT_STAT_CONN_DATA_INCR(session, cursor_search);
WT_RET(__wt_txn_search_check(session));
__cursor_state_save(cursor, &state);
/*
* The pinned page goes away if we search the tree, get a local copy of any pinned key and
* discard any pinned value, then re-save the cursor state. Done before searching pinned pages
* (unlike other cursor functions), because we don't anticipate applications searching for a key
* they currently have pinned.)
*/
WT_ERR(__wt_cursor_localkey(cursor));
__cursor_novalue(cursor);
__cursor_state_save(cursor, &state);
/*
* Check that the provided search key is within bounds. If not, return WT_NOTFOUND and early
* exit.
*/
WT_ERR(__btcur_bounds_contains_key(
session, cursor, &cursor->key, cursor->recno, &key_out_of_bounds, NULL));
if (key_out_of_bounds) {
WT_STAT_CONN_DATA_INCR(session, cursor_bounds_search_early_exit);
WT_ERR(WT_NOTFOUND);
}
/*
* If we have a page pinned, search it; if we don't have a page pinned, or the search of the
* pinned page doesn't find an exact match, search from the root.
*/
if (__cursor_page_pinned(cbt, true)) {
__wt_txn_cursor_op(session);
if (btree->type == BTREE_ROW)
WT_ERR(__cursor_row_search(cbt, false, cbt->ref, &leaf_found));
else
WT_ERR(__cursor_col_search(cbt, cbt->ref, &leaf_found));
if (leaf_found && cbt->compare == 0)
WT_ERR(__wt_cursor_valid(cbt, &valid, false));
}
if (!valid) {
WT_ERR(__wt_cursor_func_init(cbt, true));
if (btree->type == BTREE_ROW)
WT_ERR(__cursor_row_search(cbt, false, NULL, NULL));
else
WT_ERR(__cursor_col_search(cbt, NULL, NULL));
if (cbt->compare == 0)
WT_ERR(__wt_cursor_valid(cbt, &valid, false));
}
if (valid)
ret = F_ISSET(cursor, WT_CURSTD_KEY_ONLY) ? __wt_key_return(cbt) :
__cursor_kv_return(cbt, cbt->upd_value);
else if (__cursor_fix_implicit(btree, cbt)) {
/*
* Creating a record past the end of the tree in a fixed-length column-store implicitly
* fills the gap with empty records.
*/
cbt->recno = cursor->recno;
cbt->v = 0;
cursor->value.data = &cbt->v;
cursor->value.size = 1;
F_CLR(cursor, WT_CURSTD_KEY_SET | WT_CURSTD_VALUE_SET);
F_SET(cursor, WT_CURSTD_KEY_INT | WT_CURSTD_VALUE_INT);
} else
ret = WT_NOTFOUND;
/*
* The format test program does repeatable reads testing, and wants to dump the cursor page on
* failure. It sets up a callback for that purpose, and we pay a cache miss per search to make
* that work.
*/
if (session->format_private != NULL)
session->format_private(cursor, ret, session->format_private_arg);
#ifdef HAVE_DIAGNOSTIC
if (ret == 0)
WT_ERR(__wt_cursor_key_order_init(cbt));
#endif
if (ret == 0)
WT_ERR(__wt_btcur_evict_reposition(cbt));
err:
if (ret != 0) {
WT_TRET(__cursor_reset(cbt));
__cursor_state_restore(cursor, &state);
}
return (ret);
}
/*
* __btcur_search_neighboring --
* Search for a valid record around the cursor location.
*
*/
static int
__btcur_search_neighboring(WT_CURSOR_BTREE *cbt, WT_CURFILE_STATE *state, int *exact)
{
WT_BTREE *btree;
WT_CURSOR *cursor;
WT_DECL_RET;
WT_SESSION_IMPL *session;
btree = CUR2BT(cbt);
cursor = &cbt->iface;
session = CUR2S(cbt);
/*
* We didn't find an exact match: try after the search key, then before. We have to loop here
* because at low isolation levels, new records could appear as we are stepping through the
* tree.
*/
while ((ret = __wt_btcur_next(cbt, false)) != WT_NOTFOUND) {
WT_RET(ret);
if (btree->type == BTREE_ROW)
WT_RET(__wt_compare(session, btree->collator, &cursor->key, &state->key, exact));
else
*exact = cbt->recno < state->recno ? -1 : cbt->recno == state->recno ? 0 : 1;
if (*exact >= 0)
return (ret);
}
/*
* We walked to the end of the tree without finding a match. Walk backwards instead.
*/
while ((ret = __wt_btcur_prev(cbt, false)) != WT_NOTFOUND) {
WT_RET(ret);
if (btree->type == BTREE_ROW)
WT_RET(__wt_compare(session, btree->collator, &cursor->key, &state->key, exact));
else
*exact = cbt->recno < state->recno ? -1 : cbt->recno == state->recno ? 0 : 1;
if (*exact <= 0)
return (ret);
}
return (ret);
}
/*
* __btcur_search_near_row_pinned_page --
* Search a row store pinned page before searching from the root in search near.
*/
static inline int
__btcur_search_near_row_pinned_page(WT_CURSOR_BTREE *cbt, bool *validp)
{
WT_SESSION_IMPL *session;
bool leaf_found;
leaf_found = false;
session = CUR2S(cbt);
/* We only do this search for row-store. */
if (CUR2BT(cbt)->type != BTREE_ROW || !__cursor_page_pinned(cbt, true))
return (0);
/* If we have a row-store page pinned, search it. */
__wt_txn_cursor_op(session);
/*
* Set the "insert" flag for row-store search; we may intend to position the cursor at the the
* end of the tree, rather than match an existing record. (LSM requires this semantic.)
*/
WT_RET(__cursor_row_search(cbt, true, cbt->ref, &leaf_found));
/*
* Only use the pinned page search results if search returns an exact match or a slot other than
* the page's boundary slots, if that's not the case, a neighbor page might offer a better
* match. This test is simplistic as we're ignoring append lists (there may be no page slots or
* we might be legitimately positioned after the last page slot). Ignore those cases, it makes
* things too complicated.
*
* If there's an exact match, the row-store search function built the key in the cursor's
* temporary buffer.
*/
if (leaf_found &&
(cbt->compare == 0 || (cbt->slot != 0 && cbt->slot != cbt->ref->page->entries - 1)))
WT_RET(__wt_cursor_valid(cbt, validp, true));
return (0);
}
/*
* __wt_btcur_search_near --
* Search for a record in the tree.
*/
int
__wt_btcur_search_near(WT_CURSOR_BTREE *cbt, int *exactp)
{
WT_BTREE *btree;
WT_CURFILE_STATE state;
WT_CURSOR *cursor;
WT_DECL_RET;
WT_SESSION_IMPL *session;
int exact;
bool valid;
btree = CUR2BT(cbt);
cursor = &cbt->iface;
session = CUR2S(cbt);
exact = 0;
valid = false;
WT_STAT_CONN_DATA_INCR(session, cursor_search_near);
WT_RET(__wt_txn_search_check(session));
__cursor_state_save(cursor, &state);
/*
* The pinned page goes away if we search the tree, get a local copy of any pinned key and
* discard any pinned value, then re-save the cursor state. Done before searching pinned pages
* (unlike other cursor functions), because we don't anticipate applications searching for a key
* they currently have pinned.)
*/
WT_ERR(__wt_cursor_localkey(cursor));
__cursor_novalue(cursor);
__cursor_state_save(cursor, &state);
/*
* If the given cursor has bounds set we should search from those bounds. This is required when
* the search key is outside the bounds. Otherwise search near should behave as normal with an
* additional bounds check after the call to row/col search.
*/
WT_ERR(__btcur_bounds_search_near_reposition(session, cbt));