op_store: make Vec inside ViewId and OperationId non-public

This commit is contained in:
Martin von Zweigbergk 2021-11-17 14:13:41 -08:00
parent 8cf5dd286a
commit 046b3c0541
4 changed files with 45 additions and 13 deletions

View file

@ -84,7 +84,7 @@ impl OpHeadsStore {
let op_head_file_name = op_head_entry.unwrap().file_name();
let op_head_file_name = op_head_file_name.to_str().unwrap();
if let Ok(op_head) = hex::decode(op_head_file_name) {
op_heads.push(OperationId(op_head));
op_heads.push(OperationId::new(op_head));
}
}
op_heads

View file

@ -18,7 +18,7 @@ use std::fmt::{Debug, Error, Formatter};
use crate::backend::{CommitId, Timestamp};
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct ViewId(pub Vec<u8>);
pub struct ViewId(Vec<u8>);
impl Debug for ViewId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
@ -27,13 +27,29 @@ impl Debug for ViewId {
}
impl ViewId {
pub fn new(value: Vec<u8>) -> Self {
Self(value)
}
pub fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}
pub fn hex(&self) -> String {
hex::encode(&self.0)
}
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct OperationId(pub Vec<u8>);
pub struct OperationId(Vec<u8>);
impl Debug for OperationId {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
@ -42,6 +58,22 @@ impl Debug for OperationId {
}
impl OperationId {
pub fn new(value: Vec<u8>) -> Self {
Self(value)
}
pub fn from_hex(hex: &str) -> Self {
Self(hex::decode(hex).unwrap())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn to_bytes(&self) -> Vec<u8> {
self.0.clone()
}
pub fn hex(&self) -> String {
hex::encode(&self.0)
}

View file

@ -100,7 +100,7 @@ impl OpStore for SimpleOpStore {
temp_file.as_file().write_all(&proto_bytes)?;
let id = ViewId(Blake2b::digest(&proto_bytes).to_vec());
let id = ViewId::new(Blake2b::digest(&proto_bytes).to_vec());
persist_content_addressed_temp_file(temp_file, self.view_path(&id))?;
Ok(id)
@ -123,7 +123,7 @@ impl OpStore for SimpleOpStore {
temp_file.as_file().write_all(&proto_bytes)?;
let id = OperationId(Blake2b::digest(&proto_bytes).to_vec());
let id = OperationId::new(Blake2b::digest(&proto_bytes).to_vec());
persist_content_addressed_temp_file(temp_file, self.operation_path(&id))?;
Ok(id)
@ -178,18 +178,18 @@ fn operation_metadata_from_proto(
fn operation_to_proto(operation: &Operation) -> crate::protos::op_store::Operation {
let mut proto = crate::protos::op_store::Operation::new();
proto.set_view_id(operation.view_id.0.clone());
proto.set_view_id(operation.view_id.as_bytes().to_vec());
for parent in &operation.parents {
proto.parents.push(parent.0.clone());
proto.parents.push(parent.to_bytes());
}
proto.set_metadata(operation_metadata_to_proto(&operation.metadata));
proto
}
fn operation_from_proto(proto: &crate::protos::op_store::Operation) -> Operation {
let operation_id_from_proto = |parent: &Vec<u8>| OperationId(parent.clone());
let operation_id_from_proto = |parent: &Vec<u8>| OperationId::new(parent.clone());
let parents = proto.parents.iter().map(operation_id_from_proto).collect();
let view_id = ViewId(proto.view_id.to_vec());
let view_id = ViewId::new(proto.view_id.clone());
let metadata = operation_metadata_from_proto(proto.get_metadata());
Operation {
view_id,
@ -399,10 +399,10 @@ mod tests {
let temp_dir = TempDir::new().unwrap();
let store = SimpleOpStore::init(temp_dir.path().to_owned());
let operation = Operation {
view_id: ViewId(b"aaa111".to_vec()),
view_id: ViewId::from_hex("aaa111"),
parents: vec![
OperationId(b"bbb111".to_vec()),
OperationId(b"bbb222".to_vec()),
OperationId::from_hex("bbb111"),
OperationId::from_hex("bbb222"),
],
metadata: OperationMetadata {
start_time: Timestamp {

View file

@ -504,7 +504,7 @@ fn resolve_single_op_from_store(
op_str: &str,
) -> Result<Operation, CommandError> {
if let Ok(binary_op_id) = hex::decode(op_str) {
let op_id = OperationId(binary_op_id);
let op_id = OperationId::new(binary_op_id);
match op_store.read_operation(&op_id) {
Ok(operation) => {
return Ok(Operation::new(op_store.clone(), op_id, operation));