Skip to content

Commit

Permalink
A little package cleanup. Moved slotbox and friends into 'tuples' mod…
Browse files Browse the repository at this point in the history
…ule.
  • Loading branch information
rdaum committed Jan 2, 2024
1 parent 94b6e11 commit 906f832
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 119 deletions.
3 changes: 2 additions & 1 deletion crates/db/src/object_relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use moor_values::util::slice_ref::SliceRef;
use moor_values::var::objid::Objid;
use moor_values::AsByteBuffer;

use crate::tuplebox::{RelationId, Transaction, TupleError};
use crate::tuplebox::tuples::TupleError;
use crate::tuplebox::{RelationId, Transaction};

/// The set of binary relations that are used to represent the world state in the moor system.
#[repr(usize)]
Expand Down
4 changes: 2 additions & 2 deletions crates/db/src/tb_worldstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use strum::{EnumCount, IntoEnumIterator};
use tracing::warn;
use uuid::Uuid;

use crate::tuplebox::tuples::TupleError;
use moor_values::model::defset::HasUuid;
use moor_values::model::objects::{ObjAttrs, ObjFlag};
use moor_values::model::objset::ObjSet;
Expand All @@ -35,7 +36,6 @@ use moor_values::util::bitenum::BitEnum;
use moor_values::var::objid::Objid;
use moor_values::var::{v_none, Var};
use moor_values::{AsByteBuffer, NOTHING, SYSTEM_OBJECT};
use tuplebox::TupleError;

use crate::db_tx::DbTransaction;
use crate::db_worldstate::DbTxWorldState;
Expand All @@ -45,7 +45,7 @@ use crate::object_relations::{
};
use crate::tuplebox::tb::{RelationInfo, TupleBox};
use crate::tuplebox::{CommitError, Transaction};
use crate::{object_relations, tuplebox, Database};
use crate::{object_relations, Database};

