From 272f5689f429afcecc87b2cca1b6d5dd281f591e Mon Sep 17 00:00:00 2001 From: Jiang Liu Date: Tue, 28 Nov 2023 22:21:44 +0800 Subject: [PATCH] storage: move cas db from util into storage Move cas db from util into storage. Signed-off-by: Jiang Liu --- Cargo.lock | 2 +- storage/Cargo.toml | 1 + .../cas.rs => storage/src/cache/dedup/db.rs | 38 +++---------------- storage/src/cache/dedup/mod.rs | 38 +++++++++++++++++++ storage/src/cache/mod.rs | 1 + utils/Cargo.toml | 1 - utils/src/lib.rs | 1 - 7 files changed, 46 insertions(+), 36 deletions(-) rename utils/src/cas.rs => storage/src/cache/dedup/db.rs (92%) create mode 100644 storage/src/cache/dedup/mod.rs diff --git a/Cargo.lock b/Cargo.lock index a94a790e1ba..5e32af89d62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1326,6 +1326,7 @@ dependencies = [ "nydus-utils", "regex", "reqwest", + "rusqlite", "serde", "serde_json", "sha1", @@ -1355,7 +1356,6 @@ dependencies = [ "nix", "nydus-api", "openssl", - "rusqlite", "serde", "serde_json", "sha2", diff --git a/storage/Cargo.toml b/storage/Cargo.toml index 0d75e9e8084..8b7005a2fb0 100644 --- a/storage/Cargo.toml +++ b/storage/Cargo.toml @@ -24,6 +24,7 @@ libc = "0.2" log = "0.4.8" nix = "0.24" reqwest = { version = "0.11.14", features = ["blocking", "json"], optional = true } +rusqlite = { version = "0.30", features = ["bundled"] } serde = { version = "1.0.110", features = ["serde_derive", "rc"] } serde_json = "1.0.53" sha1 = { version = "0.10.5", optional = true } diff --git a/utils/src/cas.rs b/storage/src/cache/dedup/db.rs similarity index 92% rename from utils/src/cas.rs rename to storage/src/cache/dedup/db.rs index f2e72f3fe95..36b62883461 100644 --- a/utils/src/cas.rs +++ b/storage/src/cache/dedup/db.rs @@ -2,41 +2,13 @@ // // SPDX-License-Identifier: Apache-2.0 -use std::fmt::{self, Display, Formatter}; -use std::io::Error; +#![allow(unused)] + use std::path::Path; use rusqlite::{Connection, DropBehavior, OptionalExtension, Transaction}; -/// Error codes related to local cas. -#[derive(Debug)] -pub enum CasError { - Io(Error), - Db(rusqlite::Error), -} - -impl Display for CasError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } -} - -impl std::error::Error for CasError {} - -impl From for CasError { - fn from(e: rusqlite::Error) -> Self { - CasError::Db(e) - } -} - -impl From for CasError { - fn from(e: Error) -> Self { - CasError::Io(e) - } -} - -/// Specialized `Result` for local cas. -type Result = std::result::Result; +use super::Result; pub struct CasDb { conn: Connection, @@ -206,7 +178,7 @@ impl CasDb { let tran = self.begin_transaction()?; for chunk in chunks { match Self::get_blob_id_with_tx(&tran, &chunk.2) { - Err(e) => return Err(e.into()), + Err(e) => return Err(e), Ok(id) => { if let Err(e) = tran.execute(sql, (&chunk.0, &chunk.1, id)) { return Err(e.into()); @@ -224,7 +196,7 @@ impl CasDb { let tran = self.begin_transaction()?; match Self::get_blob_id_with_tx(&tran, blob_id) { - Err(e) => return Err(e.into()), + Err(e) => return Err(e), Ok(id) => { if let Err(e) = tran.execute(sql, (chunk_id, chunk_offset, id)) { return Err(e.into()); diff --git a/storage/src/cache/dedup/mod.rs b/storage/src/cache/dedup/mod.rs new file mode 100644 index 00000000000..09723666cb1 --- /dev/null +++ b/storage/src/cache/dedup/mod.rs @@ -0,0 +1,38 @@ +// Copyright (C) 2022-2023 Alibaba Cloud. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 + +use std::fmt::{self, Display, Formatter}; +use std::io::Error; + +mod db; + +/// Error codes related to local cas. +#[derive(Debug)] +pub enum CasError { + Io(Error), + Db(rusqlite::Error), +} + +impl Display for CasError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl std::error::Error for CasError {} + +impl From for CasError { + fn from(e: rusqlite::Error) -> Self { + CasError::Db(e) + } +} + +impl From for CasError { + fn from(e: Error) -> Self { + CasError::Io(e) + } +} + +/// Specialized `Result` for local cas. +type Result = std::result::Result; diff --git a/storage/src/cache/mod.rs b/storage/src/cache/mod.rs index cc65f842919..2f86f5fd047 100644 --- a/storage/src/cache/mod.rs +++ b/storage/src/cache/mod.rs @@ -36,6 +36,7 @@ use crate::utils::{alloc_buf, check_digest}; use crate::{StorageResult, RAFS_MAX_CHUNK_SIZE}; mod cachedfile; +mod dedup; mod dummycache; mod filecache; #[cfg(target_os = "linux")] diff --git a/utils/Cargo.toml b/utils/Cargo.toml index 56dc00fcbf6..a9b0f2aaa68 100644 --- a/utils/Cargo.toml +++ b/utils/Cargo.toml @@ -17,7 +17,6 @@ libc = "0.2" log = "0.4" lz4-sys = "1.9.4" lz4 = "1.24.0" -rusqlite = { version = "0.30", features = ["bundled"] } openssl = { version = "0.10.48", features = ["vendored"], optional = true } serde = { version = ">=1.0.27", features = ["serde_derive", "rc"] } serde_json = ">=1.0.9" diff --git a/utils/src/lib.rs b/utils/src/lib.rs index 8e771493460..4785e36a9f4 100644 --- a/utils/src/lib.rs +++ b/utils/src/lib.rs @@ -20,7 +20,6 @@ pub use self::reader::*; pub use self::types::*; pub mod async_helper; -pub mod cas; pub mod compact; pub mod compress; #[cfg(feature = "encryption")]