feat(tvix/store): add blobservice
This adds a BlobService trait, and an implementation for it using sled, and one using a HashMap. Change-Id: Id6bc1b629195d0b26fc503bd7d2dc9e43c41c317 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8087 Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
parent
119aa43171
commit
b74ffda583
4 changed files with 146 additions and 0 deletions
59
tvix/store/src/blobservice/memory.rs
Normal file
59
tvix/store/src/blobservice/memory.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use data_encoding::BASE64;
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
use tracing::instrument;
|
||||
|
||||
use crate::{proto, Error};
|
||||
|
||||
use super::BlobService;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MemoryBlobService {
|
||||
db: Arc<RwLock<HashMap<Vec<u8>, proto::BlobMeta>>>,
|
||||
}
|
||||
|
||||
impl MemoryBlobService {
|
||||
pub fn new() -> Self {
|
||||
let db = Arc::new(RwLock::new(HashMap::default()));
|
||||
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
impl BlobService for MemoryBlobService {
|
||||
#[instrument(skip(self, req), fields(blob.digest=BASE64.encode(&req.digest)))]
|
||||
fn stat(&self, req: &proto::StatBlobRequest) -> Result<Option<proto::BlobMeta>, Error> {
|
||||
if req.include_bao {
|
||||
todo!("not implemented yet")
|
||||
}
|
||||
|
||||
let db = self.db.read().unwrap();
|
||||
// if include_chunks is also false, the user only wants to know if the
|
||||
// blob is present at all.
|
||||
if !req.include_chunks {
|
||||
Ok(if db.contains_key(&req.digest) {
|
||||
Some(proto::BlobMeta::default())
|
||||
} else {
|
||||
None
|
||||
})
|
||||
} else {
|
||||
match db.get(&req.digest) {
|
||||
None => Ok(None),
|
||||
Some(blob_meta) => Ok(Some(blob_meta.clone())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self, blob_meta, blob_digest), fields(blob.digest = BASE64.encode(blob_digest)))]
|
||||
fn put(&self, blob_digest: &[u8], blob_meta: proto::BlobMeta) -> Result<(), Error> {
|
||||
let mut db = self.db.write().unwrap();
|
||||
|
||||
db.insert(blob_digest.to_vec(), blob_meta);
|
||||
|
||||
Ok(())
|
||||
// TODO: make sure all callers make sure the chunks exist.
|
||||
// TODO: where should we calculate the bao?
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue