Skip to content

Commit

Permalink
Add Entry and VersionedEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
al8n committed May 19, 2024
1 parent ca12dc2 commit b8a740a
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ impl Arena {
/// - The caller must make sure that `size` must be less than the capacity of the arena.
/// - The caller must make sure that `offset + size` must be less than the capacity of the arena.
#[inline]
pub(super) unsafe fn get_bytes(&self, offset: usize, size: usize) -> &[u8] {
pub(super) const unsafe fn get_bytes(&self, offset: usize, size: usize) -> &[u8] {
if offset == 0 {
return &[];
}
Expand All @@ -497,7 +497,7 @@ impl Arena {
/// ## Safety:
/// - The caller must make sure that `offset` must be less than the capacity of the arena.
#[inline]
pub(super) unsafe fn get_pointer(&self, offset: usize) -> *const u8 {
pub(super) const unsafe fn get_pointer(&self, offset: usize) -> *const u8 {
if offset == 0 {
return ptr::null();
}
Expand Down
26 changes: 15 additions & 11 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,14 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
f: impl FnOnce(OccupiedValue<'a>) -> Result<(), E> + Copy,
ins: &mut Inserter<T>,
upsert: bool,
) -> Result<Option<OptionEntryRef<'a, T, C>>, Either<E, Error>> {
) -> Result<Option<VersionedEntryRef<'a, T, C>>, Either<E, Error>> {
let version = trailer.version();
// Safety: a fresh new Inserter, so safe here
unsafe {
let (found, ptr) = self.find_splice(version, key, ins, true);
if found {
let node_ptr = ptr.expect("the NodePtr cannot be `None` when we found");
let old = OptionEntryRef::from_node(node_ptr, self);
let old = VersionedEntryRef::from_node(node_ptr, self);

if upsert {
node_ptr
Expand Down Expand Up @@ -773,7 +773,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let node_ptr = fr
.curr
.expect("the current should not be `None` when we found");
let old = OptionEntryRef::from_node(node_ptr, self);
let old = VersionedEntryRef::from_node(node_ptr, self);

if upsert {
node_ptr
Expand Down Expand Up @@ -821,8 +821,8 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
upsert: bool,
) -> Result<
Either<
Option<OptionEntryRef<'a, T, C>>,
Result<OptionEntryRef<'a, T, C>, OptionEntryRef<'a, T, C>>,
Option<VersionedEntryRef<'a, T, C>>,
Result<VersionedEntryRef<'a, T, C>, VersionedEntryRef<'a, T, C>>,
>,
Error,
> {
Expand All @@ -832,7 +832,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let (found, ptr) = self.find_splice(version, key, ins, true);
if found {
let node_ptr = ptr.expect("the NodePtr cannot be `None` when we found");
let old = OptionEntryRef::from_node(node_ptr, self);
let old = VersionedEntryRef::from_node(node_ptr, self);

if upsert {
let node = node_ptr.as_ptr();
Expand All @@ -841,21 +841,23 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
Ok((offset, len)) => {
let trailer = node.get_trailer_by_offset(&self.arena, offset);
let value = node.get_value_by_offset(&self.arena, offset, len);
Ok(Either::Right(Ok(OptionEntryRef {
Ok(Either::Right(Ok(VersionedEntryRef {
map: self,
key,
trailer,
value,
ptr: node_ptr,
})))
}
Err((offset, len)) => {
let trailer = node.get_trailer_by_offset(&self.arena, offset);
let value = node.get_value_by_offset(&self.arena, offset, len);
Ok(Either::Right(Err(OptionEntryRef {
Ok(Either::Right(Err(VersionedEntryRef {
map: self,
key,
trailer,
value,
ptr: node_ptr,
})))
}
};
Expand Down Expand Up @@ -989,7 +991,7 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let node_ptr = fr
.curr
.expect("the current should not be `None` when we found");
let old = OptionEntryRef::from_node(node_ptr, self);
let old = VersionedEntryRef::from_node(node_ptr, self);

if upsert {
let node = node_ptr.as_ptr();
Expand All @@ -998,21 +1000,23 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
Ok((offset, len)) => {
let trailer = node.get_trailer_by_offset(&self.arena, offset);
let value = node.get_value_by_offset(&self.arena, offset, len);
Ok(Either::Right(Ok(OptionEntryRef {
Ok(Either::Right(Ok(VersionedEntryRef {
map: self,
key,
trailer,
value,
ptr: node_ptr,
})))
}
Err((offset, len)) => {
let trailer = node.get_trailer_by_offset(&self.arena, offset);
let value = node.get_value_by_offset(&self.arena, offset, len);
Ok(Either::Right(Err(OptionEntryRef {
Ok(Either::Right(Err(VersionedEntryRef {
map: self,
key,
trailer,
value,
ptr: node_ptr,
})))
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/map/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,12 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
let (trailer, value) = node.get_value_and_trailer(&self.arena);
if eq {
return value.map(|val| {
EntryRef(OptionEntryRef {
EntryRef(VersionedEntryRef {
map: self,
key: node_key,
trailer,
value: Some(val),
ptr: n,
})
});
}
Expand All @@ -587,11 +588,12 @@ impl<T: Trailer, C: Comparator> SkipMap<T, C> {
}

value.map(|val| {
EntryRef(OptionEntryRef {
EntryRef(VersionedEntryRef {
map: self,
key: node_key,
trailer,
value: Some(val),
ptr: n,
})
})
}
Expand Down
Loading

0 comments on commit b8a740a

Please sign in to comment.