Skip to content

Commit

Permalink
support load api from example stroage plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
milyin committed Oct 19, 2023
1 parent be46e36 commit 89e67dd
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions plugins/example-storage-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{collections::{hash_map::Entry, HashMap}, sync::Arc};
use std::{
collections::{hash_map::Entry, HashMap},
sync::Arc,
};

use async_std::sync::RwLock;
//
Expand All @@ -19,9 +22,25 @@ use zenoh::{prelude::OwnedKeyExpr, sample::Sample, time::Timestamp, value::Value
use zenoh_backend_traits::{
config::{StorageConfig, VolumeConfig},
Capability, History, Persistence, Storage, StorageInsertionResult, StoredData, Volume,
VolumePlugin,
};
use zenoh_plugin_trait::Plugin;
use zenoh_result::ZResult;

zenoh_plugin_trait::declare_plugin!(ExampleBackend);

impl Plugin for ExampleBackend {
type StartArgs = VolumeConfig;
type RunningPlugin = VolumePlugin;

fn start(_name: &str, _args: &Self::StartArgs) -> ZResult<Self::RunningPlugin> {
let volume = ExampleBackend {};
Ok(Box::new(volume))
}

const STATIC_NAME: &'static str = "example_backend";
}

#[no_mangle]
pub fn create_volume(_config: VolumeConfig) -> ZResult<Box<dyn Volume>> {
let volume = ExampleBackend {};
Expand Down Expand Up @@ -55,7 +74,7 @@ impl Volume for ExampleBackend {
}
}
async fn create_storage(&mut self, _props: StorageConfig) -> ZResult<Box<dyn Storage>> {
Ok(Box::new(ExampleStorage::default()))
Ok(Box::<ExampleStorage>::default())
}
fn incoming_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>> {
None
Expand Down
4 changes: 3 additions & 1 deletion plugins/zenoh-backend-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ serde_json = { workspace = true }
zenoh = { workspace = true }
zenoh-result = { workspace = true }
zenoh-util = { workspace = true }
schemars = { workspace = true }
schemars = { workspace = true }
zenoh-plugin-trait = { workspace = true }
const_format = { workspace = true }
31 changes: 30 additions & 1 deletion plugins/zenoh-backend-traits/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use schemars::JsonSchema;
use serde_json::{Map, Value};
use std::convert::TryFrom;
use std::time::Duration;
use zenoh::{key_expr::keyexpr, prelude::OwnedKeyExpr, Result as ZResult};
use zenoh::{
key_expr::keyexpr, prelude::OwnedKeyExpr, Result as ZResult,
};
use zenoh_plugin_trait::{CompatibilityVersion, concat_enabled_features};
use zenoh_result::{bail, zerror, Error};

#[derive(JsonSchema, Debug, Clone, AsMut, AsRef)]
Expand Down Expand Up @@ -67,6 +70,32 @@ pub struct ReplicaConfig {
pub delta: Duration,
}

const VOLUME_CONFIG_VERSION: &str = "1";

impl CompatibilityVersion for VolumeConfig {
fn version() -> &'static str {
concat_enabled_features!(
VOLUME_CONFIG_VERSION,
"auth_pubkey",
"auth_usrpwd",
"complete_n",
"shared-memory",
"stats",
"transport_multilink",
"transport_quic",
"transport_serial",
"transport_unixpipe",
"transport_tcp",
"transport_tls",
"transport_udp",
"transport_unixsock-stream",
"transport_ws",
"unstable",
"default"
)
}
}

impl Default for ReplicaConfig {
fn default() -> Self {
Self {
Expand Down
29 changes: 29 additions & 0 deletions plugins/zenoh-backend-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
//! ```
use async_trait::async_trait;
use zenoh_plugin_trait::{CompatibilityVersion, concat_enabled_features};
use std::sync::Arc;
use zenoh::prelude::{KeyExpr, OwnedKeyExpr, Sample, Selector};
use zenoh::queryable::ReplyBuilder;
Expand Down Expand Up @@ -214,6 +215,34 @@ pub trait Volume: Send + Sync {
fn outgoing_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>>;
}

pub type VolumePlugin = Box<dyn Volume + 'static>;

const VOLUME_PLUGIN_VERSION: &str = "1";

impl CompatibilityVersion for VolumePlugin {
fn version() -> &'static str {
concat_enabled_features!(
VOLUME_PLUGIN_VERSION,
"auth_pubkey",
"auth_usrpwd",
"complete_n",
"shared-memory",
"stats",
"transport_multilink",
"transport_quic",
"transport_serial",
"transport_unixpipe",
"transport_tcp",
"transport_tls",
"transport_udp",
"transport_unixsock-stream",
"transport_ws",
"unstable",
"default"
)
}
}

/// Trait to be implemented by a Storage.
#[async_trait]
pub trait Storage: Send + Sync {
Expand Down
2 changes: 1 addition & 1 deletion plugins/zenoh-plugin-trait/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub mod vtable;
use zenoh_result::ZResult;

pub mod prelude {
pub use crate::{loading::*, vtable::*, CompatibilityVersion, Plugin};
pub use crate::{loading::*, vtable::*, CompatibilityVersion, Plugin, concat_enabled_features};
}

#[macro_export]
Expand Down

0 comments on commit 89e67dd

Please sign in to comment.