@@ -247,27 +247,16 @@ type Iterator struct {
247
247
externalIter * externalIterState
248
248
// Following fields used when constructing an iterator stack, eg, in Clone
249
249
// and SetOptions or when re-fragmenting a batch's range keys/range dels.
250
- // Non-nil if this Iterator includes a Batch.
251
- batch * Batch
252
250
fc * fileCacheHandle
253
251
newIters tableNewIters
254
252
newIterRangeKey keyspanimpl.TableNewSpanIter
255
253
lazyCombinedIter lazyCombinedIter
256
254
seqNum base.SeqNum
257
- // batchSeqNum is used by Iterators over indexed batches to detect when the
258
- // underlying batch has been mutated. The batch beneath an indexed batch may
259
- // be mutated while the Iterator is open, but new keys are not surfaced
260
- // until the next call to SetOptions.
261
- batchSeqNum base.SeqNum
262
- // batch{PointIter,RangeDelIter,RangeKeyIter} are used when the Iterator is
263
- // configured to read through an indexed batch. If a batch is set, these
264
- // iterators will be included within the iterator stack regardless of
265
- // whether the batch currently contains any keys of their kind. These
266
- // pointers are used during a call to SetOptions to refresh the Iterator's
267
- // view of its indexed batch.
268
- batchPointIter batchIter
269
- batchRangeDelIter keyspan.Iter
270
- batchRangeKeyIter keyspan.Iter
255
+ // batch is non-nil if this Iterator includes an indexed batch. Batch
256
+ // contains all the state pertaining to iterating over the indexed batch.
257
+ // The iteratorBatchState struct is bundled within the iterAlloc struct to
258
+ // reduce allocations.
259
+ batch * iteratorBatchState
271
260
// merging is a pointer to this iterator's point merging iterator. It
272
261
// appears here because key visibility is handled by the merging iterator.
273
262
// During SetOptions on an iterator over an indexed batch, this field is
@@ -336,6 +325,28 @@ func (i *Iterator) equal(a, b []byte) bool {
336
325
return i .comparer .Equal (a , b )
337
326
}
338
327
328
+ // iteratorBatchState holds state pertaining to iterating over an indexed batch.
329
+ // When an iterator is configured to read through an indexed batch, the iterator
330
+ // maintains a pointer to this struct. This struct is embedded within the
331
+ // iterAlloc struct to reduce allocations.
332
+ type iteratorBatchState struct {
333
+ batch * Batch
334
+ // batchSeqNum is used by Iterators over indexed batches to detect when the
335
+ // underlying batch has been mutated. The batch beneath an indexed batch may
336
+ // be mutated while the Iterator is open, but new keys are not surfaced
337
+ // until the next call to SetOptions.
338
+ batchSeqNum base.SeqNum
339
+ // batch{PointIter,RangeDelIter,RangeKeyIter} are used when the Iterator is
340
+ // configured to read through an indexed batch. If a batch is set, these
341
+ // iterators will be included within the iterator stack regardless of
342
+ // whether the batch currently contains any keys of their kind. These
343
+ // pointers are used during a call to SetOptions to refresh the Iterator's
344
+ // view of its indexed batch.
345
+ pointIter batchIter
346
+ rangeDelIter keyspan.Iter
347
+ rangeKeyIter keyspan.Iter
348
+ }
349
+
339
350
// iteratorRangeKeyState holds an iterator's range key iteration state.
340
351
type iteratorRangeKeyState struct {
341
352
// rangeKeyIter holds the range key iterator stack that iterates over the
@@ -2621,18 +2632,18 @@ func (i *Iterator) SetOptions(o *IterOptions) {
2621
2632
// iterator or range-key iterator but we require one, it'll be created in
2622
2633
// the slow path that reconstructs the iterator in finishInitializingIter.
2623
2634
if i .batch != nil {
2624
- nextBatchSeqNum := (base .SeqNum (len (i .batch .data )) | base .SeqNumBatchBit )
2625
- if nextBatchSeqNum != i .batchSeqNum {
2626
- i .batchSeqNum = nextBatchSeqNum
2635
+ nextBatchSeqNum := (base .SeqNum (len (i .batch .batch . data )) | base .SeqNumBatchBit )
2636
+ if nextBatchSeqNum != i .batch . batchSeqNum {
2637
+ i .batch . batchSeqNum = nextBatchSeqNum
2627
2638
if i .merging != nil {
2628
2639
i .merging .batchSnapshot = nextBatchSeqNum
2629
2640
}
2630
2641
// Prevent a no-op seek optimization on the next seek. We won't be
2631
2642
// able to reuse the top-level Iterator state, because it may be
2632
2643
// incorrect after the inclusion of new batch mutations.
2633
2644
i .batchJustRefreshed = true
2634
- if i .pointIter != nil && i .batch .countRangeDels > 0 {
2635
- if i .batchRangeDelIter .Count () == 0 {
2645
+ if i .pointIter != nil && i .batch .batch . countRangeDels > 0 {
2646
+ if i .batch . rangeDelIter .Count () == 0 {
2636
2647
// When we constructed this iterator, there were no
2637
2648
// rangedels in the batch. Iterator construction will
2638
2649
// have excluded the batch rangedel iterator from the
@@ -2651,11 +2662,11 @@ func (i *Iterator) SetOptions(o *IterOptions) {
2651
2662
// which is the count of fragmented range deletions, NOT
2652
2663
// the number of range deletions written to the batch
2653
2664
// [i.batch.countRangeDels].
2654
- i .batch .initRangeDelIter (& i .opts , & i .batchRangeDelIter , nextBatchSeqNum )
2665
+ i .batch .batch . initRangeDelIter (& i .opts , & i .batch . rangeDelIter , nextBatchSeqNum )
2655
2666
}
2656
2667
}
2657
- if i .rangeKey != nil && i .batch .countRangeKeys > 0 {
2658
- if i .batchRangeKeyIter .Count () == 0 {
2668
+ if i .rangeKey != nil && i .batch .batch . countRangeKeys > 0 {
2669
+ if i .batch . rangeKeyIter .Count () == 0 {
2659
2670
// When we constructed this iterator, there were no range
2660
2671
// keys in the batch. Iterator construction will have
2661
2672
// excluded the batch rangekey iterator from the range key
@@ -2673,7 +2684,7 @@ func (i *Iterator) SetOptions(o *IterOptions) {
2673
2684
// tell based on i.batchRangeKeyIter.Count(), which is the
2674
2685
// count of fragmented range keys, NOT the number of
2675
2686
// range keys written to the batch [i.batch.countRangeKeys].
2676
- i .batch .initRangeKeyIter (& i .opts , & i .batchRangeKeyIter , nextBatchSeqNum )
2687
+ i .batch .batch . initRangeKeyIter (& i .opts , & i .batch . rangeKeyIter , nextBatchSeqNum )
2677
2688
i .invalidate ()
2678
2689
}
2679
2690
}
@@ -2705,7 +2716,7 @@ func (i *Iterator) SetOptions(o *IterOptions) {
2705
2716
// used by the iterator now contains range keys. Lazy combined iteration
2706
2717
// is not compatible with batch range keys because we always need to
2707
2718
// merge the batch's range keys into iteration.
2708
- if i .rangeKey != nil || ! i .opts .rangeKeys () || i .batch == nil || i .batch .countRangeKeys == 0 {
2719
+ if i .rangeKey != nil || ! i .opts .rangeKeys () || i .batch == nil || i .batch .batch . countRangeKeys == 0 {
2709
2720
// Fast path. This preserves the Seek-using-Next optimizations as
2710
2721
// long as the iterator wasn't already invalidated up above.
2711
2722
return
@@ -2869,21 +2880,22 @@ func (i *Iterator) CloneWithContext(ctx context.Context, opts CloneOptions) (*It
2869
2880
keyBuf : buf .keyBuf ,
2870
2881
prefixOrFullSeekKey : buf .prefixOrFullSeekKey ,
2871
2882
boundsBuf : buf .boundsBuf ,
2872
- batch : i .batch ,
2873
- batchSeqNum : i .batchSeqNum ,
2874
2883
fc : i .fc ,
2875
2884
newIters : i .newIters ,
2876
2885
newIterRangeKey : i .newIterRangeKey ,
2877
2886
seqNum : i .seqNum ,
2878
2887
}
2879
- dbi .processBounds (dbi .opts .LowerBound , dbi .opts .UpperBound )
2880
-
2881
- // If the caller requested the clone have a current view of the indexed
2882
- // batch, set the clone's batch sequence number appropriately.
2883
- if i .batch != nil && opts .RefreshBatchView {
2884
- dbi .batchSeqNum = (base .SeqNum (len (i .batch .data )) | base .SeqNumBatchBit )
2888
+ if i .batch != nil {
2889
+ dbi .batch = & buf .batchState
2890
+ dbi .batch .batch = i .batch .batch
2891
+ dbi .batch .batchSeqNum = i .batch .batchSeqNum
2892
+ // If the caller requested the clone have a current view of the indexed
2893
+ // batch, set the clone's batch sequence number appropriately.
2894
+ if opts .RefreshBatchView {
2895
+ dbi .batch .batchSeqNum = (base .SeqNum (len (i .batch .batch .data )) | base .SeqNumBatchBit )
2896
+ }
2885
2897
}
2886
-
2898
+ dbi . processBounds ( dbi . opts . LowerBound , dbi . opts . UpperBound )
2887
2899
return finishInitializingIter (ctx , buf ), nil
2888
2900
}
2889
2901
0 commit comments