Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Nov 13, 2023
1 parent 1cee02c commit ef2f883
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 212 deletions.
2 changes: 1 addition & 1 deletion crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bench = false
[dependencies]
spacetimedb-lib = { path = "../lib" }
spacetimedb-core = { path = "../core" }
spacetimedb-sats= { path = "../sats" }
spacetimedb-sats = { path = "../sats" }
spacetimedb-standalone = { path = "../standalone" }
spacetimedb-client-api = { path = "../client-api" }
spacetimedb-testing = { path = "../testing" }
Expand Down
9 changes: 3 additions & 6 deletions crates/bindings-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,19 @@ mod module;
extern crate core;
extern crate proc_macro;

use std::collections::HashMap;
use std::time::Duration;

use bitflags::Flags;
use module::{derive_deserialize, derive_satstype, derive_serialize};
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, quote_spanned, TokenStreamExt};
use spacetimedb_primitives::ColumnAttribute;
use std::collections::HashMap;
use std::time::Duration;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{
parse_quote, BinOp, Expr, ExprBinary, ExprLit, ExprUnary, FnArg, Ident, ItemFn, ItemStruct, Member, Token, Type,
UnOp,
};
#[path = "../../sats/src/db/attr.rs"]
mod attr;
use attr::ColumnAttribute;

mod sym {
/// A symbol known at compile-time against
Expand Down
14 changes: 5 additions & 9 deletions crates/core/src/db/datastore/system_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::error::{DBError, TableError};
use core::fmt;
use nonempty::NonEmpty;
use once_cell::sync::Lazy;

use spacetimedb_primitives::*;
use spacetimedb_sats::db::auth::{StAccess, StTableType};
use spacetimedb_sats::db::def::*;
Expand Down Expand Up @@ -734,17 +733,14 @@ impl StIndexRow<&str> {
}

fn to_cols(row: &ProductValue, col_pos: ColId, col_name: &'static str) -> Result<NonEmpty<ColId>, DBError> {
let col_pos = col_pos.idx();
let cols = row.field_as_array(col_pos, Some(col_name))?;
let index = col_pos.idx();
let name = Some(col_name);
let cols = row.field_as_array(index, name)?;
if let ArrayValue::U32(x) = &cols {
let x: Vec<_> = x.iter().map(|x| ColId::from(*x)).collect();
Ok(NonEmpty::from_slice(&x).unwrap())
} else {
Err(InvalidFieldError {
name: Some(col_name),
col_pos: col_pos.into(),
}
.into())
Err(InvalidFieldError { name, index }.into())
}
}

Expand All @@ -756,7 +752,7 @@ impl<'a> TryFrom<&'a ProductValue> for StIndexRow<&'a str> {
let index_name = row.field_as_str(StIndexFields::IndexName.col_idx(), None)?;
let index_type = row.field_as_u8(StIndexFields::IndexType.col_idx(), None)?;
let index_type = IndexType::try_from(index_type).map_err(|_| InvalidFieldError {
col_pos: StIndexFields::IndexType.col_id(),
index: StIndexFields::IndexType.col_idx(),
name: Some(StIndexFields::IndexType.name()),
})?;
let columns = to_cols(row, StIndexFields::Columns.col_id(), StIndexFields::Columns.name())?;
Expand Down
24 changes: 11 additions & 13 deletions crates/core/src/db/relational_db.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
use fs2::FileExt;
use nonempty::NonEmpty;
use std::borrow::Cow;
use std::fs::{create_dir_all, File};
use std::ops::RangeBounds;
use std::path::Path;
use std::sync::{Arc, Mutex};

use super::commit_log::{CommitLog, CommitLogView};
use super::datastore::locking_tx_datastore::Locking;
use super::datastore::locking_tx_datastore::{DataRef, Iter, IterByColEq, IterByColRange, MutTxId, RowId};
use super::datastore::traits::{MutProgrammable, MutTx, MutTxDatastore, Programmable, TxData};
use super::message_log::MessageLog;
Expand All @@ -22,14 +15,18 @@ use crate::db::ostorage::ObjectDB;
use crate::error::{DBError, DatabaseError, IndexError, TableError};
use crate::execution_context::ExecutionContext;
use crate::hash::Hash;
use fs2::FileExt;
use nonempty::NonEmpty;
use spacetimedb_lib::PrimaryKey;
use spacetimedb_primitives::*;
use spacetimedb_primitives::{ColId, ColumnAttribute, IndexId, SequenceId, TableId};
use spacetimedb_sats::data_key::ToDataKey;
use spacetimedb_sats::db::attr::ColumnAttribute;
use spacetimedb_sats::db::def::*;
use spacetimedb_sats::db::def::{IndexDef, SequenceDef, TableDef, TableSchema};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue, ProductType, ProductValue};

