Skip to content

Commit

Permalink
Merge pull request #236 from amosproj/in_memory_filesystem
Browse files Browse the repository at this point in the history
In memory filesystem
  • Loading branch information
fhilgers authored Jan 29, 2025
2 parents fddcdfe + 174c38a commit db3e81e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
40 changes: 31 additions & 9 deletions rust/backend/daemon/src/filesystem/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,41 @@

use std::io;

use std::collections::HashMap;
use tokio::sync::RwLock;

use shared::config::Configuration;

use super::Filesystem;
use super::ConfigurationStorage;

pub struct MemoryConfigurationStorage {
storage: RwLock<HashMap<String, Configuration>>,
}

// TODO: members + implementation
pub struct MemoryFilesystem;
impl MemoryConfigurationStorage {
#[allow(dead_code)]
pub fn new() -> Self {
MemoryConfigurationStorage {
storage: RwLock::new(HashMap::new()),
}
}
}

impl Filesystem for MemoryFilesystem {
fn load(&self, _path: &str) -> io::Result<Configuration> {
todo!()
impl ConfigurationStorage for MemoryConfigurationStorage {
async fn load(&self, path: &str) -> io::Result<Configuration> {
tokio::task::block_in_place(|| {
let storage = self.storage.blocking_read();
storage.get(path).cloned().ok_or_else(|| {
io::Error::new(io::ErrorKind::NotFound, "Configuration not found")
})
})
}

fn save(&self, _config: &Configuration, _path: &str) -> io::Result<()> {
todo!()
async fn save(&self, config: &Configuration, path: &str) -> io::Result<()> {
tokio::task::block_in_place(|| {
let mut storage = self.storage.blocking_write();
storage.insert(path.to_string(), config.clone());
Ok(())
})
}
}
}
20 changes: 7 additions & 13 deletions rust/backend/daemon/src/filesystem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,22 @@
// SPDX-License-Identifier: MIT

use std::io;
use std::future::Future;

use shared::config::Configuration;


mod normal;
mod memory;

pub use normal::NormalFilesystem;
pub use normal::NormalConfigurationStorage;

// TODO: pub use memory::MemoryFilesystem;
#[allow(unused_imports)]
pub use memory::MemoryConfigurationStorage;

/*
* TODOs:
* - This should probably not be named Filesystem, because the functionality is much more narrow
* than that. Maybe something like ConfigurationStore or ConfigurationStorage?
* - The trait should definetly be async, because otherwise we always have to use spawn_blocking.
* See the tokio documentation for why: https://docs.rs/tokio/latest/tokio/task/fn.spawn_blocking.html
* You can use tokio::fs for file system operations.
*/
pub trait Filesystem: Send + Sync + 'static {
fn load(&self, path: &str) -> io::Result<Configuration>;
pub trait ConfigurationStorage: Send + Sync + 'static {
fn load(&self, path: &str) -> impl Future<Output = io::Result<Configuration>> + Send ;

fn save(&self, config: &Configuration, path: &str) -> io::Result<()>;
fn save(&self, config: &Configuration, path: &str) -> impl Future<Output = io::Result<()>> + Send ;
}

10 changes: 5 additions & 5 deletions rust/backend/daemon/src/filesystem/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ use std::{fs::File, io::{self, BufReader, BufWriter}};

use shared::config::Configuration;

use super::Filesystem;
use super::ConfigurationStorage;


pub struct NormalFilesystem;
pub struct NormalConfigurationStorage;

impl Filesystem for NormalFilesystem {
fn load(&self, path: &str) -> io::Result<Configuration> {
impl ConfigurationStorage for NormalConfigurationStorage {
async fn load(&self, path: &str) -> io::Result<Configuration> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let config = serde_json::from_reader(reader)?;
Ok(config)
}

fn save(&self, config: &Configuration, path: &str) -> io::Result<()> {
async fn save(&self, config: &Configuration, path: &str) -> io::Result<()> {
let file = File::create(path)?;
let writer = BufWriter::new(file);
serde_json::to_writer(writer, config)?;
Expand Down
36 changes: 16 additions & 20 deletions rust/backend/daemon/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// SPDX-License-Identifier: MIT

use crate::collector::{CollectorSupervisor, CollectorSupervisorArguments};
use crate::filesystem::{Filesystem, NormalFilesystem};
use crate::filesystem::{ConfigurationStorage, NormalConfigurationStorage};
use crate::registry;
use crate::symbols::actors::{GetOffsetRequest, SearchReq, SymbolActor, SymbolActorMsg};
use crate::symbols::SymbolHandler;
Expand Down Expand Up @@ -35,33 +35,31 @@ use tokio::sync::{mpsc, Mutex};
use tokio_stream::wrappers::ReceiverStream;
use tonic::{transport::Server, Request, Response, Status};

pub struct ZiofaImpl<F>
where
F: Filesystem,
{
pub struct ZiofaImpl<C>
where C: ConfigurationStorage {
features: Arc<Mutex<Features>>,
channel: Arc<Channel>,
symbol_handler: Arc<Mutex<SymbolHandler>>,
filesystem: F,
configuration_storage: C,
symbol_actor_ref: ActorRef<SymbolActorMsg>,
}

impl<F> ZiofaImpl<F>
impl<C> ZiofaImpl<C>
where
F: Filesystem,
C: ConfigurationStorage,
{
pub fn new(
features: Arc<Mutex<Features>>,
channel: Arc<Channel>,
symbol_handler: Arc<Mutex<SymbolHandler>>,
filesystem: F,
configuration_storage: C,
symbol_actor_ref: ActorRef<SymbolActorMsg>,
) -> ZiofaImpl<F> {
) -> ZiofaImpl<C> {
ZiofaImpl {
features,
channel,
symbol_handler,
filesystem,
configuration_storage,
symbol_actor_ref,
}
}
Expand All @@ -80,10 +78,8 @@ impl Channel {
}

#[tonic::async_trait]
impl<F> Ziofa for ZiofaImpl<F>
where
F: Filesystem,
{
impl<C> Ziofa for ZiofaImpl<C>
where C: ConfigurationStorage {
async fn check_server(&self, _: Request<()>) -> Result<Response<CheckServerResponse>, Status> {
// dummy data
let response = CheckServerResponse {};
Expand All @@ -97,7 +93,8 @@ where

async fn get_configuration(&self, _: Request<()>) -> Result<Response<Configuration>, Status> {
//TODO: if ? fails needs valid return value for the function so that the server doesn't crash.
let config = self.filesystem.load(constants::DEV_DEFAULT_FILE_PATH)?;
let res = self.configuration_storage.load(constants::DEV_DEFAULT_FILE_PATH).await;
let config = res?;
Ok(Response::new(config))
}

Expand All @@ -107,8 +104,7 @@ where
) -> Result<Response<()>, Status> {
let config = request.into_inner();

self.filesystem
.save(&config, constants::DEV_DEFAULT_FILE_PATH)?;
self.configuration_storage.save(&config, constants::DEV_DEFAULT_FILE_PATH).await?;

let mut features_guard = self.features.lock().await;

Expand Down Expand Up @@ -289,7 +285,7 @@ where
}


async fn setup() -> (ActorRef<()>, ZiofaServer<ZiofaImpl<NormalFilesystem>>) {
async fn setup() -> (ActorRef<()>, ZiofaServer<ZiofaImpl<NormalConfigurationStorage>>) {
let registry = registry::load_and_pin().unwrap();

let symbol_actor_ref = SymbolActor::spawn().await.unwrap();
Expand All @@ -310,7 +306,7 @@ async fn setup() -> (ActorRef<()>, ZiofaServer<ZiofaImpl<NormalFilesystem>>) {

let features = Arc::new(Mutex::new(features));

let filesystem = NormalFilesystem;
let filesystem = NormalConfigurationStorage;

let ziofa_server = ZiofaServer::new(ZiofaImpl::new(features, channel, symbol_handler, filesystem, symbol_actor_ref));

Expand Down

0 comments on commit db3e81e

Please sign in to comment.