8 releases

0.10.1-rc Dec 20, 2023
0.10.0-rc Sep 27, 2023
0.7.2-rc Jun 6, 2023
0.7.0-rc Dec 21, 2022
0.5.0-beta.8 Mar 31, 2021

#1350 in Network programming

Download history 104/week @ 2023-12-19 2/week @ 2023-12-26 13/week @ 2024-01-02 34/week @ 2024-01-09 28/week @ 2024-01-16 2/week @ 2024-01-23 3/week @ 2024-02-13 13/week @ 2024-02-20 36/week @ 2024-02-27 17/week @ 2024-03-05 22/week @ 2024-03-12 11/week @ 2024-03-19 1/week @ 2024-03-26 38/week @ 2024-04-02

79 downloads per month
Used in 7 crates

EPL-2.0 OR Apache-2.0

1MB
19K SLoC

⚠️ WARNING ⚠️

This crate is intended for Zenoh's internal use.


lib.rs:

⚠️ 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:

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::prelude::r#async::*;
use zenoh::time::Timestamp;
use zenoh_backend_traits::*;
use zenoh_backend_traits::config::*;
use zenoh::Result as ZResult;

#[no_mangle]
pub fn create_volume(config: VolumeConfig) -> ZResult<Box<dyn Volume>> {
    Ok(Box::new(MyVolumeType { config }))
}

// Your Backend implementation
struct MyVolumeType {
    config: VolumeConfig,
}

#[async_trait]
impl Volume for MyVolumeType {
    fn get_admin_status(&self) -> serde_json::Value {
        // 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()
    }

    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,
            read_cost: 0,
        }
    }

    async fn create_storage(&mut self, properties: StorageConfig) -> ZResult<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?))
    }

    fn incoming_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>> {
        // No interception point for incoming data (on PUT operations)
        None
    }

    fn outgoing_data_interceptor(&self) -> Option<Arc<dyn Fn(Sample) -> Sample + Send + Sync>> {
        // No interception point for outgoing data (on GET operations)
        None
    }
}

// Your Storage implementation
struct MyStorage {
    config: StorageConfig,
}

impl MyStorage {
    async fn new(config: StorageConfig) -> ZResult<MyStorage> {
        Ok(MyStorage { config })
    }
}

#[async_trait]
impl Storage for MyStorage {
    fn get_admin_status(&self) -> serde_json::Value {
        // 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()
    }

    async fn put(&mut self, key: Option<OwnedKeyExpr>, value: Value, timestamp: Timestamp) -> ZResult<StorageInsertionResult> {
        // the key will be None if it exactly matched with the strip_prefix
        // create a storge 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) -> ZResult<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) -> ZResult<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) -> ZResult<Vec<(Option<OwnedKeyExpr>, Timestamp)>> {
        // @TODO: get the list of (key, timestamp) in the datastore
        Ok(Vec::new())
    }
}

Dependencies

~23–38MB
~577K SLoC