Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom types to implement insert_reserve() support #728

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub use table::{
};
pub use transactions::{DatabaseStats, Durability, ReadTransaction, WriteTransaction};
pub use tree_store::{AccessGuard, AccessGuardMut, Savepoint};
pub use types::{RedbKey, RedbValue, TypeName};
pub use types::{MutInPlaceValue, RedbKey, RedbValue, TypeName};

type Result<T = (), E = StorageError> = std::result::Result<T, E>;

Expand Down
4 changes: 2 additions & 2 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::tree_store::{
AccessGuardMut, Btree, BtreeDrain, BtreeDrainFilter, BtreeMut, BtreeRangeIter, Checksum,
PageHint, PageNumber, RawBtree, TransactionalMemory, MAX_VALUE_LENGTH,
};
use crate::types::{RedbKey, RedbValue, RedbValueMutInPlace};
use crate::types::{MutInPlaceValue, RedbKey, RedbValue};
use crate::{AccessGuard, StorageError, WriteTransaction};
use crate::{Result, TableHandle};
use std::borrow::Borrow;
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'db, 'txn, K: RedbKey + 'static, V: RedbValue + 'static> Table<'db, 'txn, K
}
}

impl<'db, 'txn, K: RedbKey + 'static, V: RedbValueMutInPlace + 'static> Table<'db, 'txn, K, V> {
impl<'db, 'txn, K: RedbKey + 'static, V: MutInPlaceValue + 'static> Table<'db, 'txn, K, V> {
/// Reserve space to insert a key-value pair
/// The returned reference will have length equal to value_length
pub fn insert_reserve<'a>(
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::tree_store::page_store::{CachePriority, Page, PageImpl, PageMut, Tran
use crate::tree_store::{
AccessGuardMut, AllPageNumbersBtreeIter, BtreeDrainFilter, BtreeRangeIter, PageHint, PageNumber,
};
use crate::types::{RedbKey, RedbValue, RedbValueMutInPlace};
use crate::types::{MutInPlaceValue, RedbKey, RedbValue};
use crate::{AccessGuard, Result};
#[cfg(feature = "logging")]
use log::trace;
Expand Down Expand Up @@ -429,7 +429,7 @@ impl<'a, K: RedbKey + 'a, V: RedbValue + 'a> BtreeMut<'a, K, V> {
}
}

impl<'a, K: RedbKey + 'a, V: RedbValueMutInPlace + 'a> BtreeMut<'a, K, V> {
impl<'a, K: RedbKey + 'a, V: MutInPlaceValue + 'a> BtreeMut<'a, K, V> {
/// Reserve space to insert a key-value pair
/// The returned reference will have length equal to value_length
// Return type has the same lifetime as &self, because the tree must not be modified until the mutable guard is dropped
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/btree_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tree_store::page_store::{
xxh3_checksum, CachePriority, Page, PageImpl, PageMut, TransactionalMemory,
};
use crate::tree_store::PageNumber;
use crate::types::{RedbKey, RedbValue, RedbValueMutInPlace};
use crate::types::{MutInPlaceValue, RedbKey, RedbValue};
use crate::{Result, StorageError};
use std::cmp::Ordering;
use std::marker::PhantomData;
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a, V: RedbValue> AccessGuardMut<'a, V> {
}
}

impl<'a, V: RedbValueMutInPlace> AsMut<V::BaseRefType> for AccessGuardMut<'a, V> {
impl<'a, V: MutInPlaceValue> AsMut<V::BaseRefType> for AccessGuardMut<'a, V> {
fn as_mut(&mut self) -> &mut V::BaseRefType {
V::from_bytes_mut(&mut self.page.memory_mut()[self.offset..(self.offset + self.len)])
}
Expand Down
4 changes: 2 additions & 2 deletions src/tree_store/table_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::tree_store::btree::{btree_stats, UntypedBtreeMut};
use crate::tree_store::btree_base::Checksum;
use crate::tree_store::btree_iters::AllPageNumbersBtreeIter;
use crate::tree_store::{BtreeMut, BtreeRangeIter, PageNumber, RawBtree, TransactionalMemory};
use crate::types::{RedbKey, RedbValue, RedbValueMutInPlace, TypeName};
use crate::types::{MutInPlaceValue, RedbKey, RedbValue, TypeName};
use crate::{DatabaseStats, Result};
use std::cmp::max;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -158,7 +158,7 @@ impl RedbValue for FreedPageList<'_> {
}
}

impl RedbValueMutInPlace for FreedPageList<'_> {
impl MutInPlaceValue for FreedPageList<'_> {
type BaseRefType = FreedPageListMut;

fn initialize(data: &mut [u8]) {
Expand Down
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub trait RedbValue: Debug {

/// Implementing this trait indicates that the type can be mutated in-place as a &mut [u8].
/// This enables the .insert_reserve() method on Table
pub trait RedbValueMutInPlace: RedbValue {
pub trait MutInPlaceValue: RedbValue {
/// The base type such that &mut [u8] can be safely transmuted to &mut BaseRefType
type BaseRefType: Debug + ?Sized;

Expand All @@ -112,7 +112,7 @@ pub trait RedbValueMutInPlace: RedbValue {
fn from_bytes_mut(data: &mut [u8]) -> &mut Self::BaseRefType;
}

impl RedbValueMutInPlace for &[u8] {
impl MutInPlaceValue for &[u8] {
type BaseRefType = [u8];

fn initialize(_data: &mut [u8]) {
Expand Down
Loading