Bind a record to its unique id #130

Merged
savanni merged 2 commits from emseries/bound-records into main 2023-12-27 22:38:43 +00:00
4 changed files with 34 additions and 34 deletions
Showing only changes of commit d2f4ec97c0 - Show all commits

View File

@ -76,4 +76,4 @@ mod types;
pub use criteria::*; pub use criteria::*;
pub use series::Series; pub use series::Series;
pub use types::{EmseriesReadError, EmseriesWriteError, Recordable, Timestamp, UniqueId}; pub use types::{EmseriesReadError, EmseriesWriteError, RecordId, Recordable, Timestamp};

View File

@ -24,7 +24,7 @@ use std::io::{BufRead, BufReader, LineWriter, Write};
use std::iter::Iterator; use std::iter::Iterator;
use criteria::Criteria; use criteria::Criteria;
use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}; use types::{EmseriesReadError, EmseriesWriteError, Record, RecordId, Recordable};
/// An open time series database. /// An open time series database.
/// ///
@ -33,7 +33,7 @@ use types::{EmseriesReadError, EmseriesWriteError, Record, Recordable, UniqueId}
pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> { pub struct Series<T: Clone + Recordable + DeserializeOwned + Serialize> {
//path: String, //path: String,
writer: LineWriter<File>, writer: LineWriter<File>,
records: HashMap<UniqueId, T>, records: HashMap<RecordId, T>,
} }
impl<T> Series<T> impl<T> Series<T>
@ -62,8 +62,8 @@ where
} }
/// Load a file and return all of the records in it. /// Load a file and return all of the records in it.
fn load_file(f: &File) -> Result<HashMap<UniqueId, T>, EmseriesReadError> { fn load_file(f: &File) -> Result<HashMap<RecordId, T>, EmseriesReadError> {
let mut records: HashMap<UniqueId, T> = HashMap::new(); let mut records: HashMap<RecordId, T> = HashMap::new();
let reader = BufReader::new(f); let reader = BufReader::new(f);
for line in reader.lines() { for line in reader.lines() {
match line { match line {
@ -87,14 +87,14 @@ where
/// Put a new record into the database. A unique id will be assigned to the record and /// Put a new record into the database. A unique id will be assigned to the record and
/// returned. /// returned.
pub fn put(&mut self, entry: T) -> Result<UniqueId, EmseriesWriteError> { pub fn put(&mut self, entry: T) -> Result<RecordId, EmseriesWriteError> {
let uuid = UniqueId::default(); let uuid = RecordId::default();
self.update(uuid.clone(), entry).map(|_| uuid) self.update(uuid.clone(), entry).map(|_| uuid)
} }
/// Update an existing record. The `UniqueId` of the record passed into this function must match /// Update an existing record. The [RecordId] of the record passed into this function must match
/// the `UniqueId` of a record already in the database. /// the [RecordId] of a record already in the database.
pub fn update(&mut self, uuid: UniqueId, entry: T) -> Result<(), EmseriesWriteError> { pub fn update(&mut self, uuid: RecordId, entry: T) -> Result<(), EmseriesWriteError> {
self.records.insert(uuid.clone(), entry.clone()); self.records.insert(uuid.clone(), entry.clone());
let write_res = match serde_json::to_string(&Record { let write_res = match serde_json::to_string(&Record {
id: uuid, id: uuid,
@ -118,7 +118,7 @@ where
/// Future note: while this deletes a record from the view, it only adds an entry to the /// 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 /// database that indicates `data: null`. If record histories ever become important, the record
/// and its entire history (including this delete) will still be available. /// 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) { if !self.records.contains_key(uuid) {
return Ok(()); return Ok(());
}; };
@ -138,7 +138,7 @@ where
} }
/// Get all of the records in the database. /// 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() self.records.iter()
} }
@ -148,29 +148,29 @@ where
pub fn search<'s>( pub fn search<'s>(
&'s self, &'s self,
criteria: impl Criteria + 's, 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)) self.records().filter(move |&tr| criteria.apply(tr.1))
} }
/// Perform a search and sort the resulting records based on the comparison. /// 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 where
C: Criteria + 's, C: Criteria + 's,
CMP: FnMut(&(&UniqueId, &T), &(&UniqueId, &T)) -> Ordering, CMP: FnMut(&(&RecordId, &T), &(&RecordId, &T)) -> Ordering,
{ {
let search_iter = self.search(criteria); 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.sort_by(compare);
records records
} }
/// Get an exact record from the database based on unique id. /// 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() self.records.get(uuid).cloned()
} }
/* /*
pub fn remove(&self, uuid: UniqueId) -> Result<(), EmseriesError> { pub fn remove(&self, uuid: RecordId) -> Result<(), EmseriesError> {
unimplemented!() unimplemented!()
} }
*/ */

View File

@ -145,26 +145,26 @@ pub trait Recordable {
/// ///
/// This is a wrapper around a basic uuid with some extra convenience methods. /// This is a wrapper around a basic uuid with some extra convenience methods.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)] #[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 { fn default() -> Self {
Self(Uuid::new_v4()) Self(Uuid::new_v4())
} }
} }
impl str::FromStr for UniqueId { impl str::FromStr for RecordId {
type Err = EmseriesReadError; 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> { fn from_str(val: &str) -> Result<Self, Self::Err> {
Uuid::parse_str(val) Uuid::parse_str(val)
.map(UniqueId) .map(RecordId)
.map_err(EmseriesReadError::UUIDParseError) .map_err(EmseriesReadError::UUIDParseError)
} }
} }
impl fmt::Display for UniqueId { impl fmt::Display for RecordId {
/// Convert to a hyphenated string /// Convert to a hyphenated string
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0.to_hyphenated()) write!(f, "{}", self.0.to_hyphenated())
@ -175,7 +175,7 @@ impl fmt::Display for UniqueId {
/// Recordable trait. /// Recordable trait.
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
pub struct Record<T: Clone + Recordable> { pub struct Record<T: Clone + Recordable> {
pub id: UniqueId, pub id: RecordId,
pub data: Option<T>, pub data: Option<T>,
} }

View File

@ -162,7 +162,7 @@ mod test {
ts.put(trip.clone()).expect("expect a successful put"); 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( .search(exact_time(Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
.unwrap() .unwrap()
@ -185,7 +185,7 @@ mod test {
ts.put(trip.clone()).expect("expect a successful put"); 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( time_range(
Timestamp::DateTime( Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
@ -226,7 +226,7 @@ mod test {
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); 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( time_range(
Timestamp::DateTime( Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
@ -268,7 +268,7 @@ mod test {
{ {
let mut ts: Series<BikeTrip> = let mut ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); 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( time_range(
Timestamp::DateTime( Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
@ -296,7 +296,7 @@ mod test {
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); 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( time_range(
Timestamp::DateTime( Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0) UTC.with_ymd_and_hms(2011, 10, 31, 0, 0, 0)
@ -384,10 +384,10 @@ mod test {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); 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); assert_eq!(trips.len(), 3);
let trips: Vec<(&UniqueId, &BikeTrip)> = ts let trips: Vec<(&RecordId, &BikeTrip)> = ts
.search(exact_time(Timestamp::DateTime( .search(exact_time(Timestamp::DateTime(
UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0) UTC.with_ymd_and_hms(2011, 11, 02, 0, 0, 0)
.unwrap() .unwrap()
@ -421,14 +421,14 @@ mod test {
ts.put(trips[2].clone()).expect("expect a successful put"); ts.put(trips[2].clone()).expect("expect a successful put");
ts.delete(&trip_id).expect("successful delete"); 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); assert_eq!(recs.len(), 2);
} }
{ {
let ts: Series<BikeTrip> = let ts: Series<BikeTrip> =
Series::open(&path).expect("expect the time series to open correctly"); 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); assert_eq!(recs.len(), 2);
} }
}) })