Bind a record to its unique id #130
|
@ -76,4 +76,4 @@ mod types;
|
|||
|
||||
pub use criteria::*;
|
||||
pub use series::Series;
|
||||
pub use types::{EmseriesReadError, EmseriesWriteError, Recordable, Timestamp, UniqueId};
|
||||
pub use types::{EmseriesReadError, EmseriesWriteError, RecordId, Recordable, Timestamp};
|
||||
|
|
|
@ -24,7 +24,7 @@ use std::io::{BufRead, BufReader, LineWriter, Write};
|
|||
use std::iter::Iterator;
|
||||
|
||||
use criteria::Criteria;
|
||||
use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId};
|
||||
use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable};
|
||||
|
||||
/// An open time series database.
|
||||
///
|
||||
|
@ -33,7 +33,7 @@ use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}
|
|||
pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> {
|
||||
//path: String,
|
||||
writer: LineWriter<File>,
|
||||
records: HashMap<UniqueId, T>,
|
||||
records: HashMap<RecordId, T>,
|
||||
}
|
||||
|
||||
impl<T> Series<T>
|
||||
|
@ -62,8 +62,8 @@ where
|
|||
}
|
||||
|
||||
/// Load a file and return all of the records in it.
|
||||
fn load_file(f: &File) -> Result<HashMap<UniqueId, T>, EmseriesReadError> {
|
||||
let mut records: HashMap<UniqueId, T> = HashMap::new();
|
||||
fn load_file(f: &File) -> Result<HashMap<RecordId, T>, EmseriesReadError> {
|
||||
let mut records: HashMap<RecordId, T> = HashMap::new();
|
||||
let reader = BufReader::new(f);
|
||||
for line in reader.lines() {
|
||||
match line {
|
||||
|
@ -87,14 +87,14 @@ where
|
|||
|
||||
/// Put a new record into the database. A unique id will be assigned to the record and
|
||||
/// returned.
|
||||
pub fn put(&mut self, entry: T) -> Result<UniqueId, EmseriesWriteError> {
|
||||
let uuid = UniqueId::default();
|
||||
pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
|
||||
let uuid = RecordId::default();
|
||||
self.update(uuid.clone(), entry).map(|_| uuid)
|
||||
}
|
||||
|
||||
/// Update an existing record. The `UniqueId` of the record passed into this function must match
|
||||
/// the `UniqueId` of a record already in the database.
|
||||
pub fn update(&mut self, uuid: UniqueId, entry: T) -> Result<(), EmseriesWriteError> {
|
||||
/// Update an existing record. The [RecordId] of the record passed into this function must match
|
||||
/// the [RecordId] of a record already in the database.
|
||||
pub fn update(&mut self, uuid: RecordId, entry: T) -> Result<(), EmseriesWriteError> {
|
||||
self.records.insert(uuid.clone(), entry.clone());
|
||||
let write_res = match serde_json::to_string(&Record {
|
||||
id: uuid,
|
||||
|
@ -118,7 +118,7 @@ where
|
|||
/// Future note: while this deletes a record from the view, it only adds an entry to the
|
||||
/// database that indicates `data: null`. If record histories ever become important, the record
|
||||
/// and its entire history (including this delete) will still be available.
|
||||
pub fn delete(&mut self, uuid: &UniqueId) -> Result<(), EmseriesWriteError> {
|
||||
pub fn delete(&mut self, uuid: &RecordId) -> Result<(), EmseriesWriteError> {
|
||||
if !self.records.contains_key(uuid) {
|
||||
return Ok(());
|
||||
};
|
||||
|
@ -138,7 +138,7 @@ where
|
|||
}
|
||||
|
||||
/// Get all of the records in the database.
|
||||
pub fn records(&self) -> impl Iterator<Item = (&UniqueId, &T)> {
|
||||
pub fn records(&self) -> impl Iterator<Item = (&RecordId, &T)> {
|
||||
self.records.iter()
|
||||
}
|
||||
|
||||
|
@ -148,29 +148,29 @@ where
|
|||
pub fn search<'s>(
|
||||
&'s self,
|
||||
criteria: impl Criteria + 's,
|
||||
) -> impl Iterator<Item = (&'s UniqueId, &'s T)> + 's {
|
||||
) -> impl Iterator<Item = (&'s RecordId, &'s T)> + 's {
|
||||
self.records().filter(move |&tr| criteria.apply(tr.1))
|
||||
}
|
||||
|
||||
/// Perform a search and sort the resulting records based on the comparison.
|
||||
pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&UniqueId, &T)>
|
||||
pub fn search_sorted<'s, C, CMP>(&'s self, criteria: C, compare: CMP) -> Vec<(&RecordId, &T)>
|
||||
where
|
||||
C: Criteria + 's,
|
||||
CMP: FnMut(&(&UniqueId, &T), &(&UniqueId, &T)) -> Ordering,
|
||||
CMP: FnMut(&(&RecordId, &T), &(&RecordId, &T)) -> Ordering,
|
||||
{
|
||||
let search_iter = self.search(criteria);
|
||||
let mut records: Vec<(&UniqueId, &T)> = search_iter.collect();
|
||||
let mut records: Vec<(&RecordId, &T)> = search_iter.collect();
|
||||
records.sort_by(compare);
|
||||
records
|
||||
}
|
||||
|
||||
/// Get an exact record from the database based on unique id.
|
||||
pub fn get(&self, uuid: &UniqueId) -> Option<T> {
|
||||
pub fn get(&self, uuid: &RecordId) -> Option<T> {
|
||||
self.records.get(uuid).cloned()
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn remove(&self, uuid: UniqueId) -> Result<(), EmseriesError> {
|
||||
pub fn remove(&self, uuid: RecordId) -> Result<(), EmseriesError> {
|
||||
unimplemented!()
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -145,26 +145,26 @@ pub trait Recordable {
|
|||
///
|
||||
/// This is a wrapper around a basic uuid with some extra convenience methods.
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
|
||||
pub struct UniqueId(Uuid);
|
||||
pub struct RecordId(Uuid);
|
||||
|
||||
impl Default for UniqueId {
|
||||
impl Default for RecordId {
|
||||
fn default() -> Self {
|
||||
Self(Uuid::new_v4())
|
||||
}
|
||||
}
|
||||
|
||||
impl str::FromStr for UniqueId {
|
||||
impl str::FromStr for RecordId {
|
||||
type Err = EmseriesReadError;
|
||||
|
||||
/// Parse a UniqueId from a string. Raise UUIDParseError if the parsing fails.
|
||||
/// Parse a RecordId from a string. Raise UUIDParseError if the parsing fails.
|
||||
fn from_str(val: &str) -> Result<Self, Self::Err> {
|
||||
Uuid::parse_str(val)
|
||||
.map(UniqueId)
|
||||
.map(RecordId)
|
||||
.map_err(EmseriesReadError::UUIDParseError)
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for UniqueId {
|
||||
impl fmt::Display for RecordId {
|
||||
/// Convert to a hyphenated string
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||
write!(f, "{}", self.0.to_hyphenated())
|
||||
|
@ -175,7 +175,7 @@ impl fmt::Display for UniqueId {
|
|||
/// Recordable trait.
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct Record<T: Clone + Recordable> {
|
||||
pub id: UniqueId,
|
||||
pub id: RecordId,
|
||||
pub data: Option<T>,
|
||||
}
|
||||
|
||||
|
|
|
@ -162,7 +162,7 @@ mod test {
|
|||
ts.put(trip.clone()).expect("expect a successful put");
|
||||
}
|
||||
|
||||
let v: Vec<(&UniqueId, &BikeTrip)> = ts
|
||||
let v: Vec<(&RecordId, &BikeTrip)> = ts
|
||||
.search(exact_time(Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
||||
.unwrap()
|
||||
|
@ -185,7 +185,7 @@ mod test {
|
|||
ts.put(trip.clone()).expect("expect a successful put");
|
||||
}
|
||||
|
||||
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
||||
time_range(
|
||||
Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
||||
|
@ -226,7 +226,7 @@ mod test {
|
|||
{
|
||||
let ts: Series<BikeTrip> =
|
||||
Series::open(&path).expect("expect the time series to open correctly");
|
||||
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
||||
time_range(
|
||||
Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
||||
|
@ -268,7 +268,7 @@ mod test {
|
|||
{
|
||||
let mut ts: Series<BikeTrip> =
|
||||
Series::open(&path).expect("expect the time series to open correctly");
|
||||
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
||||
time_range(
|
||||
Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
||||
|
@ -296,7 +296,7 @@ mod test {
|
|||
{
|
||||
let ts: Series<BikeTrip> =
|
||||
Series::open(&path).expect("expect the time series to open correctly");
|
||||
let v: Vec<(&UniqueId, &BikeTrip)> = ts.search_sorted(
|
||||
let v: Vec<(&RecordId, &BikeTrip)> = ts.search_sorted(
|
||||
time_range(
|
||||
Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
|
||||
|
@ -384,10 +384,10 @@ mod test {
|
|||
let ts: Series<BikeTrip> =
|
||||
Series::open(&path).expect("expect the time series to open correctly");
|
||||
|
||||
let trips: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||
let trips: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
||||
assert_eq!(trips.len(), 3);
|
||||
|
||||
let trips: Vec<(&UniqueId, &BikeTrip)> = ts
|
||||
let trips: Vec<(&RecordId, &BikeTrip)> = ts
|
||||
.search(exact_time(Timestamp::DateTime(
|
||||
UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
|
||||
.unwrap()
|
||||
|
@ -421,14 +421,14 @@ mod test {
|
|||
ts.put(trips[2].clone()).expect("expect a successful put");
|
||||
ts.delete(&trip_id).expect("successful delete");
|
||||
|
||||
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||
let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
||||
assert_eq!(recs.len(), 2);
|
||||
}
|
||||
|
||||
{
|
||||
let ts: Series<BikeTrip> =
|
||||
Series::open(&path).expect("expect the time series to open correctly");
|
||||
let recs: Vec<(&UniqueId, &BikeTrip)> = ts.records().collect();
|
||||
let recs: Vec<(&RecordId, &BikeTrip)> = ts.records().collect();
|
||||
assert_eq!(recs.len(), 2);
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue