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
use core::{
borrow::Borrow,
mem,
ops::{Bound, RangeBounds},
};
use dbutils::{buffer::VacantBuffer, equivalentor::BytesComparator};
use rarena_allocator::Allocator as _;
use crate::{
allocator::{Allocator, Meta, Node, NodePointer},
error::Error,
random_height,
types::{Height, ValueBuilder},
Active, Header, MaybeTombstone, Version,
};
use super::{iterator, EntryRef, RefCounter, SkipList};
mod update;
type RemoveValueBuilder<E> =
ValueBuilder<std::boxed::Box<dyn Fn(&mut VacantBuffer<'_>) -> Result<usize, E>>>;
impl<C, A, R> SkipList<C, A, R>
where
A: Allocator,
R: RefCounter,
{
/// Sets remove on drop, only works on mmap with a file backend.
///
/// Default is `false`.
///
/// > **WARNING:** Once set to `true`, the backed file will be removed when the allocator is dropped, even though the file is opened in
/// > read-only mode.
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
pub fn remove_on_drop(&self, val: bool) {
self.arena.remove_on_drop(val);
}
/// Returns the header of the `SkipList`.
///
/// By default, `SkipList` will allocate meta, head node, and tail node in the ARENA,
/// and the data section will be allocated after the tail node.
///
/// This method will return the header of the `SkipList`.
#[inline]
pub const fn header(&self) -> Option<&Header> {
self.header.as_ref()
}
/// Returns the version number of the [`SkipList`].
#[inline]
pub fn version(&self) -> u16 {
self.arena.magic_version()
}
/// Returns the magic version number of the [`SkipList`].
///
/// This value can be used to check the compatibility for application using [`SkipList`].
#[inline]
pub fn magic_version(&self) -> u16 {
self.meta().magic_version()
}
/// Returns the height of the highest tower within any of the nodes that
/// have ever been allocated as part of this skiplist.
#[inline]
pub fn height(&self) -> u8 {
self.meta().height()
}
/// Returns the number of entries in the skipmap.
#[inline]
pub fn len(&self) -> usize {
self.meta().len() as usize
}
/// Returns true if the skipmap is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Gets the number of pointers to this `SkipList` similar to [`Arc::strong_count`](std::sync::Arc::strong_count).
#[inline]
pub fn refs(&self) -> usize {
self.arena.refs()
}
/// Returns the maximum version of all entries in the map.
#[inline]
pub fn maximum_version(&self) -> u64 {
self.meta().maximum_version()
}
/// Returns the minimum version of all entries in the map.
#[inline]
pub fn minimum_version(&self) -> u64 {
self.meta().minimum_version()
}
/// Returns `true` if the map may contain an entry whose version is less or equal to the given version.
#[inline]
pub fn may_contain_version(&self, version: Version) -> bool {
self.minimum_version() <= version
}
/// Returns a random generated height.
///
/// This method is useful when you want to check if the underlying allocator can allocate a node.
#[inline]
pub fn random_height(&self) -> Height {
random_height(self.arena.options().max_height())
}
/// Returns the estimated size of a node with the given height and key/value sizes.
///
/// **Note**: The returned size is only an estimate and may not be accurate, which means that the actual size is less than or equal to the returned size.
#[inline]
pub fn estimated_node_size(height: Height, key_size: usize, value_size: usize) -> usize {
let height: usize = height.into();
7 // max padding
+ mem::size_of::<A::Node>()
+ mem::size_of::<<A::Node as Node>::Link>() * height
+ key_size
+ value_size
}
/// Flushes outstanding memory map modifications to disk.
///
/// When this method returns with a non-error result,
/// all outstanding changes to a file-backed memory map are guaranteed to be durably stored.
/// The file's metadata (including last modification timestamp) may not be updated.
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
pub fn flush(&self) -> std::io::Result<()> {
self.arena.flush()
}
/// Asynchronously flushes outstanding memory map modifications to disk.
///
/// This method initiates flushing modified pages to durable storage, but it will not wait for
/// the operation to complete before returning. The file's metadata (including last
/// modification timestamp) may not be updated.
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
pub fn flush_async(&self) -> std::io::Result<()> {
self.arena.flush_async()
}
}
impl<C, A, RC> SkipList<C, A, RC>
where
A: Allocator,
RC: RefCounter,
C: BytesComparator,
{
/// Returns `true` if the key exists in the map.
///
/// This method will return `false` if the entry is marked as removed. If you want to check if the key exists even if it is marked as removed,
/// you can use [`contains_key_with_tombstone`](SkipList::contains_key_with_tombstone).
#[inline]
pub fn contains_key(&self, version: Version, key: &[u8]) -> bool {
self.get(version, key).is_some()
}
/// Returns `true` if the key exists in the map, even if it is marked as removed.
#[inline]
pub fn contains_key_with_tombstone(&self, version: Version, key: &[u8]) -> bool {
self.get_with_tombstone(version, key).is_some()
}
/// Returns the first entry in the map.
pub fn first(&self, version: Version) -> Option<EntryRef<'_, Active, C, A, RC>> {
self.iter(version).next()
}
/// Returns the last entry in the map.
pub fn last(&self, version: Version) -> Option<EntryRef<'_, Active, C, A, RC>> {
self.iter(version).last()
}
/// Returns the first entry in the map.
pub fn first_with_tombstone(
&self,
version: Version,
) -> Option<EntryRef<'_, MaybeTombstone, C, A, RC>> {
self.iter_all(version).next()
}
/// Returns the last entry in the map.
pub fn last_with_tombstone(
&self,
version: Version,
) -> Option<EntryRef<'_, MaybeTombstone, C, A, RC>> {
self.iter_all(version).last()
}
/// Returns the value associated with the given key, if it exists.
///
/// This method will return `None` if the entry is marked as removed. If you want to get the entry even if it is marked as removed,
/// you can use [`get_with_tombstone`](SkipList::get_with_tombstone).
pub fn get(&self, version: Version, key: &[u8]) -> Option<EntryRef<'_, Active, C, A, RC>> {
unsafe {
let (n, eq) = self.find_near(version, key, false, true); // findLessOrEqual.
let node = n?;
let raw_node_key = node.get_key(&self.arena);
let (value, _) = node.get_value_with_pointer(&self.arena);
if eq {
return value.map(|value| {
EntryRef::from_node_with_pointer(version, node, self, Some(raw_node_key), value)
});
}
if !self.cmp.equivalent(key, raw_node_key) {
return None;
}
if node.version() > version {
return None;
}
value.map(|value| {
EntryRef::from_node_with_pointer(version, node, self, Some(raw_node_key), value)
})
}
}
/// Returns the value associated with the given key, if it exists.
///
/// The difference between `get` and `get_with_tombstone` is that `get_with_tombstone` will return the value even if the entry is removed.
pub fn get_with_tombstone(
&self,
version: Version,
key: &[u8],
) -> Option<EntryRef<'_, MaybeTombstone, C, A, RC>> {
unsafe {
let (n, eq) = self.find_near(version, key, false, true); // findLessOrEqual.
let node = n?;
let raw_node_key = node.get_key(&self.arena);
let (value, _) = node.get_value_with_pointer(&self.arena);
if eq {
return Some(EntryRef::from_node_with_pointer(
version,
node,
self,
Some(raw_node_key),
value,
));
}
if !self.cmp.equivalent(key, raw_node_key) {
return None;
}
if node.version() > version {
return None;
}
Some(EntryRef::from_node_with_pointer(
version,
node,
self,
Some(raw_node_key),
value,
))
}
}
/// Returns an `EntryRef` pointing to the highest element whose key is below the given bound.
/// If no such element is found then `None` is returned.
pub fn upper_bound(
&self,
version: Version,
upper: Bound<&[u8]>,
) -> Option<EntryRef<'_, Active, C, A, RC>> {
self.iter(version).seek_upper_bound(upper)
}
/// Returns an `EntryRef` pointing to the lowest element whose key is above the given bound.
/// If no such element is found then `None` is returned.
pub fn lower_bound(
&self,
version: Version,
lower: Bound<&[u8]>,
) -> Option<EntryRef<'_, Active, C, A, RC>> {
self.iter(version).seek_lower_bound(lower)
}
}
impl<C, A, RC> SkipList<C, A, RC>
where
A: Allocator,
RC: RefCounter,
{
/// Returns a new iterator, this iterator will yield the latest version of all entries in the map less or equal to the given version.
#[inline]
pub fn iter(&self, version: Version) -> iterator::Iter<'_, Active, C, A, RC> {
iterator::Iter::new(version, self, false)
}
/// Returns a new iterator, this iterator will yield all versions for all entries in the map less or equal to the given version.
#[inline]
pub fn iter_all(&self, version: Version) -> iterator::Iter<'_, MaybeTombstone, C, A, RC> {
iterator::Iter::new(version, self, true)
}
/// Returns a iterator that within the range, this iterator will yield the latest version of all entries in the range less or equal to the given version.
#[inline]
pub fn range<Q, R>(
&self,
version: Version,
range: R,
) -> iterator::Iter<'_, Active, C, A, RC, Q, R>
where
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
{
iterator::Iter::range(version, self, range, false)
}
/// Returns a iterator that within the range, this iterator will yield all versions for all entries in the range less or equal to the given version.
#[inline]
pub fn range_all<Q, R>(
&self,
version: Version,
range: R,
) -> iterator::Iter<'_, MaybeTombstone, C, A, RC, Q, R>
where
Q: ?Sized + Borrow<[u8]>,
R: RangeBounds<Q>,
{
iterator::Iter::range(version, self, range, true)
}
}