Skip to content

Commit 4e1f29f

Browse files
committed
feat: TxPartition::approximate_len
1 parent f4bba30 commit 4e1f29f

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: src/tx/partition.rs

+32
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ impl TransactionalPartitionHandle {
3838
self.inner.path().into()
3939
}
4040

41+
/// Approximates the amount of items in the partition.
42+
///
43+
/// For update -or delete-heavy workloads, this value will
44+
/// diverge from the real value, but is a O(1) operation.
45+
///
46+
/// For insert-only workloads (e.g. logs, time series)
47+
/// this value is reliable.
48+
///
49+
/// # Examples
50+
///
51+
/// ```
52+
/// # use fjall::{Config, Keyspace, PartitionCreateOptions};
53+
/// #
54+
/// # let folder = tempfile::tempdir()?;
55+
/// # let keyspace = Config::new(folder).open_transactional()?;
56+
/// # let partition = keyspace.open_partition("default", PartitionCreateOptions::default())?;
57+
/// assert_eq!(partition.len()?, 0);
58+
///
59+
/// partition.insert("1", "abc")?;
60+
/// assert_eq!(partition.approximate_len(), 1);
61+
///
62+
/// partition.remove("1")?;
63+
/// // Oops! approximate_len will not be reliable here
64+
/// assert_eq!(partition.approximate_len(), 2);
65+
/// #
66+
/// # Ok::<(), fjall::Error>(())
67+
/// ```
68+
#[must_use]
69+
pub fn approximate_len(&self) -> usize {
70+
self.inner.approximate_len()
71+
}
72+
4173
/// Removes an item and returns its value if it existed.
4274
///
4375
/// The operation will run wrapped in a transaction.

0 commit comments

Comments
 (0)