use crate::{
hash_type::HashType,
infrastructure::{Database, DatabaseError},
ir::BuildId,
path_pool::PathPool,
};
use alloc::sync::Arc;
use core::str;
use fjall::Keyspace;
const TIMESTAMP_HASH_TAG: u8 = 0;
const CONTENT_HASH_TAG: u8 = 1;
const HEADER_DEPENDENCY_TAG: u8 = 2;
const OUTPUT_TAG: u8 = 3;
const SOURCE_TAG: u8 = 4;
const BINCODE_CONFIG: bincode::config::Configuration = bincode::config::standard();
pub struct FjallDatabase {
keyspace: Keyspace,
path_pool: Arc<PathPool>,
}
impl FjallDatabase {
pub const fn new(keyspace: Keyspace, path_pool: Arc<PathPool>) -> Self {
Self {
keyspace,
path_pool,
}
}
fn insert(&self, key: &[u8], value: &[u8]) -> Result<(), DatabaseError> {
self.keyspace.insert(key, value)?;
Ok(())
}
}
const fn hash_tag(r#type: HashType) -> u8 {
match r#type {
HashType::Content => CONTENT_HASH_TAG,
HashType::Timestamp => TIMESTAMP_HASH_TAG,
}
}
fn key(tag: u8, payload: &[u8]) -> Vec<u8> {
[[tag].as_slice(), payload].concat()
}
impl Database for FjallDatabase {
fn get_hash(&self, r#type: HashType, id: BuildId) -> Result<Option<u64>, DatabaseError> {
Ok(self
.keyspace
.get(key(hash_tag(r#type), &id.to_bytes()))?
.map(|value| bincode::decode_from_slice(&value, BINCODE_CONFIG).map(|(value, _)| value))
.transpose()?)
}
fn set_hash(&self, r#type: HashType, id: BuildId, hash: u64) -> Result<(), DatabaseError> {
self.insert(
&key(hash_tag(r#type), &id.to_bytes()),
&bincode::encode_to_vec(hash, BINCODE_CONFIG)?,
)
}
fn get_header_inputs(&self, id: BuildId) -> Result<Vec<Arc<str>>, DatabaseError> {
Ok(self
.keyspace
.get(key(HEADER_DEPENDENCY_TAG, &id.to_bytes()))?
.map(|value| {
bincode::borrow_decode_from_slice::<Vec<&str>, _>(&value, BINCODE_CONFIG).map(
|(dependencies, _)| {
dependencies
.into_iter()
.map(|path| self.path_pool.intern(path))
.collect()
},
)
})
.transpose()?
.unwrap_or_default())
}
fn set_header_inputs(
&self,
id: BuildId,
dependencies: &[Arc<str>],
) -> Result<(), DatabaseError> {
self.insert(
&key(HEADER_DEPENDENCY_TAG, &id.to_bytes()),
&bincode::encode_to_vec(dependencies, BINCODE_CONFIG)?,
)
}
fn get_outputs(&self) -> Result<Vec<String>, DatabaseError> {
self.keyspace
.prefix([OUTPUT_TAG])
.map(|guard| Ok(str::from_utf8(&guard.key()?[1..])?.into()))
.collect::<Result<_, _>>()
}
fn set_output(&self, path: &str) -> Result<(), DatabaseError> {
self.insert(&key(OUTPUT_TAG, path.as_bytes()), &[])
}
fn get_source(&self, output: &str) -> Result<Option<String>, DatabaseError> {
self.keyspace
.get(key(SOURCE_TAG, output.as_bytes()))?
.map(|source| Ok::<_, DatabaseError>(str::from_utf8(&source)?.into()))
.transpose()
}
fn set_source(&self, output: &str, source: &str) -> Result<(), DatabaseError> {
self.insert(&key(SOURCE_TAG, output.as_bytes()), source.as_bytes())
}
}
#[cfg(test)]
mod tests {
use super::*;
use fjall::KeyspaceCreateOptions;
use std::path::Path;
use tempfile::tempdir;
fn open(path: &Path) -> (FjallDatabase, fjall::Database) {
let database = fjall::Database::builder(path).open().unwrap();
(
FjallDatabase::new(
database
.keyspace("build", KeyspaceCreateOptions::default)
.unwrap(),
Default::default(),
),
database,
)
}
#[test]
fn new() {
open(tempdir().unwrap().path());
}
#[test]
fn timestamp_hash() {
let (database, _fjall) = open(tempdir().unwrap().path());
database
.set_hash(HashType::Timestamp, BuildId::new(0), 42)
.unwrap();
assert_eq!(
database
.get_hash(HashType::Timestamp, BuildId::new(0))
.unwrap(),
Some(42)
);
assert_eq!(
database
.get_hash(HashType::Content, BuildId::new(0))
.unwrap(),
None,
);
}
#[test]
fn content_hash() {
let (database, _fjall) = open(tempdir().unwrap().path());
database
.set_hash(HashType::Content, BuildId::new(0), 42)
.unwrap();
assert_eq!(
database
.get_hash(HashType::Content, BuildId::new(0))
.unwrap(),
Some(42)
);
assert_eq!(
database
.get_hash(HashType::Timestamp, BuildId::new(0))
.unwrap(),
None,
);
}
#[test]
fn set_output() {
let (database, _fjall) = open(tempdir().unwrap().path());
database.set_output("foo").unwrap();
}
#[test]
fn get_output() {
let (database, _fjall) = open(tempdir().unwrap().path());
database.set_output("foo").unwrap();
assert_eq!(database.get_outputs().unwrap(), vec!["foo"]);
}
#[test]
fn get_output_with_source() {
let (database, _fjall) = open(tempdir().unwrap().path());
database.set_output("foo").unwrap();
database.set_source("foo", "bar").unwrap();
assert_eq!(database.get_outputs().unwrap(), vec!["foo"]);
}
#[test]
fn header_dependencies() {
let (database, _fjall) = open(tempdir().unwrap().path());
database
.set_header_inputs(BuildId::new(0), &["foo".into(), "bar".into()])
.unwrap();
assert_eq!(
database.get_header_inputs(BuildId::new(0)).unwrap(),
vec!["foo".into(), "bar".into()]
);
}
#[test]
fn intern_header_dependencies() {
let directory = tempdir().unwrap();
let path_pool = Arc::new(PathPool::new());
let database = FjallDatabase::new(
fjall::Database::builder(directory.path())
.open()
.unwrap()
.keyspace("build", KeyspaceCreateOptions::default)
.unwrap(),
path_pool.clone(),
);
database
.set_header_inputs(BuildId::new(0), &["foo".into()])
.unwrap();
assert!(Arc::ptr_eq(
&database.get_header_inputs(BuildId::new(0)).unwrap()[0],
&path_pool.intern("foo")
));
}
#[test]
fn set_source() {
let (database, _fjall) = open(tempdir().unwrap().path());
database.set_source("foo", "bar").unwrap();
}
#[test]
fn get_source() {
let (database, _fjall) = open(tempdir().unwrap().path());
database.set_source("foo", "bar").unwrap();
assert_eq!(database.get_source("foo").unwrap(), Some("bar".into()));
}
#[test]
fn reopen() {
let directory = tempdir().unwrap();
let (database, fjall) = open(directory.path());
database
.set_hash(HashType::Timestamp, BuildId::new(0), 42)
.unwrap();
database
.set_header_inputs(BuildId::new(0), &["foo".into()])
.unwrap();
database.set_output("foo").unwrap();
database.set_source("foo", "bar").unwrap();
drop(database);
drop(fjall);
let (database, _fjall) = open(directory.path());
assert_eq!(
database
.get_hash(HashType::Timestamp, BuildId::new(0))
.unwrap(),
Some(42)
);
assert_eq!(
database.get_header_inputs(BuildId::new(0)).unwrap(),
vec!["foo".into()]
);
assert_eq!(database.get_outputs().unwrap(), vec!["foo"]);
assert_eq!(database.get_source("foo").unwrap(), Some("bar".into()));
}
}