-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4067 from Xuanwo/dal2
dal2: Implement DAL Layer support
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// 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 std::sync::Arc; | ||
|
||
use crate::Accessor; | ||
|
||
/// Layer is used to intercept the operations on the underlying storage. | ||
/// | ||
/// Struct that implement this trait must accept input `Arc<dyn Accessor>` as inner, | ||
/// and returns a new `Arc<dyn Accessor>` as output. | ||
/// | ||
/// All functions in `Accessor` requires `&self`, so it's implementor's responsibility | ||
/// to maintain the internal mutability. Please also keep in mind that `Accessor` | ||
/// requires `Send` and `Sync`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use dal2::{Accessor, Layer}; | ||
/// | ||
/// struct Trace { | ||
/// inner: Arc<dyn Accessor>, | ||
/// } | ||
/// | ||
/// impl Accessor for Trace {} | ||
/// | ||
/// impl Layer for Trace { | ||
/// fn layer(&self, inner: Arc<dyn Accessor>) -> Arc<dyn Accessor> { | ||
/// Arc::new(Trace { inner }) | ||
/// } | ||
/// } | ||
/// ``` | ||
pub trait Layer { | ||
fn layer(&self, inner: Arc<dyn Accessor>) -> Arc<dyn Accessor>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2022 Datafuse Labs. | ||
// | ||
// 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 std::sync::Arc; | ||
|
||
use common_dal2::ops::OpDelete; | ||
use common_dal2::services::fs; | ||
use common_dal2::Accessor; | ||
use common_dal2::Layer; | ||
use common_dal2::Operator; | ||
use futures::lock::Mutex; | ||
|
||
struct Test { | ||
#[allow(dead_code)] | ||
inner: Arc<dyn Accessor>, | ||
deleted: Arc<Mutex<bool>>, | ||
} | ||
|
||
impl Layer for &Test { | ||
fn layer(&self, inner: Arc<dyn Accessor>) -> Arc<dyn Accessor> { | ||
Arc::new(Test { | ||
inner: inner.clone(), | ||
deleted: self.deleted.clone(), | ||
}) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Accessor for Test { | ||
async fn delete(&self, _args: &OpDelete) -> common_dal2::error::Result<()> { | ||
let mut x = self.deleted.lock().await; | ||
*x = true; | ||
|
||
// We will not call anything here to test the layer. | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_layer() { | ||
let test = Test { | ||
inner: Arc::new(fs::Backend::build().finish()), | ||
deleted: Arc::new(Mutex::new(false)), | ||
}; | ||
|
||
let op = Operator::new(fs::Backend::build().finish()).layer(&test); | ||
|
||
op.delete("xxxxx").run().await.unwrap(); | ||
|
||
assert!(*test.deleted.clone().lock().await); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters