From 89e67ddde295471f8e59dd39a718ca3c01894734 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Thu, 19 Oct 2023 18:02:56 +0200 Subject: [PATCH] support load api from example stroage plugin --- Cargo.lock | 2 ++ plugins/example-storage-plugin/src/lib.rs | 23 ++++++++++++++-- plugins/zenoh-backend-traits/Cargo.toml | 4 ++- plugins/zenoh-backend-traits/src/config.rs | 31 +++++++++++++++++++++- plugins/zenoh-backend-traits/src/lib.rs | 29 ++++++++++++++++++++ plugins/zenoh-plugin-trait/src/lib.rs | 2 +- 6 files changed, 86 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 715da2c689..8ff6e7cfd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5057,10 +5057,12 @@ version = "0.11.0-dev" dependencies = [ "async-std", "async-trait", + "const_format", "derive_more", "schemars", "serde_json", "zenoh", + "zenoh-plugin-trait", "zenoh-result", "zenoh-util", ] diff --git a/plugins/example-storage-plugin/src/lib.rs b/plugins/example-storage-plugin/src/lib.rs index a3a50504e0..a6d5d9760a 100644 --- a/plugins/example-storage-plugin/src/lib.rs +++ b/plugins/example-storage-plugin/src/lib.rs @@ -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; // @@ -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 { + let volume = ExampleBackend {}; + Ok(Box::new(volume)) + } + + const STATIC_NAME: &'static str = "example_backend"; +} + #[no_mangle] pub fn create_volume(_config: VolumeConfig) -> ZResult> { let volume = ExampleBackend {}; @@ -55,7 +74,7 @@ impl Volume for ExampleBackend { } } async fn create_storage(&mut self, _props: StorageConfig) -> ZResult> { - Ok(Box::new(ExampleStorage::default())) + Ok(Box::::default()) } fn incoming_data_interceptor(&self) -> Option Sample + Send + Sync>> { None diff --git a/plugins/zenoh-backend-traits/Cargo.toml b/plugins/zenoh-backend-traits/Cargo.toml index b9a6cef183..8c13247c5e 100644 --- a/plugins/zenoh-backend-traits/Cargo.toml +++ b/plugins/zenoh-backend-traits/Cargo.toml @@ -34,4 +34,6 @@ serde_json = { workspace = true } zenoh = { workspace = true } zenoh-result = { workspace = true } zenoh-util = { workspace = true } -schemars = { workspace = true } \ No newline at end of file +schemars = { workspace = true } +zenoh-plugin-trait = { workspace = true } +const_format = { workspace = true } \ No newline at end of file diff --git a/plugins/zenoh-backend-traits/src/config.rs b/plugins/zenoh-backend-traits/src/config.rs index ba9ac3b2bc..20c3d5f3f4 100644 --- a/plugins/zenoh-backend-traits/src/config.rs +++ b/plugins/zenoh-backend-traits/src/config.rs @@ -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)] @@ -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 { diff --git a/plugins/zenoh-backend-traits/src/lib.rs b/plugins/zenoh-backend-traits/src/lib.rs index ac1e1c973c..8ab5844de9 100644 --- a/plugins/zenoh-backend-traits/src/lib.rs +++ b/plugins/zenoh-backend-traits/src/lib.rs @@ -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; @@ -214,6 +215,34 @@ pub trait Volume: Send + Sync { fn outgoing_data_interceptor(&self) -> Option Sample + Send + Sync>>; } +pub type VolumePlugin = Box; + +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 { diff --git a/plugins/zenoh-plugin-trait/src/lib.rs b/plugins/zenoh-plugin-trait/src/lib.rs index d6b4475bcb..47b9d153bb 100644 --- a/plugins/zenoh-plugin-trait/src/lib.rs +++ b/plugins/zenoh-plugin-trait/src/lib.rs @@ -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]