Mostly implement simple-cas.
This commit is contained in:
@@ -14,10 +14,13 @@
|
||||
// along with cdb. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
use super::error::Result;
|
||||
use std::path::Path;
|
||||
|
||||
use super::error::{Error, Result};
|
||||
use super::object_id::ObjectId;
|
||||
use super::object_type::ObjectType;
|
||||
use super::object_metadata::ObjectMetadata;
|
||||
use super::pipeline::{Reader, Writer};
|
||||
|
||||
|
||||
// pub struct ObjectIdIterator;
|
||||
@@ -25,16 +28,41 @@ use super::object_metadata::ObjectMetadata;
|
||||
|
||||
pub trait Cas {
|
||||
fn object_id_from_string(&self, hex: &str) -> Result<ObjectId>;
|
||||
fn object_id_from_partial(&self, hex: &str) -> Result<ObjectId>;
|
||||
|
||||
fn has_object_id(&self, oid: &ObjectId) -> Result<bool>;
|
||||
// fn iter_object_id(&self) -> Result<ObjectIdIterator>;
|
||||
fn iter_object_id<'s>(&'s self) -> Result<Box<dyn 's + Iterator<Item = Result<ObjectId>>>>;
|
||||
|
||||
fn open_object(&self, oid: &ObjectId) -> Result<(ObjectMetadata, Box<dyn Reader>)>;
|
||||
fn new_writer(&mut self, otype: &ObjectType, size: u64) -> Result<Box<dyn Writer>>;
|
||||
|
||||
fn read_object(&self, oid: &ObjectId) -> Result<(ObjectMetadata, Vec<u8>)> {
|
||||
let (metadata, mut reader) = self.open_object(oid)?;
|
||||
let mut data = Vec::new();
|
||||
|
||||
reader.read_to_end(&mut data).or_else(|err|
|
||||
Err(Error::error(format!("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)))
|
||||
}
|
||||
}
|
||||
|
||||
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))))?;
|
||||
writer.finalize()
|
||||
}
|
||||
|
||||
fn read_object(&self, oid: &ObjectId) -> Result<(ObjectMetadata, &dyn std::io::Read)>;
|
||||
fn write_object(&mut self, otype: ObjectType, data: &mut dyn std::io::Read) -> Result<ObjectId>;
|
||||
fn remove_object(&mut self, oid: &ObjectId) -> Result<()>;
|
||||
}
|
||||
|
||||
fn read_ref(&self, key: &str) -> Result<ObjectId>;
|
||||
fn write_ref(&mut self, key: &str, value: &ObjectId) -> Result<()>;
|
||||
fn remove_ref(&mut self, key: &str) -> Result<()>;
|
||||
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<()>;
|
||||
}
|
||||
Reference in New Issue
Block a user