69 lines
2.4 KiB
Rust
69 lines
2.4 KiB
Rust
// This file is part of bsv.
|
|
//
|
|
// bsv is free software: you can redistribute it and/or modify it under the
|
|
// terms of the GNU Affero General Public License as published by the Free
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
// any later version.
|
|
//
|
|
// bsv is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
|
|
// more details.
|
|
//
|
|
// You should have received a copy of the Affero GNU General Public License
|
|
// along with bsv. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
use camino::Utf8Path;
|
|
|
|
use super::err;
|
|
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;
|
|
|
|
|
|
pub trait Cas {
|
|
fn object_id_from_string(&self, hex: &str) -> Result<ObjectId>;
|
|
|
|
fn has_object_id(&self, oid: &ObjectId) -> Result<bool>;
|
|
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!("failed to read object {}: {}", oid, err))?;
|
|
|
|
let result_oid = reader.finalize()?;
|
|
if &result_oid == oid {
|
|
Ok((metadata, data))
|
|
}
|
|
else {
|
|
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!("failed to write object: {}", err))?;
|
|
writer.finalize()
|
|
}
|
|
|
|
fn remove_object(&mut self, oid: &ObjectId) -> Result<()>;
|
|
}
|
|
|
|
pub trait RefStore {
|
|
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<()>;
|
|
} |