Unified · Split
| @@ -4,8 +4,6 @@ const HEADER_READ_UNCOMPRESSED_BYTES: usize = 512; · 3 unmodified lines | ||
| 4 | 4 | pub(crate) mod db; |
| 5 | 5 | |
| 6 | 6 | pub mod object; |
| 7 | −pub use object::*; | |
| 8 | − | |
| 9 | −pub mod stream; | |
| 7 | +pub use object::Object; | |
| 10 | 8 | |
| 11 | 9 | pub use db::{Db, Error}; |
| @@ -1,99 +0,0 @@ | ||
| 1 | −use crate::loose::{HEADER_READ_COMPRESSED_BYTES, HEADER_READ_UNCOMPRESSED_BYTES}; | |
| 2 | −use git_object as object; | |
| 3 | −use smallvec::SmallVec; | |
| 4 | −use std::path::PathBuf; | |
| 5 | − | |
| 6 | −#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | |
| 7 | −pub struct Object { | |
| 8 | − pub kind: object::Kind, | |
| 9 | − pub size: usize, | |
| 10 | − pub(crate) decompressed_data: SmallVec<[u8; HEADER_READ_UNCOMPRESSED_BYTES]>, | |
| 11 | − pub(crate) compressed_data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>, | |
| 12 | − pub(crate) header_size: usize, | |
| 13 | − pub(crate) path: Option<PathBuf>, | |
| 14 | − pub(crate) decompression_complete: bool, | |
| 15 | −} | |
| 16 | − | |
| 17 | −pub mod decode { | |
| 18 | − use crate::{loose, loose::stream::Reader, zlib}; | |
| 19 | − use git_object as object; | |
| 20 | − use miniz_oxide::inflate::decompress_to_vec_zlib; | |
| 21 | − use object::borrowed; | |
| 22 | − use quick_error::quick_error; | |
| 23 | − use smallvec::SmallVec; | |
| 24 | − use std::{io::Read, path::PathBuf}; | |
| 25 | − | |
| 26 | − quick_error! { | |
| 27 | − #[derive(Debug)] | |
| 28 | − pub enum Error { | |
| 29 | − Decompress(err: zlib::Error) { | |
| 30 | − display("decompression of object data failed") | |
| 31 | − from() | |
| 32 | − cause(err) | |
| 33 | − } | |
| 34 | − ParseTag(err: borrowed::Error) { | |
| 35 | − display("Could not parse tag object") | |
| 36 | − from() | |
| 37 | − cause(err) | |
| 38 | − } | |
| 39 | − Io(err: std::io::Error, action: &'static str, path: PathBuf) { | |
| 40 | − display("Could not {} data at '{}'", action, path.display()) | |
| 41 | − cause(err) | |
| 42 | − } | |
| 43 | − } | |
| 44 | − } | |
| 45 | − | |
| 46 | − impl loose::Object { | |
| 47 | − // Note: Blobs are loaded or mapped into memory and are made available that way. | |
| 48 | − // Consider the streaming API if large Blobs are expected. | |
| 49 | − pub fn decode(&mut self) -> Result<borrowed::Object, Error> { | |
| 50 | − self.decompress_all()?; | |
| 51 | − let bytes = &self.decompressed_data[self.header_size..]; | |
| 52 | − Ok(match self.kind { | |
| 53 | − object::Kind::Tag => borrowed::Object::Tag(borrowed::Tag::from_bytes(bytes)?), | |
| 54 | − object::Kind::Tree => borrowed::Object::Tree(borrowed::Tree::from_bytes(bytes)?), | |
| 55 | − object::Kind::Commit => borrowed::Object::Commit(borrowed::Commit::from_bytes(bytes)?), | |
| 56 | − object::Kind::Blob => borrowed::Object::Blob(borrowed::Blob { data: bytes }), | |
| 57 | − }) | |
| 58 | − } | |
| 59 | − | |
| 60 | − pub fn stream(&self) -> Result<Reader, Error> { | |
| 61 | − match &self.path { | |
| 62 | − Some(path) => Ok(Reader::from_read( | |
| 63 | − self.header_size, | |
| 64 | − std::fs::File::open(path).map_err(|e| Error::Io(e, "open", path.to_owned()))?, | |
| 65 | − )), | |
| 66 | − None => Ok(Reader::from_data(self.header_size, &self.decompressed_data.as_slice())), | |
| 67 | − } | |
| 68 | − } | |
| 69 | − | |
| 70 | − fn decompress_all(&mut self) -> Result<(), Error> { | |
| 71 | − if self.decompression_complete { | |
| 72 | − debug_assert!( | |
| 73 | − self.size + self.header_size == self.decompressed_data.len(), | |
| 74 | − "when decompression is done, we have stored everything in memory" | |
| 75 | − ); | |
| 76 | − return Ok(()); | |
| 77 | − } | |
| 78 | − let total_size = self.header_size + self.size; | |
| 79 | − if let Some(path) = self.path.take() { | |
| 80 | − // NOTE: For now we just re-read everything from the beginning without seeking, as our buffer | |
| 81 | − // is small so the seek might be more expensive than just reading everything. | |
| 82 | − let mut file = std::fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.clone()))?; | |
| 83 | − let file_size = file | |
| 84 | − .metadata() | |
| 85 | − .map_err(|e| Error::Io(e, "read metadata", path.clone()))? | |
| 86 | − .len() as usize; | |
| 87 | − let mut buf = Vec::with_capacity(file_size); | |
| 88 | − file.read_to_end(&mut buf).map_err(|e| Error::Io(e, "read", path))?; | |
| 89 | − self.compressed_data = SmallVec::from(buf); | |
| 90 | − } | |
| 91 | − self.decompressed_data = SmallVec::from(decompress_to_vec_zlib(&self.compressed_data[..]).unwrap()); | |
| 92 | − self.decompressed_data.shrink_to_fit(); | |
| 93 | − assert!(self.decompressed_data.len() == total_size); | |
| 94 | − self.decompression_complete = true; | |
| 95 | − self.compressed_data = Default::default(); | |
| 96 | − Ok(()) | |
| 97 | − } | |
| 98 | − } | |
| 99 | −} | |
| @@ -0,0 +1,85 @@ | ||
| 1 | +use super::stream; | |
| 2 | +use crate::{loose, zlib}; | |
| 3 | +use git_object as object; | |
| 4 | +use miniz_oxide::inflate::decompress_to_vec_zlib; | |
| 5 | +use object::borrowed; | |
| 6 | +use quick_error::quick_error; | |
| 7 | +use smallvec::SmallVec; | |
| 8 | +use std::{io::Read, path::PathBuf}; | |
| 9 | + | |
| 10 | +quick_error! { | |
| 11 | + #[derive(Debug)] | |
| 12 | + pub enum Error { | |
| 13 | + Decompress(err: zlib::Error) { | |
| 14 | + display("decompression of object data failed") | |
| 15 | + from() | |
| 16 | + cause(err) | |
| 17 | + } | |
| 18 | + ParseTag(err: borrowed::Error) { | |
| 19 | + display("Could not parse tag object") | |
| 20 | + from() | |
| 21 | + cause(err) | |
| 22 | + } | |
| 23 | + Io(err: std::io::Error, action: &'static str, path: PathBuf) { | |
| 24 | + display("Could not {} data at '{}'", action, path.display()) | |
| 25 | + cause(err) | |
| 26 | + } | |
| 27 | + } | |
| 28 | +} | |
| 29 | + | |
| 30 | +impl loose::Object { | |
| 31 | + // Note: Blobs are loaded or mapped into memory and are made available that way. | |
| 32 | + // Consider the streaming API if large Blobs are expected. | |
| 33 | + pub fn decode(&mut self) -> Result<borrowed::Object, Error> { | |
| 34 | + self.decompress_all()?; | |
| 35 | + let bytes = &self.decompressed_data[self.header_size..]; | |
| 36 | + Ok(match self.kind { | |
| 37 | + object::Kind::Tag => borrowed::Object::Tag(borrowed::Tag::from_bytes(bytes)?), | |
| 38 | + object::Kind::Tree => borrowed::Object::Tree(borrowed::Tree::from_bytes(bytes)?), | |
| 39 | + object::Kind::Commit => borrowed::Object::Commit(borrowed::Commit::from_bytes(bytes)?), | |
| 40 | + object::Kind::Blob => borrowed::Object::Blob(borrowed::Blob { data: bytes }), | |
| 41 | + }) | |
| 42 | + } | |
| 43 | + | |
| 44 | + pub fn stream(&self) -> Result<stream::Reader, Error> { | |
| 45 | + match &self.path { | |
| 46 | + Some(path) => Ok(stream::Reader::from_read( | |
| 47 | + self.header_size, | |
| 48 | + std::fs::File::open(path).map_err(|e| Error::Io(e, "open", path.to_owned()))?, | |
| 49 | + )), | |
| 50 | + None => Ok(stream::Reader::from_data( | |
| 51 | + self.header_size, | |
| 52 | + &self.decompressed_data.as_slice(), | |
| 53 | + )), | |
| 54 | + } | |
| 55 | + } | |
| 56 | + | |
| 57 | + fn decompress_all(&mut self) -> Result<(), Error> { | |
| 58 | + if self.decompression_complete { | |
| 59 | + debug_assert!( | |
| 60 | + self.size + self.header_size == self.decompressed_data.len(), | |
| 61 | + "when decompression is done, we have stored everything in memory" | |
| 62 | + ); | |
| 63 | + return Ok(()); | |
| 64 | + } | |
| 65 | + let total_size = self.header_size + self.size; | |
| 66 | + if let Some(path) = self.path.take() { | |
| 67 | + // NOTE: For now we just re-read everything from the beginning without seeking, as our buffer | |
| 68 | + // is small so the seek might be more expensive than just reading everything. | |
| 69 | + let mut file = std::fs::File::open(&path).map_err(|e| Error::Io(e, "open", path.clone()))?; | |
| 70 | + let file_size = file | |
| 71 | + .metadata() | |
| 72 | + .map_err(|e| Error::Io(e, "read metadata", path.clone()))? | |
| 73 | + .len() as usize; | |
| 74 | + let mut buf = Vec::with_capacity(file_size); | |
| 75 | + file.read_to_end(&mut buf).map_err(|e| Error::Io(e, "read", path))?; | |
| 76 | + self.compressed_data = SmallVec::from(buf); | |
| 77 | + } | |
| 78 | + self.decompressed_data = SmallVec::from(decompress_to_vec_zlib(&self.compressed_data[..]).unwrap()); | |
| 79 | + self.decompressed_data.shrink_to_fit(); | |
| 80 | + assert!(self.decompressed_data.len() == total_size); | |
| 81 | + self.decompression_complete = true; | |
| 82 | + self.compressed_data = Default::default(); | |
| 83 | + Ok(()) | |
| 84 | + } | |
| 85 | +} | |
| @@ -0,0 +1,18 @@ | ||
| 1 | +use crate::loose::{HEADER_READ_COMPRESSED_BYTES, HEADER_READ_UNCOMPRESSED_BYTES}; | |
| 2 | +use git_object as object; | |
| 3 | +use smallvec::SmallVec; | |
| 4 | +use std::path::PathBuf; | |
| 5 | + | |
| 6 | +#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)] | |
| 7 | +pub struct Object { | |
| 8 | + pub kind: object::Kind, | |
| 9 | + pub size: usize, | |
| 10 | + pub(crate) decompressed_data: SmallVec<[u8; HEADER_READ_UNCOMPRESSED_BYTES]>, | |
| 11 | + pub(crate) compressed_data: SmallVec<[u8; HEADER_READ_COMPRESSED_BYTES]>, | |
| 12 | + pub(crate) header_size: usize, | |
| 13 | + pub(crate) path: Option<PathBuf>, | |
| 14 | + pub(crate) decompression_complete: bool, | |
| 15 | +} | |
| 16 | + | |
| 17 | +pub mod decode; | |
| 18 | +pub mod stream; | |
File renamed without changes.