|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::fmt::{Debug, Display}; |
| 3 | +use std::hash::Hash; |
| 4 | +use uuid::Uuid; |
| 5 | +use warp::r#type::guid::TypeGUID; |
| 6 | +use warp::r#type::Type; |
| 7 | +use warp::signature::function::{Function, FunctionGUID}; |
| 8 | + |
| 9 | +pub mod disk; |
| 10 | +pub mod memory; |
| 11 | +pub mod network; |
| 12 | + |
| 13 | +/// Represents the ID for a single container source. |
| 14 | +/// |
| 15 | +/// A source is used to relate types and functions separate from the container. This allows |
| 16 | +/// type name lookups and for containers which are bandwidth sensitive to exist. |
| 17 | +/// |
| 18 | +/// An example of a bandwidth sensitive container would be a container which pulls functions over |
| 19 | +/// the network instead of from memory or disk. |
| 20 | +#[derive(Clone, Debug, Eq, PartialEq, Hash, Copy)] |
| 21 | +pub struct SourceId(Uuid); |
| 22 | + |
| 23 | +impl SourceId { |
| 24 | + pub fn new() -> Self { |
| 25 | + Self(Uuid::new_v4()) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Storage for WARP functions. |
| 30 | +/// |
| 31 | +/// Containers are made up of sources, see [`SourceId`] for more details. |
| 32 | +pub trait Container: Send + Sync + Display + Debug { |
| 33 | + /// Get the sources that contain a type with the given [`TypeGUID`]. |
| 34 | + fn sources_with_type_guid(&self, guid: &TypeGUID) -> Vec<&SourceId>; |
| 35 | + |
| 36 | + /// Plural version of [`Container::sources_with_type_guid`]. |
| 37 | + /// |
| 38 | + /// Each source will have a list of the containing GUID's so that when looking up a source you give |
| 39 | + /// it only the GUID's that it knows about, for networking this means cutting down traffic significantly. |
| 40 | + fn sources_with_type_guids<'a>( |
| 41 | + &'a self, |
| 42 | + guids: &'a [TypeGUID], |
| 43 | + ) -> HashMap<&'a TypeGUID, Vec<&'a SourceId>>; |
| 44 | + |
| 45 | + /// Retrieve all [`TypeGUID`]'s with the given name. |
| 46 | + fn type_guids_with_name(&self, source: &SourceId, name: &str) -> Vec<TypeGUID>; |
| 47 | + |
| 48 | + fn type_with_guid(&self, source: &SourceId, guid: &TypeGUID) -> Option<Type>; |
| 49 | + |
| 50 | + fn has_type_with_guid(&self, source: &SourceId, guid: &TypeGUID) -> bool { |
| 51 | + self.type_with_guid(source, guid).is_some() |
| 52 | + } |
| 53 | + |
| 54 | + /// Get the sources that contain functions with the given [`FunctionGUID`]. |
| 55 | + fn sources_with_function_guid(&self, guid: &FunctionGUID) -> Vec<&SourceId>; |
| 56 | + |
| 57 | + // TODO: Allocating with Vec is not good. |
| 58 | + /// Plural version of [`Container::sources_with_function_guid`]. |
| 59 | + /// |
| 60 | + /// Each source will have a list of the containing GUID's so that when looking up a source you give |
| 61 | + /// it only the GUID's that it knows about, for networking this means cutting down traffic significantly. |
| 62 | + fn sources_with_function_guids<'a>( |
| 63 | + &'a self, |
| 64 | + guids: &'a [FunctionGUID], |
| 65 | + ) -> HashMap<&'a FunctionGUID, Vec<&'a SourceId>>; |
| 66 | + |
| 67 | + fn functions_with_guid(&self, source: &SourceId, guid: &FunctionGUID) -> Vec<Function>; |
| 68 | + |
| 69 | + fn has_function_with_guid(&self, source: &SourceId, guid: &FunctionGUID) -> bool { |
| 70 | + !self.functions_with_guid(source, guid).is_empty() |
| 71 | + } |
| 72 | +} |
0 commit comments