/// An implementation of `WorldState` / `WorldStateSource` that uses the TupleBox as its backing
pub struct TupleBoxWorldStateSource {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/base_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use std::sync::Arc;

use moor_values::util::slice_ref::SliceRef;

use crate::tuplebox::slots::{SlotBox, TupleId};
use crate::tuplebox::tuples::TupleRef;
use crate::tuplebox::tuples::{SlotBox, TupleId};
use crate::tuplebox::RelationId;

/// Represents a 'canonical' base binary relation, which is a set of tuples of domain, codomain,
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/coldstorage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use tracing::{debug, error, info, warn};
use crate::tuplebox::backing::{BackingStoreClient, WriterMessage};
use crate::tuplebox::base_relation::BaseRelation;
use crate::tuplebox::page_storage::{PageStore, PageStoreMutation};
use crate::tuplebox::slots::{PageId, SlotBox, SlotId, TupleId};
use crate::tuplebox::tb::RelationInfo;
use crate::tuplebox::tuples::TxTuple;
use crate::tuplebox::tuples::{PageId, SlotBox, SlotId, TupleId};
use crate::tuplebox::tx::working_set::WorkingSet;
use crate::tuplebox::RelationId;

Expand Down
4 changes: 2 additions & 2 deletions crates/db/src/tuplebox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ mod base_relation;
mod coldstorage;
mod page_storage;
mod pool;
mod slots;

pub mod tb;
mod tuples;
pub mod tuples;
mod tx;

pub use tuples::TupleError;
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/page_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// TODO: there's no way this is "robust" enough to be used in production

use crate::tuplebox::slots::PageId;
use crate::tuplebox::tuples::PageId;
use crate::tuplebox::RelationId;
use im::{HashMap, HashSet};
use io_uring::squeue::Flags;
Expand Down
21 changes: 0 additions & 21 deletions crates/db/src/tuplebox/slots/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/tb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use tracing::info;

use crate::tuplebox::backing::BackingStoreClient;
use crate::tuplebox::base_relation::BaseRelation;
use crate::tuplebox::slots::SlotBox;
use crate::tuplebox::tuples::SlotBox;
use crate::tuplebox::tuples::TxTuple;
use crate::tuplebox::tx::transaction::{CommitError, CommitSet, Transaction};
use crate::tuplebox::tx::working_set::WorkingSet;
Expand Down
76 changes: 13 additions & 63 deletions crates/db/src/tuplebox/tuples/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,22 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use crate::tuplebox::slots::{SlotBox, TupleId};
use moor_values::util::slice_ref::ByteSource;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
pub use slotbox::{PageId, SlotBox, SlotBoxError, SlotId, TupleId};
use thiserror::Error;
pub use tuple::Tuple;
pub use tx_tuple::{TupleError, TxTuple};
pub use tuple_ref::TupleRef;
pub use tx_tuple::TxTuple;

mod slotbox;
mod slotted_page;
mod tuple;
mod tuple_ref;
mod tx_tuple;

pub struct TupleRef {
sb: Arc<SlotBox>,
pub id: TupleId,
}

impl PartialEq for TupleRef {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl Eq for TupleRef {}

impl Hash for TupleRef {
fn hash<H: Hasher>(&self, state: &mut H) {
let slc = self.sb.get(self.id).expect("Unable to get tuple");
slc.hash(state)
}
}
impl TupleRef {
pub fn new(sb: Arc<SlotBox>, id: TupleId) -> Self {
sb.upcount(id).expect("Unable to add tuple");
Self { sb, id }
}

pub fn get(&self) -> Tuple {
Tuple::from_tuple_id(self.sb.clone(), self.id)
}
}

impl Drop for TupleRef {
fn drop(&mut self) {
self.sb.dncount(self.id).expect("Unable to dncount tuple");
}
}

impl Clone for TupleRef {
fn clone(&self) -> Self {
self.sb.upcount(self.id).expect("Unable to upcount tuple");
Self {
sb: self.sb.clone(),
id: self.id,
}
}
}

impl ByteSource for TupleRef {
fn as_slice(&self) -> &[u8] {
self.sb.get(self.id).expect("Unable to get tuple").get_ref()
}

fn len(&self) -> usize {
self.as_slice().len()
}

fn touch(&self) {
// noop
}
#[derive(Debug, Clone, Eq, PartialEq, Error)]
pub enum TupleError {
#[error("Tuple not found")]
NotFound,
#[error("Tuple already exists")]
Duplicate,
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
// considering only the reported available "content" area when fitting slots, and there seems
// to be a sporadic failure where we end up with a "Page not found" error in the allocator on
// free, meaning the page was not found in the used pages list.
// TODO: improve TupleRef so it can hold a direct address to the tuple, and not just an id.
// some swizzling will probably be required. (though at this point we're never paging
// tuples out, so we may not need to swizzle). avoiding the lookup on every reference
// should improve performance massively.
// TODO: rename me, _I_ am the tuplebox. The "slots" are just where my tuples get stored.

use std::cmp::max;
use std::pin::Pin;
Expand All @@ -34,8 +39,8 @@ use thiserror::Error;
use tracing::error;

use crate::tuplebox::pool::{Bid, BufferPool, PagerError};
pub use crate::tuplebox::slots::slotted_page::SlotId;
use crate::tuplebox::slots::slotted_page::{
pub use crate::tuplebox::tuples::slotted_page::SlotId;
use crate::tuplebox::tuples::slotted_page::{
slot_index_overhead, slot_page_empty_size, SlottedPage,
};
use crate::tuplebox::RelationId;
Expand Down Expand Up @@ -437,8 +442,8 @@ mod tests {
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use crate::tuplebox::slots::slotbox::{SlotBox, SlotBoxError, TupleId};
use crate::tuplebox::slots::slotted_page::slot_page_empty_size;
use crate::tuplebox::tuples::slotbox::{SlotBox, SlotBoxError, TupleId};
use crate::tuplebox::tuples::slotted_page::slot_page_empty_size;
use crate::tuplebox::RelationId;

fn fill_until_full(sb: &mut SlotBox) -> Vec<(TupleId, Vec<u8>)> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::sync::atomic::{AtomicPtr, AtomicU16, AtomicU32};
use atomic_wait::{wait, wake_all, wake_one};
use tracing::error;

use crate::tuplebox::slots::slotbox::SlotBoxError;
use crate::tuplebox::tuples::slotbox::SlotBoxError;

pub type SlotId = usize;

Expand Down Expand Up @@ -697,8 +697,8 @@ impl<'a> Drop for PageReadGuard<'a> {
mod tests {
use std::sync::atomic::AtomicPtr;

use crate::tuplebox::slots::slotbox::SlotBoxError;
use crate::tuplebox::slots::slotted_page::{
use crate::tuplebox::tuples::slotbox::SlotBoxError;
use crate::tuplebox::tuples::slotted_page::{
slot_page_empty_size, SlotId, SlotIndexEntry, SlottedPage,
};

Expand Down
7 changes: 3 additions & 4 deletions crates/db/src/tuplebox/tuples/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::sync::Arc;
use crate::tuplebox::RelationId;
use moor_values::util::slice_ref::SliceRef;

use crate::tuplebox::slots::{SlotBox, TupleId};
use crate::tuplebox::tuples::TupleRef;
use crate::tuplebox::tuples::{SlotBox, TupleId};

define_layout!(tuple_header, LittleEndian, {
ts: u64,
Expand Down Expand Up @@ -87,9 +87,8 @@ impl Tuple {
let domain_size = tuple_header::View::new(buffer.as_slice())
.domain_size()
.read();
buffer.slice(
tuple_header::SIZE.unwrap()..tuple_header::SIZE.unwrap() + domain_size as usize,
)
buffer
.slice(tuple_header::SIZE.unwrap()..tuple_header::SIZE.unwrap() + domain_size as usize)
}

pub fn codomain(&self) -> SliceRef {
Expand Down
64 changes: 64 additions & 0 deletions crates/db/src/tuplebox/tuples/tuple_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::tuplebox::tuples::{SlotBox, Tuple, TupleId};
use moor_values::util::slice_ref::ByteSource;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

pub struct TupleRef {
sb: Arc<SlotBox>,
pub id: TupleId,
}

impl PartialEq for TupleRef {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}

impl Eq for TupleRef {}

impl Hash for TupleRef {
fn hash<H: Hasher>(&self, state: &mut H) {
let slc = self.sb.get(self.id).expect("Unable to get tuple");
slc.hash(state)
}
}
impl TupleRef {
pub fn new(sb: Arc<SlotBox>, id: TupleId) -> Self {
sb.upcount(id).expect("Unable to add tuple");
Self { sb, id }
}

pub fn get(&self) -> Tuple {
Tuple::from_tuple_id(self.sb.clone(), self.id)
}
}

impl Drop for TupleRef {
fn drop(&mut self) {
self.sb.dncount(self.id).expect("Unable to dncount tuple");
}
}

impl Clone for TupleRef {
fn clone(&self) -> Self {
self.sb.upcount(self.id).expect("Unable to upcount tuple");
Self {
sb: self.sb.clone(),
id: self.id,
}
}
}

impl ByteSource for TupleRef {
fn as_slice(&self) -> &[u8] {
self.sb.get(self.id).expect("Unable to get tuple").get_ref()
}

fn len(&self) -> usize {
self.as_slice().len()
}

fn touch(&self) {
// noop
}
}
12 changes: 1 addition & 11 deletions crates/db/src/tuplebox/tuples/tx_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,10 @@
// this program. If not, see <https://www.gnu.org/licenses/>.
//

use thiserror::Error;

use crate::tuplebox::slots::TupleId;
use crate::tuplebox::tuples::TupleId;
use crate::tuplebox::tuples::TupleRef;
use moor_values::util::slice_ref::SliceRef;

#[derive(Debug, Clone, Eq, PartialEq, Error)]
pub enum TupleError {
#[error("Tuple not found")]
NotFound,
#[error("Tuple already exists")]
Duplicate,
}

/// Possible operations on tuples, in the context of a transaction .
#[derive(Clone)]
pub enum TxTuple {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/tuplebox/tx/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use tokio::sync::RwLock;
use moor_values::util::slice_ref::SliceRef;

use crate::tuplebox::base_relation::BaseRelation;
use crate::tuplebox::slots::SlotBox;
use crate::tuplebox::tb::TupleBox;
use crate::tuplebox::tuples::SlotBox;
use crate::tuplebox::tuples::TupleError;
use crate::tuplebox::tx::relvar::RelVar;
use crate::tuplebox::tx::working_set::WorkingSet;
Expand Down
8 changes: 5 additions & 3 deletions crates/db/src/tuplebox/tx/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use std::sync::Arc;

use moor_values::util::slice_ref::SliceRef;

use crate::tuplebox::slots::SlotBox;
use crate::tuplebox::tb::{RelationInfo, TupleBox};
use crate::tuplebox::tuples::{Tuple, TupleError, TxTuple};
use crate::tuplebox::tuples::{SlotBox, TupleError};
use crate::tuplebox::tuples::{Tuple, TxTuple};
use crate::tuplebox::RelationId;

/// The local tx "working set" of mutations to base relations, and consists of the set of operations
Expand Down Expand Up @@ -239,7 +239,9 @@ impl WorkingSet {
}

// Now we have a map of domain -> tuple, so we can just pull out the tuples and return them.
Ok(by_domain.into_values().map(|t| (t.domain(), t.codomain()))
Ok(by_domain
.into_values()
.map(|t| (t.domain(), t.codomain()))
.collect())
}

Expand Down

0 comments on commit 906f832

Please sign in to comment.