use super::datastore::locking_tx_datastore::Locking;
use std::borrow::Cow;
use std::fs::{create_dir_all, File};
use std::ops::RangeBounds;
use std::path::Path;
use std::sync::{Arc, Mutex};

#[derive(Clone)]
pub struct RelationalDB {
Expand Down Expand Up @@ -674,6 +671,7 @@ mod tests {
use crate::db::relational_db::tests_utils::make_test_db;
use spacetimedb_lib::error::ResultTest;
use spacetimedb_sats::db::auth::{StAccess, StTableType};
use spacetimedb_sats::db::def::{ColumnDef, IndexType};
use spacetimedb_sats::product;

fn column(name: &str, ty: AlgebraicType) -> ColumnDef {
Expand Down
10 changes: 4 additions & 6 deletions crates/core/src/host/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::time::Duration;

use anyhow::Context;
use bytes::Bytes;
use bytestring::ByteString;
use derive_more::Display;
use enum_map::Enum;
use spacetimedb_lib::de::serde::SeedWrapper;
use spacetimedb_lib::de::DeserializeSeed;
use spacetimedb_lib::{bsatn, Identity};
use spacetimedb_lib::{ProductValue, ReducerDef};
use spacetimedb_lib::{bsatn, Identity, ProductValue, ReducerDef};
use spacetimedb_sats::WithTypespace;
use std::time::Duration;

mod host_controller;
pub(crate) mod module_host;
Expand All @@ -18,8 +18,6 @@ pub mod instance_env;
mod timestamp;
mod wasm_common;

use derive_more::Display;
use enum_map::Enum;
pub use host_controller::{
DescribedEntityType, EnergyDiff, EnergyQuanta, HostController, ReducerCallResult, ReducerOutcome, UpdateOutcome,
};
Expand Down
18 changes: 8 additions & 10 deletions crates/core/src/host/module_host.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Weak};
use std::time::Duration;

use base64::{engine::general_purpose::STANDARD as BASE_64_STD, Engine as _};
use futures::{Future, FutureExt};
use indexmap::IndexMap;
use tokio::sync::oneshot;

use super::host_controller::HostThreadpool;
use super::{ArgsTuple, EnergyDiff, InvalidReducerArguments, ReducerArgs, ReducerCallResult, ReducerId, Timestamp};
use crate::client::ClientConnectionSender;
Expand All @@ -26,10 +16,18 @@ use crate::util::lending_pool::{Closed, LendingPool, LentResource, PoolClosed, W
use crate::util::notify_once::NotifyOnce;
use crate::util::prometheus_handle::{GaugeInc, IntGaugeExt};
use crate::worker_metrics::WORKER_METRICS;
use base64::{engine::general_purpose::STANDARD as BASE_64_STD, Engine as _};
use futures::{Future, FutureExt};
use indexmap::IndexMap;
use spacetimedb_lib::{Address, ReducerDef, TableDef};
use spacetimedb_primitives::TableId;
use spacetimedb_sats::relation::MemTable;
use spacetimedb_sats::{ProductValue, Typespace, WithTypespace};
use std::collections::HashMap;
use std::fmt;
use std::sync::{Arc, Weak};
use std::time::Duration;
use tokio::sync::oneshot;

#[derive(Debug, Default, Clone)]
pub struct DatabaseUpdate {
Expand Down
9 changes: 4 additions & 5 deletions crates/core/src/vm.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
//! The [DbProgram] that execute arbitrary queries & code against the database.
use std::collections::HashMap;
use std::ops::RangeBounds;

use itertools::Itertools;
use tracing::debug;
use crate::db::cursor::{CatalogCursor, IndexCursor, TableCursor};
use crate::db::datastore::locking_tx_datastore::{IterByColEq, MutTxId};
use crate::db::relational_db::RelationalDB;
use crate::execution_context::ExecutionContext;
use itertools::Itertools;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_primitives::{ColId, TableId};
use spacetimedb_sats::db::auth::{StAccess, StTableType};
Expand All @@ -23,6 +19,9 @@ use spacetimedb_vm::eval::IterRows;
use spacetimedb_vm::expr::*;
use spacetimedb_vm::program::{ProgramRef, ProgramVm};
use spacetimedb_vm::rel_ops::RelOps;
use std::collections::HashMap;
use std::ops::RangeBounds;
use tracing::debug;

//TODO: This is partially duplicated from the `vm` crate to avoid borrow checker issues
//and pull all that crate in core. Will be revisited after trait refactor
Expand Down
3 changes: 1 addition & 2 deletions crates/lib/src/identity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::{fmt, str::FromStr};

use spacetimedb_bindings_macro::{Deserialize, Serialize};
use spacetimedb_sats::hex::HexString;
use spacetimedb_sats::{hash, impl_st, AlgebraicType};
use std::{fmt, str::FromStr};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AuthCtx {
Expand Down
17 changes: 6 additions & 11 deletions crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use spacetimedb_primitives::ColId;
use spacetimedb_sats::db::auth::{StAccess, StTableType};
use spacetimedb_sats::db::def::{ColumnDef, IndexDef, IndexType, AUTO_TABLE_ID};
use spacetimedb_sats::{impl_serialize, WithTypespace};
use std::iter;

pub mod address;
pub mod filter;
Expand Down Expand Up @@ -122,7 +121,7 @@ impl TableDef {

let index_for_column = table.indexes.iter().find(|index| {
// Ignore multi-column indexes
matches!(index.cols.split_first(), (index_col_id,[]) if index_col_id.idx() == col_id)
index.cols.tail.is_empty() && index.cols.head.idx() == col_id
});

// If there's an index defined for this column already, use it,
Expand Down Expand Up @@ -154,18 +153,14 @@ impl TableDef {

// Multi-column indexes cannot be unique (yet), so just add them.
let multi_col_indexes = table.indexes.iter().filter_map(|index| {
if let (a, [b, rest @ ..]) = index.cols.split_first() {
let idx = spacetimedb_sats::db::def::IndexDef::new_cols(
(index.cols.len() > 1).then(|| {
spacetimedb_sats::db::def::IndexDef::new_cols(
index.name.clone(),
AUTO_TABLE_ID,
false,
(*a, iter::once(*b).chain(rest.iter().copied()).collect()),
);

Some(idx)
} else {
None
}
index.cols.clone(),
)
})
});
indexes.extend(multi_col_indexes);

Expand Down
16 changes: 6 additions & 10 deletions crates/primitives/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum AttributeKind {
// and is distinct to `Constraints` in `sats/db/def.rs`
bitflags! {
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
pub struct ColumnIndexAttribute: u8 {
pub struct ColumnAttribute: u8 {
const UNSET = Self::empty().bits();
/// Index no unique
const INDEXED = 0b0001;
Expand All @@ -43,31 +43,27 @@ bitflags! {
}
}

impl TryFrom<u8> for ColumnIndexAttribute {
impl TryFrom<u8> for ColumnAttribute {
type Error = ();

fn try_from(v: u8) -> Result<Self, Self::Error> {
Self::from_bits(v).ok_or(())
}
}

impl ColumnIndexAttribute {
impl ColumnAttribute {
/// Checks if either 'IDENTITY' or 'PRIMARY_KEY_AUTO' constraints are set because the imply the use of
/// auto increment sequence.
pub const fn has_autoinc(&self) -> bool {
self.contains(ColumnIndexAttribute::IDENTITY)
|| self.contains(ColumnIndexAttribute::PRIMARY_KEY_AUTO)
|| self.contains(ColumnIndexAttribute::AUTO_INC)
self.contains(Self::IDENTITY) || self.contains(Self::PRIMARY_KEY_AUTO) || self.contains(Self::AUTO_INC)
}

pub const fn has_unique(&self) -> bool {
self.contains(ColumnIndexAttribute::UNIQUE)
self.contains(Self::UNIQUE)
}

pub const fn has_primary(&self) -> bool {
self.contains(ColumnIndexAttribute::IDENTITY)
|| self.contains(ColumnIndexAttribute::PRIMARY_KEY)
|| self.contains(ColumnIndexAttribute::PRIMARY_KEY_AUTO)
self.contains(Self::IDENTITY) || self.contains(Self::PRIMARY_KEY) || self.contains(Self::PRIMARY_KEY_AUTO)
}

/// Returns the [ColumnIndexAttribute] of constraints as an enum variant.
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
mod attr;
mod ids;

pub use attr::{AttributeKind, ColumnIndexAttribute};
pub use attr::{AttributeKind, ColumnAttribute};
pub use ids::{ColId, ConstraintId, IndexId, SequenceId, TableId};
Loading

0 comments on commit ef2f883

Please sign in to comment.