Skip to content

Commit

Permalink
SchemaBatch and ChangeSet changes immutable sov_state::Storage
Browse files Browse the repository at this point in the history
…migration (#5)

* Temporary constructor for the ChangeSet while migration

* Public "get" for schema batch
  • Loading branch information
citizen-stig authored May 14, 2024
1 parent b224c28 commit 11c7f74
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/cache/change_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ impl ChangeSet {
}
}

/// Create new `ChangeSet
pub fn new_with_operations(id: SnapshotId, operations: SchemaBatch) -> Self {
Self { id, operations }
}

/// Get value from its own cache
pub fn get<S: Schema>(&self, key: &impl KeyCodec<S>) -> anyhow::Result<Option<&Operation>> {
self.operations.get(key)
self.operations.get_operation(key)
}

/// Get the ID of this [`ChangeSet`].
Expand Down
16 changes: 14 additions & 2 deletions src/schema_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ impl SchemaBatch {
column_writes.insert(key, operation);
}

pub(crate) fn get<S: Schema>(
/// Getting the operation from current schema batch if present
pub(crate) fn get_operation<S: Schema>(
&self,
key: &impl KeyCodec<S>,
) -> anyhow::Result<Option<&Operation>> {
Expand All @@ -63,6 +64,17 @@ impl SchemaBatch {
}
}

/// Getting value by key if it was written in this batch.
/// Deleted operation will return None as well as missing key
pub fn get_value<S: Schema>(&self, key: &impl KeyCodec<S>) -> anyhow::Result<Option<S::Value>> {
let operation = self.get_operation(key)?;
if let Some(operation) = operation {
let value = operation.decode_value::<S>()?;
return Ok(value);
}
Ok(None)
}

/// Iterator over all values in lexicographic order.
pub fn iter<S: Schema>(&self) -> btree_map::Iter<SchemaKey, Operation> {
self.last_writes
Expand Down Expand Up @@ -319,7 +331,7 @@ mod tests {

let get_value = |field: &TestField| -> Option<TestField> {
batch1
.get::<TestSchema1>(field)
.get_operation::<TestSchema1>(field)
.unwrap()
.unwrap()
.decode_value::<TestSchema1>()
Expand Down

0 comments on commit 11c7f74

Please sign in to comment.