Skip to content

Commit

Permalink
start stubbing in "pack" command
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdickinson committed Dec 2, 2019
1 parent aae3e3c commit 5ff3aca
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/bin/eos.rs
Expand Up @@ -21,6 +21,8 @@ enum Command {
},
Get {
hashes: Vec<String>
},
Pack {
}
}

Expand Down Expand Up @@ -118,7 +120,8 @@ async fn main () -> anyhow::Result<()> {
let loose = LooseStore::<Sha256>::new(destination);
match &eos.command {
Command::Add { files } => cmd_add(loose, files).await?,
Command::Get { hashes } => cmd_get(loose, hashes).await?
Command::Get { hashes } => cmd_get(loose, hashes).await?,
Command::Pack { } => loose.to_packed_store().await?
};
Ok(())
}
54 changes: 47 additions & 7 deletions src/stores/loose.rs
Expand Up @@ -14,6 +14,7 @@ use std::io::Write;
use flate2::write::{ ZlibEncoder };
use flate2::bufread::{ ZlibDecoder };
use flate2::Compression;
use std::pin::Pin;

#[derive(Clone)]
pub struct LooseStore<D> {
Expand All @@ -34,16 +35,52 @@ impl<D> LooseStore<D> {
0
}

pub async fn to_packed_store() -> anyhow::Result<()> {
// enumerate all objects
// fill out objects with recency/sortorderinfo
// sort objects by type, then "sortpath" (basename/dir for blobs, semver order descending for packages)
// walk each object type with a sliding window of comparisons. write deltas if they're >50% compression.
//
// enumerate all objects
// fill out objects with recency/sortorderinfo
// - sort objects by type, then "sortpath"
// - basename/dir for blobs, semver order descending for packages
// - walk each object type with a sliding window of comparisons.
// - write deltas if they're >50% compression.
pub async fn to_packed_store(&self) -> anyhow::Result<()> {
let entries: Vec<String> = std::fs::read_dir(&self.location)?
.filter_map(|xs| {
let dent = xs.ok()?;
let filename = dent.file_name();
let name = filename.to_string_lossy();
if name.len() != 2 {
return None
}
name.parse::<u8>().ok()?;
Some(dent.path())
})
.filter_map(|fullpath| {
Some(std::fs::read_dir(&fullpath).ok()?.filter_map(|xs| {
let dent = xs.ok()?;
let name = dent.file_name();
Some(format!("{}{}", fullpath.file_name()?.to_string_lossy(), name.to_string_lossy()))
}).collect::<Vec<_>>())
})
.flatten()
.collect();

println!("len={}", entries.len());
Ok(())
}
}

#[derive(Clone)]
pub struct LooseObjectStream<D> {
location: PathBuf,
phantom: PhantomData<D>
}

impl<D> Stream for LooseObjectStream<D> {
type Item = Object<Vec<u8>>;
fn poll_next(self: Pin<&mut Self>, cx: &mut futures::task::Context) -> futures::task::Poll<Option<Self::Item>> {
unimplemented!();
}
}

#[async_trait]
impl<D: 'static + Digest + Send + Sync> WritableStore<D> for LooseStore<D> {
async fn add<T: AsRef<[u8]> + Send>(&self, object: Object<T>) -> anyhow::Result<bool> {
Expand Down Expand Up @@ -108,6 +145,8 @@ impl<D: 'static + Digest + Send + Sync> WritableStore<D> for LooseStore<D> {

#[async_trait]
impl<D: 'static + Digest + Send + Sync> ReadableStore for LooseStore<D> {
type ObjectStream = LooseObjectStream<D>;

async fn get<T: AsRef<[u8]> + Send>(&self, item: T) -> anyhow::Result<Option<Object<Vec<u8>>>> {
let bytes = item.as_ref();
let bytes_encoded = hex::encode(bytes);
Expand Down Expand Up @@ -162,7 +201,8 @@ impl<D: 'static + Digest + Send + Sync> ReadableStore for LooseStore<D> {
}
}

async fn list<R: Stream<Item = Vec<u8>>>(&self) -> R {
async fn list(&self) -> Self::ObjectStream {

unimplemented!()
}

Expand Down
4 changes: 3 additions & 1 deletion src/stores/mod.rs
Expand Up @@ -24,7 +24,9 @@ pub trait WritableStore<D: Digest + Send + Sync> {

#[async_trait]
pub trait ReadableStore {
type ObjectStream;

async fn get<T: AsRef<[u8]> + Send>(&self, item: T) -> anyhow::Result<Option<Object<Vec<u8>>>>;
async fn list<R: Stream<Item = Vec<u8>>>(&self) -> R;
async fn list(&self) -> Self::ObjectStream;
async fn get_stream<'a, T: AsRef<[u8]> + Send, R: Stream<Item = &'a [u8]>>(&self, item: T) -> Option<R>;
}

0 comments on commit 5ff3aca

Please sign in to comment.