feat(tvix/store): eliminate generics in BlobStore
To construct various stores at runtime, we need to eliminate associated types from the BlobService trait, and return Box<dyn …> instead of specific types. This also means we can't consume self in the close() method, so everything we write to is put in an Option<>, and during the first close we take from there. Change-Id: Ia523b6ab2f2a5276f51cb5d17e81a5925bce69b6 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8647 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
parent
5139cc45c2
commit
27ff98000b
15 changed files with 227 additions and 140 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use std::io::Cursor;
|
||||
use std::io::{self, Cursor};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
|
|
@ -14,63 +14,102 @@ pub struct MemoryBlobService {
|
|||
}
|
||||
|
||||
impl BlobService for MemoryBlobService {
|
||||
type BlobReader = Cursor<Vec<u8>>;
|
||||
type BlobWriter = MemoryBlobWriter;
|
||||
|
||||
#[instrument(skip(self, digest), fields(blob.digest=%digest))]
|
||||
fn has(&self, digest: &B3Digest) -> Result<bool, Error> {
|
||||
let db = self.db.read().unwrap();
|
||||
Ok(db.contains_key(digest))
|
||||
}
|
||||
|
||||
fn open_read(&self, digest: &B3Digest) -> Result<Option<Self::BlobReader>, Error> {
|
||||
fn open_read(&self, digest: &B3Digest) -> Result<Option<Box<dyn io::Read + Send>>, Error> {
|
||||
let db = self.db.read().unwrap();
|
||||
|
||||
Ok(db.get(digest).map(|x| Cursor::new(x.clone())))
|
||||
match db.get(digest).map(|x| Cursor::new(x.clone())) {
|
||||
Some(result) => Ok(Some(Box::new(result))),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(skip(self))]
|
||||
fn open_write(&self) -> Result<Self::BlobWriter, Error> {
|
||||
Ok(MemoryBlobWriter::new(self.db.clone()))
|
||||
fn open_write(&self) -> Result<Box<dyn BlobWriter>, Error> {
|
||||
Ok(Box::new(MemoryBlobWriter::new(self.db.clone())))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MemoryBlobWriter {
|
||||
db: Arc<RwLock<HashMap<B3Digest, Vec<u8>>>>,
|
||||
|
||||
buf: Vec<u8>,
|
||||
/// Contains the Vec and hasher, or None if already closed
|
||||
writers: Option<(Vec<u8>, blake3::Hasher)>,
|
||||
|
||||
/// The digest that has been returned, if we successfully closed.
|
||||
digest: Option<B3Digest>,
|
||||
}
|
||||
|
||||
impl MemoryBlobWriter {
|
||||
fn new(db: Arc<RwLock<HashMap<B3Digest, Vec<u8>>>>) -> Self {
|
||||
Self {
|
||||
buf: Vec::new(),
|
||||
db,
|
||||
writers: Some((Vec::new(), blake3::Hasher::new())),
|
||||
digest: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl std::io::Write for MemoryBlobWriter {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
self.buf.write(buf)
|
||||
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
|
||||
match &mut self.writers {
|
||||
None => Err(io::Error::new(
|
||||
io::ErrorKind::NotConnected,
|
||||
"already closed",
|
||||
)),
|
||||
Some((ref mut buf, ref mut hasher)) => {
|
||||
let bytes_written = buf.write(b)?;
|
||||
hasher.write(&buf[..bytes_written])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
self.buf.flush()
|
||||
match &mut self.writers {
|
||||
None => Err(io::Error::new(
|
||||
io::ErrorKind::NotConnected,
|
||||
"already closed",
|
||||
)),
|
||||
Some(_) => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BlobWriter for MemoryBlobWriter {
|
||||
fn close(self) -> Result<B3Digest, Error> {
|
||||
// in this memory implementation, we don't actually bother hashing
|
||||
// incrementally while writing, but do it at the end.
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
hasher.update(&self.buf);
|
||||
let digest = B3Digest::from_vec(hasher.finalize().as_bytes().to_vec()).unwrap();
|
||||
fn close(&mut self) -> Result<B3Digest, Error> {
|
||||
if self.writers.is_none() {
|
||||
match &self.digest {
|
||||
Some(digest) => Ok(digest.clone()),
|
||||
None => Err(crate::Error::StorageError(
|
||||
"previously closed with error".to_string(),
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
let (buf, hasher) = self.writers.take().unwrap();
|
||||
|
||||
// open the database for writing.
|
||||
let mut db = self.db.write()?;
|
||||
db.insert(digest.clone(), self.buf);
|
||||
// We know self.hasher is doing blake3 hashing, so this won't fail.
|
||||
let digest = B3Digest::from_vec(hasher.finalize().as_bytes().to_vec()).unwrap();
|
||||
|
||||
Ok(digest)
|
||||
// Only insert if the blob doesn't already exist.
|
||||
let db = self.db.read()?;
|
||||
if !db.contains_key(&digest) {
|
||||
// drop the read lock, so we can open for writing.
|
||||
drop(db);
|
||||
|
||||
// open the database for writing.
|
||||
let mut db = self.db.write()?;
|
||||
|
||||
// and put buf in there. This will move buf out.
|
||||
db.insert(digest.clone(), buf);
|
||||
}
|
||||
|
||||
self.digest = Some(digest.clone());
|
||||
|
||||
Ok(digest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue