Pre: Replace PartitionMapping trait with newtype. r=grisha

Generally, I think that Mentat is using too many small traits rather
than wrapping types into newtypes.  Wrapping into newtypes is cheap in
Rust, and it makes it easier to reason about the code.
This commit is contained in:
Nick Alexander 2018-07-12 16:50:08 -07:00 committed by Grisha Kruglov
parent 675a865896
commit 38a92229d7
3 changed files with 39 additions and 16 deletions

View file

@ -14,12 +14,10 @@ use failure::{
ResultExt, ResultExt,
}; };
use std::borrow::Borrow;
use std::collections::HashMap; use std::collections::HashMap;
use std::collections::hash_map::{ use std::collections::hash_map::{
Entry, Entry,
}; };
use std::fmt::Display;
use std::iter::{once, repeat}; use std::iter::{once, repeat};
use std::ops::Range; use std::ops::Range;
use std::path::Path; use std::path::Path;
@ -1080,20 +1078,14 @@ SELECT EXISTS
Ok(()) Ok(())
} }
pub trait PartitionMapping { impl PartitionMap {
fn allocate_entid<S: ?Sized + Ord + Display>(&mut self, partition: &S) -> i64 where String: Borrow<S>;
fn allocate_entids<S: ?Sized + Ord + Display>(&mut self, partition: &S, n: usize) -> Range<i64> where String: Borrow<S>;
fn contains_entid(&self, entid: Entid) -> bool;
}
impl PartitionMapping for PartitionMap {
/// Allocate a single fresh entid in the given `partition`. /// Allocate a single fresh entid in the given `partition`.
fn allocate_entid<S: ?Sized + Ord + Display>(&mut self, partition: &S) -> i64 where String: Borrow<S> { pub(crate) fn allocate_entid(&mut self, partition: &str) -> i64 {
self.allocate_entids(partition, 1).start self.allocate_entids(partition, 1).start
} }
/// Allocate `n` fresh entids in the given `partition`. /// Allocate `n` fresh entids in the given `partition`.
fn allocate_entids<S: ?Sized + Ord + Display>(&mut self, partition: &S, n: usize) -> Range<i64> where String: Borrow<S> { pub(crate) fn allocate_entids(&mut self, partition: &str, n: usize) -> Range<i64> {
match self.get_mut(partition) { match self.get_mut(partition) {
Some(partition) => { Some(partition) => {
let idx = partition.index; let idx = partition.index;
@ -1105,7 +1097,7 @@ impl PartitionMapping for PartitionMap {
} }
} }
fn contains_entid(&self, entid: Entid) -> bool { pub(crate) fn contains_entid(&self, entid: Entid) -> bool {
self.values().any(|partition| partition.contains_entid(entid)) self.values().any(|partition| partition.contains_entid(entid))
} }
} }
@ -1114,6 +1106,10 @@ impl PartitionMapping for PartitionMap {
mod tests { mod tests {
extern crate env_logger; extern crate env_logger;
use std::borrow::{
Borrow,
};
use super::*; use super::*;
use debug::{TestConn,tempids}; use debug::{TestConn,tempids};
use edn::{ use edn::{

View file

@ -60,7 +60,6 @@ use std::iter::{
use db; use db;
use db::{ use db::{
MentatStoring, MentatStoring,
PartitionMapping,
}; };
use edn::{ use edn::{
InternSet, InternSet,

View file

@ -10,10 +10,17 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::collections::HashMap;
use std::collections::{ use std::collections::{
BTreeMap, BTreeMap,
BTreeSet, BTreeSet,
HashMap,
};
use std::iter::{
FromIterator,
};
use std::ops::{
Deref,
DerefMut,
}; };
extern crate mentat_core; extern crate mentat_core;
@ -37,7 +44,7 @@ use edn::entities::{
use errors; use errors;
/// Represents one partition of the entid space. /// Represents one partition of the entid space.
#[derive(Clone,Debug,Eq,Hash,Ord,PartialOrd,PartialEq)] #[derive(Clone, Debug, Eq, Hash, Ord, PartialOrd, PartialEq)]
#[cfg_attr(feature = "syncable", derive(Serialize,Deserialize))] #[cfg_attr(feature = "syncable", derive(Serialize,Deserialize))]
pub struct Partition { pub struct Partition {
/// The first entid in the partition. /// The first entid in the partition.
@ -66,7 +73,28 @@ impl Partition {
} }
/// Map partition names to `Partition` instances. /// Map partition names to `Partition` instances.
pub type PartitionMap = BTreeMap<String, Partition>; #[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialOrd, PartialEq)]
pub struct PartitionMap(BTreeMap<String, Partition>);
impl Deref for PartitionMap {
type Target = BTreeMap<String, Partition>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PartitionMap {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl FromIterator<(String, Partition)> for PartitionMap {
fn from_iter<T: IntoIterator<Item=(String, Partition)>>(iter: T) -> Self {
PartitionMap(iter.into_iter().collect())
}
}
/// Represents the metadata required to query from, or apply transactions to, a Mentat store. /// Represents the metadata required to query from, or apply transactions to, a Mentat store.
/// ///