Line data Source code
1 : // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 : // of this source code is governed by a BSD-style license that can be found in 3 : // the LICENSE file. 4 : 5 : package keyspan 6 : 7 : import "github.com/cockroachdb/pebble/internal/base" 8 : 9 : // Get returns the newest span that contains the target key. If no span 10 : // contains the target key, an empty span is returned. The snapshot 11 : // parameter controls the visibility of spans (only spans older than the 12 : // snapshot sequence number are visible). The iterator must contain 13 : // fragmented spans: no span may overlap another. 14 1 : func Get(cmp base.Compare, iter FragmentIterator, key []byte) *Span { 15 1 : // NB: We use SeekLT in order to land on the proper span for a search 16 1 : // key that resides in the middle of a span. Consider the scenario: 17 1 : // 18 1 : // a---e 19 1 : // e---i 20 1 : // 21 1 : // The spans are indexed by their start keys `a` and `e`. If the 22 1 : // search key is `c` we want to land on the span [a,e). If we were 23 1 : // to use SeekGE then the search key `c` would land on the span 24 1 : // [e,i) and we'd have to backtrack. The one complexity here is what 25 1 : // happens for the search key `e`. In that case SeekLT will land us 26 1 : // on the span [a,e) and we'll have to move forward. 27 1 : iterSpan := iter.SeekLT(key) 28 1 : if iterSpan == nil { 29 1 : iterSpan = iter.Next() 30 1 : if iterSpan == nil { 31 1 : // The iterator is empty. 32 1 : return nil 33 1 : } 34 1 : if cmp(key, iterSpan.Start) < 0 { 35 1 : // The search key lies before the first span. 36 1 : return nil 37 1 : } 38 : } 39 : 40 : // Invariant: key > iterSpan.Start 41 1 : if cmp(key, iterSpan.End) >= 0 { 42 1 : // The current span lies before the search key. Advance the iterator 43 1 : // once to potentially land on a key with a start key exactly equal to 44 1 : // key. (See the comment at the beginning of this function.) 45 1 : iterSpan = iter.Next() 46 1 : if iterSpan == nil || cmp(key, iterSpan.Start) < 0 { 47 1 : // We've run out of spans or we've moved on to a span which 48 1 : // starts after our search key. 49 1 : return nil 50 1 : } 51 : } 52 1 : return iterSpan 53 : }