about summary refs log tree commit diff
path: root/tvix/store/src/blobservice/sled.rs
blob: 2b090335344db329b476da66f2dbb08a6df1cbcf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use super::{BlobService, BlobWriter};
use crate::{B3Digest, Error};
use std::{
    io::{self, Cursor},
    path::PathBuf,
};
use tracing::instrument;

#[derive(Clone)]
pub struct SledBlobService {
    db: sled::Db,
}

impl SledBlobService {
    pub fn new(p: PathBuf) -> Result<Self, sled::Error> {
        let config = sled::Config::default().use_compression(true).path(p);
        let db = config.open()?;

        Ok(Self { db })
    }

    pub fn new_temporary() -> Result<Self, sled::Error> {
        let config = sled::Config::default().temporary(true);
        let db = config.open()?;

        Ok(Self { db })
    }
}

impl BlobService for SledBlobService {
    type BlobReader = Cursor<Vec<u8>>;
    type BlobWriter = SledBlobWriter;

    #[instrument(skip(self), fields(blob.digest=%digest))]
    fn has(&self, digest: &B3Digest) -> Result<bool, Error> {
        match self.db.contains_key(digest.to_vec()) {
            Ok(has) => Ok(has),
            Err(e) => Err(Error::StorageError(e.to_string())),
        }
    }

    #[instrument(skip(self), fields(blob.digest=%digest))]
    fn open_read(&self, digest: &B3Digest) -> Result<Option<Self::BlobReader>, Error> {
        match self.db.get(digest.to_vec()) {
            Ok(None) => Ok(None),
            Ok(Some(data)) => Ok(Some(Cursor::new(data[..].to_vec()))),
            Err(e) => Err(Error::StorageError(e.to_string())),
        }
    }

    #[instrument(skip(self))]
    fn open_write(&self) -> Result<Self::BlobWriter, Error> {
        Ok(SledBlobWriter::new(self.db.clone()))
    }
}

pub struct SledBlobWriter {
    db: sled::Db,
    buf: Vec<u8>,
    hasher: blake3::Hasher,
}

impl SledBlobWriter {
    pub fn new(db: sled::Db) -> Self {
        Self {
            buf: Vec::default(),
            db,
            hasher: blake3::Hasher::new(),
        }
    }
}

impl io::Write for SledBlobWriter {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        let bytes_written = self.buf.write(buf)?;
        self.hasher.write(&buf[..bytes_written])
    }

    fn flush(&mut self) -> io::Result<()> {
        self.buf.flush()
    }
}

impl BlobWriter for SledBlobWriter {
    fn close(self) -> Result<B3Digest, Error> {
        let digest = self.hasher.finalize();
        self.db
            .insert(digest.as_bytes(), self.buf)
            .map_err(|e| Error::StorageError(format!("unable to insert blob: {}", e)))?;

        // We know self.hasher is doing blake3 hashing, so this won't fail.
        Ok(B3Digest::from_vec(digest.as_bytes().to_vec()).unwrap())
    }
}