use lunar_lib::{
database::{
CompareAndSwapTransaction, Createable, CustomTransactionError, DatabaseEntry, DbIdExt,
DbIdIterExt, Deleteable, Entry, Mergeable, TransactionError, caching::Cacheable,
},
id::Id,
log::warn,
vec_ext::VecExtensions,
};
use crate::{
SeleneIdExt,
database::{Searchable, Selene},
library::artist::{Artist, ArtistCreateArgs, ArtistCreationError},
};
impl DatabaseEntry for Artist {
type DbInner = Selene;
const VERSION_NUMBER: u32 = 1;
const TREE_NAME: &str = "artist";
fn pre_upsert(
entry: &mut Entry<Self>,
is_new: bool,
cas_tx: &mut CompareAndSwapTransaction<Selene>,
) -> Result<(), TransactionError> {
if is_new {
Self::update_search_index(entry, cas_tx)?;
}
Ok(())
}
}
impl Cacheable for Artist {}
impl Searchable for Artist {
const SEARCH_INDEX: &'static str = "artist_search";
fn search_name(&self) -> Option<&str> {
Some(&*self.name)
}
}
impl Mergeable for Artist {
fn merge_data(&mut self, from: Self) {
self.name = from.name;
self.art = self.art.take().or(from.art);
self.description = self.description.take().or(from.description);
self.tracks.extend_unique(from.tracks);
self.albums.extend_unique(from.albums);
}
fn relink_references(
from: &Entry<Self>,
to: Id<Self>,
cas_tx: &mut CompareAndSwapTransaction<Self::DbInner>,
) -> Result<(), TransactionError> {
from.tracks.iter().tx_fetch_and_update(
|old, _| {
let Some(mut track) = old else {
warn!(
"Artist '{}' attempted to relink to a track which does not exist yet",
from.name
);
return Ok(None);
};
track.metadata.artists.push_unique(to);
Ok(Some(track))
},
cas_tx,
)?;
from.albums.iter().tx_fetch_and_update(
|old, _| {
let Some(mut album) = old else {
warn!(
"Artist '{}' attempted to relink to a album which does not exist yet",
from.name
);
return Ok(None);
};
album.artists.push_unique(to);
Ok(Some(album))
},
cas_tx,
)?;
Ok(())
}
}
impl Createable for Artist {
type CreateArgs = ArtistCreateArgs;
type Err = ArtistCreationError;
fn create(
args: Self::CreateArgs,
cas_tx: &mut CompareAndSwapTransaction<Self::DbInner>,
) -> Result<Entry<Self>, CustomTransactionError<Self::Err>> {
let id = Id::from_string_hash(&args.name);
if id.tx_check(cas_tx)? {
return Err(CustomTransactionError::Transaction(
TransactionError::AlreadyInDatabase,
));
}
args.tracks.tx_fetch_and_update(
|old, _| {
let mut track = old.expect("Invalid ref");
track.metadata.artists.push_unique(id);
Ok(Some(track))
},
cas_tx,
)?;
args.albums.tx_fetch_and_update(
|old, _| {
let mut album = old.expect("Invalid ref");
album.artists.push_unique(id);
Ok(Some(album))
},
cas_tx,
)?;
let artist = Artist::new(args.name)
.map_err(CustomTransactionError::Closure)?
.to_entry(id);
Ok(artist)
}
}
impl Deleteable for Artist {
fn unlink_references(
_old: Entry<Self>,
_cas_tx: &mut CompareAndSwapTransaction<Self::DbInner>,
) -> Result<(), TransactionError> {
todo!()
}
}