Skip to content

Commit fddc59d

Browse files
authored
Merge branch 'master' into jdetter/test-pr
2 parents 6ebb737 + 7475969 commit fddc59d

11 files changed

Lines changed: 466 additions & 306 deletions

File tree

crates/bindings-typescript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
"name": "esm (gzip)",
115115
"path": "dist/index.mjs",
116116
"gzip": true,
117-
"limit": "40 kB"
117+
"limit": "48 kB"
118118
},
119119
{
120120
"name": "esm (uncompressed)",

crates/table/src/table.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ impl TableInner {
179179
// during a table scan or index seek.
180180
// As such, our `delete` and `insert` methods can be `unsafe`
181181
// and trust that the `RowPointer` is valid.
182-
fn is_row_present(&self, _squashed_offset: SquashedOffset, ptr: RowPointer) -> bool {
183-
if _squashed_offset != ptr.squashed_offset() {
182+
fn is_row_present(&self, squashed_offset: SquashedOffset, ptr: RowPointer) -> bool {
183+
if squashed_offset != ptr.squashed_offset() {
184184
return false;
185185
}
186186
let Some((page, offset)) = self.try_page_and_offset(ptr) else {
@@ -2334,13 +2334,7 @@ impl Table {
23342334
// As such, our `delete` and `insert` methods can be `unsafe`
23352335
// and trust that the `RowPointer` is valid.
23362336
fn is_row_present(&self, ptr: RowPointer) -> bool {
2337-
if self.squashed_offset != ptr.squashed_offset() {
2338-
return false;
2339-
}
2340-
let Some((page, offset)) = self.inner.try_page_and_offset(ptr) else {
2341-
return false;
2342-
};
2343-
page.has_row_offset(self.row_size(), offset)
2337+
self.inner.is_row_present(self.squashed_offset, ptr)
23442338
}
23452339

23462340
/// Returns the row size for a row in the table.
@@ -2542,8 +2536,7 @@ pub(crate) mod test {
25422536
let index = table.get_index_by_id(index_id).unwrap();
25432537

25442538
index
2545-
.seek_range(&(..))
2546-
.unwrap()
2539+
.iter()
25472540
.map(|row_ptr| {
25482541
let row_ref = table.get_row_ref(blob_store, row_ptr).unwrap();
25492542
let key = row_ref.project(&index.indexed_columns).unwrap();

crates/table/src/table_index/btree_index.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::same_key_entry::{same_key_iter, SameKeyEntry, SameKeyEntryIter};
22
use super::{key_size::KeyBytesStorage, Index, KeySize, RangedIndex};
33
use crate::indexes::RowPointer;
4+
use crate::table_index::same_key_entry::ManySameKeyEntryIter;
45
use core::borrow::Borrow;
56
use core::ops::RangeBounds;
67
use spacetimedb_sats::memory_usage::MemoryUsage;
7-
use std::collections::btree_map::{BTreeMap, Range};
8+
use std::collections::btree_map::{BTreeMap, Range, Values};
89

910
/// A multi map that relates a `K` to a *set* of `RowPointer`s.
1011
#[derive(Debug, PartialEq, Eq)]
@@ -78,6 +79,15 @@ impl<K: Ord + KeySize> Index for BTreeIndex<K> {
7879
self.seek_point(point)
7980
}
8081

82+
type Iter<'a>
83+
= BTreeIndexIter<'a, K>
84+
where
85+
Self: 'a;
86+
87+
fn iter(&self) -> Self::Iter<'_> {
88+
BTreeIndexIter::new(self.map.values())
89+
}
90+
8191
fn num_keys(&self) -> usize {
8292
self.map.len()
8393
}
@@ -182,35 +192,29 @@ impl<K: KeySize + Ord> BTreeIndex<K> {
182192
where
183193
<Self as Index>::Key: Borrow<Q>,
184194
{
185-
BTreeIndexRangeIter {
186-
outer: self.map.range((range.start_bound(), range.end_bound())),
187-
inner: SameKeyEntry::empty_iter(),
188-
}
195+
BTreeIndexRangeIter::new(RangeValues(self.map.range((range.start_bound(), range.end_bound()))))
189196
}
190197
}
191198

199+
/// An iterator over all the values in a [`BTreeIndex`].
200+
pub type BTreeIndexIter<'a, K> = ManySameKeyEntryIter<'a, Values<'a, K, SameKeyEntry>>;
201+
192202
/// An iterator over values in a [`BTreeIndex`] where the keys are in a certain range.
193-
#[derive(Clone)]
194-
pub struct BTreeIndexRangeIter<'a, K> {
195-
/// The outer iterator seeking for matching keys in the range.
196-
outer: Range<'a, K, SameKeyEntry>,
197-
/// The inner iterator for the value set for a found key.
198-
inner: SameKeyEntryIter<'a>,
203+
pub type BTreeIndexRangeIter<'a, K> = ManySameKeyEntryIter<'a, RangeValues<'a, K, SameKeyEntry>>;
204+
205+
/// An iterator over a key range in a [`BTreeMap`] providing only the values.
206+
pub struct RangeValues<'a, K, V>(Range<'a, K, V>);
207+
208+
impl<K, V> Clone for RangeValues<'_, K, V> {
209+
fn clone(&self) -> Self {
210+
Self(self.0.clone())
211+
}
199212
}
200213

201-
impl<K> Iterator for BTreeIndexRangeIter<'_, K> {
202-
type Item = RowPointer;
214+
impl<'a, K, V> Iterator for RangeValues<'a, K, V> {
215+
type Item = &'a V;
203216

204217
fn next(&mut self) -> Option<Self::Item> {
205-
loop {
206-
// While the inner iterator has elements, yield them.
207-
if let Some(val) = self.inner.next() {
208-
return Some(val);
209-
}
210-
// Advance and get a new inner, if possible, or quit.
211-
// We'll come back and yield elements from it in the next iteration.
212-
let inner = self.outer.next().map(|(_, i)| i)?;
213-
self.inner = inner.iter();
214-
}
218+
self.0.next().map(|(_, v)| v)
215219
}
216220
}

crates/table/src/table_index/hash_index.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::{
22
key_size::KeyBytesStorage,
3-
same_key_entry::{same_key_iter, SameKeyEntry, SameKeyEntryIter},
3+
same_key_entry::{same_key_iter, ManySameKeyEntryIter, SameKeyEntry, SameKeyEntryIter},
44
Index, KeySize,
55
};
66
use crate::indexes::RowPointer;
77
use core::borrow::Borrow;
88
use core::hash::Hash;
9-
use spacetimedb_data_structures::map::hash_map::EntryRef;
9+
use spacetimedb_data_structures::map::hash_map::{EntryRef, Values};
1010
use spacetimedb_sats::memory_usage::MemoryUsage;
1111

1212
// Faster than ahash, so we use this explicitly.
@@ -85,6 +85,15 @@ impl<K: KeySize + Eq + Hash> Index for HashIndex<K> {
8585
self.seek_point(point)
8686
}
8787

88+
type Iter<'a>
89+
= HashIndexIter<'a, K>
90+
where
91+
Self: 'a;
92+
93+
fn iter(&self) -> Self::Iter<'_> {
94+
HashIndexIter::new(self.map.values())
95+
}
96+
8897
fn num_keys(&self) -> usize {
8998
self.map.len()
9099
}
@@ -156,3 +165,6 @@ impl<K: KeySize + Eq + Hash> HashIndex<K> {
156165
same_key_iter(self.map.get(point))
157166
}
158167
}
168+
169+
/// An iterator over all the values in a [`BTreeIndex`].
170+
pub type HashIndexIter<'a, K> = ManySameKeyEntryIter<'a, Values<'a, K, SameKeyEntry>>;

crates/table/src/table_index/index.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ pub trait Index {
123123
///
124124
/// If the index is unique, this will at most return one element.
125125
fn seek_point(&self, point: &Self::Key) -> Self::PointIter<'_>;
126+
127+
type Iter<'a>: Iterator<Item = RowPointer>
128+
where
129+
Self: 'a;
130+
131+
/// Returns an iterator over all entries in this index.
132+
///
133+
/// The trait imposes no particular order.
134+
/// Implementations may provide a non-deterministic order.
135+
fn iter(&self) -> Self::Iter<'_>;
126136
}
127137

128138
pub trait RangedIndex: Index {

0 commit comments

Comments
 (0)