Skip to content

Commit 8645049

Browse files
committed
Switch to content block type
1 parent fa94cc3 commit 8645049

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/models/immutable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Clone, Debug, Deserialize, Serialize)]
4+
pub struct ContentBlock {
5+
pub data: Vec<u8>,
6+
pub references: Vec<blake3::Hash>,
7+
}

src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
mod immutable;
12
mod name;
23
mod namespace;
34
mod peer;
45
mod value;
56

7+
pub use immutable::ContentBlock;
68
pub use name::Name;
79
pub use namespace::NamespaceValues;
810
pub use peer::Peers;

src/server/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
http::{HttpConnection, NamespaceResponse},
2020
},
2121
crypto::{Signed, encode::decode_verifying_key},
22-
models::Peers,
22+
models::{ContentBlock, Peers},
2323
server::{
2424
data_controller::DataController, immutable_controller::ImmutableController,
2525
sqlite_store::SqliteStore, task_controller::TaskController,
@@ -196,14 +196,14 @@ async fn get_immutable(
196196
let hash = blake3::Hash::from_hex(&hash).unwrap();
197197
let data = state.immutable_controller.get(&hash).await;
198198
match data {
199-
Some(data) => (StatusCode::OK, Json(data)),
200-
None => (StatusCode::NOT_FOUND, Json(Vec::new())),
199+
Some(data) => (StatusCode::OK, Json(data)).into_response(),
200+
None => (StatusCode::NOT_FOUND, "404 not found").into_response(),
201201
}
202202
}
203203

204204
async fn post_immutable(
205205
State(state): State<AppState>,
206-
Json(body): Json<Vec<u8>>,
206+
Json(body): Json<ContentBlock>,
207207
) -> impl IntoResponse {
208208
let hash = state.immutable_controller.set(&body).await;
209209
Json(hash)

src/server/immutable_controller.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::{path::PathBuf, sync::Arc};
22

33
use tokio::sync::RwLock;
44

5+
use crate::models::ContentBlock;
6+
57
#[derive(Debug, Clone)]
68
pub struct ImmutableController {
79
basedir: PathBuf,
@@ -17,18 +19,20 @@ impl ImmutableController {
1719
}
1820
}
1921

20-
pub async fn get(&self, hash: &blake3::Hash) -> Option<Vec<u8>> {
22+
pub async fn get(&self, hash: &blake3::Hash) -> Option<ContentBlock> {
2123
let _guard = self.filesystem_lock.read().await;
2224
let path = self.basedir.join(hash.to_string());
23-
tokio::fs::read(&path).await.ok()
25+
let encoded = tokio::fs::read(&path).await.ok()?;
26+
bincode::deserialize(&encoded).ok()
2427
}
2528

26-
pub async fn set(&self, content: &Vec<u8>) -> blake3::Hash {
29+
pub async fn set(&self, content: &ContentBlock) -> blake3::Hash {
2730
let _guard = self.filesystem_lock.write().await;
28-
let hash = blake3::hash(content);
31+
let encoded = bincode::serialize(content).unwrap();
32+
let hash = blake3::hash(&encoded);
2933
let path = self.basedir.join(hash.to_string());
3034
if !path.exists() {
31-
tokio::fs::write(&path, &content).await.unwrap();
35+
tokio::fs::write(&path, &encoded).await.unwrap();
3236
}
3337
hash
3438
}

0 commit comments

Comments
 (0)