use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use serde::{Deserialize, Serialize};
use super::actions::QueuedAction;
use crate::error::Result;
use crate::library::MediaItem;
use crate::types::ItemWithRelations;
use crate::types::book::Book;
use crate::types::episode::Episode;
use crate::types::library::ServerLibraryForRead;
use crate::types::media::MediaQuery;
use crate::types::movie::Movie;
use crate::types::person::Person;
use crate::types::serie::Serie;
use crate::types::tag::Tag;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SyncKind {
Tags,
People,
Series,
Movies,
Episodes,
Books,
Medias,
Deleted,
}
impl SyncKind {
pub const ALL: [SyncKind; 8] = [
SyncKind::Tags,
SyncKind::People,
SyncKind::Series,
SyncKind::Movies,
SyncKind::Episodes,
SyncKind::Books,
SyncKind::Medias,
SyncKind::Deleted,
];
pub fn name(self) -> &'static str {
match self {
SyncKind::Tags => "tags",
SyncKind::People => "people",
SyncKind::Series => "series",
SyncKind::Movies => "movies",
SyncKind::Episodes => "episodes",
SyncKind::Books => "books",
SyncKind::Medias => "medias",
SyncKind::Deleted => "deleted",
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SyncState {
pub generation: i64,
pub watermarks: BTreeMap<SyncKind, i64>,
#[serde(default)]
pub media_resume: Option<String>,
}
#[derive(Debug, Clone, Default)]
pub struct Changes {
pub medias: Vec<MediaItem>,
pub tags: Vec<Tag>,
pub people: Vec<Person>,
pub series: Vec<ItemWithRelations<Serie>>,
pub movies: Vec<ItemWithRelations<Movie>>,
pub episodes: Vec<Episode>,
pub books: Vec<ItemWithRelations<Book>>,
pub removed: Vec<Removal>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Removal {
pub kind: SyncKind,
pub id: String,
pub date: Option<i64>,
}
impl Changes {
pub fn movie(movie: ItemWithRelations<Movie>) -> Changes {
Changes {
movies: vec![movie],
..Default::default()
}
}
pub fn episode(episode: Episode) -> Changes {
Changes {
episodes: vec![episode],
..Default::default()
}
}
pub fn is_empty(&self) -> bool {
self.medias.is_empty()
&& self.tags.is_empty()
&& self.people.is_empty()
&& self.series.is_empty()
&& self.movies.is_empty()
&& self.episodes.is_empty()
&& self.books.is_empty()
&& self.removed.is_empty()
}
pub fn kinds(&self) -> Vec<SyncKind> {
let mut kinds = Vec::new();
let mut add = |kind: SyncKind, present: bool| {
if present && !kinds.contains(&kind) {
kinds.push(kind);
}
};
add(SyncKind::Tags, !self.tags.is_empty());
add(SyncKind::People, !self.people.is_empty());
add(SyncKind::Series, !self.series.is_empty());
add(SyncKind::Movies, !self.movies.is_empty());
add(SyncKind::Episodes, !self.episodes.is_empty());
add(SyncKind::Books, !self.books.is_empty());
add(SyncKind::Medias, !self.medias.is_empty());
for removal in &self.removed {
add(removal.kind, true);
}
kinds
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ItemIds {
pub added: BTreeSet<String>,
pub updated: BTreeSet<String>,
pub removed: BTreeSet<String>,
}
impl ItemIds {
pub fn is_empty(&self) -> bool {
self.added.is_empty() && self.updated.is_empty() && self.removed.is_empty()
}
pub fn len(&self) -> usize {
self.added.len() + self.updated.len() + self.removed.len()
}
pub fn merge(&mut self, later: ItemIds) {
for id in later.added {
if self.removed.remove(&id) || self.updated.contains(&id) {
self.updated.insert(id);
} else {
self.added.insert(id);
}
}
for id in later.updated {
if !self.added.contains(&id) {
self.removed.remove(&id);
self.updated.insert(id);
}
}
for id in later.removed {
if !self.added.remove(&id) {
self.updated.remove(&id);
self.removed.insert(id);
}
}
}
}
pub type Written = BTreeMap<SyncKind, ItemIds>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThumbRecord {
pub library: String,
pub media: String,
pub size: u64,
pub version: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FileLinks {
pub movies: HashSet<String>,
pub episodes: HashMap<String, Vec<EpisodeSpan>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EpisodeSpan {
pub season: u32,
pub first: u32,
pub last: u32,
}
impl FileLinks {
pub fn has_episode(&self, serie: &str, season: u32, number: u32) -> bool {
(self.episodes.get(serie).into_iter().flatten())
.any(|span| span.season == season && (span.first..=span.last).contains(&number))
}
}
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
pub trait MirrorStore: crate::transport::MaybeSend {
async fn libraries(&self) -> Result<Vec<ServerLibraryForRead>>;
async fn set_libraries(&self, libraries: Vec<ServerLibraryForRead>) -> Result<Vec<String>>;
async fn put_library(&self, library: ServerLibraryForRead) -> Result<()>;
async fn remove_library(&self, library: &str) -> Result<()>;
async fn sync_state(&self, library: &str) -> Result<Option<SyncState>>;
async fn set_sync_state(&self, library: &str, state: SyncState) -> Result<()>;
async fn apply(&self, library: &str, generation: i64, changes: Changes) -> Result<Written>;
async fn sweep(&self, library: &str, kind: SyncKind, generation: i64) -> Result<Vec<String>>;
async fn medias(
&self,
library: &str,
query: MediaQuery,
offset: usize,
) -> Result<Vec<MediaItem>>;
async fn count_medias(&self, library: &str, query: MediaQuery) -> Result<u64>;
async fn media(&self, library: &str, id: &str) -> Result<Option<MediaItem>>;
async fn items(
&self,
library: &str,
kind: SyncKind,
serie: Option<String>,
) -> Result<Vec<serde_json::Value>>;
async fn items_by_id(
&self,
library: &str,
kind: SyncKind,
ids: Vec<String>,
) -> Result<Vec<serde_json::Value>>;
async fn count(&self, library: &str, kind: SyncKind) -> Result<u64>;
async fn file_links(&self, library: &str) -> Result<FileLinks>;
async fn actions(&self) -> Result<Vec<QueuedAction>>;
async fn queue_actions(
&self,
library: &str,
generation: i64,
actions: Vec<QueuedAction>,
replaced: Vec<i64>,
changes: Changes,
) -> Result<(Vec<i64>, Written)>;
async fn update_actions(&self, actions: Vec<QueuedAction>) -> Result<()>;
async fn remove_actions(
&self,
library: &str,
generation: i64,
ids: Vec<i64>,
changes: Changes,
) -> Result<Written>;
async fn thumb(&self, library: &str, media: &str) -> Result<Option<ThumbRecord>>;
async fn put_thumb(&self, record: ThumbRecord) -> Result<()>;
async fn remove_thumbs(&self, library: &str, medias: Vec<String>) -> Result<()>;
async fn thumbs_size(&self) -> Result<u64>;
async fn thumbs_over(&self, limit: u64) -> Result<Vec<ThumbRecord>>;
}