optimize blob implementation

This commit is contained in:
Alexander Weiss 2022-02-04 08:52:34 +01:00
parent 6302705560
commit 64c8ef475a
5 changed files with 44 additions and 34 deletions

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
thiserror = "1.0"
derive-new = "0.5"
aes256ctr_poly1305aes = { path = "../aes256ctr_poly1305aes" }
base64 = "0.13"
scrypt = { version = "0.8", default-features = false }
@ -21,3 +22,4 @@ boomphf = "0.5"
walkdir = "2"
# commands
clap = { version = "3", features = ["derive"] }

View File

@ -1,5 +1,8 @@
use anyhow::Result;
use derive_new::new;
use serde::{Deserialize, Serialize};
use crate::backend::{FileType, ReadBackend};
use crate::id::Id;
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
@ -10,34 +13,52 @@ pub enum BlobType {
Tree,
}
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, new)]
pub struct Blob {
pub tpe: BlobType,
pub id: Id,
tpe: BlobType,
id: Id,
}
#[derive(Debug)]
#[derive(Debug, new)]
pub struct BlobInformation {
pub blob: Blob,
pub offset: u32,
pub length: u32,
blob: Blob,
offset: u32,
length: u32,
}
#[derive(Debug)]
#[derive(Debug, new)]
pub struct IndexEntry {
pub pack: Id,
pub bi: BlobInformation,
pack: Id,
bi: BlobInformation,
}
impl IndexEntry {
/// Get a blob described by IndexEntry from the backend
pub fn read_data<B: ReadBackend>(&self, be: &B) -> Result<Vec<u8>> {
Ok(be.read_partial(FileType::Pack, self.pack, self.offset(), self.length())?)
}
#[inline]
pub fn id(&self) -> &Id {
&self.bi.blob.id
}
#[inline]
pub fn tpe(&self) -> &BlobType {
&self.bi.blob.tpe
}
#[inline]
pub fn offset(&self) -> u32 {
self.bi.offset
}
#[inline]
pub fn length(&self) -> u32 {
self.bi.length
}
#[inline]
pub fn blob(&self) -> &Blob {
&self.bi.blob
}

View File

@ -20,10 +20,10 @@ pub(super) fn execute(be: &impl ReadBackend, dbe: &impl ReadBackend, opts: Opts)
let tpe = match opts.tpe.as_str() {
// special treatment for catingg blobs: read the index and use it to locate the blob
"blob" => {
let blob = AllIndexFiles::new(be)
.get_id(id)
.ok_or(anyhow!("blob not found in index"))?;
let dec = be.read_partial(FileType::Pack, blob.pack, blob.bi.offset, blob.bi.length)?;
let dec = AllIndexFiles::new(be)
.get_id(&id)
.ok_or(anyhow!("blob not found in index"))?
.read_data(be)?;
print!("{}", String::from_utf8_lossy(&dec));
return Ok(());
}

View File

@ -6,11 +6,11 @@ use crate::id::Id;
pub trait ReadIndex {
fn iter(&self) -> Box<dyn Iterator<Item = IndexEntry> + '_>;
fn get_id(&self, id: Id) -> Option<IndexEntry> {
self.iter().find(|e| e.bi.blob.id == id)
fn get_id(&self, id: &Id) -> Option<IndexEntry> {
self.iter().find(|e| e.id() == id)
}
fn get_blob(&self, blob: Blob) -> Option<IndexEntry> {
self.iter().find(|e| e.bi.blob == blob)
fn get_blob(&self, blob: &Blob) -> Option<IndexEntry> {
self.iter().find(|e| e.blob() == blob)
}
}

View File

@ -28,10 +28,7 @@ impl IntoIterator for IndexFile {
fn into_iter(self) -> Self::IntoIter {
Box::new(self.packs.into_iter().flat_map(|p| {
p.blobs.into_iter().map(move |b| IndexEntry {
pack: p.id,
bi: b.to_bi(),
})
p.blobs.into_iter().map(move |b| IndexEntry::new(p.id,b.to_bi()))
}))
}
}
@ -39,10 +36,7 @@ impl IntoIterator for IndexFile {
impl ReadIndex for IndexFile {
fn iter(&self) -> Box<dyn Iterator<Item = IndexEntry> + '_> {
Box::new(self.packs.iter().flat_map(|p| {
p.blobs.iter().map(|b| IndexEntry {
pack: p.id,
bi: b.to_bi(),
})
p.blobs.iter().map(|b| IndexEntry::new(p.id,b.to_bi()))
}))
}
}
@ -64,13 +58,6 @@ struct BlobIndex {
impl BlobIndex {
pub fn to_bi(&self) -> BlobInformation {
BlobInformation {
blob: Blob {
id: self.id,
tpe: self.tpe,
},
offset: self.offset,
length: self.length,
}
BlobInformation::new(Blob::new(self.tpe, self.id), self.offset, self.length)
}
}