Resources lookup impl

This commit is contained in:
Nadja Reitzenstein
2022-03-13 20:33:26 +01:00
parent d5833f30c4
commit 07a7cbe42b
4 changed files with 49 additions and 16 deletions

View File

@ -3,6 +3,7 @@ use std::sync::Arc;
use futures_signals::signal::{Mutable, Signal, SignalExt};
use lmdb::RoTransaction;
use rkyv::Archived;
use crate::config::MachineDescription;
use crate::db::LMDBorrow;
use crate::resources::modules::fabaccess::{MachineState, Status};
use crate::resources::state::db::StateDB;
@ -22,11 +23,12 @@ pub struct PermissionDenied;
pub(crate) struct Inner {
id: String,
db: StateDB,
db: Arc<StateDB>,
signal: Mutable<MachineState>,
desc: MachineDescription,
}
impl Inner {
pub fn new(id: String, db: StateDB) -> Self {
pub fn new(id: String, db: Arc<StateDB>, desc: MachineDescription) -> Self {
let state = if let Some(previous) = db.get_output(id.as_bytes()).unwrap() {
let state = MachineState::from(&previous);
tracing::info!(%id, ?state, "Found previous state");
@ -37,7 +39,7 @@ impl Inner {
};
let signal = Mutable::new(state);
Self { id, db, signal }
Self { id, db, signal, desc }
}
pub fn signal(&self) -> impl Signal<Item=MachineState> {

View File

@ -1,13 +1,21 @@
use std::collections::HashMap;
use std::sync::Arc;
use crate::resources::Resource;
struct Inner {
id: HashMap<String, Resource>,
}
impl Inner {
pub fn new() -> Self {
Self { }
pub fn new(resources: impl IntoIterator<Item=Resource>) -> Self {
let mut id = HashMap::new();
for resource in resources {
let old = id.insert(resource.inner.id.clone(), resource);
assert!(old.is_none());
}
Self { id }
}
}
@ -17,22 +25,34 @@ pub struct ResourcesHandle {
}
impl ResourcesHandle {
pub fn new() -> Self {
pub fn new(resources: impl IntoIterator<Item=Resource>) -> Self {
Self {
inner: Arc::new(Inner::new()),
inner: Arc::new(Inner::new(resources)),
}
}
pub fn list_all(&self) -> impl IntoIterator<Item=&Resource> {
unimplemented!();
&[]
self.inner.id.values()
}
pub fn get_by_id(&self, id: &str) -> Option<&Resource> {
unimplemented!()
self.inner.id.get(id)
}
pub fn get_by_urn(&self, urn: &str) -> Option<&Resource> {
unimplemented!()
if let Some(id) = {
let mut parts = urn.split_terminator(':');
let part_urn = parts.next().map(|u| u == "urn").unwrap_or(false);
let part_fabaccess = parts.next().map(|f| f == "fabaccess").unwrap_or(false);
let part_resource = parts.next().map(|r| r == "resource").unwrap_or(false);
if !(part_urn && part_fabaccess && part_resource) {
return None;
}
parts.next().map(|s| s.to_string())
} {
self.get_by_id(&id)
} else {
None
}
}
}