Skip to content
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
3 changes: 2 additions & 1 deletion merk/src/element/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ mod tests {
.unwrap()
.unwrap();

let batch2 = StorageBatch::new();
let ctx = storage
.get_transactional_storage_context(SubtreePath::empty(), None, &transaction)
.get_transactional_storage_context(SubtreePath::empty(), Some(&batch2), &transaction)
.unwrap();
let mut merk = Merk::open_base(
ctx,
Expand Down
21 changes: 18 additions & 3 deletions merk/src/merk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,10 +1291,15 @@ mod test {
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
.expect("cannot open rocksdb storage");
let transaction = storage.start_transaction();
let storage_batch = StorageBatch::new();

let mut merk = Merk::open_base(
storage
.get_transactional_storage_context(SubtreePath::empty(), None, &transaction)
.get_transactional_storage_context(
SubtreePath::empty(),
Some(&storage_batch),
&transaction,
)
.unwrap(),
TreeType::NormalTree,
None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
Expand All @@ -1319,10 +1324,15 @@ mod test {
let storage = RocksDbStorage::default_rocksdb_with_path(tmp_dir.path())
.expect("cannot open rocksdb storage");
let transaction = storage.start_transaction();
let storage_batch = StorageBatch::new();

let mut merk = Merk::open_base(
storage
.get_transactional_storage_context(SubtreePath::empty(), None, &transaction)
.get_transactional_storage_context(
SubtreePath::empty(),
Some(&storage_batch),
&transaction,
)
.unwrap(),
TreeType::NormalTree,
None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
Expand Down Expand Up @@ -1605,9 +1615,14 @@ mod test {
.unwrap()
.expect("cannot commit batch");

let batch2 = StorageBatch::new();
let mut merk = Merk::open_base(
storage
.get_transactional_storage_context(SubtreePath::empty(), None, &transaction)
.get_transactional_storage_context(
SubtreePath::empty(),
Some(&batch2),
&transaction,
)
.unwrap(),
TreeType::NormalTree,
None::<&fn(&[u8], &GroveVersion) -> Option<ValueDefinedCostType>>,
Expand Down
164 changes: 115 additions & 49 deletions storage/src/rocksdb_storage/storage_context/context_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,21 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
children_sizes: ChildrenSizesWithIsSumTree,
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.put(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
children_sizes,
cost_info,
);
}
let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted put operation on transactional context without a batch".to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.put(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
children_sizes,
cost_info,
);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -141,13 +148,21 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
value: &[u8],
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.put_aux(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
}
let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted put_aux operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.put_aux(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -157,13 +172,21 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
value: &[u8],
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.put_root(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
}
let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted put_root operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.put_root(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -173,13 +196,21 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
value: &[u8],
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.put_meta(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
}
let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted put_meta operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.put_meta(
make_prefixed_key(&self.prefix, key),
value.to_vec(),
cost_info,
);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -188,10 +219,17 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
key: K,
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.delete(make_prefixed_key(&self.prefix, key), cost_info);
}

let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted delete operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.delete(make_prefixed_key(&self.prefix, key), cost_info);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -200,10 +238,17 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
key: K,
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.delete_aux(make_prefixed_key(&self.prefix, key), cost_info);
}

let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted delete_aux operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.delete_aux(make_prefixed_key(&self.prefix, key), cost_info);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -212,10 +257,17 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
key: K,
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.delete_root(make_prefixed_key(&self.prefix, key), cost_info);
}

let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted delete_root operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.delete_root(make_prefixed_key(&self.prefix, key), cost_info);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand All @@ -224,10 +276,17 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
key: K,
cost_info: Option<KeyValueStorageCost>,
) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.delete_meta(make_prefixed_key(&self.prefix, key), cost_info);
}

let batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted delete_meta operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
batch.delete_meta(make_prefixed_key(&self.prefix, key), cost_info);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand Down Expand Up @@ -303,10 +362,17 @@ impl<'db> StorageContext<'db> for PrefixedRocksDbTransactionContext<'db> {
}

fn commit_batch(&self, batch: Self::Batch) -> CostResult<(), Error> {
if let Some(existing_batch) = self.batch {
existing_batch.merge(batch.batch);
}

let existing_batch = match self.batch {
Some(b) => b,
None => {
return Err(Error::StorageError(
"attempted commit_batch operation on transactional context without a batch"
.to_string(),
))
.wrap_with_cost(OperationCost::default())
}
};
existing_batch.merge(batch.batch);
Ok(()).wrap_with_cost(OperationCost::default())
}

Expand Down
Loading
Loading