2023-09-04 21:19:46 +00:00
|
|
|
mod alias;
|
|
|
|
mod delete_token;
|
|
|
|
mod hash;
|
|
|
|
mod metrics;
|
|
|
|
mod migrate;
|
2024-03-31 21:00:23 +00:00
|
|
|
mod notification_map;
|
2023-09-04 21:19:46 +00:00
|
|
|
|
2022-04-24 22:10:15 +00:00
|
|
|
use crate::{
|
|
|
|
config,
|
|
|
|
details::Details,
|
2023-09-02 01:50:10 +00:00
|
|
|
error_code::{ErrorCode, OwnedErrorCode},
|
2023-08-16 00:19:03 +00:00
|
|
|
stream::LocalBoxStream,
|
2022-04-24 22:10:15 +00:00
|
|
|
};
|
2023-08-28 21:02:11 +00:00
|
|
|
use base64::Engine;
|
2023-09-11 00:08:01 +00:00
|
|
|
use futures_core::Stream;
|
2023-08-15 01:00:00 +00:00
|
|
|
use std::{fmt::Debug, sync::Arc};
|
2023-07-23 15:23:37 +00:00
|
|
|
use url::Url;
|
2022-03-24 18:16:41 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2023-09-02 16:52:55 +00:00
|
|
|
pub(crate) mod postgres;
|
2022-03-24 22:09:15 +00:00
|
|
|
pub(crate) mod sled;
|
|
|
|
|
2023-09-03 17:47:06 +00:00
|
|
|
pub(crate) use alias::Alias;
|
|
|
|
pub(crate) use delete_token::DeleteToken;
|
2023-08-14 03:06:42 +00:00
|
|
|
pub(crate) use hash::Hash;
|
2023-08-16 21:32:19 +00:00
|
|
|
pub(crate) use migrate::{migrate_04, migrate_repo};
|
2024-04-01 01:26:15 +00:00
|
|
|
pub(crate) use notification_map::NotificationEntry;
|
2024-03-31 21:00:23 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
pub(crate) type ArcRepo = Arc<dyn FullRepo>;
|
2023-08-14 03:06:42 +00:00
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2022-03-25 23:47:50 +00:00
|
|
|
pub(crate) enum Repo {
|
|
|
|
Sled(self::sled::SledRepo),
|
2023-09-02 16:52:55 +00:00
|
|
|
Postgres(self::postgres::PostgresRepo),
|
2022-03-25 23:47:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
enum MaybeUuid {
|
|
|
|
Uuid(Uuid),
|
|
|
|
Name(String),
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
#[derive(Debug)]
|
2023-07-07 18:17:26 +00:00
|
|
|
pub(crate) struct HashAlreadyExists;
|
2023-08-16 00:19:03 +00:00
|
|
|
#[derive(Debug)]
|
2023-07-07 18:17:26 +00:00
|
|
|
pub(crate) struct AliasAlreadyExists;
|
2023-08-16 20:12:16 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct VariantAlreadyExists;
|
2023-07-07 18:17:26 +00:00
|
|
|
|
2022-04-03 01:56:29 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2022-04-01 21:51:12 +00:00
|
|
|
pub(crate) struct UploadId {
|
|
|
|
id: Uuid,
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:50:10 +00:00
|
|
|
#[derive(Debug)]
|
2022-04-01 21:51:12 +00:00
|
|
|
pub(crate) enum UploadResult {
|
2023-09-02 01:50:10 +00:00
|
|
|
Success {
|
|
|
|
alias: Alias,
|
|
|
|
token: DeleteToken,
|
|
|
|
},
|
|
|
|
Failure {
|
|
|
|
message: String,
|
|
|
|
code: OwnedErrorCode,
|
|
|
|
},
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum RepoError {
|
|
|
|
#[error("Error in sled")]
|
|
|
|
SledError(#[from] crate::repo::sled::SledError),
|
|
|
|
|
2023-09-02 18:52:15 +00:00
|
|
|
#[error("Error in postgres")]
|
|
|
|
PostgresError(#[from] crate::repo::postgres::PostgresError),
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
#[error("Upload was already claimed")]
|
|
|
|
AlreadyClaimed,
|
|
|
|
|
|
|
|
#[error("Panic in blocking operation")]
|
|
|
|
Canceled,
|
2023-07-05 14:52:19 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 01:50:10 +00:00
|
|
|
impl RepoError {
|
|
|
|
pub(crate) const fn error_code(&self) -> ErrorCode {
|
|
|
|
match self {
|
|
|
|
Self::SledError(e) => e.error_code(),
|
2023-09-02 23:30:45 +00:00
|
|
|
Self::PostgresError(e) => e.error_code(),
|
2023-09-02 01:50:10 +00:00
|
|
|
Self::AlreadyClaimed => ErrorCode::ALREADY_CLAIMED,
|
|
|
|
Self::Canceled => ErrorCode::PANIC,
|
|
|
|
}
|
|
|
|
}
|
2023-09-05 02:51:27 +00:00
|
|
|
|
|
|
|
pub(crate) const fn is_disconnected(&self) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::PostgresError(e) => e.is_disconnected(),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 01:50:10 +00:00
|
|
|
}
|
|
|
|
|
2022-04-02 21:44:03 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait FullRepo:
|
2023-02-25 17:34:48 +00:00
|
|
|
UploadRepo
|
2022-04-02 21:44:03 +00:00
|
|
|
+ SettingsRepo
|
2023-08-16 21:09:40 +00:00
|
|
|
+ DetailsRepo
|
2022-04-02 21:44:03 +00:00
|
|
|
+ AliasRepo
|
|
|
|
+ QueueRepo
|
|
|
|
+ HashRepo
|
2024-03-28 17:04:40 +00:00
|
|
|
+ VariantRepo
|
2023-08-16 16:47:36 +00:00
|
|
|
+ StoreMigrationRepo
|
2023-07-19 02:56:13 +00:00
|
|
|
+ AliasAccessRepo
|
2023-07-22 22:57:52 +00:00
|
|
|
+ VariantAccessRepo
|
2023-07-23 15:23:37 +00:00
|
|
|
+ ProxyRepo
|
2022-04-02 21:44:03 +00:00
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ Debug
|
|
|
|
{
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn health_check(&self) -> Result<(), RepoError>;
|
2023-01-29 17:36:09 +00:00
|
|
|
|
2022-09-28 04:19:52 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn identifier_from_alias(&self, alias: &Alias) -> Result<Option<Arc<str>>, RepoError> {
|
2023-07-05 21:46:44 +00:00
|
|
|
let Some(hash) = self.hash(alias).await? else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
|
2023-07-07 18:17:26 +00:00
|
|
|
self.identifier(hash).await
|
2022-04-02 21:44:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 04:19:52 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn aliases_from_alias(&self, alias: &Alias) -> Result<Vec<Alias>, RepoError> {
|
2023-07-05 21:46:44 +00:00
|
|
|
let Some(hash) = self.hash(alias).await? else {
|
|
|
|
return Ok(vec![]);
|
|
|
|
};
|
|
|
|
|
2023-09-01 23:41:04 +00:00
|
|
|
self.aliases_for_hash(hash).await
|
2022-04-02 21:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:36:09 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> FullRepo for Arc<T>
|
2023-01-29 17:47:28 +00:00
|
|
|
where
|
|
|
|
T: FullRepo,
|
|
|
|
{
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn health_check(&self) -> Result<(), RepoError> {
|
2023-01-29 17:36:09 +00:00
|
|
|
T::health_check(self).await
|
|
|
|
}
|
|
|
|
}
|
2022-09-11 00:29:22 +00:00
|
|
|
|
2023-08-15 01:00:00 +00:00
|
|
|
pub(crate) trait BaseRepo {}
|
2022-03-29 17:51:16 +00:00
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> BaseRepo for Arc<T> where T: BaseRepo {}
|
2022-09-11 00:29:22 +00:00
|
|
|
|
2023-07-23 15:23:37 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait ProxyRepo: BaseRepo {
|
|
|
|
async fn relate_url(&self, url: Url, alias: Alias) -> Result<(), RepoError>;
|
|
|
|
|
|
|
|
async fn related(&self, url: Url) -> Result<Option<Alias>, RepoError>;
|
|
|
|
|
|
|
|
async fn remove_relation(&self, alias: Alias) -> Result<(), RepoError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> ProxyRepo for Arc<T>
|
2023-07-23 15:23:37 +00:00
|
|
|
where
|
|
|
|
T: ProxyRepo,
|
|
|
|
{
|
|
|
|
async fn relate_url(&self, url: Url, alias: Alias) -> Result<(), RepoError> {
|
|
|
|
T::relate_url(self, url, alias).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn related(&self, url: Url) -> Result<Option<Alias>, RepoError> {
|
|
|
|
T::related(self, url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn remove_relation(&self, alias: Alias) -> Result<(), RepoError> {
|
|
|
|
T::remove_relation(self, alias).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 02:56:13 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait AliasAccessRepo: BaseRepo {
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn accessed_alias(&self, alias: Alias) -> Result<(), RepoError> {
|
|
|
|
self.set_accessed_alias(alias, time::OffsetDateTime::now_utc())
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn set_accessed_alias(
|
|
|
|
&self,
|
|
|
|
alias: Alias,
|
|
|
|
accessed: time::OffsetDateTime,
|
|
|
|
) -> Result<(), RepoError>;
|
|
|
|
|
|
|
|
async fn alias_accessed_at(
|
|
|
|
&self,
|
|
|
|
alias: Alias,
|
|
|
|
) -> Result<Option<time::OffsetDateTime>, RepoError>;
|
2023-07-19 02:56:13 +00:00
|
|
|
|
|
|
|
async fn older_aliases(
|
|
|
|
&self,
|
|
|
|
timestamp: time::OffsetDateTime,
|
2023-08-16 00:19:03 +00:00
|
|
|
) -> Result<LocalBoxStream<'static, Result<Alias, RepoError>>, RepoError>;
|
2023-07-19 02:56:13 +00:00
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn remove_alias_access(&self, alias: Alias) -> Result<(), RepoError>;
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> AliasAccessRepo for Arc<T>
|
2023-07-19 02:56:13 +00:00
|
|
|
where
|
|
|
|
T: AliasAccessRepo,
|
|
|
|
{
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn set_accessed_alias(
|
|
|
|
&self,
|
|
|
|
alias: Alias,
|
|
|
|
accessed: time::OffsetDateTime,
|
|
|
|
) -> Result<(), RepoError> {
|
|
|
|
T::set_accessed_alias(self, alias, accessed).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn alias_accessed_at(
|
|
|
|
&self,
|
|
|
|
alias: Alias,
|
|
|
|
) -> Result<Option<time::OffsetDateTime>, RepoError> {
|
|
|
|
T::alias_accessed_at(self, alias).await
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn older_aliases(
|
|
|
|
&self,
|
|
|
|
timestamp: time::OffsetDateTime,
|
2023-08-16 00:19:03 +00:00
|
|
|
) -> Result<LocalBoxStream<'static, Result<Alias, RepoError>>, RepoError> {
|
2023-07-19 02:56:13 +00:00
|
|
|
T::older_aliases(self, timestamp).await
|
|
|
|
}
|
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn remove_alias_access(&self, alias: Alias) -> Result<(), RepoError> {
|
|
|
|
T::remove_alias_access(self, alias).await
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-07-22 22:57:52 +00:00
|
|
|
pub(crate) trait VariantAccessRepo: BaseRepo {
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn accessed_variant(&self, hash: Hash, variant: String) -> Result<(), RepoError> {
|
|
|
|
self.set_accessed_variant(hash, variant, time::OffsetDateTime::now_utc())
|
|
|
|
.await
|
|
|
|
}
|
2023-07-19 02:56:13 +00:00
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn set_accessed_variant(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
accessed: time::OffsetDateTime,
|
|
|
|
) -> Result<(), RepoError>;
|
|
|
|
|
|
|
|
async fn variant_accessed_at(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
) -> Result<Option<time::OffsetDateTime>, RepoError>;
|
2023-07-22 23:50:04 +00:00
|
|
|
|
2023-07-22 22:57:52 +00:00
|
|
|
async fn older_variants(
|
2023-07-19 02:56:13 +00:00
|
|
|
&self,
|
|
|
|
timestamp: time::OffsetDateTime,
|
2023-08-16 00:19:03 +00:00
|
|
|
) -> Result<LocalBoxStream<'static, Result<(Hash, String), RepoError>>, RepoError>;
|
2023-07-19 02:56:13 +00:00
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn remove_variant_access(&self, hash: Hash, variant: String) -> Result<(), RepoError>;
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> VariantAccessRepo for Arc<T>
|
2023-07-19 02:56:13 +00:00
|
|
|
where
|
2023-07-22 22:57:52 +00:00
|
|
|
T: VariantAccessRepo,
|
2023-07-19 02:56:13 +00:00
|
|
|
{
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn set_accessed_variant(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
accessed: time::OffsetDateTime,
|
|
|
|
) -> Result<(), RepoError> {
|
|
|
|
T::set_accessed_variant(self, hash, variant, accessed).await
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn variant_accessed_at(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
) -> Result<Option<time::OffsetDateTime>, RepoError> {
|
|
|
|
T::variant_accessed_at(self, hash, variant).await
|
2023-07-22 23:50:04 +00:00
|
|
|
}
|
|
|
|
|
2023-07-22 22:57:52 +00:00
|
|
|
async fn older_variants(
|
2023-07-19 02:56:13 +00:00
|
|
|
&self,
|
|
|
|
timestamp: time::OffsetDateTime,
|
2023-08-16 00:19:03 +00:00
|
|
|
) -> Result<LocalBoxStream<'static, Result<(Hash, String), RepoError>>, RepoError> {
|
2023-07-22 22:57:52 +00:00
|
|
|
T::older_variants(self, timestamp).await
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn remove_variant_access(&self, hash: Hash, variant: String) -> Result<(), RepoError> {
|
|
|
|
T::remove_variant_access(self, hash, variant).await
|
2023-07-19 02:56:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:51:12 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait UploadRepo: BaseRepo {
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn create_upload(&self) -> Result<UploadId, RepoError>;
|
2022-04-03 01:56:29 +00:00
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn wait(&self, upload_id: UploadId) -> Result<UploadResult, RepoError>;
|
2022-04-01 21:51:12 +00:00
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn claim(&self, upload_id: UploadId) -> Result<(), RepoError>;
|
2022-04-01 21:51:12 +00:00
|
|
|
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn complete_upload(
|
|
|
|
&self,
|
|
|
|
upload_id: UploadId,
|
|
|
|
result: UploadResult,
|
|
|
|
) -> Result<(), RepoError>;
|
2022-04-01 21:51:12 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> UploadRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
|
|
|
T: UploadRepo,
|
|
|
|
{
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn create_upload(&self) -> Result<UploadId, RepoError> {
|
|
|
|
T::create_upload(self).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn wait(&self, upload_id: UploadId) -> Result<UploadResult, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::wait(self, upload_id).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
async fn claim(&self, upload_id: UploadId) -> Result<(), RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::claim(self, upload_id).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn complete_upload(
|
|
|
|
&self,
|
|
|
|
upload_id: UploadId,
|
|
|
|
result: UploadResult,
|
|
|
|
) -> Result<(), RepoError> {
|
|
|
|
T::complete_upload(self, upload_id, result).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 19:12:38 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub(crate) struct JobId(Uuid);
|
|
|
|
|
2024-03-09 18:15:23 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub(crate) enum JobResult {
|
|
|
|
Success,
|
|
|
|
Failure,
|
|
|
|
Aborted,
|
|
|
|
}
|
|
|
|
|
2023-08-13 19:12:38 +00:00
|
|
|
impl JobId {
|
|
|
|
pub(crate) fn gen() -> Self {
|
2023-08-14 00:47:20 +00:00
|
|
|
Self(Uuid::now_v7())
|
2023-08-13 19:12:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) const fn as_bytes(&self) -> &[u8; 16] {
|
|
|
|
self.0.as_bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) const fn from_bytes(bytes: [u8; 16]) -> Self {
|
|
|
|
Self(Uuid::from_bytes(bytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:51:16 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait QueueRepo: BaseRepo {
|
2024-01-30 20:57:48 +00:00
|
|
|
async fn queue_length(&self) -> Result<u64, RepoError>;
|
|
|
|
|
2024-01-24 23:14:31 +00:00
|
|
|
async fn push(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
job: serde_json::Value,
|
|
|
|
unique_key: Option<&'static str>,
|
|
|
|
) -> Result<Option<JobId>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn pop(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
2023-09-03 17:47:06 +00:00
|
|
|
) -> Result<(JobId, serde_json::Value), RepoError>;
|
2023-08-13 19:12:38 +00:00
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn heartbeat(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
|
|
|
job_id: JobId,
|
|
|
|
) -> Result<(), RepoError>;
|
2023-08-13 19:12:38 +00:00
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn complete_job(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
|
|
|
job_id: JobId,
|
2024-03-09 18:15:23 +00:00
|
|
|
job_status: JobResult,
|
2023-08-15 02:17:57 +00:00
|
|
|
) -> Result<(), RepoError>;
|
2022-03-29 17:51:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> QueueRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
|
|
|
T: QueueRepo,
|
|
|
|
{
|
2024-01-30 20:57:48 +00:00
|
|
|
async fn queue_length(&self) -> Result<u64, RepoError> {
|
|
|
|
T::queue_length(self).await
|
|
|
|
}
|
|
|
|
|
2024-01-24 23:14:31 +00:00
|
|
|
async fn push(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
job: serde_json::Value,
|
|
|
|
unique_key: Option<&'static str>,
|
|
|
|
) -> Result<Option<JobId>, RepoError> {
|
|
|
|
T::push(self, queue, job, unique_key).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn pop(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
2023-09-03 17:47:06 +00:00
|
|
|
) -> Result<(JobId, serde_json::Value), RepoError> {
|
2023-08-15 02:17:57 +00:00
|
|
|
T::pop(self, queue, worker_id).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
2023-08-13 19:12:38 +00:00
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn heartbeat(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
|
|
|
job_id: JobId,
|
|
|
|
) -> Result<(), RepoError> {
|
|
|
|
T::heartbeat(self, queue, worker_id, job_id).await
|
2023-08-13 19:12:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 02:17:57 +00:00
|
|
|
async fn complete_job(
|
|
|
|
&self,
|
|
|
|
queue: &'static str,
|
|
|
|
worker_id: Uuid,
|
|
|
|
job_id: JobId,
|
2024-03-09 18:15:23 +00:00
|
|
|
job_status: JobResult,
|
2023-08-15 02:17:57 +00:00
|
|
|
) -> Result<(), RepoError> {
|
2024-03-09 18:15:23 +00:00
|
|
|
T::complete_job(self, queue, worker_id, job_id, job_status).await
|
2023-08-13 19:12:38 +00:00
|
|
|
}
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 17:51:16 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait SettingsRepo: BaseRepo {
|
2023-08-15 01:00:00 +00:00
|
|
|
async fn set(&self, key: &'static str, value: Arc<[u8]>) -> Result<(), RepoError>;
|
|
|
|
async fn get(&self, key: &'static str) -> Result<Option<Arc<[u8]>>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> SettingsRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
|
|
|
T: SettingsRepo,
|
|
|
|
{
|
2023-08-15 01:00:00 +00:00
|
|
|
async fn set(&self, key: &'static str, value: Arc<[u8]>) -> Result<(), RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::set(self, key, value).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-15 01:00:00 +00:00
|
|
|
async fn get(&self, key: &'static str) -> Result<Option<Arc<[u8]>>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::get(self, key).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 21:09:40 +00:00
|
|
|
pub(crate) trait DetailsRepo: BaseRepo {
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn relate_details(
|
2022-03-26 21:49:23 +00:00
|
|
|
&self,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
2022-03-26 21:49:23 +00:00
|
|
|
details: &Details,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<(), RepoError>;
|
|
|
|
async fn details(&self, identifier: &Arc<str>) -> Result<Option<Details>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn cleanup_details(&self, identifier: &Arc<str>) -> Result<(), RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 21:09:40 +00:00
|
|
|
impl<T> DetailsRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
2023-08-16 21:09:40 +00:00
|
|
|
T: DetailsRepo,
|
2022-09-11 00:29:22 +00:00
|
|
|
{
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn relate_details(
|
2022-09-11 00:29:22 +00:00
|
|
|
&self,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
2022-09-11 00:29:22 +00:00
|
|
|
details: &Details,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<(), RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::relate_details(self, identifier, details).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn details(&self, identifier: &Arc<str>) -> Result<Option<Details>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::details(self, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn cleanup_details(&self, identifier: &Arc<str>) -> Result<(), RepoError> {
|
2023-08-16 21:09:40 +00:00
|
|
|
T::cleanup_details(self, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-17 03:07:42 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 16:47:36 +00:00
|
|
|
pub(crate) trait StoreMigrationRepo: BaseRepo {
|
2023-07-17 03:07:42 +00:00
|
|
|
async fn is_continuing_migration(&self) -> Result<bool, RepoError>;
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn mark_migrated(
|
2023-07-17 03:07:42 +00:00
|
|
|
&self,
|
2023-09-02 23:30:45 +00:00
|
|
|
old_identifier: &Arc<str>,
|
|
|
|
new_identifier: &Arc<str>,
|
|
|
|
) -> Result<(), RepoError>;
|
2023-07-17 03:07:42 +00:00
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn is_migrated(&self, identifier: &Arc<str>) -> Result<bool, RepoError>;
|
2023-07-17 03:07:42 +00:00
|
|
|
|
|
|
|
async fn clear(&self) -> Result<(), RepoError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 16:47:36 +00:00
|
|
|
impl<T> StoreMigrationRepo for Arc<T>
|
2023-07-17 03:07:42 +00:00
|
|
|
where
|
2023-08-16 16:47:36 +00:00
|
|
|
T: StoreMigrationRepo,
|
2023-07-17 03:07:42 +00:00
|
|
|
{
|
|
|
|
async fn is_continuing_migration(&self) -> Result<bool, RepoError> {
|
|
|
|
T::is_continuing_migration(self).await
|
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn mark_migrated(
|
2023-07-17 03:07:42 +00:00
|
|
|
&self,
|
2023-09-02 23:30:45 +00:00
|
|
|
old_identifier: &Arc<str>,
|
|
|
|
new_identifier: &Arc<str>,
|
|
|
|
) -> Result<(), RepoError> {
|
2023-07-17 03:07:42 +00:00
|
|
|
T::mark_migrated(self, old_identifier, new_identifier).await
|
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn is_migrated(&self, identifier: &Arc<str>) -> Result<bool, RepoError> {
|
2023-07-17 03:07:42 +00:00
|
|
|
T::is_migrated(self, identifier).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn clear(&self) -> Result<(), RepoError> {
|
|
|
|
T::clear(self).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 23:21:46 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2023-08-28 21:02:11 +00:00
|
|
|
pub(crate) struct OrderedHash {
|
|
|
|
timestamp: time::OffsetDateTime,
|
|
|
|
hash: Hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct HashPage {
|
|
|
|
pub(crate) limit: usize,
|
2023-08-29 17:53:29 +00:00
|
|
|
prev: Option<Hash>,
|
|
|
|
next: Option<Hash>,
|
2023-08-28 21:02:11 +00:00
|
|
|
pub(crate) hashes: Vec<Hash>,
|
|
|
|
}
|
|
|
|
|
2023-08-29 17:53:29 +00:00
|
|
|
fn hash_to_slug(hash: &Hash) -> String {
|
|
|
|
base64::prelude::BASE64_URL_SAFE.encode(hash.to_bytes())
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-29 17:53:29 +00:00
|
|
|
fn hash_from_slug(s: &str) -> Option<Hash> {
|
2023-08-28 21:02:11 +00:00
|
|
|
let bytes = base64::prelude::BASE64_URL_SAFE.decode(s).ok()?;
|
2023-08-29 17:53:29 +00:00
|
|
|
let hash = Hash::from_bytes(&bytes)?;
|
2023-08-28 21:02:11 +00:00
|
|
|
|
2023-08-29 17:53:29 +00:00
|
|
|
Some(hash)
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HashPage {
|
2023-08-29 17:53:29 +00:00
|
|
|
pub(crate) fn current(&self) -> Option<String> {
|
|
|
|
self.hashes.first().map(hash_to_slug)
|
|
|
|
}
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
pub(crate) fn next(&self) -> Option<String> {
|
2023-08-29 17:53:29 +00:00
|
|
|
self.next.as_ref().map(hash_to_slug)
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn prev(&self) -> Option<String> {
|
2023-08-29 17:53:29 +00:00
|
|
|
self.prev.as_ref().map(hash_to_slug)
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
impl dyn FullRepo {
|
|
|
|
pub(crate) fn hashes(self: &Arc<Self>) -> impl Stream<Item = Result<Hash, RepoError>> {
|
|
|
|
let repo = self.clone();
|
2023-09-02 23:30:45 +00:00
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
streem::try_from_fn(|yielder| async move {
|
|
|
|
let mut slug = None;
|
2023-09-02 23:30:45 +00:00
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
loop {
|
2023-12-28 17:58:38 +00:00
|
|
|
tracing::trace!("hashes_stream: looping");
|
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
let page = repo.hash_page(slug, 100).await?;
|
2023-09-02 23:30:45 +00:00
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
slug = page.next();
|
2023-09-02 23:30:45 +00:00
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
for hash in page.hashes {
|
|
|
|
yielder.yield_ok(hash).await;
|
2023-09-02 23:30:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
if slug.is_none() {
|
|
|
|
break;
|
2023-09-02 23:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 00:08:01 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
2023-09-02 23:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2022-03-29 17:51:16 +00:00
|
|
|
pub(crate) trait HashRepo: BaseRepo {
|
2023-06-29 16:39:47 +00:00
|
|
|
async fn size(&self) -> Result<u64, RepoError>;
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
async fn hash_page(&self, slug: Option<String>, limit: usize) -> Result<HashPage, RepoError> {
|
2023-08-29 17:53:29 +00:00
|
|
|
let hash = slug.as_deref().and_then(hash_from_slug);
|
|
|
|
|
|
|
|
let bound = if let Some(hash) = hash {
|
|
|
|
self.bound(hash).await?
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2023-08-28 21:02:11 +00:00
|
|
|
|
|
|
|
self.hashes_ordered(bound, limit).await
|
|
|
|
}
|
|
|
|
|
2023-08-29 18:27:18 +00:00
|
|
|
async fn hash_page_by_date(
|
|
|
|
&self,
|
|
|
|
date: time::OffsetDateTime,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<HashPage, RepoError>;
|
|
|
|
|
2023-08-29 17:53:29 +00:00
|
|
|
async fn bound(&self, hash: Hash) -> Result<Option<OrderedHash>, RepoError>;
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
async fn hashes_ordered(
|
|
|
|
&self,
|
|
|
|
bound: Option<OrderedHash>,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<HashPage, RepoError>;
|
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn create_hash(
|
2023-07-26 01:08:18 +00:00
|
|
|
&self,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<Result<(), HashAlreadyExists>, RepoError> {
|
2023-08-28 23:43:24 +00:00
|
|
|
self.create_hash_with_timestamp(hash, identifier, time::OffsetDateTime::now_utc())
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_hash_with_timestamp(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
2023-08-28 23:43:24 +00:00
|
|
|
timestamp: time::OffsetDateTime,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<Result<(), HashAlreadyExists>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn update_identifier(&self, hash: Hash, identifier: &Arc<str>) -> Result<(), RepoError>;
|
2023-07-26 01:08:18 +00:00
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn identifier(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2024-03-02 19:27:58 +00:00
|
|
|
async fn relate_blurhash(&self, hash: Hash, blurhash: Arc<str>) -> Result<(), RepoError>;
|
|
|
|
async fn blurhash(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError>;
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn relate_motion_identifier(
|
2022-03-25 23:47:50 +00:00
|
|
|
&self,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<(), RepoError>;
|
|
|
|
async fn motion_identifier(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError>;
|
2022-03-25 23:47:50 +00:00
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn cleanup_hash(&self, hash: Hash) -> Result<(), RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> HashRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
|
|
|
T: HashRepo,
|
|
|
|
{
|
2023-06-29 16:39:47 +00:00
|
|
|
async fn size(&self) -> Result<u64, RepoError> {
|
|
|
|
T::size(self).await
|
|
|
|
}
|
|
|
|
|
2023-08-29 17:53:29 +00:00
|
|
|
async fn bound(&self, hash: Hash) -> Result<Option<OrderedHash>, RepoError> {
|
|
|
|
T::bound(self, hash).await
|
|
|
|
}
|
|
|
|
|
2023-08-28 21:02:11 +00:00
|
|
|
async fn hashes_ordered(
|
|
|
|
&self,
|
|
|
|
bound: Option<OrderedHash>,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<HashPage, RepoError> {
|
|
|
|
T::hashes_ordered(self, bound, limit).await
|
2023-08-29 18:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn hash_page_by_date(
|
|
|
|
&self,
|
|
|
|
date: time::OffsetDateTime,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<HashPage, RepoError> {
|
|
|
|
T::hash_page_by_date(self, date, limit).await
|
2023-08-28 21:02:11 +00:00
|
|
|
}
|
|
|
|
|
2023-08-28 23:43:24 +00:00
|
|
|
async fn create_hash_with_timestamp(
|
2023-07-26 01:08:18 +00:00
|
|
|
&self,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
2023-08-28 23:43:24 +00:00
|
|
|
timestamp: time::OffsetDateTime,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<Result<(), HashAlreadyExists>, RepoError> {
|
2023-08-28 23:43:24 +00:00
|
|
|
T::create_hash_with_timestamp(self, hash, identifier, timestamp).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn update_identifier(&self, hash: Hash, identifier: &Arc<str>) -> Result<(), RepoError> {
|
2023-07-26 01:08:18 +00:00
|
|
|
T::update_identifier(self, hash, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn identifier(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::identifier(self, hash).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2024-03-28 17:04:40 +00:00
|
|
|
async fn relate_blurhash(&self, hash: Hash, blurhash: Arc<str>) -> Result<(), RepoError> {
|
|
|
|
T::relate_blurhash(self, hash, blurhash).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn blurhash(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError> {
|
|
|
|
T::blurhash(self, hash).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn relate_motion_identifier(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<(), RepoError> {
|
|
|
|
T::relate_motion_identifier(self, hash, identifier).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn motion_identifier(&self, hash: Hash) -> Result<Option<Arc<str>>, RepoError> {
|
|
|
|
T::motion_identifier(self, hash).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn cleanup_hash(&self, hash: Hash) -> Result<(), RepoError> {
|
|
|
|
T::cleanup_hash(self, hash).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
pub(crate) trait VariantRepo: BaseRepo {
|
2024-03-30 14:36:31 +00:00
|
|
|
async fn claim_variant_processing_rights(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
2024-03-31 21:00:23 +00:00
|
|
|
) -> Result<Result<(), NotificationEntry>, RepoError>;
|
2024-03-30 14:36:31 +00:00
|
|
|
|
2024-04-01 01:26:15 +00:00
|
|
|
async fn variant_waiter(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
) -> Result<NotificationEntry, RepoError>;
|
|
|
|
|
2024-03-30 14:36:31 +00:00
|
|
|
async fn variant_heartbeat(&self, hash: Hash, variant: String) -> Result<(), RepoError>;
|
|
|
|
|
2024-03-31 21:00:23 +00:00
|
|
|
async fn notify_variant(&self, hash: Hash, variant: String) -> Result<(), RepoError>;
|
2024-03-30 14:36:31 +00:00
|
|
|
|
2024-03-28 17:04:40 +00:00
|
|
|
async fn relate_variant_identifier(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<Result<(), VariantAlreadyExists>, RepoError>;
|
|
|
|
|
|
|
|
async fn variant_identifier(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
) -> Result<Option<Arc<str>>, RepoError>;
|
|
|
|
|
|
|
|
async fn variants(&self, hash: Hash) -> Result<Vec<(String, Arc<str>)>, RepoError>;
|
|
|
|
|
|
|
|
async fn remove_variant(&self, hash: Hash, variant: String) -> Result<(), RepoError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl<T> VariantRepo for Arc<T>
|
|
|
|
where
|
|
|
|
T: VariantRepo,
|
|
|
|
{
|
2024-03-30 14:36:31 +00:00
|
|
|
async fn claim_variant_processing_rights(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
2024-03-31 21:00:23 +00:00
|
|
|
) -> Result<Result<(), NotificationEntry>, RepoError> {
|
2024-03-30 14:36:31 +00:00
|
|
|
T::claim_variant_processing_rights(self, hash, variant).await
|
|
|
|
}
|
|
|
|
|
2024-04-01 01:26:15 +00:00
|
|
|
async fn variant_waiter(
|
|
|
|
&self,
|
|
|
|
hash: Hash,
|
|
|
|
variant: String,
|
|
|
|
) -> Result<NotificationEntry, RepoError> {
|
|
|
|
T::variant_waiter(self, hash, variant).await
|
|
|
|
}
|
|
|
|
|
2024-03-30 14:36:31 +00:00
|
|
|
async fn variant_heartbeat(&self, hash: Hash, variant: String) -> Result<(), RepoError> {
|
|
|
|
T::variant_heartbeat(self, hash, variant).await
|
|
|
|
}
|
|
|
|
|
2024-03-31 21:00:23 +00:00
|
|
|
async fn notify_variant(&self, hash: Hash, variant: String) -> Result<(), RepoError> {
|
|
|
|
T::notify_variant(self, hash, variant).await
|
2024-03-30 14:36:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn relate_variant_identifier(
|
2022-09-11 00:29:22 +00:00
|
|
|
&self,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2022-09-11 00:29:22 +00:00
|
|
|
variant: String,
|
2023-09-02 23:30:45 +00:00
|
|
|
identifier: &Arc<str>,
|
|
|
|
) -> Result<Result<(), VariantAlreadyExists>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::relate_variant_identifier(self, hash, variant, identifier).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 00:19:03 +00:00
|
|
|
async fn variant_identifier(
|
2022-09-11 00:29:22 +00:00
|
|
|
&self,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2022-09-11 00:29:22 +00:00
|
|
|
variant: String,
|
2023-09-02 23:30:45 +00:00
|
|
|
) -> Result<Option<Arc<str>>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::variant_identifier(self, hash, variant).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-02 23:30:45 +00:00
|
|
|
async fn variants(&self, hash: Hash) -> Result<Vec<(String, Arc<str>)>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::variants(self, hash).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 03:06:42 +00:00
|
|
|
async fn remove_variant(&self, hash: Hash, variant: String) -> Result<(), RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::remove_variant(self, hash, variant).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2022-03-29 17:51:16 +00:00
|
|
|
pub(crate) trait AliasRepo: BaseRepo {
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn create_alias(
|
2022-03-24 18:16:41 +00:00
|
|
|
&self,
|
2022-03-26 21:49:23 +00:00
|
|
|
alias: &Alias,
|
|
|
|
delete_token: &DeleteToken,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2023-07-26 01:08:18 +00:00
|
|
|
) -> Result<Result<(), AliasAlreadyExists>, RepoError>;
|
|
|
|
|
2023-07-07 18:17:26 +00:00
|
|
|
async fn delete_token(&self, alias: &Alias) -> Result<Option<DeleteToken>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2023-08-14 03:06:42 +00:00
|
|
|
async fn hash(&self, alias: &Alias) -> Result<Option<Hash>, RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn aliases_for_hash(&self, hash: Hash) -> Result<Vec<Alias>, RepoError>;
|
2023-07-26 01:08:18 +00:00
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn cleanup_alias(&self, alias: &Alias) -> Result<(), RepoError>;
|
2022-03-24 18:16:41 +00:00
|
|
|
}
|
2022-03-25 23:47:50 +00:00
|
|
|
|
2022-09-11 00:29:22 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2023-08-16 00:19:03 +00:00
|
|
|
impl<T> AliasRepo for Arc<T>
|
2022-09-11 00:29:22 +00:00
|
|
|
where
|
|
|
|
T: AliasRepo,
|
|
|
|
{
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn create_alias(
|
2022-09-11 00:29:22 +00:00
|
|
|
&self,
|
|
|
|
alias: &Alias,
|
|
|
|
delete_token: &DeleteToken,
|
2023-08-14 03:06:42 +00:00
|
|
|
hash: Hash,
|
2023-07-26 01:08:18 +00:00
|
|
|
) -> Result<Result<(), AliasAlreadyExists>, RepoError> {
|
2023-08-16 21:09:40 +00:00
|
|
|
T::create_alias(self, alias, delete_token, hash).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 18:17:26 +00:00
|
|
|
async fn delete_token(&self, alias: &Alias) -> Result<Option<DeleteToken>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::delete_token(self, alias).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-14 03:06:42 +00:00
|
|
|
async fn hash(&self, alias: &Alias) -> Result<Option<Hash>, RepoError> {
|
2022-09-11 15:04:37 +00:00
|
|
|
T::hash(self, alias).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-01 23:41:04 +00:00
|
|
|
async fn aliases_for_hash(&self, hash: Hash) -> Result<Vec<Alias>, RepoError> {
|
|
|
|
T::aliases_for_hash(self, hash).await
|
2023-07-26 01:08:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-16 21:09:40 +00:00
|
|
|
async fn cleanup_alias(&self, alias: &Alias) -> Result<(), RepoError> {
|
|
|
|
T::cleanup_alias(self, alias).await
|
2022-09-11 00:29:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 23:47:50 +00:00
|
|
|
impl Repo {
|
2023-09-08 00:20:41 +00:00
|
|
|
#[tracing::instrument(skip(config))]
|
2023-09-02 16:52:55 +00:00
|
|
|
pub(crate) async fn open(config: config::Repo) -> color_eyre::Result<Self> {
|
2022-03-25 23:47:50 +00:00
|
|
|
match config {
|
2022-03-28 04:27:07 +00:00
|
|
|
config::Repo::Sled(config::Sled {
|
2023-07-08 22:35:57 +00:00
|
|
|
path,
|
2022-03-25 23:47:50 +00:00
|
|
|
cache_capacity,
|
2023-07-08 22:35:57 +00:00
|
|
|
export_path,
|
2022-03-25 23:47:50 +00:00
|
|
|
}) => {
|
2023-07-08 22:35:57 +00:00
|
|
|
let repo = self::sled::SledRepo::build(path, cache_capacity, export_path)?;
|
2022-03-25 23:47:50 +00:00
|
|
|
|
2023-07-08 22:35:57 +00:00
|
|
|
Ok(Self::Sled(repo))
|
2022-03-25 23:47:50 +00:00
|
|
|
}
|
2024-01-15 23:11:08 +00:00
|
|
|
config::Repo::Postgres(config::Postgres {
|
|
|
|
url,
|
|
|
|
use_tls,
|
|
|
|
certificate_file,
|
|
|
|
}) => {
|
|
|
|
let repo =
|
|
|
|
self::postgres::PostgresRepo::connect(url, use_tls, certificate_file).await?;
|
2023-09-02 16:52:55 +00:00
|
|
|
|
|
|
|
Ok(Self::Postgres(repo))
|
|
|
|
}
|
2022-03-25 23:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-16 00:19:03 +00:00
|
|
|
|
|
|
|
pub(crate) fn to_arc(&self) -> ArcRepo {
|
|
|
|
match self {
|
|
|
|
Self::Sled(sled_repo) => Arc::new(sled_repo.clone()),
|
2023-09-03 22:11:34 +00:00
|
|
|
Self::Postgres(postgres_repo) => Arc::new(postgres_repo.clone()),
|
2023-08-16 00:19:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-25 23:47:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
impl MaybeUuid {
|
|
|
|
fn from_str(s: &str) -> Self {
|
|
|
|
if let Ok(uuid) = Uuid::parse_str(s) {
|
|
|
|
MaybeUuid::Uuid(uuid)
|
|
|
|
} else {
|
|
|
|
MaybeUuid::Name(s.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_bytes(&self) -> &[u8] {
|
|
|
|
match self {
|
|
|
|
Self::Uuid(uuid) => &uuid.as_bytes()[..],
|
|
|
|
Self::Name(name) => name.as_bytes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 21:51:12 +00:00
|
|
|
impl UploadId {
|
|
|
|
pub(crate) fn generate() -> Self {
|
|
|
|
Self { id: Uuid::new_v4() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn as_bytes(&self) -> &[u8] {
|
|
|
|
&self.id.as_bytes()[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 01:56:29 +00:00
|
|
|
impl std::str::FromStr for UploadId {
|
|
|
|
type Err = <Uuid as std::str::FromStr>::Err;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
Ok(UploadId { id: s.parse()? })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for UploadId {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
std::fmt::Display::fmt(&self.id, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
impl std::fmt::Display for MaybeUuid {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-01-29 17:57:59 +00:00
|
|
|
Self::Uuid(id) => write!(f, "{id}"),
|
|
|
|
Self::Name(name) => write!(f, "{name}"),
|
2022-03-26 21:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|