Skip to content

Commit

Permalink
Added compile-time-variable length keys, removed static transition in…
Browse files Browse the repository at this point in the history
…terface, restructured data models, fixed KVStore interface, style changes
  • Loading branch information
maxfierrog committed May 6, 2024
1 parent 005505a commit f0c57e3
Show file tree
Hide file tree
Showing 21 changed files with 616 additions and 807 deletions.
24 changes: 11 additions & 13 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use anyhow::Result;

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

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

/* RE-EXPORTS */
Expand Down Expand Up @@ -86,27 +86,25 @@ 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>(&mut self, key: Key, record: &R) -> Result<()>
where
R: Record;
fn put<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`
/// being considered valid (but not necessarily existent).
fn get(&self, key: Key) -> Option<&RawRecord>;
fn get(&self, key: &Key) -> Option<&Value>;

/// 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 delete(&mut self, key: &Key);
}

/// Allows a database to be evicted to persistent media. Implementing this trait
/// requires custom handling of what happens when the database is closed; if it
/// has data on memory, then it should persist dirty data to ensure consistency
/// via [`Drop`]. Database file structure is implementation-specific.
pub trait Persistent<'a, T>
pub trait Persistent<T>
where
Self: Tabular<'a, T> + Drop,
Self: Tabular<T> + Drop,
T: Table,
{
/// Interprets the contents of a directory at `path` to be the contents of
Expand All @@ -132,17 +130,17 @@ where
/// fixed-length records that share attributes under a single [`Schema`]. This
/// allows consumers of this implementation to have simultaneous references to
/// different mutable tables.
pub trait Tabular<'a, T>
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: &TableID, schema: Schema) -> Result<&mut T>;
fn create_table(&self, id: Identifier, schema: Schema) -> Result<&mut T>;

/// 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: &TableID) -> Result<&mut T>;
fn select_table(&self, id: Identifier) -> Result<&mut 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.
Expand All @@ -169,15 +167,15 @@ where
fn size(&self) -> u64;

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

/* RECORD INTERFACE */

/// Represents an in-memory sequence of bits that can be directly accessed.
pub trait Record {
/// Returns a reference to the sequence of bits in `self`.
fn raw(&self) -> &RawRecord;
fn raw(&self) -> &Value;
}

/* IMPLEMENTATIONS */
Expand Down
4 changes: 1 addition & 3 deletions src/database/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ impl SchemaBuilder {
Datatype::SPFP => s != 32,
Datatype::DPFP => s != 64,
Datatype::CSTR => s % 8 != 0,
Datatype::UINT | Datatype::ENUM => {
unreachable!("UINTs and ENUMs can be of any nonzero size.")
},
Datatype::UINT | Datatype::ENUM => s == 0,
} {
Err(DatabaseError::InvalidSize {
size: new.size(),
Expand Down
22 changes: 13 additions & 9 deletions src/database/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::path::Path;

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

/* DEFINITIONS */
Expand All @@ -32,7 +32,7 @@ pub struct Table {}

/* IMPLEMENTATIONS */

impl<'a> Persistent<'a, Table> for Database {
impl Persistent<Table> for Database {
fn from(path: &Path) -> Result<Self>
where
Self: Sized,
Expand All @@ -55,12 +55,16 @@ impl Drop for Database {
}
}

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

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

Expand All @@ -82,24 +86,24 @@ impl database::Table for Table {
todo!()
}

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

impl KVStore for Table {
fn put<R>(&mut self, key: Key, value: &R) -> Result<()>
fn put<R>(&mut self, key: &Key, value: &R) -> Result<()>
where
R: Record,
{
todo!()
}

fn get(&self, key: Key) -> Option<&RawRecord> {
fn get(&self, key: &Key) -> Option<&Value> {
todo!()
}

fn delete(&mut self, key: Key) {
fn delete(&mut self, key: &Key) {
todo!()
}
}
20 changes: 12 additions & 8 deletions src/database/volatile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bitvec::{order::Msb0, slice::BitSlice};

use crate::{
database::{self, KVStore, Record, Schema, Tabular},
model::{State, TableID},
model::database::{Identifier, Key},
};

/* DEFINITIONS */
Expand All @@ -28,12 +28,16 @@ impl Database {
}
}

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

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

Expand All @@ -55,21 +59,21 @@ impl database::Table for Table {
todo!()
}

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

impl KVStore for Table {
fn put<R: Record>(&mut self, key: State, value: &R) -> Result<()> {
fn put<R: Record>(&mut self, key: &Key, value: &R) -> Result<()> {
todo!()
}

fn get(&self, key: State) -> Option<&BitSlice<u8, Msb0>> {
fn get(&self, key: &Key) -> Option<&BitSlice<u8, Msb0>> {
todo!()
}

fn delete(&mut self, key: crate::model::Key) {
fn delete(&mut self, key: &Key) {
todo!()
}
}
27 changes: 9 additions & 18 deletions src/game/crossteaser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ use anyhow::{Context, Result};

use crate::game::Bounded;
use crate::game::Codec;
use crate::game::DTransition;
use crate::game::Extensive;
use crate::game::Forward;
use crate::game::Game;
use crate::game::GameData;
use crate::game::GeneralSum;
use crate::game::Transition;
use crate::interface::IOMode;
use crate::interface::SolutionMode;
use crate::model::SimpleUtility;
use crate::model::State;
use crate::model::Turn;
use crate::model::Utility;
use crate::model::database::Identifier;
use crate::model::game::State;
use crate::model::solver::SUtility;
use crate::solver::ClassicPuzzle;
use variants::*;

use super::ClassicPuzzle;
use super::SimpleSum;

/* SUBMODULES */

mod states;
Expand Down Expand Up @@ -94,12 +89,8 @@ impl Game for Session {
}
}

fn id(&self) -> String {
if let Some(variant) = self.variant.clone() {
format!("{}.{}", NAME, variant)
} else {
NAME.to_owned()
}
fn id(&self) -> Identifier {
todo!()
}

fn info(&self) -> GameData {
Expand All @@ -113,7 +104,7 @@ impl Game for Session {

/* TRAVERSAL IMPLEMENTATIONS */

impl DTransition for Session {
impl Transition for Session {
fn prograde(&self, state: State) -> Vec<State> {
todo!()
}
Expand Down Expand Up @@ -154,7 +145,7 @@ impl Forward for Session {
/* SOLVING IMPLEMENTATIONS */

impl ClassicPuzzle for Session {
fn utility(&self, state: State) -> SimpleUtility {
fn utility(&self, state: State) -> SUtility {
todo!()
}
}
6 changes: 3 additions & 3 deletions src/game/mock/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::{HashMap, HashSet};

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

/* DEFINITIONS */

Expand Down Expand Up @@ -137,7 +137,7 @@ impl<'a> SessionBuilder<'a> {
pub fn build(self) -> Result<Session<'a>> {
let start = self.check_starting_state()?;
self.check_terminal_state(start)?;
self.check_outgoing_edges(start)?;
self.check_outgoing_edges()?;
let (players, _) = self.players;
Ok(Session {
inserted: self.inserted,
Expand Down Expand Up @@ -273,7 +273,7 @@ impl<'a> SessionBuilder<'a> {

/// Fails if there exists a node marked as medial in the game graph which
/// does not have any outgoing edges.
fn check_outgoing_edges(&self, start: NodeIndex) -> Result<()> {
fn check_outgoing_edges(&self) -> Result<()> {
if self.game.node_indices().any(|i| {
self.game[i].medial()
&& self
Expand Down
Loading

0 comments on commit f0c57e3

Please sign in to comment.