Skip to content

Commit

Permalink
column: put lowest ix in a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Oct 30, 2024
1 parent 252678e commit bd11f22
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ use crate::storage::{Storage, StorageMut};
use super::common::TryGetError;
use super::{BoundFor, BoundedIterableAccessor, IterableAccessor, NonTerminal, Storable};

/// The last index that has been pushed to the column.
/// This does not have to be the index of the last element as it is
/// not reset in case the last element is removed.
const META_LAST_IX: &[u8] = &[0];
const META_LEN: &[u8] = &[1];
/// The first (lowest) index that is pushed to the column.
const FIRST_INDEX: u32 = 1;

/// Storage keys for metadata.
mod meta_keys {
/// The last index that has been pushed to the column.
/// This does not have to be the index of the last element as it is
/// not reset in case the last element is removed.
pub const META_LAST_IX: &[u8] = &[0];
pub const META_LEN: &[u8] = &[1];
}

/// A collection of rows indexed by `u32` keys. This is somewhat similar to a traditional
/// database table with an auto-incrementing primary key.
Expand Down Expand Up @@ -257,7 +263,7 @@ where
// TODO: bounds check + error handlinge

self.storage
.get_meta(META_LEN)
.get_meta(meta_keys::META_LEN)
.map(|bytes| {
if bytes.len() != 4 {
Err(LenError::InconsistentState)
Expand Down Expand Up @@ -331,22 +337,24 @@ where

let ix = match self
.storage
.get_meta(META_LAST_IX)
.get_meta(meta_keys::META_LAST_IX)
.map(|bytes| u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
{
Some(last_ix) => last_ix.checked_add(1).ok_or(PushError::IndexOverflow)?,
None => 1,
None => FIRST_INDEX,
};

self.storage.set(&encode_ix(ix), &bytes);

self.storage.set_meta(META_LAST_IX, &(ix).to_be_bytes());
self.storage
.set_meta(meta_keys::META_LAST_IX, &(ix).to_be_bytes());
let len = self
.storage
.get_meta(META_LEN)
.get_meta(meta_keys::META_LEN)
.map(|bytes| u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
.unwrap_or(0);
self.storage.set_meta(META_LEN, &(len + 1).to_be_bytes());
self.storage
.set_meta(meta_keys::META_LEN, &(len + 1).to_be_bytes());

Ok(ix)
}
Expand Down Expand Up @@ -406,10 +414,11 @@ where

let len = self
.storage
.get_meta(META_LEN)
.get_meta(meta_keys::META_LEN)
.map(|bytes| u32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
.ok_or(RemoveError::InconsistentState)?;
self.storage.set_meta(META_LEN, &(len - 1).to_be_bytes());
self.storage
.set_meta(meta_keys::META_LEN, &(len - 1).to_be_bytes());

Ok(())
}
Expand Down

0 comments on commit bd11f22

Please sign in to comment.