types: make DataEntry fields private

Summary: Change the API so that the act of getting the data from a data entry (optionally) forces hash validation.

Reviewed By: xavierd

Differential Revision: D15405742

fbshipit-source-id: c127286322c707eb066f89021ac45315e89aa718
This commit is contained in:
Arun Kulshreshtha 2019-05-20 11:49:20 -07:00 committed by Facebook Github Bot
parent 64cfee5321
commit cdd92e9951
3 changed files with 26 additions and 13 deletions

View File

@ -163,10 +163,9 @@ impl EdenApi for EdenApiCurlClient {
let mut files = Vec::new();
for entry in responses.into_iter().flatten() {
if self.validate_files {
log::trace!("Validating file: {}", &entry.key);
entry.validate()?;
log::trace!("Validating file: {}", entry.key());
}
files.push((entry.key, entry.data));
files.push((entry.key().clone(), entry.data(self.validate_files)?));
}
write_to_deltastore(store, files)

View File

@ -25,15 +25,33 @@ use crate::{key::Key, node::Node, parents::Parents};
Deserialize
)]
pub struct DataEntry {
pub key: Key,
pub data: Bytes,
pub parents: Parents,
key: Key,
data: Bytes,
parents: Parents,
}
impl DataEntry {
/// Compute the filenode hash of this `DataEntry` using the parents and
pub fn new(key: Key, data: Bytes, parents: Parents) -> Self {
Self { key, data, parents }
}
pub fn key(&self) -> &Key {
&self.key
}
/// Get this entry's data content. If validate is set to true, this method
/// will recompute the entry's node hash and verify that it matches the
/// expected node hash in the entry's key, returning an error otherwise.
pub fn data(&self, validate: bool) -> Fallible<Bytes> {
if validate {
self.validate()?;
}
Ok(self.data.clone())
}
/// Compute the filenode hash of this `DataEntry` using its parents and
/// content, and compare it with the known node hash from the entry's `Key`.
pub fn validate(&self) -> Fallible<()> {
fn validate(&self) -> Fallible<()> {
// Mercurial hashes the parent nodes in sorted order
// when computing the node hash.
let (p1, p2) = match self.parents.clone().into_nodes() {

View File

@ -62,9 +62,5 @@ pub fn null_key(path: &str) -> Key {
}
pub fn data_entry(key: Key, data: impl AsRef<[u8]>) -> DataEntry {
DataEntry {
key,
data: data.as_ref().into(),
parents: Parents::None,
}
DataEntry::new(key, data.as_ref().into(), Parents::None)
}