Better error management, Utf8Paths & other stuff.

This commit is contained in:
2022-08-08 11:40:45 +02:00
parent b1ceaaf636
commit 5d08b1ea57
33 changed files with 1483 additions and 1216 deletions

View File

@@ -14,8 +14,9 @@
// along with cdb. If not, see <https://www.gnu.org/licenses/>.
use std::path::Path;
use camino::Utf8Path;
use super::err;
use super::error::{Error, Result};
use super::object_id::ObjectId;
use super::object_type::ObjectType;
@@ -40,21 +41,21 @@ pub trait Cas {
let mut data = Vec::new();
reader.read_to_end(&mut data).or_else(|err|
Err(Error::error(format!("failed to read object {}: {}", oid, err))))?;
err!("failed to read object {}: {}", oid, err))?;
let result_oid = reader.finalize()?;
if &result_oid == oid {
Ok((metadata, data))
}
else {
Err(Error::error(format!("object id mismatch: requested {}, read {}", oid, result_oid)))
err!("object id mismatch: requested {}, read {}", oid, result_oid)
}
}
fn write_object(&mut self, otype: &ObjectType, data: &[u8]) -> Result<ObjectId> {
let mut writer = self.new_writer(otype, data.len() as u64)?;
writer.write_all(data).or_else(|err|
Err(Error::error(format!("failed to write object: {}", err))))?;
err!("failed to write object: {}", err))?;
writer.finalize()
}
@@ -62,7 +63,7 @@ pub trait Cas {
}
pub trait RefStore {
fn get_ref<P: AsRef<Path>>(&self, key: P) -> Result<ObjectId>;
fn set_ref<P: AsRef<Path>>(&mut self, key: P, value: &ObjectId) -> Result<()>;
fn remove_ref<P: AsRef<Path>>(&mut self, key: P) -> Result<()>;
fn get_ref<P: AsRef<Utf8Path>>(&self, key: P) -> Result<ObjectId>;
fn set_ref<P: AsRef<Utf8Path>>(&mut self, key: P, value: &ObjectId) -> Result<()>;
fn remove_ref<P: AsRef<Utf8Path>>(&mut self, key: P) -> Result<()>;
}