Line data Source code
1 : /*
2 : * Copyright 2017 Dgraph Labs, Inc. and Contributors
3 : * Modifications copyright (C) 2017 Andy Kimball and Contributors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : package arenaskl
19 :
20 : import (
21 : "context"
22 : "sync"
23 :
24 : "github.com/cockroachdb/pebble/internal/base"
25 : "github.com/cockroachdb/pebble/internal/treeprinter"
26 : )
27 :
28 : type splice struct {
29 : prev *node
30 : next *node
31 : }
32 :
33 1 : func (s *splice) init(prev, next *node) {
34 1 : s.prev = prev
35 1 : s.next = next
36 1 : }
37 :
38 : // Iterator is an iterator over the skiplist object. Use Skiplist.NewIter
39 : // to construct an iterator. The current state of the iterator can be cloned by
40 : // simply value copying the struct. All iterator methods are thread-safe.
41 : type Iterator struct {
42 : list *Skiplist
43 : nd *node
44 : kv base.InternalKV
45 : lower []byte
46 : upper []byte
47 : // {lower|upper}Node are lazily populated with an arbitrary node that is
48 : // beyond the lower or upper bound respectively. Note the node is
49 : // "arbitrary" because it may not be the first node that exceeds the bound.
50 : // Concurrent insertions into the skiplist may introduce new nodes with keys
51 : // that exceed the bounds but are closer to the bounds than the current
52 : // values of [lower|upper]Node.
53 : //
54 : // Once populated, [lower|upper]Node may be used to detect when iteration
55 : // has reached a bound without performing a key comparison. This may be
56 : // beneficial when performing repeated SeekGEs with TrySeekUsingNext and an
57 : // upper bound set. Once the upper bound has been met, no additional key
58 : // comparisons are necessary.
59 : lowerNode *node
60 : upperNode *node
61 : }
62 :
63 : // Iterator implements the base.InternalIterator interface.
64 : var _ base.InternalIterator = (*Iterator)(nil)
65 :
66 : var iterPool = sync.Pool{
67 1 : New: func() interface{} {
68 1 : return &Iterator{}
69 1 : },
70 : }
71 :
72 : // Close resets the iterator.
73 1 : func (it *Iterator) Close() error {
74 1 : *it = Iterator{}
75 1 : iterPool.Put(it)
76 1 : return nil
77 1 : }
78 :
79 1 : func (it *Iterator) String() string {
80 1 : return "memtable"
81 1 : }
82 :
83 : // Error returns any accumulated error.
84 1 : func (it *Iterator) Error() error {
85 1 : return nil
86 1 : }
87 :
88 : // SeekGE moves the iterator to the first entry whose key is greater than or
89 : // equal to the given key. Returns the KV pair if the iterator is pointing at a
90 : // valid entry, and nil otherwise. Note that SeekGE only checks the upper bound.
91 : // It is up to the caller to ensure that key is greater than or equal to the
92 : // lower bound.
93 1 : func (it *Iterator) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
94 1 : if flags.TrySeekUsingNext() {
95 1 : if it.nd == it.list.tail || it.nd == it.upperNode {
96 1 : // Iterator is done.
97 1 : return nil
98 1 : }
99 1 : less := it.list.cmp(it.kv.K.UserKey, key) < 0
100 1 : // Arbitrary constant. By measuring the seek cost as a function of the
101 1 : // number of elements in the skip list, and fitting to a model, we
102 1 : // could adjust the number of nexts based on the current size of the
103 1 : // skip list.
104 1 : const numNexts = 5
105 1 : kv := &it.kv
106 1 : for i := 0; less && i < numNexts; i++ {
107 1 : if kv = it.Next(); kv == nil {
108 1 : // Iterator is done.
109 1 : return nil
110 1 : }
111 1 : less = it.list.cmp(kv.K.UserKey, key) < 0
112 : }
113 1 : if !less {
114 1 : return kv
115 1 : }
116 : }
117 1 : _, it.nd, _ = it.seekForBaseSplice(key)
118 1 : if it.nd == it.list.tail || it.nd == it.upperNode {
119 1 : return nil
120 1 : }
121 1 : it.decodeKey()
122 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
123 1 : it.upperNode = it.nd
124 1 : return nil
125 1 : }
126 1 : it.kv.V = base.MakeInPlaceValue(it.value())
127 1 : return &it.kv
128 : }
129 :
130 : // SeekPrefixGE moves the iterator to the first entry whose key is greater than
131 : // or equal to the given key. This method is equivalent to SeekGE and is
132 : // provided so that an arenaskl.Iterator implements the
133 : // internal/base.InternalIterator interface.
134 1 : func (it *Iterator) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) *base.InternalKV {
135 1 : return it.SeekGE(key, flags)
136 1 : }
137 :
138 : // SeekLT moves the iterator to the last entry whose key is less than the given
139 : // key. Returns the KV pair if the iterator is pointing at a valid entry, and
140 : // nil otherwise. Note that SeekLT only checks the lower bound. It is up to the
141 : // caller to ensure that key is less than the upper bound.
142 1 : func (it *Iterator) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
143 1 : // NB: the top-level Iterator has already adjusted key based on
144 1 : // the upper-bound.
145 1 : it.nd, _, _ = it.seekForBaseSplice(key)
146 1 : if it.nd == it.list.head || it.nd == it.lowerNode {
147 1 : return nil
148 1 : }
149 1 : it.decodeKey()
150 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
151 1 : it.lowerNode = it.nd
152 1 : return nil
153 1 : }
154 1 : it.kv.V = base.MakeInPlaceValue(it.value())
155 1 : return &it.kv
156 : }
157 :
158 : // First seeks position at the first entry in list. Returns the KV pair if the
159 : // iterator is pointing at a valid entry, and nil otherwise. Note that First
160 : // only checks the upper bound. It is up to the caller to ensure that key is
161 : // greater than or equal to the lower bound (e.g. via a call to SeekGE(lower)).
162 1 : func (it *Iterator) First() *base.InternalKV {
163 1 : it.nd = it.list.getNext(it.list.head, 0)
164 1 : if it.nd == it.list.tail || it.nd == it.upperNode {
165 1 : return nil
166 1 : }
167 1 : it.decodeKey()
168 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
169 1 : it.upperNode = it.nd
170 1 : return nil
171 1 : }
172 1 : it.kv.V = base.MakeInPlaceValue(it.value())
173 1 : return &it.kv
174 : }
175 :
176 : // Last seeks position at the last entry in list. Returns the KV pair if the
177 : // iterator is pointing at a valid entry, and nil otherwise. Note that Last only
178 : // checks the lower bound. It is up to the caller to ensure that key is less
179 : // than the upper bound (e.g. via a call to SeekLT(upper)).
180 1 : func (it *Iterator) Last() *base.InternalKV {
181 1 : it.nd = it.list.getPrev(it.list.tail, 0)
182 1 : if it.nd == it.list.head || it.nd == it.lowerNode {
183 1 : return nil
184 1 : }
185 1 : it.decodeKey()
186 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
187 0 : it.lowerNode = it.nd
188 0 : return nil
189 0 : }
190 1 : it.kv.V = base.MakeInPlaceValue(it.value())
191 1 : return &it.kv
192 : }
193 :
194 : // Next advances to the next position. Returns the KV pair if the iterator is
195 : // pointing at a valid entry, and nil otherwise.
196 : // Note: flushIterator.Next mirrors the implementation of Iterator.Next
197 : // due to performance. Keep the two in sync.
198 1 : func (it *Iterator) Next() *base.InternalKV {
199 1 : it.nd = it.list.getNext(it.nd, 0)
200 1 : if it.nd == it.list.tail || it.nd == it.upperNode {
201 1 : return nil
202 1 : }
203 1 : it.decodeKey()
204 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
205 1 : it.upperNode = it.nd
206 1 : return nil
207 1 : }
208 1 : it.kv.V = base.MakeInPlaceValue(it.value())
209 1 : return &it.kv
210 : }
211 :
212 : // NextPrefix advances to the next position with a new prefix. Returns the KV
213 : // pair if the iterator is pointing at a valid entry and nil otherwise.
214 1 : func (it *Iterator) NextPrefix(succKey []byte) *base.InternalKV {
215 1 : return it.SeekGE(succKey, base.SeekGEFlagsNone.EnableTrySeekUsingNext())
216 1 : }
217 :
218 : // Prev moves to the previous position. Returns the KV pair if the iterator is
219 : // pointing at a valid entry and nil otherwise.
220 1 : func (it *Iterator) Prev() *base.InternalKV {
221 1 : it.nd = it.list.getPrev(it.nd, 0)
222 1 : if it.nd == it.list.head || it.nd == it.lowerNode {
223 1 : return nil
224 1 : }
225 1 : it.decodeKey()
226 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
227 1 : it.lowerNode = it.nd
228 1 : return nil
229 1 : }
230 1 : it.kv.V = base.MakeInPlaceValue(it.value())
231 1 : return &it.kv
232 : }
233 :
234 : // value returns the value at the current position.
235 1 : func (it *Iterator) value() []byte {
236 1 : return it.nd.getValue(it.list.arena)
237 1 : }
238 :
239 : // SetBounds sets the lower and upper bounds for the iterator. Note that the
240 : // result of Next and Prev will be undefined until the iterator has been
241 : // repositioned with SeekGE, SeekPrefixGE, SeekLT, First, or Last.
242 1 : func (it *Iterator) SetBounds(lower, upper []byte) {
243 1 : it.lower = lower
244 1 : it.upper = upper
245 1 : it.lowerNode = nil
246 1 : it.upperNode = nil
247 1 : }
248 :
249 : // SetContext implements base.InternalIterator.
250 0 : func (it *Iterator) SetContext(_ context.Context) {}
251 :
252 : // DebugTree is part of the InternalIterator interface.
253 0 : func (it *Iterator) DebugTree(tp treeprinter.Node) {
254 0 : tp.Childf("%T(%p)", it, it)
255 0 : }
256 :
257 1 : func (it *Iterator) decodeKey() {
258 1 : it.kv.K.UserKey = it.list.arena.getBytes(it.nd.keyOffset, it.nd.keySize)
259 1 : it.kv.K.Trailer = it.nd.keyTrailer
260 1 : }
261 :
262 1 : func (it *Iterator) seekForBaseSplice(key []byte) (prev, next *node, found bool) {
263 1 : ikey := base.MakeSearchKey(key)
264 1 : level := int(it.list.Height() - 1)
265 1 :
266 1 : prev = it.list.head
267 1 : for {
268 1 : prev, next, found = it.list.findSpliceForLevel(ikey, level, prev)
269 1 :
270 1 : if found {
271 0 : if level != 0 {
272 0 : // next is pointing at the target node, but we need to find previous on
273 0 : // the bottom level.
274 0 : prev = it.list.getPrev(next, 0)
275 0 : }
276 0 : break
277 : }
278 :
279 1 : if level == 0 {
280 1 : break
281 : }
282 :
283 1 : level--
284 : }
285 :
286 1 : return
287 : }
|