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 3734360
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
96 changes: 48 additions & 48 deletions packages/storey/src/containers/column.rs
Original file line number Diff line number Diff line change
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 @@ -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 3734360

Please sign in to comment.