get rids of nelson

This commit is contained in:
Tamo 2022-09-13 15:54:35 +02:00 committed by Clément Renault
parent a9844bd4f6
commit 2afb381f95
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 134 additions and 130 deletions

89
Cargo.lock generated
View File

@ -961,6 +961,41 @@ dependencies = [
"memchr",
]
[[package]]
name = "darling"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "649c91bc01e8b1eac09fb91e8dbc7d517684ca6be8ebc75bb9cafc894f9fdb6f"
dependencies = [
"fnv",
"ident_case",
"proc-macro2 1.0.46",
"quote 1.0.21",
"strsim",
"syn 1.0.101",
]
[[package]]
name = "darling_macro"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
dependencies = [
"darling_core",
"quote 1.0.21",
"syn 1.0.101",
]
[[package]]
name = "derivative"
version = "2.2.0"
@ -1169,14 +1204,37 @@ dependencies = [
"instant",
]
[[package]]
name = "faux"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c3b5e56a69ca67c241191cd9d484e14fb0fe89f5e539c2e8448eafd1f65c1f0"
dependencies = [
"faux_macros",
"paste",
]
[[package]]
name = "faux_macros"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35c9bb4a2c13ffb3a93a39902aaf4e7190a1706a4779b6db0449aee433d26c4a"
dependencies = [
"darling",
"proc-macro2 1.0.46",
"quote 1.0.21",
"syn 1.0.101",
"uuid 0.8.2",
]
[[package]]
name = "file-store"
version = "0.1.0"
dependencies = [
"nelson",
"faux",
"tempfile",
"thiserror",
"uuid",
"uuid 1.1.2",
]
[[package]]
@ -1654,6 +1712,12 @@ dependencies = [
"tokio-rustls",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "0.3.0"
@ -1694,7 +1758,7 @@ dependencies = [
"tempfile",
"thiserror",
"time",
"uuid",
"uuid 1.1.2",
]
[[package]]
@ -2141,7 +2205,7 @@ dependencies = [
"sha2",
"thiserror",
"time",
"uuid",
"uuid 1.1.2",
]
[[package]]
@ -2216,7 +2280,7 @@ dependencies = [
"tokio-stream",
"toml",
"urlencoding",
"uuid",
"uuid 1.1.2",
"vergen",
"walkdir",
"yaup",
@ -2281,7 +2345,7 @@ dependencies = [
"thiserror",
"time",
"tokio",
"uuid",
"uuid 1.1.2",
"walkdir",
"whoami",
]
@ -2363,7 +2427,7 @@ dependencies = [
"tempfile",
"thiserror",
"time",
"uuid",
"uuid 1.1.2",
]
[[package]]
@ -2408,7 +2472,7 @@ dependencies = [
"tempfile",
"thiserror",
"time",
"uuid",
"uuid 1.1.2",
]
[[package]]
@ -3920,6 +3984,15 @@ version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5190c9442dcdaf0ddd50f37420417d219ae5261bbf5db120d0f9bab996c9cba1"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
]
[[package]]
name = "uuid"
version = "1.1.2"

View File

@ -11,4 +11,4 @@ tempfile = "3.3.0"
thiserror = "1.0.30"
[dev-dependencies]
nelson = { git = "https://github.com/meilisearch/nelson.git", rev = "675f13885548fb415ead8fbb447e9e6d9314000a"}
faux = "0.1.8"

View File

@ -5,36 +5,17 @@ use std::path::{Path, PathBuf};
use tempfile::NamedTempFile;
use uuid::Uuid;
#[cfg(not(test))]
pub use store::UpdateFileStore;
#[cfg(test)]
pub use test::MockUpdateFileStore as UpdateFileStore;
const UPDATE_FILES_PATH: &str = "updates/updates_files";
pub struct UpdateFile {
path: PathBuf,
file: NamedTempFile,
}
#[derive(Debug, thiserror::Error)]
pub enum UpdateFileStoreError {
#[error("Error while persisting update to disk")]
Error,
pub enum Error {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
PersistError(#[from] tempfile::PersistError),
}
pub type Result<T> = std::result::Result<T, UpdateFileStoreError>;
impl UpdateFile {
pub fn persist(self) -> Result<()> {
self.file.persist(&self.path)?;
Ok(())
}
}
pub type Result<T> = std::result::Result<T, Error>;
impl Deref for UpdateFile {
type Target = NamedTempFile;
@ -50,117 +31,67 @@ impl DerefMut for UpdateFile {
}
}
mod store {
use super::*;
// #[cfg_attr(test, faux::create)]
#[derive(Clone, Debug)]
pub struct UpdateFileStore {
path: PathBuf,
}
#[derive(Clone, Debug)]
pub struct UpdateFileStore {
path: PathBuf,
// #[cfg_attr(test, faux::methods)]
impl UpdateFileStore {
pub fn new(path: impl AsRef<Path>) -> Result<UpdateFileStore> {
let path = path.as_ref().join(UPDATE_FILES_PATH);
std::fs::create_dir_all(&path)?;
Ok(UpdateFileStore { path })
}
impl UpdateFileStore {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref().join(UPDATE_FILES_PATH);
std::fs::create_dir_all(&path)?;
Ok(Self { path })
}
/// Creates a new temporary update file.
/// A call to `persist` is needed to persist the file in the database.
pub fn new_update(&self) -> Result<(Uuid, UpdateFile)> {
let file = NamedTempFile::new_in(&self.path)?;
let uuid = Uuid::new_v4();
let path = self.path.join(uuid.to_string());
let update_file = UpdateFile { file, path };
/// Creates a new temporary update file.
/// A call to `persist` is needed to persist the file in the database.
pub fn new_update(&self) -> Result<(Uuid, UpdateFile)> {
let file = NamedTempFile::new_in(&self.path)?;
let uuid = Uuid::new_v4();
let path = self.path.join(uuid.to_string());
let update_file = UpdateFile { file, path };
Ok((uuid, update_file))
}
Ok((uuid, update_file))
}
/// Returns the file corresponding to the requested uuid.
pub fn get_update(&self, uuid: Uuid) -> Result<File> {
let path = self.path.join(uuid.to_string());
let file = File::open(path)?;
Ok(file)
}
/// Returns the file corresponding to the requested uuid.
pub fn get_update(&self, uuid: Uuid) -> Result<File> {
let path = self.path.join(uuid.to_string());
let file = File::open(path)?;
Ok(file)
}
/// Copies the content of the update file pointed to by `uuid` to the `dst` directory.
pub fn snapshot(&self, uuid: Uuid, dst: impl AsRef<Path>) -> Result<()> {
let src = self.path.join(uuid.to_string());
let mut dst = dst.as_ref().join(UPDATE_FILES_PATH);
std::fs::create_dir_all(&dst)?;
dst.push(uuid.to_string());
std::fs::copy(src, dst)?;
Ok(())
}
/// Copies the content of the update file pointed to by `uuid` to the `dst` directory.
pub fn snapshot(&self, uuid: Uuid, dst: impl AsRef<Path>) -> Result<()> {
let src = self.path.join(uuid.to_string());
let mut dst = dst.as_ref().join(UPDATE_FILES_PATH);
std::fs::create_dir_all(&dst)?;
dst.push(uuid.to_string());
std::fs::copy(src, dst)?;
Ok(())
}
pub fn get_size(&self, uuid: Uuid) -> Result<u64> {
Ok(self.get_update(uuid)?.metadata()?.len())
}
pub fn get_size(&self, uuid: Uuid) -> Result<u64> {
Ok(self.get_update(uuid)?.metadata()?.len())
}
pub fn delete(&self, uuid: Uuid) -> Result<()> {
let path = self.path.join(uuid.to_string());
std::fs::remove_file(path)?;
Ok(())
}
pub fn delete(&self, uuid: Uuid) -> Result<()> {
let path = self.path.join(uuid.to_string());
std::fs::remove_file(path)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use std::sync::Arc;
pub struct UpdateFile {
path: PathBuf,
file: NamedTempFile,
}
use nelson::Mocker;
use super::*;
#[derive(Clone)]
pub enum MockUpdateFileStore {
Real(store::UpdateFileStore),
Mock(Arc<Mocker>),
}
impl MockUpdateFileStore {
pub fn mock(mocker: Mocker) -> Self {
Self::Mock(Arc::new(mocker))
}
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
store::UpdateFileStore::new(path).map(Self::Real)
}
pub fn new_update(&self) -> Result<(Uuid, UpdateFile)> {
match self {
MockUpdateFileStore::Real(s) => s.new_update(),
MockUpdateFileStore::Mock(_) => todo!(),
}
}
pub fn get_update(&self, uuid: Uuid) -> Result<File> {
match self {
MockUpdateFileStore::Real(s) => s.get_update(uuid),
MockUpdateFileStore::Mock(_) => todo!(),
}
}
pub fn snapshot(&self, uuid: Uuid, dst: impl AsRef<Path>) -> Result<()> {
match self {
MockUpdateFileStore::Real(s) => s.snapshot(uuid, dst),
MockUpdateFileStore::Mock(_) => todo!(),
}
}
pub fn get_size(&self, uuid: Uuid) -> Result<u64> {
match self {
MockUpdateFileStore::Real(s) => s.get_size(uuid),
MockUpdateFileStore::Mock(_) => todo!(),
}
}
pub fn delete(&self, uuid: Uuid) -> Result<()> {
match self {
MockUpdateFileStore::Real(s) => s.delete(uuid),
MockUpdateFileStore::Mock(mocker) => unsafe { mocker.get("delete").call(uuid) },
}
}
impl UpdateFile {
pub fn persist(self) -> Result<()> {
self.file.persist(&self.path)?;
Ok(())
}
}