mononoke: add get_file_blob() method to Blobrepo

Summary:
Let's create a separate function `fetch_file_blob_from_blobstore` that will be
used to get content of the blob without creating file::BlobEntry.
file::BlobEntry requires Path and type parameters that won't be passed to eden
server.

Reviewed By: jsgf

Differential Revision: D5537647

fbshipit-source-id: 13fb2fbba7d3068b88aac2d842cc92b4da6ca80c
This commit is contained in:
Stanislau Hlebik 2017-08-09 08:38:16 -07:00 committed by Facebook Github Bot
parent 22b6b350a1
commit ad876328e4
2 changed files with 58 additions and 11 deletions

View File

@ -33,6 +33,52 @@ pub struct RawNodeBlob {
blob: hash::Sha1,
}
fn get_node<B>(blobstore: &B, nodeid: NodeHash) -> BoxFuture<RawNodeBlob, Error>
where B: Blobstore<Key = String>,
B::ValueOut: AsRef<[u8]>,
{
let key = format!("node:{}.bincode", nodeid);
blobstore
.get(&key)
.map_err(blobstore_err)
.and_then(move |got| got.ok_or(ErrorKind::NodeMissing(nodeid).into()))
.and_then(move |blob| {
bincode::deserialize(blob.as_ref()).into_future().from_err()
})
.boxed()
}
pub fn fetch_file_blob_from_blobstore<B>(
blobstore: B,
nodeid: NodeHash,
) -> BoxFuture<Vec<u8>, Error>
where
B: Blobstore<Key = String> + Clone,
B::ValueOut: AsRef<[u8]>,
{
get_node(&blobstore, nodeid)
.and_then({
let blobstore = blobstore.clone();
move |node| {
let key = format!("sha1:{}", node.blob);
blobstore
.get(&key)
.map_err(blobstore_err)
.and_then(move |blob| {
blob.ok_or(ErrorKind::ContentMissing(nodeid, node.blob).into())
})
}
})
.and_then({
|blob| {
Ok(Vec::from(blob.as_ref()))
}
})
.boxed()
}
impl<B> BlobEntry<B>
where
B: Blobstore<Key = String>,
@ -48,17 +94,7 @@ where
}
fn get_node(&self) -> BoxFuture<RawNodeBlob, Error> {
let nodeid = self.nodeid;
let key = format!("node:{}.bincode", self.nodeid);
self.blobstore
.get(&key)
.map_err(blobstore_err)
.and_then(move |got| got.ok_or(ErrorKind::NodeMissing(nodeid).into()))
.and_then(move |blob| {
bincode::deserialize(blob.as_ref()).into_future().from_err()
})
.boxed()
get_node(&self.blobstore, self.nodeid)
}
}

View File

@ -20,6 +20,7 @@ use mercurial_types::{Changeset, Manifest, NodeHash, Repo, repo};
use BlobChangeset;
use BlobManifest;
use file::fetch_file_blob_from_blobstore;
use errors::*;
pub struct BlobRepo<Head, Book, Blob> {
@ -44,6 +45,16 @@ impl<Head, Book, Blob> BlobRepo<Head, Book, Blob> {
}
}
impl<Head, Book, Blob> BlobRepo<Head, Book, Blob>
where
Blob: Blobstore<Key = String> + Clone + Sync,
Blob::ValueOut: AsRef<[u8]> + Send,
{
pub fn get_file_blob(&self, key: &NodeHash) -> BoxFuture<Vec<u8>, Error> {
fetch_file_blob_from_blobstore(self.inner.blobstore.clone(), *key)
}
}
impl<Head, Book, Blob> Repo for BlobRepo<Head, Book, Blob>
where
Head: Heads<Key = NodeHash> + Sync,