Skip to content

Commit

Permalink
ready for release (#42)
Browse files Browse the repository at this point in the history
proofreading documentation
finalizing Cargo.toml
hello clippy
  • Loading branch information
salmans committed Nov 1, 2020
1 parent 67598e3 commit c8026e2
Show file tree
Hide file tree
Showing 21 changed files with 164 additions and 137 deletions.
9 changes: 7 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ name = "codd"
version = "0.1.0"
authors = ["Salman Saghafi <salman.saghafi@me.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
license = "MIT"
description = "codd is a minimal in-memory database with relational algebraic expressions as queries."
homepage = "https://github.com/salmans/codd"
repository = "https://github.com/salmans/codd"
keywords = ["codd", "relational-algebra", "database"]
categories = ["mathematics", "database-implementations"]
readme = "../README.md"

[dependencies]
thiserror = "^1.0"
Expand Down
32 changes: 20 additions & 12 deletions core/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ impl Clone for ViewEntry {
}
}

/// Is a database that stores tuples in relation instances and offers and maintains views
/// over its data.
/// Stores data in relation instances and implements incremental view maintenance over them.
///
/// **Example**:
/// ```rust
Expand Down Expand Up @@ -187,8 +186,10 @@ impl Database {
expression.collect_recent(&evaluate::Evaluator::new(self))
}

/// Adds a new relation instance identified by `name` to the database and returns the a
/// corresponding `Relation` object.
/// Adds a new relation instance identified by `name` to the database and returns a
/// [`Relation`] object that can be used to access the instance.
///
/// [`Relation`]: ./expression/struct.Relation.html
pub fn add_relation<T>(&mut self, name: &str) -> Result<Relation<T>, Error>
where
T: Tuple + 'static,
Expand All @@ -202,7 +203,7 @@ impl Database {
}
}

/// Inserts tuples in the relation `Instance` for `relation`.
/// Inserts tuples in the `Instance` corresponding to `relation`.
pub fn insert<T>(&self, relation: &Relation<T>, tuples: Tuples<T>) -> Result<(), Error>
where
T: Tuple + 'static,
Expand All @@ -227,7 +228,8 @@ impl Database {
Ok(result)
}

/// Stores a new view over `expression` and returns the corresponding [`View`] expression.
/// Stores a new view over `expression` and returns a [`View`] objeect that can be
/// evaluated as a view.
///
/// [`View`]: ./expression/struct.View.html
pub fn store_view<T, E, I>(&mut self, expression: I) -> Result<View<T, E>, Error>
Expand All @@ -247,17 +249,17 @@ impl Database {

// track relation dependencies of this view:
for r in relation_deps.into_iter() {
self.relations
.get_mut(&r)
.map(|rs| rs.add_dependent_view(reference.clone()));
if let Some(rs) = self.relations.get_mut(&r) {
rs.add_dependent_view(reference.clone())
}
entry.dependee_relations.insert(r);
}

// track view dependencies of this view:
for r in view_deps.into_iter() {
self.views
.get_mut(&r)
.map(|rs| rs.add_dependent_view(reference.clone()));
if let Some(rs) = self.views.get_mut(&r) {
rs.add_dependent_view(reference.clone())
}
entry.dependee_views.insert(r.clone());
}

Expand Down Expand Up @@ -342,6 +344,12 @@ impl Database {
}
}

impl Default for Database {
fn default() -> Self {
Self::new()
}
}

