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 : )
26 :
27 : type splice struct {
28 : prev *node
29 : next *node
30 : }
31 :
32 1 : func (s *splice) init(prev, next *node) {
33 1 : s.prev = prev
34 1 : s.next = next
35 1 : }
36 :
37 : // Iterator is an iterator over the skiplist object. Use Skiplist.NewIter
38 : // to construct an iterator. The current state of the iterator can be cloned by
39 : // simply value copying the struct. All iterator methods are thread-safe.
40 : type Iterator struct {
41 : list *Skiplist
42 : nd *node
43 : kv base.InternalKV
44 : lower []byte
45 : upper []byte
46 : }
47 :
48 : // Iterator implements the base.InternalIterator interface.
49 : var _ base.InternalIterator = (*Iterator)(nil)
50 :
51 : var iterPool = sync.Pool{
52 1 : New: func() interface{} {
53 1 : return &Iterator{}
54 1 : },
55 : }
56 :
57 : // Close resets the iterator.
58 1 : func (it *Iterator) Close() error {
59 1 : it.list = nil
60 1 : it.nd = nil
61 1 : it.lower = nil
62 1 : it.upper = nil
63 1 : iterPool.Put(it)
64 1 : return nil
65 1 : }
66 :
67 1 : func (it *Iterator) String() string {
68 1 : return "memtable"
69 1 : }
70 :
71 : // Error returns any accumulated error.
72 1 : func (it *Iterator) Error() error {
73 1 : return nil
74 1 : }
75 :
76 : // SeekGE moves the iterator to the first entry whose key is greater than or
77 : // equal to the given key. Returns the key and value if the iterator is
78 : // pointing at a valid entry, and (nil, nil) otherwise. Note that SeekGE only
79 : // checks the upper bound. It is up to the caller to ensure that key is greater
80 : // than or equal to the lower bound.
81 1 : func (it *Iterator) SeekGE(key []byte, flags base.SeekGEFlags) *base.InternalKV {
82 1 : if flags.TrySeekUsingNext() {
83 1 : if it.nd == it.list.tail {
84 1 : // Iterator is done.
85 1 : return nil
86 1 : }
87 1 : less := it.list.cmp(it.kv.K.UserKey, key) < 0
88 1 : // Arbitrary constant. By measuring the seek cost as a function of the
89 1 : // number of elements in the skip list, and fitting to a model, we
90 1 : // could adjust the number of nexts based on the current size of the
91 1 : // skip list.
92 1 : const numNexts = 5
93 1 : kv := &it.kv
94 1 : for i := 0; less && i < numNexts; i++ {
95 1 : if kv = it.Next(); kv == nil {
96 1 : // Iterator is done.
97 1 : return nil
98 1 : }
99 1 : less = it.list.cmp(kv.K.UserKey, key) < 0
100 : }
101 1 : if !less {
102 1 : return kv
103 1 : }
104 : }
105 1 : _, it.nd, _ = it.seekForBaseSplice(key)
106 1 : if it.nd == it.list.tail {
107 1 : return nil
108 1 : }
109 1 : it.decodeKey()
110 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
111 1 : it.nd = it.list.tail
112 1 : return nil
113 1 : }
114 1 : it.kv.V = base.MakeInPlaceValue(it.value())
115 1 : return &it.kv
116 : }
117 :
118 : // SeekPrefixGE moves the iterator to the first entry whose key is greater than
119 : // or equal to the given key. This method is equivalent to SeekGE and is
120 : // provided so that an arenaskl.Iterator implements the
121 : // internal/base.InternalIterator interface.
122 1 : func (it *Iterator) SeekPrefixGE(prefix, key []byte, flags base.SeekGEFlags) *base.InternalKV {
123 1 : return it.SeekGE(key, flags)
124 1 : }
125 :
126 : // SeekLT moves the iterator to the last entry whose key is less than the given
127 : // key. Returns the key and value if the iterator is pointing at a valid entry,
128 : // and (nil, nil) otherwise. Note that SeekLT only checks the lower bound. It
129 : // is up to the caller to ensure that key is less than the upper bound.
130 1 : func (it *Iterator) SeekLT(key []byte, flags base.SeekLTFlags) *base.InternalKV {
131 1 : // NB: the top-level Iterator has already adjusted key based on
132 1 : // the upper-bound.
133 1 : it.nd, _, _ = it.seekForBaseSplice(key)
134 1 : if it.nd == it.list.head {
135 1 : return nil
136 1 : }
137 1 : it.decodeKey()
138 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
139 1 : it.nd = it.list.head
140 1 : return nil
141 1 : }
142 1 : it.kv.V = base.MakeInPlaceValue(it.value())
143 1 : return &it.kv
144 : }
145 :
146 : // First seeks position at the first entry in list. Returns the key and value
147 : // if the iterator is pointing at a valid entry, and (nil, nil) otherwise. Note
148 : // that First only checks the upper bound. It is up to the caller to ensure
149 : // that key is greater than or equal to the lower bound (e.g. via a call to SeekGE(lower)).
150 1 : func (it *Iterator) First() *base.InternalKV {
151 1 : it.nd = it.list.getNext(it.list.head, 0)
152 1 : if it.nd == it.list.tail {
153 1 : return nil
154 1 : }
155 1 : it.decodeKey()
156 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
157 1 : it.nd = it.list.tail
158 1 : return nil
159 1 : }
160 1 : it.kv.V = base.MakeInPlaceValue(it.value())
161 1 : return &it.kv
162 : }
163 :
164 : // Last seeks position at the last entry in list. Returns the key and value if
165 : // the iterator is pointing at a valid entry, and (nil, nil) otherwise. Note
166 : // that Last only checks the lower bound. It is up to the caller to ensure that
167 : // key is less than the upper bound (e.g. via a call to SeekLT(upper)).
168 1 : func (it *Iterator) Last() *base.InternalKV {
169 1 : it.nd = it.list.getPrev(it.list.tail, 0)
170 1 : if it.nd == it.list.head {
171 1 : return nil
172 1 : }
173 1 : it.decodeKey()
174 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
175 0 : it.nd = it.list.head
176 0 : return nil
177 0 : }
178 1 : it.kv.V = base.MakeInPlaceValue(it.value())
179 1 : return &it.kv
180 : }
181 :
182 : // Next advances to the next position. Returns the key and value if the
183 : // iterator is pointing at a valid entry, and (nil, nil) otherwise.
184 : // Note: flushIterator.Next mirrors the implementation of Iterator.Next
185 : // due to performance. Keep the two in sync.
186 1 : func (it *Iterator) Next() *base.InternalKV {
187 1 : it.nd = it.list.getNext(it.nd, 0)
188 1 : if it.nd == it.list.tail {
189 1 : return nil
190 1 : }
191 1 : it.decodeKey()
192 1 : if it.upper != nil && it.list.cmp(it.upper, it.kv.K.UserKey) <= 0 {
193 1 : it.nd = it.list.tail
194 1 : return nil
195 1 : }
196 1 : it.kv.V = base.MakeInPlaceValue(it.value())
197 1 : return &it.kv
198 : }
199 :
200 : // NextPrefix advances to the next position with a new prefix. Returns the key
201 : // and value if the iterator is pointing at a valid entry, and (nil, nil)
202 : // otherwise.
203 1 : func (it *Iterator) NextPrefix(succKey []byte) *base.InternalKV {
204 1 : return it.SeekGE(succKey, base.SeekGEFlagsNone.EnableTrySeekUsingNext())
205 1 : }
206 :
207 : // Prev moves to the previous position. Returns the key and value if the
208 : // iterator is pointing at a valid entry, and (nil, nil) otherwise.
209 1 : func (it *Iterator) Prev() *base.InternalKV {
210 1 : it.nd = it.list.getPrev(it.nd, 0)
211 1 : if it.nd == it.list.head {
212 1 : return nil
213 1 : }
214 1 : it.decodeKey()
215 1 : if it.lower != nil && it.list.cmp(it.lower, it.kv.K.UserKey) > 0 {
216 1 : it.nd = it.list.head
217 1 : return nil
218 1 : }
219 1 : it.kv.V = base.MakeInPlaceValue(it.value())
220 1 : return &it.kv
221 : }
222 :
223 : // value returns the value at the current position.
224 1 : func (it *Iterator) value() []byte {
225 1 : return it.nd.getValue(it.list.arena)
226 1 : }
227 :
228 : // Head true iff the iterator is positioned at the sentinel head node.
229 0 : func (it *Iterator) Head() bool {
230 0 : return it.nd == it.list.head
231 0 : }
232 :
233 : // Tail true iff the iterator is positioned at the sentinel tail node.
234 0 : func (it *Iterator) Tail() bool {
235 0 : return it.nd == it.list.tail
236 0 : }
237 :
238 : // SetBounds sets the lower and upper bounds for the iterator. Note that the
239 : // result of Next and Prev will be undefined until the iterator has been
240 : // repositioned with SeekGE, SeekPrefixGE, SeekLT, First, or Last.
241 1 : func (it *Iterator) SetBounds(lower, upper []byte) {
242 1 : it.lower = lower
243 1 : it.upper = upper
244 1 : }
245 :
246 : // SetContext implements base.InternalIterator.
247 0 : func (it *Iterator) SetContext(_ context.Context) {}
248 :
249 1 : func (it *Iterator) decodeKey() {
250 1 : it.kv.K.UserKey = it.list.arena.getBytes(it.nd.keyOffset, it.nd.keySize)
251 1 : it.kv.K.Trailer = it.nd.keyTrailer
252 1 : }
253 :
254 1 : func (it *Iterator) seekForBaseSplice(key []byte) (prev, next *node, found bool) {
255 1 : ikey := base.MakeSearchKey(key)
256 1 : level := int(it.list.Height() - 1)
257 1 :
258 1 : prev = it.list.head
259 1 : for {
260 1 : prev, next, found = it.list.findSpliceForLevel(ikey, level, prev)
261 1 :
262 1 : if found {
263 0 : if level != 0 {
264 0 : // next is pointing at the target node, but we need to find previous on
265 0 : // the bottom level.
266 0 : prev = it.list.getPrev(next, 0)
267 0 : }
268 0 : break
269 : }
270 :
271 1 : if level == 0 {
272 1 : break
273 : }
274 :
275 1 : level--
276 : }
277 :
278 1 : return
279 : }
|