Skip to content

Commit

Permalink
fix!: start column ids with 1
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Oct 23, 2024
1 parent 1b621ae commit 252678e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
126 changes: 63 additions & 63 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ const META_LEN: &[u8] = &[1];
/// access.push(&1337).unwrap();
/// access.push(&42).unwrap();
///
/// assert_eq!(access.get(0).unwrap(), Some(1337));
/// assert_eq!(access.get(1).unwrap(), Some(42));
/// assert_eq!(access.get(2).unwrap(), None);
/// assert_eq!(access.get(1).unwrap(), Some(1337));
/// assert_eq!(access.get(2).unwrap(), Some(42));
/// assert_eq!(access.get(3).unwrap(), None);
/// ```
pub struct Column<T, E> {
prefix: u8,
Expand Down Expand Up @@ -175,8 +175,8 @@ where
/// let mut access = column.access(&mut storage);
///
/// access.push(&1337).unwrap();
/// assert_eq!(access.get(0).unwrap(), Some(1337));
/// assert_eq!(access.get(1).unwrap(), None);
/// assert_eq!(access.get(1).unwrap(), Some(1337));
/// assert_eq!(access.get(2).unwrap(), None);
/// ```
pub fn get(&self, key: u32) -> Result<Option<T>, E::DecodeError> {
self.storage
Expand Down Expand Up @@ -205,8 +205,8 @@ where
/// let mut access = column.access(&mut storage);
///
/// access.push(&1337).unwrap();
/// assert_eq!(access.try_get(0).unwrap(), 1337);
/// assert!(access.try_get(1).is_err());
/// assert_eq!(access.try_get(1).unwrap(), 1337);
/// assert!(access.try_get(2).is_err());
/// ```
pub fn try_get(&self, key: u32) -> Result<T, TryGetError<E::DecodeError>> {
self.get(key)?.ok_or(TryGetError::Empty)
Expand All @@ -226,9 +226,9 @@ where
/// let column = Column::<u64, TestEncoding>::new(0);
/// let mut access = column.access(&mut storage);
///
/// assert_eq!(access.get_or(0, 42).unwrap(), 42);
/// assert_eq!(access.get_or(1, 42).unwrap(), 42);
/// access.push(&1337).unwrap();
/// assert_eq!(access.get_or(0, 42).unwrap(), 1337);
/// assert_eq!(access.get_or(1, 42).unwrap(), 1337);
/// ```
pub fn get_or(&self, key: u32, default: T) -> Result<T, E::DecodeError> {
self.get(key).map(|value| value.unwrap_or(default))
Expand Down Expand Up @@ -335,7 +335,7 @@ where
.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 => 0,
None => 1,
};

self.storage.set(&encode_ix(ix), &bytes);
Expand Down Expand Up @@ -364,10 +364,10 @@ where
/// let mut access = column.access(&mut storage);
///
/// access.push(&1337).unwrap();
/// assert_eq!(access.get(0).unwrap(), Some(1337));
/// assert_eq!(access.get(1).unwrap(), Some(1337));
///
/// access.update(0, &9001).unwrap();
/// assert_eq!(access.get(0).unwrap(), Some(9001));
/// access.update(1, &9001).unwrap();
/// assert_eq!(access.get(1).unwrap(), Some(9001));
/// ```
pub fn update(&mut self, key: u32, value: &T) -> Result<(), UpdateError<E::EncodeError>> {
self.storage
Expand Down Expand Up @@ -396,10 +396,10 @@ where
/// let mut access = column.access(&mut storage);
///
/// access.push(&1337).unwrap();
/// assert_eq!(access.get(0).unwrap(), Some(1337));
/// assert_eq!(access.get(1).unwrap(), Some(1337));
///
/// access.remove(0).unwrap();
/// assert_eq!(access.get(0).unwrap(), None);
/// access.remove(1).unwrap();
/// assert_eq!(access.get(1).unwrap(), None);
/// ```
pub fn remove(&mut self, key: u32) -> Result<(), RemoveError> {
self.storage.remove(&encode_ix(key));
Expand Down Expand Up @@ -471,20 +471,20 @@ mod tests {
let column = Column::<u64, TestEncoding>::new(0);
let mut access = column.access(&mut storage);

assert_eq!(access.push(&1337).unwrap(), 0);
assert_eq!(access.push(&42).unwrap(), 1);
assert_eq!(access.push(&1337).unwrap(), 1);
assert_eq!(access.push(&42).unwrap(), 2);

assert_eq!(access.get(0).unwrap(), Some(1337));
assert_eq!(access.get(1).unwrap(), Some(42));
assert_eq!(access.get(2).unwrap(), None);
assert_eq!(access.get(1).unwrap(), Some(1337));
assert_eq!(access.get(2).unwrap(), Some(42));
assert_eq!(access.get(3).unwrap(), None);
assert_eq!(access.len().unwrap(), 2);

access.remove(0).unwrap();
assert_eq!(access.update(0, &9001), Err(UpdateError::NotFound));
access.update(1, &9001).unwrap();
access.remove(1).unwrap();
assert_eq!(access.update(1, &9001), Err(UpdateError::NotFound));
access.update(2, &9001).unwrap();

assert_eq!(access.get(0).unwrap(), None);
assert_eq!(access.get(1).unwrap(), Some(9001));
assert_eq!(access.get(1).unwrap(), None);
assert_eq!(access.get(2).unwrap(), Some(9001));
assert_eq!(access.len().unwrap(), 1);
}

Expand All @@ -495,26 +495,26 @@ mod tests {
let column = Column::<u64, TestEncoding>::new(0);
let mut access = column.access(&mut storage);

assert_eq!(access.push(&1337).unwrap(), 0);
assert_eq!(access.push(&42).unwrap(), 1);
assert_eq!(access.push(&17).unwrap(), 2);
assert_eq!(access.push(&1337).unwrap(), 1);
assert_eq!(access.push(&42).unwrap(), 2);
assert_eq!(access.push(&17).unwrap(), 3);
assert_eq!(access.len().unwrap(), 3);

// remove middle
access.remove(1).unwrap();
access.remove(2).unwrap();
assert_eq!(access.len().unwrap(), 2);

// remove first
access.remove(0).unwrap();
access.remove(10).unwrap();
assert_eq!(access.len().unwrap(), 1);

// remove last
access.remove(2).unwrap();
access.remove(3).unwrap();
assert_eq!(access.len().unwrap(), 0);

// Above removals do not reset the auto-incrementor,
// such that we get a fresh key for the next push.
assert_eq!(access.push(&99).unwrap(), 3);
assert_eq!(access.push(&99).unwrap(), 4);
assert_eq!(access.len().unwrap(), 1);
}

Expand All @@ -528,16 +528,16 @@ mod tests {
access.push(&1337).unwrap();
access.push(&42).unwrap();
access.push(&9001).unwrap();
access.remove(1).unwrap();
access.remove(2).unwrap();

assert_eq!(
access.pairs().collect::<Result<Vec<_>, _>>().unwrap(),
vec![(0, 1337), (2, 9001)]
vec![(1, 1337), (3, 9001)]
);

assert_eq!(
access.keys().collect::<Result<Vec<_>, _>>().unwrap(),
vec![0, 2]
vec![1, 3]
);

assert_eq!(
Expand All @@ -556,16 +556,16 @@ mod tests {
access.push(&1337).unwrap();
access.push(&42).unwrap();
access.push(&9001).unwrap();
access.remove(1).unwrap();
access.remove(2).unwrap();

assert_eq!(
access.rev_pairs().collect::<Result<Vec<_>, _>>().unwrap(),
vec![(2, 9001), (0, 1337)]
vec![(3, 9001), (1, 1337)]
);

assert_eq!(
access.rev_keys().collect::<Result<Vec<_>, _>>().unwrap(),
vec![2, 0]
vec![3, 1]
);

assert_eq!(
Expand All @@ -586,26 +586,26 @@ mod tests {
access.push(&9001).unwrap();
access.push(&1).unwrap();
access.push(&2).unwrap();
access.remove(2).unwrap();
access.remove(3).unwrap();

// start and end set
assert_eq!(
access
.bounded_pairs(Some(1), Some(4))
.bounded_pairs(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(1, 42), (3, 1)]
vec![(2, 42), (4, 1)]
);
assert_eq!(
access
.bounded_keys(Some(1), Some(4))
.bounded_keys(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1, 3]
vec![2, 4]
);
assert_eq!(
access
.bounded_values(Some(1), Some(4))
.bounded_values(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![42, 1]
Expand All @@ -614,21 +614,21 @@ mod tests {
// end unset
assert_eq!(
access
.bounded_pairs(Some(1), None)
.bounded_pairs(Some(2), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(1, 42), (3, 1), (4, 2)]
vec![(2, 42), (4, 1), (5, 2)]
);
assert_eq!(
access
.bounded_keys(Some(1), None)
.bounded_keys(Some(2), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1, 3, 4]
vec![2, 4, 5]
);
assert_eq!(
access
.bounded_values(Some(1), None)
.bounded_values(Some(2), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![42, 1, 2]
Expand All @@ -637,21 +637,21 @@ mod tests {
// start unset
assert_eq!(
access
.bounded_pairs(None, Some(4))
.bounded_pairs(None, Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(0, 1337), (1, 42), (3, 1)]
vec![(1, 1337), (2, 42), (4, 1)]
);
assert_eq!(
access
.bounded_keys(None, Some(4))
.bounded_keys(None, Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![0, 1, 3]
vec![1, 2, 4]
);
assert_eq!(
access
.bounded_values(None, Some(4))
.bounded_values(None, Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1337, 42, 1]
Expand All @@ -670,26 +670,26 @@ mod tests {
access.push(&9001).unwrap();
access.push(&1).unwrap();
access.push(&2).unwrap();
access.remove(2).unwrap();
access.remove(3).unwrap();

// start and end set
assert_eq!(
access
.bounded_rev_pairs(Some(1), Some(4))
.bounded_rev_pairs(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(3, 1), (1, 42)]
vec![(4, 1), (2, 42)]
);
assert_eq!(
access
.bounded_rev_keys(Some(1), Some(4))
.bounded_rev_keys(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![3, 1]
vec![4, 2]
);
assert_eq!(
access
.bounded_rev_values(Some(1), Some(4))
.bounded_rev_values(Some(2), Some(5))
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![1, 42]
Expand All @@ -698,10 +698,10 @@ mod tests {
// end unset
assert_eq!(
access
.bounded_rev_pairs(Some(1), None)
.bounded_rev_pairs(Some(2), None)
.collect::<Result<Vec<_>, _>>()
.unwrap(),
vec![(4, 2), (3, 1), (1, 42)]
vec![(5, 2), (4, 1), (2, 42)]
);
}
}
14 changes: 7 additions & 7 deletions packages/storey/tests/composition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,21 @@ fn map_of_column() {
access.entry_mut("foo").push(&42).unwrap();
access.entry_mut("bar").push(&9001).unwrap();

assert_eq!(access.entry("foo").get(0).unwrap(), Some(1337));
assert_eq!(access.entry("foo").get(1).unwrap(), Some(42));
assert_eq!(access.entry("foo").get(2).unwrap(), None);
assert_eq!(access.entry("foo").get(1).unwrap(), Some(1337));
assert_eq!(access.entry("foo").get(2).unwrap(), Some(42));
assert_eq!(access.entry("foo").get(3).unwrap(), None);
assert_eq!(access.entry("foo").len().unwrap(), 2);

assert_eq!(access.entry("bar").get(0).unwrap(), Some(9001));
assert_eq!(access.entry("bar").get(1).unwrap(), Some(9001));
assert_eq!(access.entry("bar").len().unwrap(), 1);

let all = access.pairs().collect::<Result<Vec<_>, _>>().unwrap();
assert_eq!(
all,
vec![
(("bar".to_string(), 0), 9001),
(("foo".to_string(), 0), 1337),
(("foo".to_string(), 1), 42)
(("bar".to_string(), 1), 9001),
(("foo".to_string(), 1), 1337),
(("foo".to_string(), 2), 42)
]
);
}

0 comments on commit 252678e

Please sign in to comment.