Skip to content

Commit 53db566

Browse files
authored
Add Db::commit_changes_shared (#247)
* Add `commit_changes_shared` that passes values as `Arc`s. * Cleanup + changelog. * Update doc.
1 parent 53ed662 commit 53db566

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The format is based on [Keep a Changelog].
66

77
## [Unreleased]
88

9+
- Add `Db::commit_changes_shared` that passes values as `Arc<Vec<u8>>`, potentially without an extra copy.
10+
911
## [v0.5.2] - 2024-05-28
1012

1113
- Compilation support for riscv64

src/btree/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ pub mod commit_overlay {
383383
BTreeChangeSet { col, changes: Default::default() }
384384
}
385385

386-
pub fn push(&mut self, change: Operation<Value, Value>) -> Result<()> {
386+
pub fn push<K: Into<RcKey>, V: Into<RcValue>>(
387+
&mut self,
388+
change: Operation<K, V>,
389+
) -> Result<()> {
387390
// No key hashing
388391
self.changes.push(match change {
389392
Operation::Set(k, v) => Operation::Set(k.into(), v.into()),

src/column.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,9 +1129,9 @@ impl HashColumn {
11291129
}
11301130

11311131
/// returns value for the root node and vector of NodeChange for nodes.
1132-
pub fn claim_tree_values(
1132+
pub fn claim_tree_values<K, V>(
11331133
&self,
1134-
change: &Operation<Value, Value>,
1134+
change: &Operation<K, V>,
11351135
) -> Result<(Vec<u8>, Vec<NodeChange>)> {
11361136
match change {
11371137
Operation::InsertTree(_key, node) => {

src/db.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ impl From<Value> for RcValue {
103103
}
104104
}
105105

106+
impl From<Arc<Value>> for RcValue {
107+
fn from(value: Arc<Value>) -> Self {
108+
Self(value)
109+
}
110+
}
111+
106112
#[cfg(test)]
107113
impl<const N: usize> TryFrom<RcValue> for [u8; N] {
108114
type Error = <[u8; N] as TryFrom<Vec<u8>>>::Error;
@@ -495,9 +501,10 @@ impl DbInner {
495501
}))
496502
}
497503

498-
fn commit_changes<I>(&self, tx: I) -> Result<()>
504+
fn commit_changes<I, V>(&self, tx: I) -> Result<()>
499505
where
500-
I: IntoIterator<Item = (ColId, Operation<Vec<u8>, Vec<u8>>)>,
506+
I: IntoIterator<Item = (ColId, Operation<Vec<u8>, V>)>,
507+
V: Into<RcValue>,
501508
{
502509
let mut commit: CommitChangeSet = Default::default();
503510
for (col, change) in tx.into_iter() {
@@ -564,7 +571,8 @@ impl DbInner {
564571
},
565572
Operation::ReferenceTree(..) => {
566573
if !self.options.columns[col as usize].append_only {
567-
let root_operation = Operation::Reference(change.key());
574+
let root_operation =
575+
Operation::<&_, V>::Reference(change.key());
568576
commit
569577
.indexed
570578
.entry(col)
@@ -1562,6 +1570,16 @@ impl Db {
15621570
self.inner.commit_changes(tx)
15631571
}
15641572

1573+
/// Commit a set of changes to the database.
1574+
///
1575+
/// This method passes values as `Arc<Vec<u8>>` potentially eliminating an extra copy.
1576+
pub fn commit_changes_shared<I>(&self, tx: I) -> Result<()>
1577+
where
1578+
I: IntoIterator<Item = (ColId, Operation<Vec<u8>, Arc<Vec<u8>>>)>,
1579+
{
1580+
self.inner.commit_changes(tx)
1581+
}
1582+
15651583
pub(crate) fn commit_raw(&self, commit: CommitChangeSet) -> Result<()> {
15661584
self.inner.commit_raw(commit)
15671585
}
@@ -2091,9 +2109,9 @@ impl IndexedChangeSet {
20912109
}
20922110
}
20932111

2094-
fn push<K: AsRef<[u8]>>(
2112+
fn push<K: AsRef<[u8]>, V: Into<RcValue>>(
20952113
&mut self,
2096-
change: Operation<K, Vec<u8>>,
2114+
change: Operation<K, V>,
20972115
options: &Options,
20982116
db_version: u32,
20992117
) -> Result<()> {

0 commit comments

Comments
 (0)