Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Privatize Table::row_layout + related BTreeIndex refactoring #1262

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use spacetimedb_sats::{
};
use spacetimedb_table::{
blob_store::{BlobStore, HashMapBlobStore},
btree_index::BTreeIndex,
indexes::{RowPointer, SquashedOffset},
table::{IndexScanIter, InsertError, RowRef, Table},
};
Expand Down Expand Up @@ -302,14 +301,8 @@ impl CommittedState {
let Some((table, blob_store)) = self.get_table_and_blob_store(index_row.table_id) else {
panic!("Cannot create index for table which doesn't exist in committed state");
};
let mut index = BTreeIndex::new(
index_row.index_id,
table.row_layout(),
&index_row.columns,
index_row.is_unique,
)?;
index.build_from_rows(&index_row.columns, table.scan_rows(blob_store))?;
table.indexes.insert(index_row.columns, index);
let index = table.new_index(index_row.index_id, &index_row.columns, index_row.is_unique)?;
table.insert_index(blob_store, index_row.columns, index);
}
Ok(())
}
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/db/datastore/locking_tx_datastore/mut_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ use spacetimedb_sats::{
AlgebraicValue, ProductType, ProductValue,
};
use spacetimedb_table::{
btree_index::BTreeIndex,
indexes::{RowPointer, SquashedOffset},
table::{InsertError, RowRef, Table},
};
Expand Down Expand Up @@ -325,7 +324,7 @@ impl MutTxId {
self.tx_state.get_table_and_blob_store(table_id).unwrap()
};

let mut insert_index = BTreeIndex::new(index.index_id, table.row_layout(), &index.columns, index.is_unique)?;
let mut insert_index = table.new_index(index.index_id, &index.columns, index.is_unique)?;
insert_index.build_from_rows(&index.columns, table.scan_rows(blob_store))?;

// NOTE: Also add all the rows in the already committed table to the index.
Expand Down
3 changes: 1 addition & 2 deletions crates/table/benches/page_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use spacetimedb_primitives::{ColList, IndexId, TableId};
use spacetimedb_sats::db::def::{TableDef, TableSchema};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};
use spacetimedb_table::blob_store::NullBlobStore;
use spacetimedb_table::btree_index::BTreeIndex;
use spacetimedb_table::indexes::Byte;
use spacetimedb_table::indexes::{Bytes, PageOffset, RowPointer, Size, SquashedOffset, PAGE_DATA_SIZE};
use spacetimedb_table::layout::{row_size_for_bytes, row_size_for_type};
Expand Down Expand Up @@ -693,7 +692,7 @@ fn make_table_with_indexes<R: IndexedRow>() -> Table {
let mut tbl = Table::new(schema.into(), SquashedOffset::COMMITTED_STATE);

let cols = R::indexed_columns();
let idx = BTreeIndex::new(IndexId(0), &R::row_type().into(), &cols, false).unwrap();
let idx = tbl.new_index(IndexId(0), &cols, false).unwrap();
tbl.insert_index(&NullBlobStore, cols, idx);

tbl
Expand Down
6 changes: 3 additions & 3 deletions crates/table/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

pub mod bflatn_from;
pub mod bflatn_to;
pub mod bflatn_to_bsatn_fast_path;
mod bflatn_to_bsatn_fast_path;
pub mod blob_store;
pub mod btree_index;
mod btree_index;
pub mod eq;
pub mod eq_to_pv;
mod eq_to_pv;
mod fixed_bit_set;
pub mod indexes;
pub mod layout;
Expand Down
29 changes: 16 additions & 13 deletions crates/table/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::hash::{Hash, Hasher};
use core::ops::RangeBounds;
use core::{fmt, ptr};
use spacetimedb_data_structures::map::HashMap;
use spacetimedb_primitives::{ColId, ColList};
use spacetimedb_primitives::{ColId, ColList, IndexId};
use spacetimedb_sats::{
algebraic_value::ser::ValueSerializer,
bsatn::{self, ser::BsatnError},
Expand Down Expand Up @@ -56,12 +56,6 @@ pub struct Table {
pub row_count: u64,
}

impl Table {
pub fn row_layout(&self) -> &RowTypeLayout {
&self.inner.row_layout
}
}

/// The part of a `Table` concerned only with storing rows.
///
/// Separated from the "outer" parts of `Table`, especially the `indexes`,
Expand Down Expand Up @@ -517,8 +511,15 @@ impl Table {
self.schema = schema;
}

/// Returns a new [`BTreeIndex`] for `table`.
pub fn new_index(&self, id: IndexId, cols: &ColList, is_unique: bool) -> Result<BTreeIndex, InvalidFieldError> {
BTreeIndex::new(id, self.row_layout(), cols, is_unique)
}

/// Inserts a new `index` into the table.
///
/// The index will be populated using the rows of the table.
/// Panics if `cols` has some column that is out of bounds of the table's row layout.
pub fn insert_index(&mut self, blob_store: &dyn BlobStore, cols: ColList, mut index: BTreeIndex) {
index.build_from_rows(&cols, self.scan_rows(blob_store)).unwrap();
self.indexes.insert(cols, index);
Expand Down Expand Up @@ -570,11 +571,8 @@ impl Table {
for (cols, index) in self.indexes.iter() {
// `new` is known to be empty (we just constructed it!),
// so no need for an actual blob store here.
new.insert_index(
&NullBlobStore,
cols.clone(),
BTreeIndex::new(index.index_id, &self.inner.row_layout, cols, index.is_unique).unwrap(),
);
let index = new.new_index(index.index_id, cols, index.is_unique).unwrap();
new.insert_index(&NullBlobStore, cols.clone(), index);
}
new
}
Expand Down Expand Up @@ -1003,6 +1001,11 @@ impl Table {
self.inner.row_layout.size()
}

/// Returns the layout for a row in the table.
fn row_layout(&self) -> &RowTypeLayout {
&self.inner.row_layout
}

/// Returns the pages storing the physical rows of this table.
fn pages(&self) -> &Pages {
&self.inner.pages
Expand Down Expand Up @@ -1056,7 +1059,7 @@ pub(crate) mod test {
let mut table = Table::new(schema.into(), SquashedOffset::COMMITTED_STATE);
let cols = ColList::new(0.into());

let index = BTreeIndex::new(index_schema.index_id, &table.inner.row_layout, &cols, true).unwrap();
let index = table.new_index(index_schema.index_id, &cols, true).unwrap();
table.insert_index(&NullBlobStore, cols, index);

// Reserve a page so that we can check the hash.
Expand Down
Loading