Skip to content

Commit

Permalink
get_value
Browse files Browse the repository at this point in the history
  • Loading branch information
citizen-stig committed May 8, 2024
1 parent 62ddb01 commit 6febf63
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cache/change_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ChangeSet {

/// 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?
pub enum Operation {
/// Writing a value to the DB.
Put {
Expand Down
20 changes: 17 additions & 3 deletions src/schema_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ impl SchemaBatch {
column_writes.insert(key, operation);
}

/// Getting the
pub fn get<S: Schema>(&self, key: &impl KeyCodec<S>) -> anyhow::Result<Option<&Operation>> {
/// 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>> {
let key = key.encode_key()?;

if let Some(column_writes) = self.last_writes.get(&S::COLUMN_FAMILY_NAME) {
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

0 comments on commit 6febf63

Please sign in to comment.