Return file contents as Bytes in blobrepo.

Summary: This starts porting uses of Vec<u8> for file contents to the Bytes type.

Reviewed By: jsgf

Differential Revision: D7106766

fbshipit-source-id: 15d531836132317cede7a6f9d6b047a423deb5bb
This commit is contained in:
Dino Wernli 2018-02-28 17:38:30 -08:00 committed by Facebook Github Bot
parent d24a501e9e
commit 5c84d1d8de
5 changed files with 22 additions and 16 deletions

View File

@ -7,6 +7,8 @@
//! Plain files, symlinks
use std::sync::Arc;
use bytes::Bytes;
use futures::future::Future;
use futures_ext::{BoxFuture, FutureExt};
@ -34,7 +36,7 @@ pub struct BlobEntry {
pub fn fetch_file_content_and_renames_from_blobstore(
blobstore: &Arc<Blobstore>,
nodeid: NodeHash,
) -> BoxFuture<(Vec<u8>, Option<(MPath, NodeHash)>), Error> {
) -> BoxFuture<(Bytes, Option<(MPath, NodeHash)>), Error> {
get_node(blobstore, nodeid)
.and_then({
let blobstore = blobstore.clone();
@ -53,7 +55,7 @@ pub fn fetch_file_content_and_renames_from_blobstore(
file.copied_from().and_then(|from| {
file.content()
.ok_or(ErrorKind::ContentMissing(nodeid, node.blob).into())
.map(|content| (Vec::from(content), from))
.map(|content| (Bytes::from(content), from))
})
})
})

View File

@ -189,7 +189,7 @@ impl BlobRepo {
))
}
pub fn get_file_content(&self, key: &NodeHash) -> BoxFuture<Vec<u8>, Error> {
pub fn get_file_content(&self, key: &NodeHash) -> BoxFuture<Bytes, Error> {
fetch_file_content_and_renames_from_blobstore(&self.blobstore, *key)
.map(|contentrename| contentrename.0)
.boxify()

View File

@ -219,7 +219,7 @@ fn upload_blob_no_parents(repo: BlobRepo) {
// And the blob now exists
let bytes = run_future(repo.get_file_content(&expected_hash)).unwrap();
assert!(bytes == b"blob");
assert!(bytes == Bytes::from(&b"blob"[..]));
}
test_both_repotypes!(
@ -256,7 +256,7 @@ fn upload_blob_one_parent(repo: BlobRepo) {
};
// And the blob now exists
let bytes = run_future(repo.get_file_content(&expected_hash)).unwrap();
assert!(bytes == b"blob");
assert!(bytes == Bytes::from(&b"blob"[..]));
}
test_both_repotypes!(
@ -303,7 +303,7 @@ fn create_one_changeset(repo: BlobRepo) {
// And check the file blob is present
let bytes = run_future(repo.get_file_content(&filehash)).unwrap();
assert!(bytes == b"blob");
assert!(bytes == Bytes::from(&b"blob"[..]));
}
test_both_repotypes!(

View File

@ -125,7 +125,7 @@ impl DeltaCache {
.boxify(),
None => self.repo
.get_file_content(&base)
.map(move |vec| delta::apply(vec.as_slice(), &delta))
.map(move |bytes| delta::apply(bytes.to_vec().as_slice(), &delta))
.boxify(),
};
fut.map_err(move |err| {

View File

@ -16,6 +16,7 @@
/// ```
extern crate ascii;
extern crate blobrepo;
extern crate bytes;
extern crate clap;
#[macro_use]
extern crate failure_ext as failure;
@ -56,6 +57,7 @@ use std::sync::Arc;
use tokio_core::reactor::Core;
use blobrepo::BlobRepo;
use bytes::Bytes;
use clap::App;
use futures::{Future, IntoFuture, Stream};
use futures::sync::oneshot;
@ -231,7 +233,7 @@ where
&self,
reponame: String,
changesetid: &ChangesetId,
) -> Box<futures::Future<Item = Vec<u8>, Error = Error> + Send> {
) -> Box<futures::Future<Item = Bytes, Error = Error> + Send> {
let repo = match self.name_to_repo.get(&reponame) {
Some(repo) => repo,
None => {
@ -240,11 +242,13 @@ where
};
repo.get_changeset_by_changesetid(&changesetid)
.map(|cs| {
cs.manifestid()
.clone()
.into_nodehash()
.to_string()
.into_bytes()
Bytes::from(
cs.manifestid()
.clone()
.into_nodehash()
.to_string()
.into_bytes(),
)
})
.from_err()
.boxify()
@ -255,7 +259,7 @@ where
reponame: String,
hash: &NodeHash,
options: TreeMetadataOptions,
) -> Box<futures::Future<Item = Vec<u8>, Error = Error> + Send> {
) -> Box<futures::Future<Item = Bytes, Error = Error> + Send> {
let repo = match self.name_to_repo.get(&reponame) {
Some(repo) => repo,
None => {
@ -280,7 +284,7 @@ where
.collect()
.map(|entries| {
let x: serde_json::Value = entries.into();
x.to_string().into_bytes()
Bytes::from(x.to_string().into_bytes())
})
.boxify()
}
@ -289,7 +293,7 @@ where
&self,
reponame: String,
hash: &NodeHash,
) -> Box<futures::Future<Item = Vec<u8>, Error = Error> + Send> {
) -> Box<futures::Future<Item = Bytes, Error = Error> + Send> {
let repo = match self.name_to_repo.get(&reponame) {
Some(repo) => repo,
None => {