1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
// 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, <zenoh@zettascale.tech>
//
//! ⚠️ WARNING ⚠️
//!
//! This crate should be considered unstable, as in we might change the APIs anytime.
//!
//! This crate provides the traits to be implemented by a zenoh backend library:
//! - [`Volume`]
//! - [`Storage`]
//!
//! Such library must also declare a `create_volume()` operation
//! with the `#[no_mangle]` attribute as an entrypoint to be called for the Backend creation.
//!
//! # Example
//! ```
//! use std::sync::Arc;
//! use async_trait::async_trait;
//! use zenoh::{key_expr::OwnedKeyExpr, time::Timestamp, bytes::{ZBytes, Encoding}};
//! use zenoh_backend_traits::*;
//! use zenoh_backend_traits::config::*;
//! use zenoh_plugin_trait::{plugin_long_version, plugin_version, Plugin};
//! use zenoh_result::ZResult;
//! use zenoh_util::ffi::JsonValue;
//!
//!
//! // Your Backend volume implementation
//! struct MyVolumeType {
//! config: VolumeConfig,
//! }
//!
//! // Create the entry point for your backend
//! zenoh_plugin_trait::declare_plugin!(MyVolumeType);
//!
//! impl Plugin for MyVolumeType {
//! type StartArgs = VolumeConfig;
//! type Instance = VolumeInstance;
//! fn start(_name: &str, _args: &Self::StartArgs) -> ZResult<Self::Instance> {
//! let volume = MyVolumeType {config: _args.clone()};
//! Ok(Box::new(volume))
//! }
//!
//! const DEFAULT_NAME: &'static str = "my_backend";
//! const PLUGIN_VERSION: &'static str = plugin_version!();
//! const PLUGIN_LONG_VERSION: &'static str = plugin_long_version!();
//! }
//!
//!
//!
//! #[async_trait]
//! impl Volume for MyVolumeType {
//! fn get_admin_status(&self) -> JsonValue {
//! // This operation is called on GET operation on the admin space for the Volume
//! // Here we reply with a static status (containing the configuration properties).
//! // But we could add dynamic properties for Volume monitoring.
//! self.config.to_json_value().into()
//! }
//!
//! fn get_capability(&self) -> Capability {
//! // This operation is used to confirm if the volume indeed supports
//! // the capabilities requested by the configuration
//! Capability{
//! persistence: Persistence::Volatile,
//! history: History::Latest,
//! }
//! }
//!
//! async fn create_storage(&self, properties: StorageConfig) -> zenoh::Result<Box<dyn Storage>> {
//! // The properties are the ones passed via a PUT in the admin space for Storage creation.
//! Ok(Box::new(MyStorage::new(properties).await?))
//! }
//! }
//!
//! // Your Storage implementation
//! struct MyStorage {
//! config: StorageConfig,
//! }
//!
//! impl MyStorage {
//! async fn new(config: StorageConfig) -> zenoh::Result<MyStorage> {
//! Ok(MyStorage { config })
//! }
//! }
//!
//! #[async_trait]
//! impl Storage for MyStorage {
//! fn get_admin_status(&self) -> JsonValue {
//! // This operation is called on GET operation on the admin space for the Storage
//! // Here we reply with a static status (containing the configuration properties).
//! // But we could add dynamic properties for Storage monitoring.
//! self.config.to_json_value().into()
//! }
//!
//! async fn put(&mut self, key: Option<OwnedKeyExpr>, payload: ZBytes, encoding: Encoding, timestamp: Timestamp) -> zenoh::Result<StorageInsertionResult> {
//! // the key will be None if it exactly matched with the strip_prefix
//! // create a storage specific special structure to store it
//! // Store the data with timestamp
//! // @TODO:
//! // store (key, value, timestamp)
//! return Ok(StorageInsertionResult::Inserted);
//! // - if any issue: drop
//! // return Ok(StorageInsertionResult::Outdated);
//! }
//!
//! async fn delete(&mut self, key: Option<OwnedKeyExpr>, timestamp: Timestamp) -> zenoh::Result<StorageInsertionResult> {
//! // @TODO:
//! // delete the actual entry from storage
//! return Ok(StorageInsertionResult::Deleted);
//! }
//!
//! // When receiving a GET operation
//! async fn get(&mut self, key_expr: Option<OwnedKeyExpr>, parameters: &str) -> zenoh::Result<Vec<StoredData>> {
//! // @TODO:
//! // get the data associated with key_expr and return it
//! // NOTE: in case parameters is not empty something smarter should be done with returned data...
//! Ok(Vec::new())
//! }
//!
//! // To get all entries in the datastore
//! async fn get_all_entries(&self) -> zenoh::Result<Vec<(Option<OwnedKeyExpr>, Timestamp)>> {
//! // @TODO: get the list of (key, timestamp) in the datastore
//! Ok(Vec::new())
//! }
//! }
//! ```
use async_trait;
use ;
use ;
use ;
use StorageConfig;
// No features are actually used in this crate, but this dummy list allows to demonstrate how to combine feature lists
// from multiple crates. See impl `PluginStructVersion` for `VolumeConfig` below.
const FEATURES: &str =
concat_enabled_features!;
/// Capability of a storage indicates the guarantees of the storage
/// It is used by the storage manager to take decisions on the trade-offs to ensure correct performance
/// Persistence is the guarantee expected from a storage in case of failures
/// If a storage is marked Persistent::Durable, if it restarts after a crash, it will still have all the values that were saved.
/// This will include also persisting the metadata that Zenoh stores for the updates.
/// If a storage is marked Persistent::Volatile, the storage will not have any guarantees on its content after a crash.
/// This option should be used only if the storage is considered to function as a cache.
/// History is the number of values that the backend is expected to save per key
/// History::Latest saves only the latest value per key
/// History::All saves all the values including historical values
/// Trait to be implemented by a Backend.
pub type VolumeInstance = ;
/// Trait to be implemented by a Storage.