Skip to content

Commit

Permalink
Add FSChunkDB backend
Browse files Browse the repository at this point in the history
Now we can run locally again!
  • Loading branch information
billyb2 committed Apr 26, 2024
1 parent b5f4807 commit 4d1e168
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
40 changes: 31 additions & 9 deletions src/chunk_db/file.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
use s3::Bucket;
use std::sync::Arc;

use tokio::fs;

use crate::meta_db::MetaDB;

use super::ChunkDB;

pub struct FSCHunkDB {
bucket: Bucket,
}
pub struct FSChunkDB;

impl ChunkDB for FSCHunkDB {
impl ChunkDB for FSChunkDB {
fn new() -> anyhow::Result<Self> {
todo!()
Ok(Self)
}

async fn get_chunk(
&self,
chunk_id: &bfsp::ChunkID,
user_id: i64,
) -> anyhow::Result<Option<Vec<u8>>> {
todo!()
let path = Self::get_path(chunk_id, user_id).await;
match fs::read(path).await {
Ok(data) => Ok(Some(data)),
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => Ok(None),
_ => Err(err.into()),
},
}
}

async fn put_chunk(
Expand All @@ -25,14 +34,27 @@ impl ChunkDB for FSCHunkDB {
user_id: i64,
data: &[u8],
) -> anyhow::Result<()> {
todo!()
let path = Self::get_path(chunk_id, user_id).await;
fs::write(path, data).await?;
Ok(())
}

async fn delete_chunk(&self, chunk_id: &bfsp::ChunkID, user_id: i64) -> anyhow::Result<()> {
todo!()
let path = Self::get_path(chunk_id, user_id).await;
fs::remove_file(path).await?;

Ok(())
}

async fn get_path(chunk_id: &bfsp::ChunkID, user_id: i64) -> String {
let mut path = format!("./chunks/{user_id}/");
fs::create_dir_all(&path).await.unwrap();

path.push_str(&chunk_id.to_string());
path
}

async fn garbage_collect(&self, meta_db: Arc<impl MetaDB>) -> anyhow::Result<()> {
todo!()
}
}
6 changes: 4 additions & 2 deletions src/chunk_db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//pub mod file;
pub mod file;
pub mod s3;

use bfsp::ChunkID;
use std::{collections::HashMap, future::Future};
use std::{future::Future, sync::Arc};

use crate::meta_db::MetaDB;

pub trait ChunkDB: Sized + Send + Sync {
fn new() -> anyhow::Result<Self>;
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// FIXME: there's some infinite loop that causes CPU usage to spike
mod chunk_db;
mod meta_db;
mod tls;
Expand All @@ -24,6 +23,7 @@ use wtransport::Endpoint;
use wtransport::Identity;
use wtransport::ServerConfig;

use crate::chunk_db::file::FSChunkDB;
use crate::chunk_db::s3::S3ChunkDB;
use crate::meta_db::{InsertChunkError, MetaDB, PostgresMetaDB};
use anyhow::Result;
Expand Down Expand Up @@ -81,6 +81,9 @@ async fn main() -> Result<()> {
.map_err(|err| anyhow!("Error initializing database: {err:?}"))
.unwrap(),
);
#[cfg(debug_assertions)]
let chunk_db = Arc::new(FSChunkDB::new().unwrap());
#[cfg(not(debug_assertions))]
let chunk_db = Arc::new(S3ChunkDB::new().unwrap());

info!("Starting server!");
Expand Down

0 comments on commit 4d1e168

Please sign in to comment.