From a91e0cfe1dff61cfa71ef30025b5dd2b79353da7 Mon Sep 17 00:00:00 2001 From: Michael Ilyin Date: Tue, 17 Oct 2023 20:37:46 +0200 Subject: [PATCH] example storage plugin --- Cargo.lock | 19 ++++ Cargo.toml | 1 + DEFAULT_CONFIG.json5 | 18 ++-- plugins/example-storage-plugin/Cargo.toml | 39 ++++++++ plugins/example-storage-plugin/src/lib.rs | 90 +++++++++++++++++++ .../zenoh-plugin-storage-manager/config.json5 | 17 ++-- 6 files changed, 168 insertions(+), 16 deletions(-) create mode 100644 plugins/example-storage-plugin/Cargo.toml create mode 100644 plugins/example-storage-plugin/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8ebec57332..715da2c689 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4880,6 +4880,25 @@ dependencies = [ "zenoh-util", ] +[[package]] +name = "zenoh-plugin-storage-example" +version = "0.11.0-dev" +dependencies = [ + "async-std", + "async-trait", + "clap 3.2.25", + "env_logger", + "futures", + "log", + "serde_json", + "zenoh", + "zenoh-core", + "zenoh-plugin-trait", + "zenoh-result", + "zenoh-util", + "zenoh_backend_traits", +] + [[package]] name = "zenoh-plugin-storage-manager" version = "0.11.0-dev" diff --git a/Cargo.toml b/Cargo.toml index 157ba72da2..8cea280db9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,6 +40,7 @@ members = [ "io/zenoh-links/zenoh-link-unixpipe/", "io/zenoh-transport", "plugins/example-plugin", + "plugins/example-storage-plugin", "plugins/zenoh-backend-traits", "plugins/zenoh-plugin-rest", "plugins/zenoh-plugin-storage-manager", diff --git a/DEFAULT_CONFIG.json5 b/DEFAULT_CONFIG.json5 index dae3ebc9aa..1ff25a0c22 100644 --- a/DEFAULT_CONFIG.json5 +++ b/DEFAULT_CONFIG.json5 @@ -393,14 +393,14 @@ // }, // }, - // /// Plugin configuration example using `__config__` property - // plugins: { - // rest: { - // __config__: "./plugins/zenoh-plugin-rest/config.json5", - // }, - // storage_manager: { - // __config__: "./plugins/zenoh-plugin-storage-manager/config.json5", - // } - // }, + /// Plugin configuration example using `__config__` property + plugins: { + rest: { + __config__: "./plugins/zenoh-plugin-rest/config.json5", + }, + storage_manager: { + __config__: "./plugins/zenoh-plugin-storage-manager/config.json5", + } + }, } diff --git a/plugins/example-storage-plugin/Cargo.toml b/plugins/example-storage-plugin/Cargo.toml new file mode 100644 index 0000000000..37465b5f87 --- /dev/null +++ b/plugins/example-storage-plugin/Cargo.toml @@ -0,0 +1,39 @@ +# +# Copyright (c) 2023 ZettaScale Technology +# +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License 2.0 which is available at +# http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +# which is available at https://www.apache.org/licenses/LICENSE-2.0. +# +# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +# +# Contributors: +# ZettaScale Zenoh Team, +# +[package] +rust-version = { workspace = true } +name = "zenoh-plugin-storage-example" +version = { workspace = true } +authors = { workspace = true } +edition = { workspace = true } + +[lib] +name = "zenoh_backend_example" +# This crate type will make `cargo` output a dynamic library instead of a rust static library +crate-type = ["cdylib"] + +[dependencies] +async-std = { workspace = true, features = ["default"] } +clap = { workspace = true } +env_logger = { workspace = true } +futures = { workspace = true } +log = { workspace = true } +serde_json = { workspace = true } +zenoh = { workspace = true } +zenoh-core = { workspace = true } +zenoh-plugin-trait = { workspace = true } +zenoh-result = { workspace = true } +zenoh-util = { workspace = true } +async-trait = { workspace = true } +zenoh_backend_traits = { workspace = true } diff --git a/plugins/example-storage-plugin/src/lib.rs b/plugins/example-storage-plugin/src/lib.rs new file mode 100644 index 0000000000..ff21798992 --- /dev/null +++ b/plugins/example-storage-plugin/src/lib.rs @@ -0,0 +1,90 @@ +use std::sync::Arc; + +// +// Copyright (c) 2023 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, +// +use async_trait::async_trait; +use zenoh::{prelude::OwnedKeyExpr, sample::Sample, time::Timestamp, value::Value}; +use zenoh_backend_traits::{ + config::{StorageConfig, VolumeConfig}, + Capability, History, Persistence, Storage, StorageInsertionResult, StoredData, Volume, +}; +use zenoh_result::ZResult; + +#[no_mangle] +pub fn create_volume(_config: VolumeConfig) -> ZResult> { + let volume = ExampleBackendStorage {}; + Ok(Box::new(volume)) +} + +pub struct ExampleBackendStorage {} + +pub struct ExampleStorage {} + +#[async_trait] +impl Volume for ExampleBackendStorage { + fn get_admin_status(&self) -> serde_json::Value { + serde_json::Value::Null + } + fn get_capability(&self) -> Capability { + Capability { + persistence: Persistence::Volatile, + history: History::Latest, + read_cost: 0, + } + } + async fn create_storage(&mut self, _props: StorageConfig) -> ZResult> { + Ok(Box::new(ExampleStorage {})) + } + fn incoming_data_interceptor(&self) -> Option Sample + Send + Sync>> { + None + } + fn outgoing_data_interceptor(&self) -> Option Sample + Send + Sync>> { + None + } +} + +#[async_trait] +impl Storage for ExampleStorage { + fn get_admin_status(&self) -> serde_json::Value { + serde_json::Value::Null + } + async fn put( + &mut self, + _key: Option, + _value: Value, + _timestamp: Timestamp, + ) -> ZResult { + Ok(StorageInsertionResult::Inserted) + } + + async fn delete( + &mut self, + _key: Option, + _timestamp: Timestamp, + ) -> ZResult { + Ok(StorageInsertionResult::Deleted) + } + + async fn get( + &mut self, + _key: Option, + _parameters: &str, + ) -> ZResult> { + Ok(Vec::new()) + } + + async fn get_all_entries(&self) -> ZResult, Timestamp)>> { + Ok(Vec::new()) + } +} diff --git a/plugins/zenoh-plugin-storage-manager/config.json5 b/plugins/zenoh-plugin-storage-manager/config.json5 index 5f2f1c3195..204fa27e96 100644 --- a/plugins/zenoh-plugin-storage-manager/config.json5 +++ b/plugins/zenoh-plugin-storage-manager/config.json5 @@ -1,13 +1,16 @@ { "volumes": { - "influxdb_local": { - "__path__": "../zenoh-backend-influxdb/target/debug/libzenoh_backend_influxdb.dylib", - "__config__": "config_influxdb_local.json5" - }, - "influxdb_remote": { - "__path__": "../zenoh-backend-influxdb/target/debug/libzenoh_backend_influxdb.dylib", - "__config__": "config_influxdb_remote.json5" + "example" : { + "__path__": "./plugins/example-storage-plugin/target/debug/libexample_storage_plugin.dylib", } + //"influxdb_local": { + // "__path__": "../zenoh-backend-influxdb/target/debug/libzenoh_backend_influxdb.dylib", + // "__config__": "config_influxdb_local.json5" + //}, + //"influxdb_remote": { + // "__path__": "../zenoh-backend-influxdb/target/debug/libzenoh_backend_influxdb.dylib", + // "__config__": "config_influxdb_remote.json5" + //} }, "storages": {} } \ No newline at end of file