chore: add License for storage_engine (#412)

Signed-off-by: Gaius <gaius.qi@gmail.com>
This commit is contained in:
Gaius 2024-04-23 11:55:13 +08:00 committed by GitHub
parent 76aa34206f
commit fe7e25c99d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 78 additions and 15 deletions

16
Cargo.lock generated
View File

@ -903,7 +903,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client" name = "dragonfly-client"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bytes", "bytes",
@ -962,7 +962,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-backend" name = "dragonfly-client-backend"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"dragonfly-client-core", "dragonfly-client-core",
"futures", "futures",
@ -980,7 +980,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-config" name = "dragonfly-client-config"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"dragonfly-client-core", "dragonfly-client-core",
"home", "home",
@ -999,7 +999,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-core" name = "dragonfly-client-core"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"libloading", "libloading",
"reqwest", "reqwest",
@ -1010,7 +1010,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-init" name = "dragonfly-client-init"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
@ -1026,7 +1026,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-storage" name = "dragonfly-client-storage"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"base16ct", "base16ct",
"chrono", "chrono",
@ -1049,7 +1049,7 @@ dependencies = [
[[package]] [[package]]
name = "dragonfly-client-util" name = "dragonfly-client-util"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"dragonfly-api", "dragonfly-api",
"dragonfly-client-core", "dragonfly-client-core",
@ -1495,7 +1495,7 @@ dependencies = [
[[package]] [[package]]
name = "hdfs" name = "hdfs"
version = "0.1.34" version = "0.1.35"
dependencies = [ dependencies = [
"dragonfly-client-backend", "dragonfly-client-backend",
"dragonfly-client-core", "dragonfly-client-core",

View File

@ -26,7 +26,6 @@ use tokio::io::AsyncRead;
pub mod content; pub mod content;
pub mod metadata; pub mod metadata;
pub mod storage_engine; pub mod storage_engine;
// DEFAULT_WAIT_FOR_PIECE_FINISHED_INTERVAL is the default interval for waiting for the piece to be finished. // DEFAULT_WAIT_FOR_PIECE_FINISHED_INTERVAL is the default interval for waiting for the piece to be finished.

View File

@ -58,8 +58,9 @@ pub struct Task {
pub finished_at: Option<NaiveDateTime>, pub finished_at: Option<NaiveDateTime>,
} }
// Task implements the task database object.
impl DatabaseObject for Task { impl DatabaseObject for Task {
/// NAMESPACE is the namespace of [Task] objects. // NAMESPACE is the namespace of [Task] objects.
const NAMESPACE: &'static str = "task"; const NAMESPACE: &'static str = "task";
} }
@ -146,8 +147,9 @@ pub struct Piece {
pub finished_at: Option<NaiveDateTime>, pub finished_at: Option<NaiveDateTime>,
} }
// Piece implements the piece database object.
impl DatabaseObject for Piece { impl DatabaseObject for Piece {
/// NAMESPACE is the namespace of [Piece] objects. // NAMESPACE is the namespace of [Piece] objects.
const NAMESPACE: &'static str = "piece"; const NAMESPACE: &'static str = "piece";
} }

View File

@ -1,3 +1,19 @@
/*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use dragonfly_client_core::{ use dragonfly_client_core::{
error::{ErrorType, OrErr}, error::{ErrorType, OrErr},
Result, Result,
@ -41,12 +57,16 @@ impl<T: for<'db> StorageEngine<'db>> StorageEngineOwned for T {}
pub trait Operations { pub trait Operations {
/// get gets the object by key. /// get gets the object by key.
fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>>; fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>>;
/// put puts the object by key. /// put puts the object by key.
fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()>; fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()>;
/// delete deletes the object by key. /// delete deletes the object by key.
fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()>; fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()>;
/// iter iterates all objects. /// iter iterates all objects.
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>; fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>>;
/// prefix_iter iterates all objects with prefix. /// prefix_iter iterates all objects with prefix.
fn prefix_iter<O: DatabaseObject>( fn prefix_iter<O: DatabaseObject>(
&self, &self,
@ -58,8 +78,10 @@ pub trait Operations {
pub trait Transaction: Operations { pub trait Transaction: Operations {
/// get_for_update gets the object for update. /// get_for_update gets the object for update.
fn get_for_update<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>>; fn get_for_update<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>>;
/// commit commits the transaction. /// commit commits the transaction.
fn commit(self) -> Result<()>; fn commit(self) -> Result<()>;
/// rollback rolls back the transaction. /// rollback rolls back the transaction.
fn rollback(&self) -> Result<()>; fn rollback(&self) -> Result<()>;
} }

View File

@ -1,27 +1,45 @@
use std::{ops::Deref, path::Path}; /*
* Copyright 2024 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::storage_engine::{DatabaseObject, Operations, StorageEngine, Transaction};
use dragonfly_client_core::{ use dragonfly_client_core::{
error::{ErrorType, OrErr}, error::{ErrorType, OrErr},
Error, Result, Error, Result,
}; };
use std::{ops::Deref, path::Path};
use tracing::info; use tracing::info;
use crate::storage_engine::{DatabaseObject, Operations, StorageEngine, Transaction};
/// RocksdbStorageEngine is a storage engine based on rocksdb. /// RocksdbStorageEngine is a storage engine based on rocksdb.
pub struct RocksdbStorageEngine { pub struct RocksdbStorageEngine {
// inner is the inner rocksdb transaction db.
inner: rocksdb::TransactionDB, inner: rocksdb::TransactionDB,
} }
// RocksdbStorageEngine implements deref of the storage engine.
impl Deref for RocksdbStorageEngine { impl Deref for RocksdbStorageEngine {
// Target is the inner rocksdb transaction db.
type Target = rocksdb::TransactionDB; type Target = rocksdb::TransactionDB;
// deref returns the inner rocksdb transaction db.
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.inner &self.inner
} }
} }
// RocksdbStorageEngine implements the storage engine of the rocksdb.
impl RocksdbStorageEngine { impl RocksdbStorageEngine {
/// DEFAULT_DIR_NAME is the default directory name to store metadata. /// DEFAULT_DIR_NAME is the default directory name to store metadata.
const DEFAULT_DIR_NAME: &'static str = "metadata"; const DEFAULT_DIR_NAME: &'static str = "metadata";
@ -75,7 +93,9 @@ impl RocksdbStorageEngine {
} }
} }
// RocksdbStorageEngine implements the storage engine operations.
impl Operations for RocksdbStorageEngine { impl Operations for RocksdbStorageEngine {
// get gets the object by key.
fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> { fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> {
let cf = cf_handle::<O>(self)?; let cf = cf_handle::<O>(self)?;
let value = self.get_cf(cf, key).or_err(ErrorType::StorageError)?; let value = self.get_cf(cf, key).or_err(ErrorType::StorageError)?;
@ -85,6 +105,7 @@ impl Operations for RocksdbStorageEngine {
} }
} }
// put puts the object by key.
fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()> { fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()> {
let cf = cf_handle::<O>(self)?; let cf = cf_handle::<O>(self)?;
let serialized = value.serialized()?; let serialized = value.serialized()?;
@ -93,12 +114,14 @@ impl Operations for RocksdbStorageEngine {
Ok(()) Ok(())
} }
// delete deletes the object by key.
fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()> { fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()> {
let cf = cf_handle::<O>(self)?; let cf = cf_handle::<O>(self)?;
self.delete_cf(cf, key).or_err(ErrorType::StorageError)?; self.delete_cf(cf, key).or_err(ErrorType::StorageError)?;
Ok(()) Ok(())
} }
// iter iterates all objects.
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>> { fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>> {
let cf = cf_handle::<O>(self)?; let cf = cf_handle::<O>(self)?;
let iter = self.iterator_cf(cf, rocksdb::IteratorMode::Start); let iter = self.iterator_cf(cf, rocksdb::IteratorMode::Start);
@ -108,6 +131,7 @@ impl Operations for RocksdbStorageEngine {
})) }))
} }
// prefix_iter iterates all objects with prefix.
fn prefix_iter<O: DatabaseObject>( fn prefix_iter<O: DatabaseObject>(
&self, &self,
prefix: &[u8], prefix: &[u8],
@ -121,9 +145,12 @@ impl Operations for RocksdbStorageEngine {
} }
} }
// RocksdbStorageEngine implements the transaction of the storage engine.
impl<'db> StorageEngine<'db> for RocksdbStorageEngine { impl<'db> StorageEngine<'db> for RocksdbStorageEngine {
// Txn is the transaction type.
type Txn = RocksdbTransaction<'db>; type Txn = RocksdbTransaction<'db>;
// start_transaction starts a transaction.
fn start_transaction(&'db self) -> RocksdbTransaction<'db> { fn start_transaction(&'db self) -> RocksdbTransaction<'db> {
let txn = self.transaction(); let txn = self.transaction();
RocksdbTransaction { txn, db: self } RocksdbTransaction { txn, db: self }
@ -132,11 +159,16 @@ impl<'db> StorageEngine<'db> for RocksdbStorageEngine {
/// RocksdbTransaction wraps a rocksdb transaction. /// RocksdbTransaction wraps a rocksdb transaction.
pub struct RocksdbTransaction<'db> { pub struct RocksdbTransaction<'db> {
// txn is the inner rocksdb transaction.
txn: rocksdb::Transaction<'db, rocksdb::TransactionDB>, txn: rocksdb::Transaction<'db, rocksdb::TransactionDB>,
// db is the rocksdb storage engine.
db: &'db rocksdb::TransactionDB, db: &'db rocksdb::TransactionDB,
} }
// RocksdbTransaction implements the transaction operations.
impl Operations for RocksdbTransaction<'_> { impl Operations for RocksdbTransaction<'_> {
// get gets the object by key.
fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> { fn get<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> {
let cf = cf_handle::<O>(self.db)?; let cf = cf_handle::<O>(self.db)?;
let value = self.txn.get_cf(cf, key).or_err(ErrorType::StorageError)?; let value = self.txn.get_cf(cf, key).or_err(ErrorType::StorageError)?;
@ -146,6 +178,7 @@ impl Operations for RocksdbTransaction<'_> {
} }
} }
// put puts the object by key.
fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()> { fn put<O: DatabaseObject>(&self, key: &[u8], value: &O) -> Result<()> {
let cf = cf_handle::<O>(self.db)?; let cf = cf_handle::<O>(self.db)?;
let serialized = value.serialized()?; let serialized = value.serialized()?;
@ -155,6 +188,7 @@ impl Operations for RocksdbTransaction<'_> {
Ok(()) Ok(())
} }
// delete deletes the object by key.
fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()> { fn delete<O: DatabaseObject>(&self, key: &[u8]) -> Result<()> {
let cf = cf_handle::<O>(self.db)?; let cf = cf_handle::<O>(self.db)?;
self.txn self.txn
@ -163,6 +197,7 @@ impl Operations for RocksdbTransaction<'_> {
Ok(()) Ok(())
} }
// iter iterates all objects.
fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>> { fn iter<O: DatabaseObject>(&self) -> Result<impl Iterator<Item = Result<(Box<[u8]>, O)>>> {
let cf = cf_handle::<O>(self.db)?; let cf = cf_handle::<O>(self.db)?;
let iter = self.txn.iterator_cf(cf, rocksdb::IteratorMode::Start); let iter = self.txn.iterator_cf(cf, rocksdb::IteratorMode::Start);
@ -172,6 +207,7 @@ impl Operations for RocksdbTransaction<'_> {
})) }))
} }
// prefix_iter iterates all objects with prefix.
fn prefix_iter<O: DatabaseObject>( fn prefix_iter<O: DatabaseObject>(
&self, &self,
prefix: &[u8], prefix: &[u8],
@ -185,7 +221,9 @@ impl Operations for RocksdbTransaction<'_> {
} }
} }
// RocksdbTransaction implements the transaction operations.
impl Transaction for RocksdbTransaction<'_> { impl Transaction for RocksdbTransaction<'_> {
// get_for_update gets the object for update.
fn get_for_update<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> { fn get_for_update<O: DatabaseObject>(&self, key: &[u8]) -> Result<Option<O>> {
let cf = cf_handle::<O>(self.db)?; let cf = cf_handle::<O>(self.db)?;
let value = self let value = self
@ -198,11 +236,13 @@ impl Transaction for RocksdbTransaction<'_> {
} }
} }
// commit commits the transaction.
fn commit(self) -> Result<()> { fn commit(self) -> Result<()> {
self.txn.commit().or_err(ErrorType::StorageError)?; self.txn.commit().or_err(ErrorType::StorageError)?;
Ok(()) Ok(())
} }
// rollback rolls back the transaction.
fn rollback(&self) -> Result<()> { fn rollback(&self) -> Result<()> {
self.txn.rollback().or_err(ErrorType::StorageError)?; self.txn.rollback().or_err(ErrorType::StorageError)?;
Ok(()) Ok(())