Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaBatch and ChangeSet changes immutable sov_state::Storage migration #5

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ pub type SchemaValue = Vec<u8>;
/// Represents operation written to the database.
#[cfg_attr(feature = "arbitrary", derive(proptest_derive::Arbitrary))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
// TODO: Do we want "generic" operation, with 2 options: S::Value and SchemaValue?
citizen-stig marked this conversation as resolved.
Show resolved Hide resolved
pub enum Operation {
/// Writing a value to the DB.
Put {
Expand Down
18 changes: 16 additions & 2 deletions src/schema_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl SchemaBatch {
}

/// Adds an insert/update operation to the batch.
// TODO: made this pub(crate) ???
pub fn put<S: Schema>(
&mut self,
key: &impl KeyCodec<S>,
Expand All @@ -38,6 +39,7 @@ impl SchemaBatch {
}

/// Adds a delete operation to the batch.
// TODO: Make this pub(crate)
pub fn delete<S: Schema>(&mut self, key: &impl KeyCodec<S>) -> anyhow::Result<()> {
let key = key.encode_key()?;
self.insert_operation::<S>(key, Operation::Delete);
Expand All @@ -50,7 +52,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 +66,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 +333,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
Loading