Skip to content

Commit

Permalink
Add fn for mark as read based on POST of position
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Oct 27, 2024
1 parent ae5b853 commit bf5e7ad
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 28 deletions.
83 changes: 63 additions & 20 deletions crates/collection/src/cache/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,69 @@ impl CacheInner {
}
}

fn get_pos_record<K>(k: &K, db: &transaction::TransactionalTree) -> Option<PositionRecord>
where
K: AsRef<[u8]> + ?Sized,
{
db.get(k.as_ref())
.map_err(|e| error!("Db get error: {}", e))
.ok()
.flatten()
.and_then(|data| {
bincode::deserialize::<PositionRecord>(&data)
.map_err(|e| error!("Db item deserialization error: {}", e))
.ok()
})
}

// positions
impl CacheInner {
pub(crate) fn mark_as_finished<P, S>(
&self,
group: S,
path: P,
ts: Option<TimeStamp>,
) -> Result<()>
where
S: AsRef<str>,
P: AsRef<str>,
{
self.pos_folder
.transaction(|pos_folder| {
let mut folder_rec = get_pos_record(path.as_ref(), pos_folder).unwrap_or_default();
let ts = ts.unwrap_or_else(|| TimeStamp::now());
match folder_rec.get_mut(group.as_ref()) {
Some(p) => {
p.folder_finished = true;
p.timestamp = ts;
}
None => {
if !folder_rec.contains_key(group.as_ref())
&& folder_rec.len() >= MAX_GROUPS
{
return transaction::abort(Error::TooManyGroups);
}
folder_rec.insert(
group.as_ref().to_string(),
PositionItem {
file: "__PLACEHOLDER__".into(),
timestamp: ts,
position: 0.0,
folder_finished: true,
},
);
}
}
match bincode::serialize(&folder_rec).map_err(Error::from) {
Ok(data) => pos_folder.insert(path.as_ref().as_bytes(), data)?,
Err(e) => return transaction::abort(e),
};

Ok(())
})
.map_err(Error::from)
}

pub(crate) fn insert_position<S, P>(
&self,
group: S,
Expand All @@ -243,16 +304,7 @@ impl CacheInner {
if let Some((last_file, last_file_duration)) = self.get_last_file(path) {
(&self.pos_latest, &self.pos_folder)
.transaction(move |(pos_latest, pos_folder)| {
let mut folder_rec = pos_folder
.get(path)
.map_err(|e| error!("Db get error: {}", e))
.ok()
.flatten()
.and_then(|data| {
bincode::deserialize::<PositionRecord>(&data)
.map_err(|e| error!("Db item deserialization error: {}", e))
.ok()
})
let mut folder_rec = get_pos_record(path,pos_folder)
.unwrap_or_default();

if let Some(ts) = ts {
Expand Down Expand Up @@ -334,16 +386,7 @@ impl CacheInner {
None => return Ok(None),
};

Ok(pos_folder
.get(&fld)
.map_err(|e| error!("Error reading position folder record in db: {}", e))
.ok()
.flatten()
.and_then(|r| {
bincode::deserialize::<PositionRecord>(&r)
.map_err(|e| error!("Error deserializing position record {}", e))
.ok()
})
Ok(get_pos_record(&fld, pos_folder)
.and_then(|m| m.get(group.as_ref()).map(|p| p.to_position(fld, 0))))
})
.map_err(|e: TransactionError<Error>| error!("Db transaction error: {}", e))
Expand Down
8 changes: 8 additions & 0 deletions crates/collection/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ impl Drop for CollectionCache {

// positions
impl PositionsTrait for CollectionCache {
fn mark_as_finished<P, S>(&self, group: S, path: P, ts: Option<TimeStamp>) -> Result<()>
where
S: AsRef<str>,
P: AsRef<str>,
{
self.inner.mark_as_finished(group, path, ts)
}

fn insert_position<S, P>(
&self,
group: S,
Expand Down
5 changes: 5 additions & 0 deletions crates/collection/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ pub(crate) enum Collection {

#[enum_dispatch]
pub(crate) trait PositionsTrait {
fn mark_as_finished<P, S>(&self, group: S, path: P, ts: Option<TimeStamp>) -> Result<()>
where
S: AsRef<str>,
P: AsRef<str>;

fn insert_position<S, P>(
&self,
group: S,
Expand Down
20 changes: 12 additions & 8 deletions crates/collection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
path::{Path, PathBuf},
thread::JoinHandle,
};
use tokio::time::error::Elapsed;
pub use util::guess_mime_type;

use crate::{
Expand Down Expand Up @@ -511,19 +512,22 @@ impl Collections {
collection,
..
} = position;
let path = if !folder.is_empty() {
let file_is_empty = file.is_empty();
let path = if file_is_empty {
folder
} else if !folder.is_empty() {
folder + "/" + &file
} else {
file
};
spawn_blocking!({
self.get_cache(collection)?.insert_position(
group,
path,
position,
folder_finished,
Some(timestamp),
)
let col = self.get_cache(collection)?;
if folder_finished && file_is_empty {
debug!("Marked {path} as finished for group {}", group.as_ref());
col.mark_as_finished(&group, &path, Some(timestamp))
} else {
col.insert_position(group, path, position, folder_finished, Some(timestamp))
}
})
.unwrap_or_else(|e| Err(Error::from(e)))
}
Expand Down
13 changes: 13 additions & 0 deletions crates/collection/src/no_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ impl CollectionTrait for CollectionDirect {
}

impl PositionsTrait for CollectionDirect {
fn mark_as_finished<P, S>(
&self,
_group: S,
_path: P,
_ts: Option<crate::audio_meta::TimeStamp>,
) -> Result<()>
where
S: AsRef<str>,
P: AsRef<str>,
{
Ok(())
}

fn insert_position<S, P>(
&self,
_group: S,
Expand Down

0 comments on commit bf5e7ad

Please sign in to comment.