Skip to content

Commit

Permalink
Refactored data model modules
Browse files Browse the repository at this point in the history
  • Loading branch information
maxfierrog committed Nov 2, 2024
1 parent 4383251 commit 35dc426
Show file tree
Hide file tree
Showing 25 changed files with 201 additions and 202 deletions.
28 changes: 17 additions & 11 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::Result;

use std::path::{Path, PathBuf};

use crate::model::database::{Identifier, Key, Value};
use crate::database::model::{Key, SequenceKey, Value};
use crate::solver::RecordType;

/* RE-EXPORTS */
Expand All @@ -19,9 +19,11 @@ pub use util::SchemaBuilder;

#[cfg(test)]
mod test;
mod error;
mod util;

pub mod model;
pub mod error;

/* IMPLEMENTATION MODULES */

pub mod volatile;
Expand Down Expand Up @@ -84,7 +86,7 @@ pub trait KVStore {
/// Replaces the value associated with `key` with the bits of `record`,
/// creating one if it does not already exist. Fails if under any violation
/// of implementation-specific assumptions of record size or contents.
fn put<R: Record>(&mut self, key: &Key, record: &R) -> Result<()>;
fn insert<R: Record>(&mut self, key: &Key, record: &R) -> Result<()>;

/// Returns the bits associated with the value of `key`, or `None` if there
/// is no such association. Infallible due to all possible values of `key`
Expand All @@ -93,7 +95,7 @@ pub trait KVStore {

/// Removes the association of `key` to whatever value it is currently bound
/// to, or does nothing if there is no such value.
fn delete(&mut self, key: &Key);
fn remove(&mut self, key: &Key);
}

/// Allows a database to be evicted to persistent media. Implementing this trait
Expand Down Expand Up @@ -132,17 +134,21 @@ pub trait Tabular<T>
where
T: Table,
{
/// Creates a new table with `id` and `schema`. Fails if another table with
/// the same `id` already exists, or under any I/O failure.
fn create_table(&self, id: Identifier, schema: Schema) -> Result<&mut T>;
/// Creates a new table with `schema`. Returns a unique key that can be used
/// to later acquire the table.
fn insert_table(&self, schema: Schema) -> Result<SequenceKey>;

/// Obtains a mutable reference to the [`Table`] with `id`. Fails if no such
/// table exists in the underlying database, or under any I/O failure.
fn select_table(&self, id: Identifier) -> Result<&mut T>;
fn get_table_mut(&self, key: SequenceKey) -> Result<&mut T>;

/// Obtains an immutable reference to the [`Table`] with `id`. Fails if no
/// such table exists in the underlying database, or under any I/O failure.
fn get_table(&self, key: SequenceKey) -> Result<&T>;

/// Forgets about the association of `id` to any existing table, doing
/// nothing if there is no such table. Fails under any I/O failure.
fn delete_table(&self, table: &mut T) -> Result<()>;
fn remove_table(&self, table: &mut T) -> Result<()>;
}

/* TABLE INTERFACE */
Expand All @@ -162,10 +168,10 @@ where

/// Returns the total number of bytes being used to store the contents of
/// `self`, excluding metadata (both in memory and persistent media).
fn size(&self) -> u64;
fn bytes(&self) -> u64;

/// Returns the identifier associated with `self`.
fn id(&self) -> Identifier;
fn id(&self) -> SequenceKey;
}

/* RECORD INTERFACE */
Expand Down
16 changes: 16 additions & 0 deletions src/database/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! # Database Data Models Module
//!
//! Provides definitions for types used in database interfaces.

use bitvec::order::Msb0;
use bitvec::slice::BitSlice;

/// A generic number used to differentiate between objects.
pub type SequenceKey = u64;

/// The type of a raw sequence of bits encoding a database value associated
/// with a key, backed by a [`BitSlice`] with [`u8`] big-endian storage.
pub type Value = BitSlice<u8, Msb0>;

/// The type of a database key per an implementation of [`KVStore`].
pub type Key = BitSlice<u8, Msb0>;
24 changes: 12 additions & 12 deletions src/database/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use anyhow::Result;
use std::path::Path;