impl Clone for Database {
fn clone(&self) -> Self {
let mut relations = HashMap::new();
Expand Down
2 changes: 1 addition & 1 deletion core/src/database/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl<'d> StableCollector for IncrementalCollector<'d> {
where
T: Tuple,
{
Ok(Vec::new().into())
Ok(Vec::new())
}

fn collect_singleton<T>(&self, singleton: &Singleton<T>) -> Result<Vec<Tuples<T>>, Error>
Expand Down
24 changes: 12 additions & 12 deletions core/src/database/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
/// [`datafrog`]: https://github.com/rust-lang/datafrog
#[inline(always)]
pub(crate) fn gallop<T>(mut slice: &[T], mut cmp: impl FnMut(&T) -> bool) -> &[T] {
if slice.len() > 0 && cmp(&slice[0]) {
if !slice.is_empty() && cmp(&slice[0]) {
let mut step = 1;
while step < slice.len() && cmp(&slice[step]) {
slice = &slice[step..];
step = step << 1;
step <<= 1;
}

step = step >> 1;
step >>= 1;
while step > 0 {
if step < slice.len() && cmp(&slice[step]) {
slice = &slice[step..];
}
step = step >> 1;
step >>= 1;
}

slice = &slice[1..];
Expand Down Expand Up @@ -73,8 +73,8 @@ pub(crate) fn join_helper<Key: Ord, L, R>(
let count2 = slice2.iter().take_while(|x| x.0 == slice2[0].0).count();

for index1 in 0..count1 {
for index2 in 0..count2 {
result(&slice1[0].0, &slice1[index1].1, &slice2[index2].1);
for item in slice2.iter().take(count2) {
result(&slice1[0].0, &slice1[index1].1, &item.1);
}
}

Expand Down Expand Up @@ -117,19 +117,19 @@ pub(crate) fn diff_helper<T: Ord>(left: &[T], right: &[&[T]], mut result: impl F

for tuple in left {
let mut to_add = true;
for i in 0..right.len() {
for mut to_find in &mut right {
use std::cmp::Ordering;

if !right[i].is_empty() {
match tuple.cmp(&right[i][0]) {
if !to_find.is_empty() {
match tuple.cmp(&to_find[0]) {
Ordering::Less => {}
Ordering::Equal => {
right[i] = &right[i][1..];
to_add = false;
}
Ordering::Greater => {
right[i] = &gallop(right[i], |x| x < &tuple);
if !right[i].is_empty() && tuple == &right[i][0] {
let mut temp = gallop(to_find, |x| x < tuple);
to_find = &mut temp;
if !to_find.is_empty() && tuple == &to_find[0] {
to_add = false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/database/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
rc::Rc,
};

/// Is a wrapper around a vector of tuples. As an invariant, tuples of `Tuples` are sorted.
/// Is a wrapper around a vector of tuples. As an invariant, the content of `Tuples` is sorted.
///
/// **Note**: `Tuples` is borrowed from `Relation` in [`datafrog`].
///
Expand Down Expand Up @@ -196,7 +196,7 @@ where
let mut slice = &batch[..];
to_add.items.retain(|x| {
slice = gallop(slice, |y| y < x);
slice.len() == 0 || &slice[0] != x
slice.is_empty() || &slice[0] != x
});
}
*self.recent.borrow_mut() = to_add;
Expand Down
18 changes: 7 additions & 11 deletions core/src/expression.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/*! Defines relational algebraic expressions as generic over [`Tuple`] types and
can be evaluated in [`Database`].
[`Tuple`]: ../trait.Tuple.html
[`Database`]: ./database/struct.Database.html
*/
/*! Defines relational algebraic expressions as generic types over `Tuple` types.*/
mod builder;
pub(crate) mod dependency;
mod difference;
Expand Down Expand Up @@ -36,16 +31,16 @@ pub use singleton::Singleton;
pub use union::Union;
pub use view::View;

/// Is the trait of expressions in relational algebra that can be evaluated in
/// a database.
/// Is the trait of expressions in relational algebra that can be evaluated in a database.
pub trait Expression<T: Tuple>: Clone + std::fmt::Debug {
/// Visits this node by a [`Visitor`].
/// Visits this expression by a [`Visitor`].
///
/// [`Visitor`]: ./trait.Visitor.html
fn visit<V>(&self, visitor: &mut V)
where
V: Visitor;

/// Returns an expression builder over this expression.
fn builder(&self) -> Builder<T, Self> {
Builder::from(self.clone())
}
Expand Down Expand Up @@ -99,8 +94,9 @@ where
}
}

/// Is the trait of objects that visit [`Expression`]s. The default implementation guides
/// the visitor through all subexpressions of the expressions that is visited.
/// Is the trait of objects that visit sub-expressions of an [`Expression`]. The default
/// implementation guides the visitor through all sub-expressions of the expressions that
/// is visited.
///
/// [`Expression`]: ./trait.Expression.html
pub trait Visitor: Sized {
Expand Down
4 changes: 2 additions & 2 deletions core/src/expression/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::Tuple;
use std::marker::PhantomData;

/// Is a builder for building [`Expression`]s.
/// Is a builder for building [`Expression`] values.
///
/// [`Expression`]: ./trait.Expression.html
pub struct Builder<L, Left>
Expand Down Expand Up @@ -227,7 +227,7 @@ where
///
/// assert_eq!(vec!["Apple0", "Cherry4"], db.evaluate(&join).unwrap().into_tuples());
/// ```
pub fn with_key<'k, K>(self, f: impl FnMut(&L) -> K + 'static) -> WithKeyBuilder<K, L, Left>
pub fn with_key<K>(self, f: impl FnMut(&L) -> K + 'static) -> WithKeyBuilder<K, L, Left>
where
K: Tuple,
{
Expand Down
14 changes: 7 additions & 7 deletions core/src/expression/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{view::ViewRef, Expression, IntoExpression, Visitor};
use crate::Tuple;
use std::marker::PhantomData;

/// Evaluates to all tuples that are in `left` but not in `right` (`left - right`).
/// Evaluates to the tuples that are in its `left` but not in its `right` sub-expressions (`left - right`).
///
/// **Example**:
/// ```rust
Expand Down Expand Up @@ -59,33 +59,33 @@ where
let (relation_deps, view_deps) = deps.into_dependencies();

Self {
left: left.clone(),
right: right.clone(),
left,
right,
relation_deps: relation_deps.into_iter().collect(),
view_deps: view_deps.into_iter().collect(),
_marker: PhantomData,
}
}

/// Returns a reference to the expression on left.
/// Returns a reference to the left sub-expression.
#[inline(always)]
pub fn left(&self) -> &L {
&self.left
}

/// Returns a reference to the expression on right.
/// Returns a reference to the right sub-expression.
#[inline(always)]
pub fn right(&self) -> &R {
&self.right
}

/// Returns a reference to relation dependencies of the receiver.
/// Returns a reference to the relation dependencies of the receiver.
#[inline(always)]
pub(crate) fn relation_deps(&self) -> &[String] {
&self.relation_deps
}

/// Returns a reference to view dependencies of the receiver.
/// Returns a reference to the view dependencies of the receiver.
#[inline(always)]
pub(crate) fn view_deps(&self) -> &[ViewRef] {
&self.view_deps
Expand Down
6 changes: 6 additions & 0 deletions core/src/expression/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ where
visitor.visit_empty(&self);
}
}

impl<T: Tuple> Default for Empty<T> {
fn default() -> Self {
Self::new()
}
}
8 changes: 7 additions & 1 deletion core/src/expression/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{Expression, Visitor};
use crate::Tuple;
use std::marker::PhantomData;

/// Is a placeholder for a "full" instance, containing *all* tuples of type `T`.
/// Is a placeholder for a "full" instance, containing *all* tuples of its type.
///
/// **Note**: because `Full` expression cannot be described by a range-restricted
/// (see [chapter 2] of Foundations of Databases) query, any query containing
Expand Down Expand Up @@ -50,3 +50,9 @@ where
visitor.visit_full(&self);
}
}

impl<T: Tuple> Default for Full<T> {
fn default() -> Self {
Self::new()
}
}
14 changes: 7 additions & 7 deletions core/src/expression/intersect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{view::ViewRef, Expression, IntoExpression, Visitor};
use crate::Tuple;
use std::marker::PhantomData;

/// Evaluates to all tuples that are in both `left` and `right` expressions.
/// Evaluates to all tuples that are in its both `left` and `right` sub-expressions.
///
/// **Example**:
/// ```rust
Expand Down Expand Up @@ -55,33 +55,33 @@ where
let (relation_deps, view_deps) = deps.into_dependencies();

Self {
left: left.clone(),
right: right.clone(),
left,
right,
relation_deps: relation_deps.into_iter().collect(),
view_deps: view_deps.into_iter().collect(),
_marker: PhantomData,
}
}

/// Returns a reference to the expression on left.
/// Returns a reference to the left sub-expression.
#[inline(always)]
pub fn left(&self) -> &L {
&self.left
}

/// Returns a reference to the expression on right.
/// Returns a reference to the right sub-expression.
#[inline(always)]
pub fn right(&self) -> &R {
&self.right
}

/// Returns a reference to relation dependencies of the receiver.
/// Returns a reference to the relation dependencies of the receiver.
#[inline(always)]
pub(crate) fn relation_deps(&self) -> &[String] {
&self.relation_deps
}

/// Returns a reference to view dependencies of the receiver.
/// Returns a reference to the view dependencies of the receiver.
#[inline(always)]
pub(crate) fn view_deps(&self) -> &[ViewRef] {
&self.view_deps
Expand Down
Loading

0 comments on commit c8026e2

Please sign in to comment.