Skip to content

Commit

Permalink
feat: add size_of
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-j97 committed Nov 15, 2024
1 parent c1574c5 commit d1c05c5
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/partition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,31 @@ impl PartitionHandle {
Ok(self.tree.get(key)?)
}

/// Retrieves the size of an item from the partition.
///
/// # Examples
///
/// ```
/// # use fjall::{Config, Keyspace, PartitionCreateOptions};
/// #
/// # let folder = tempfile::tempdir()?;
/// # let keyspace = Config::new(folder).open()?;
/// # let partition = keyspace.open_partition("default", PartitionCreateOptions::default())?;
/// partition.insert("a", "my_value")?;
///
/// let len = partition.size_of("a")?.unwrap_or_default();
/// assert_eq!("my_value".len() as u32, len);
/// #
/// # Ok::<(), fjall::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub fn size_of<K: AsRef<[u8]>>(&self, key: K) -> crate::Result<Option<u32>> {
Ok(self.tree.size_of(key)?)
}

/// Returns the first key-value pair in the partition.
/// The key in this pair is the minimum key in the partition.
///
Expand Down
41 changes: 41 additions & 0 deletions src/tx/read_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,47 @@ impl ReadTransaction {
.map_err(Into::into)
}

/// Retrieves the size of an item from the transaction's state.
///
/// # Examples
///
/// ```
/// # use fjall::{Config, Keyspace, PartitionCreateOptions};
/// #
/// # let folder = tempfile::tempdir()?;
/// # let keyspace = Config::new(folder).open_transactional()?;
/// # let partition = keyspace.open_partition("default", PartitionCreateOptions::default())?;
/// partition.insert("a", "my_value")?;
///
/// let tx = keyspace.read_tx();
/// let item = tx.size_of(&partition, "a")?.unwrap_or_default();
/// assert_eq!("my_value".len() as u32, item);
///
/// partition.insert("b", "my_updated_value")?;
///
/// // Repeatable read
/// let item = tx.size_of(&partition, "a")?.unwrap_or_default();
/// assert_eq!("my_value".len() as u32, item);
/// #
/// # Ok::<(), fjall::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub fn size_of<K: AsRef<[u8]>>(
&self,
partition: &TxPartitionHandle,
key: K,
) -> crate::Result<Option<u32>> {
partition
.inner
.tree
.snapshot_at(self.nonce.instant)
.size_of(key)
.map_err(Into::into)
}

/// Returns `true` if the transaction's state contains the specified key.
///
/// # Examples
Expand Down
26 changes: 26 additions & 0 deletions src/tx/write/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,32 @@ impl BaseTransaction {
Ok(res)
}

/// Retrieves the size of an item from the transaction's state.
///
/// The transaction allows reading your own writes (RYOW).
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub(super) fn size_of<K: AsRef<[u8]>>(
&self,
partition: &TxPartitionHandle,
key: K,
) -> crate::Result<Option<u32>> {
if let Some(memtable) = self.memtables.get(&partition.inner.name) {
if let Some(item) = memtable.get(&key, None) {
return Ok(ignore_tombstone_value(item).map(|x| x.value.len() as u32));
}
}

let res = partition
.inner
.snapshot_at(self.nonce.instant)
.size_of(key.as_ref())?;

Ok(res)
}

/// Returns `true` if the transaction's state contains the specified key.
///
/// # Errors
Expand Down
41 changes: 41 additions & 0 deletions src/tx/write/single_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,47 @@ impl<'a> WriteTransaction<'a> {
self.inner.get(partition, key)
}

/// Retrieves an item from the transaction's state.
///
/// The transaction allows reading your own writes (RYOW).
///
/// # Examples
///
/// ```
/// # use fjall::{Config, Keyspace, PartitionCreateOptions};
/// #
/// # let folder = tempfile::tempdir()?;
/// # let keyspace = Config::new(folder).open_transactional()?;
/// # let partition = keyspace.open_partition("default", PartitionCreateOptions::default())?;
/// partition.insert("a", "previous_value")?;
/// assert_eq!(b"previous_value", &*partition.get("a")?.unwrap());
///
/// let mut tx = keyspace.write_tx();
/// tx.insert(&partition, "a", "new_value");
///
/// // Read-your-own-write
/// let len = tx.size_of(&partition, "a")?.unwrap_or_default();
/// assert_eq!("new_value".len() as u32, len);
///
/// drop(tx);
///
/// // Write was not committed
/// assert_eq!(b"previous_value", &*partition.get("a")?.unwrap());
/// #
/// # Ok::<(), fjall::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Err` if an IO error occurs.
pub fn size_of<K: AsRef<[u8]>>(
&self,
partition: &TxPartitionHandle,
key: K,
) -> crate::Result<Option<u32>> {
self.inner.size_of(partition, key)
}

/// Returns `true` if the transaction's state contains the specified key.
///
/// # Examples
Expand Down
41 changes: 41 additions & 0 deletions src/tx/write/ssi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,47 @@ impl WriteTransaction {
Ok(res)
}

/// Retrieves an item from the transaction's state.
///
/// The transaction allows reading your own writes (RYOW).
///
/// # Examples
///
/// ```
/// # use fjall::{Config, Keyspace, PartitionCreateOptions};
/// #
/// # let folder = tempfile::tempdir()?;
/// # let keyspace = Config::new(folder).open_transactional()?;
/// # let partition = keyspace.open_partition("default", PartitionCreateOptions::default())?;
/// partition.insert("a", "previous_value")?;
/// assert_eq!(b"previous_value", &*partition.get("a")?.unwrap());
///
/// let mut tx = keyspace.write_tx()?;
/// tx.insert(&partition, "a", "new_value");
///
/// // Read-your-own-write
/// let len = tx.size_of(&partition, "a")?.unwrap_or_default();
/// assert_eq!("new_value".len() as u32, len);
///
/// drop(tx);
///
/// // Write was not committed
/// assert_eq!(b"previous_value", &*partition.get("a")?.unwrap());
/// #
/// # Ok::<(), fjall::Error>(())
/// ```
///
/// # Errors
///
/// Will return `Ersr` if an IO error occurs.
pub fn size_of<K: AsRef<[u8]>>(
&self,
partition: &TxPartitionHandle,
key: K,
) -> crate::Result<Option<u32>> {
self.inner.size_of(partition, key)
}

/// Returns `true` if the transaction's state contains the specified key.
///
/// # Examples
Expand Down

0 comments on commit d1c05c5

Please sign in to comment.