use crate::{
database::model::{Key, SequenceKey, Value},
database::{self, KVStore, Persistent, Record, Schema, Tabular},
model::database::{Identifier, Key, Value},
};

/* DEFINITIONS */
Expand Down Expand Up @@ -53,19 +53,19 @@ impl Drop for Database {
}

impl Tabular<Table> for Database {
fn create_table(
&self,
id: Identifier,
schema: Schema,
) -> Result<&mut Table> {
fn insert_table(&self, schema: Schema) -> Result<SequenceKey> {
todo!()
}

fn select_table(&self, id: Identifier) -> Result<&mut Table> {
fn get_table_mut(&self, key: SequenceKey) -> Result<&mut Table> {
todo!()
}

fn delete_table(&self, id: &mut Table) -> Result<()> {
fn get_table(&self, key: SequenceKey) -> Result<&Table> {
todo!()
}

fn remove_table(&self, table: &mut Table) -> Result<()> {
todo!()
}
}
Expand All @@ -79,17 +79,17 @@ impl database::Table for Table {
todo!()
}

fn size(&self) -> u64 {
fn bytes(&self) -> u64 {
todo!()
}

fn id(&self) -> Identifier {
fn id(&self) -> SequenceKey {
todo!()
}
}

impl KVStore for Table {
fn put<R>(&mut self, key: &Key, value: &R) -> Result<()>
fn insert<R>(&mut self, key: &Key, value: &R) -> Result<()>
where
R: Record,
{
Expand All @@ -100,7 +100,7 @@ impl KVStore for Table {
todo!()
}

fn delete(&mut self, key: &Key) {
fn remove(&mut self, key: &Key) {
todo!()
}
}
13 changes: 8 additions & 5 deletions src/database/volatile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use anyhow::Result;

use std::sync::Arc;

use crate::database::model::SequenceKey;
use crate::database::util::KeySequencer;
use resource::ResourceManager;
use transaction::TransactionManager;
use crate::database::volatile::resource::ResourceManager;
use crate::database::volatile::transaction::TransactionManager;

/* RE-EXPORTS */

Expand All @@ -24,7 +25,6 @@ mod resource;

/* DEFINITIONS */

type SequenceKey = u64;
type TransactionID = SequenceKey;
type ResourceID = SequenceKey;

Expand Down Expand Up @@ -53,7 +53,7 @@ impl Sequencer {
}

impl Database {
fn new() -> Self {
pub fn new() -> Self {
let sequencer = Arc::new(Sequencer::default());
let resource_manager = ResourceManager::new(sequencer.clone());
let transaction_manager = TransactionManager::new(
Expand All @@ -68,7 +68,10 @@ impl Database {
}
}

fn create_transaction(&self, request: Request) -> Result<Arc<Transaction>> {
pub fn create_transaction(
&self,
request: Request,
) -> Result<Arc<Transaction>> {
let transaction = self
.resource_manager
.initialize_transaction(
Expand Down
6 changes: 3 additions & 3 deletions src/game/crossteaser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use anyhow::{Context, Result};

use crate::game::crossteaser::variants::*;
use crate::game::model::State;
use crate::game::model::Variant;
use crate::game::Bounded;
use crate::game::Codec;
use crate::game::Forward;
Expand All @@ -26,9 +28,7 @@ use crate::game::Transition;
use crate::game::Variable;
use crate::interface::IOMode;
use crate::interface::Solution;
use crate::model::game::State;
use crate::model::game::Variant;
use crate::model::solver::SUtility;
use crate::solver::model::SUtility;
use crate::solver::ClassicPuzzle;

/* SUBMODULES */
Expand Down
2 changes: 1 addition & 1 deletion src/game/mock/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::collections::{HashMap, HashSet};

use crate::game::mock::Node;
use crate::game::mock::Session;
use crate::model::game::PlayerCount;
use crate::game::model::PlayerCount;

/* DEFINITIONS */

Expand Down
8 changes: 4 additions & 4 deletions src/game/mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ use petgraph::{graph::NodeIndex, Graph};

use std::collections::HashMap;

use crate::game::model::Player;
use crate::game::model::PlayerCount;
use crate::game::model::State;
use crate::game::Bounded;
use crate::game::Transition;
use crate::model::game::Player;
use crate::model::game::PlayerCount;
use crate::model::game::State;
use crate::model::solver::IUtility;
use crate::solver::model::IUtility;

/* RE-EXPORTS */

Expand Down
15 changes: 8 additions & 7 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,21 @@

use anyhow::{Context, Result};

use crate::model::game::{State, Variant, DEFAULT_STATE_BYTES};
use crate::game::model::{State, Variant, DEFAULT_STATE_BYTES};

/* UTILITY MODULES */

#[cfg(test)]
pub mod mock;

#[cfg(test)]
mod test;

mod error;
mod util;

/* IMPLEMENTED GAMES */
pub mod model;
pub mod error;

/* MODULES */

#[cfg(test)]
pub mod mock;

pub mod crossteaser;
pub mod zero_by;
Expand Down
33 changes: 33 additions & 0 deletions src/game/model.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! # Game Data Models Module
//!
//! Provides definitions for types used in game interfaces.

use bitvec::{array::BitArray, order::Msb0};
use clap::ValueEnum;

/// The default number of bytes used to encode states.
pub const DEFAULT_STATE_BYTES: usize = 8;

/// Unique identifier of a particular state in a game.
pub type State<const B: usize = DEFAULT_STATE_BYTES> = BitArray<[u8; B], Msb0>;

/// String encoding some specific game's variant.
pub type Variant = String;

/// Unique identifier for a player in a game.
pub type Player = usize;

/// Unique identifier of a subset of states of a game.
pub type Partition = u64;

/// Count of the number of states in a game.
pub type StateCount = u64;

/// Count of the number of players in a game.
pub type PlayerCount = Player;

// Specifies the game offerings available through all interfaces.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub enum GameModule {
ZeroBy,
}
26 changes: 21 additions & 5 deletions src/game/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
//! more than a single game.

use std::fmt::Display;
use std::hash::{DefaultHasher, Hasher};

use anyhow::bail;
use anyhow::{Context, Result};
use anyhow::Result;

use crate::database::model::SequenceKey;
use crate::game::model::State;
use crate::game::GameData;
use crate::game::Information;
use crate::game::Variable;
use crate::game::{error::GameError, Bounded, Codec, Transition};
use crate::interface::GameAttribute;
use crate::{
game::{error::GameError, Bounded, Codec, Transition},
model::game::State,
};
use crate::util::Identify;

/* STATE HISTORY VERIFICATION */

Expand Down Expand Up @@ -163,3 +165,17 @@ impl GameData {
}
}
}

/* IDENTIFICATION */

impl<G> Identify for G
where
G: Variable,
{
fn id(&self) -> SequenceKey {
let mut hasher = DefaultHasher::new();
self.variant_string()
.hash(&mut hasher);
hasher.finish()
}
}
6 changes: 3 additions & 3 deletions src/game/zero_by/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ use anyhow::{Context, Result};
use bitvec::field::BitField;

use crate::game::error::GameError;
use crate::game::model::Variant;
use crate::game::model::{Player, PlayerCount, State};
use crate::game::zero_by::states::*;
use crate::game::zero_by::variants::*;
use crate::game::Information;
use crate::game::Variable;
use crate::game::{Bounded, Codec, Forward};
use crate::game::{GameData, Transition};
use crate::interface::{IOMode, Solution};
use crate::model::game::Variant;
use crate::model::game::{Player, PlayerCount, State};
use crate::model::solver::SUtility;
use crate::solver::algorithm::strong;
use crate::solver::model::SUtility;
use crate::solver::{Sequential, SimpleUtility};

/* SUBMODULES */
Expand Down
4 changes: 2 additions & 2 deletions src/game/zero_by/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use regex::Regex;

use crate::game::error::GameError;
use crate::game::model::Player;
use crate::game::model::State;
use crate::game::zero_by::Elements;
use crate::game::zero_by::Session;
use crate::game::zero_by::NAME;
use crate::model::game::Player;
use crate::model::game::State;

/* ZERO-BY STATE ENCODING */

Expand Down
Loading

0 comments on commit 35dc426

Please sign in to comment.