History

refactor zlib

Sebastian Thiel committed 5 years ago

Commit 496e6bb1 parent 3a4469c6 files · +19 −22Browse files

Unified · Split

6 files
  1. git-odb/src/loose/db/write.rs +5 −5
  2. git-odb/src/pack/data/encode.rs +1 −1
  3. git-odb/src/sink.rs +3 −3
  4. git-odb/src/zlib/stream/deflate.rs +5 −5
  5. git-odb/src/zlib/stream/deflate/tests.rs +4 −4
  6. git-odb/src/zlib/stream/mod.rs +1 −4
git-odb/src/loose/db/write.rs+5−5
@@ -1,5 +1,5 @@
1use super::Db;1use super::Db;
2use crate::{hash, loose, zlib::stream::DeflateWriter};2use crate::{hash, loose, zlib::stream::deflate};
3use std::{fs, io, io::Write, path::PathBuf};3use std::{fs, io, io::Write, path::PathBuf};
4use tempfile::NamedTempFile;4use tempfile::NamedTempFile;
5 5
@@ -73,7 +73,7 @@ impl crate::Write for Db { · 67 unmodified lines
73 }73 }
74}74}
75 75
76type HashAndTempFile = DeflateWriter<NamedTempFile>;76type CompressedTempfile = deflate::Writer<NamedTempFile>;
77 77
78impl Db {78impl Db {
79 fn write_header(79 fn write_header(
@@ -81,9 +81,9 @@ impl Db { · 1 unmodified line
81 kind: git_object::Kind,81 kind: git_object::Kind,
82 size: u64,82 size: u64,
83 hash: git_hash::Kind,83 hash: git_hash::Kind,
84 ) -> Result<hash::Write<HashAndTempFile>, Error> {84 ) -> Result<hash::Write<CompressedTempfile>, Error> {
85 let mut to = hash::Write::new(85 let mut to = hash::Write::new(
86 DeflateWriter::new(NamedTempFile::new_in(&self.path).map_err(|err| Error::Io {86 deflate::Writer::new(NamedTempFile::new_in(&self.path).map_err(|err| Error::Io {
87 source: err,87 source: err,
88 message: "create named temp file in",88 message: "create named temp file in",
89 path: self.path.to_owned(),89 path: self.path.to_owned(),
@@ -101,7 +101,7 @@ impl Db { · 11 unmodified lines
101 101
102 fn finalize_object(102 fn finalize_object(
103 &self,103 &self,
104 hash::Write { hash, inner: file }: hash::Write<HashAndTempFile>,104 hash::Write { hash, inner: file }: hash::Write<CompressedTempfile>,
105 ) -> Result<git_hash::ObjectId, Error> {105 ) -> Result<git_hash::ObjectId, Error> {
106 let id = git_hash::ObjectId::from(hash.digest());106 let id = git_hash::ObjectId::from(hash.digest());
107 let object_path = loose::db::sha1_path(&id, self.path.clone());107 let object_path = loose::db::sha1_path(&id, self.path.clone());
git-odb/src/pack/data/encode.rs+1−1
@@ -56,7 +56,7 @@ pub mod entry { · 55 unmodified lines
56 entry_kind: Kind::Base,56 entry_kind: Kind::Base,
57 decompressed_size: obj.data.len(),57 decompressed_size: obj.data.len(),
58 compressed_data: {58 compressed_data: {
59 let mut out = crate::zlib::stream::DeflateWriter::new(Vec::new());59 let mut out = crate::zlib::stream::deflate::Writer::new(Vec::new());
60 if let Err(err) = std::io::copy(&mut &obj.data[..], &mut out) {60 if let Err(err) = std::io::copy(&mut &obj.data[..], &mut out) {
61 match err.kind() {61 match err.kind() {
62 std::io::ErrorKind::Other => return Err(Error::ZlibDeflate(err.to_string())),62 std::io::ErrorKind::Other => return Err(Error::ZlibDeflate(err.to_string())),
git-odb/src/sink.rs+3−3
@@ -1,4 +1,4 @@
1use crate::{loose, zlib::stream::DeflateWriter};1use crate::{loose, zlib::stream::deflate};
2use std::{2use std::{
3 cell::RefCell,3 cell::RefCell,
4 convert::TryInto,4 convert::TryInto,
@@ -10,14 +10,14 @@ use std::{ · 5 unmodified lines
10/// It can optionally compress the content, similarly to what would happen when using a [`loose::Db`].10/// It can optionally compress the content, similarly to what would happen when using a [`loose::Db`].
11///11///
12pub struct Sink {12pub struct Sink {
13 compressor: Option<RefCell<DeflateWriter<io::Sink>>>,13 compressor: Option<RefCell<deflate::Writer<io::Sink>>>,
14}14}
15 15
16impl Sink {16impl Sink {
17 /// Enable or disable compression. Compression is disabled by default17 /// Enable or disable compression. Compression is disabled by default
18 pub fn compress(mut self, enable: bool) -> Self {18 pub fn compress(mut self, enable: bool) -> Self {
19 if enable {19 if enable {
20 self.compressor = Some(RefCell::new(DeflateWriter::new(io::sink())));20 self.compressor = Some(RefCell::new(deflate::Writer::new(io::sink())));
21 } else {21 } else {
22 self.compressor = None;22 self.compressor = None;
23 }23 }
git-odb/src/zlib/stream/deflate.rs+5−5
@@ -3,18 +3,18 @@ use std::io; · 2 unmodified lines
3 3
4const BUF_SIZE: usize = 4096 * 8;4const BUF_SIZE: usize = 4096 * 8;
5 5
6pub struct DeflateWriter<W> {6pub struct Writer<W> {
7 compressor: Compress,7 compressor: Compress,
8 inner: W,8 inner: W,
9 buf: [u8; BUF_SIZE],9 buf: [u8; BUF_SIZE],
10}10}
11 11
12impl<W> DeflateWriter<W>12impl<W> Writer<W>
13where13where
14 W: io::Write,14 W: io::Write,
15{15{
16 pub fn new(inner: W) -> DeflateWriter<W> {16 pub fn new(inner: W) -> Writer<W> {
17 DeflateWriter {17 Writer {
18 compressor: Compress::new(Compression::fast(), true),18 compressor: Compress::new(Compression::fast(), true),
19 inner,19 inner,
20 buf: [0; BUF_SIZE],20 buf: [0; BUF_SIZE],
@@ -67,7 +67,7 @@ where · 46 unmodified lines
67 }67 }
68}68}
69 69
70impl<W: io::Write> io::Write for DeflateWriter<W> {70impl<W: io::Write> io::Write for Writer<W> {
71 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {71 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
72 self.write_inner(buf, FlushCompress::None)72 self.write_inner(buf, FlushCompress::None)
73 }73 }
git-odb/src/zlib/stream/deflate/tests.rs+4−4
@@ -1,5 +1,5 @@
1mod deflate_stream {1mod deflate_stream {
2 use crate::zlib::stream::DeflateWriter;2 use crate::zlib::stream::deflate;
3 use bstr::ByteSlice;3 use bstr::ByteSlice;
4 use flate2::Decompress;4 use flate2::Decompress;
5 use std::{5 use std::{
@@ -52,7 +52,7 @@ mod deflate_stream { · 46 unmodified lines
52 52
53 #[test]53 #[test]
54 fn all_at_once() -> Result<(), Box<dyn std::error::Error>> {54 fn all_at_once() -> Result<(), Box<dyn std::error::Error>> {
55 let mut w = DeflateWriter::new(Vec::new());55 let mut w = deflate::Writer::new(Vec::new());
56 assert_eq!(w.write(b"hello")?, 5);56 assert_eq!(w.write(b"hello")?, 5);
57 w.flush()?;57 w.flush()?;
58 58
@@ -71,7 +71,7 @@ mod deflate_stream { · 12 unmodified lines
71 71
72 #[test]72 #[test]
73 fn big_file_small_writes() -> Result<(), Box<dyn std::error::Error>> {73 fn big_file_small_writes() -> Result<(), Box<dyn std::error::Error>> {
74 let mut w = DeflateWriter::new(Vec::new());74 let mut w = deflate::Writer::new(Vec::new());
75 let bytes = include_bytes!(75 let bytes = include_bytes!(
76 "../../../../tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"76 "../../../../tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"
77 );77 );
@@ -85,7 +85,7 @@ mod deflate_stream { · 7 unmodified lines
85 85
86 #[test]86 #[test]
87 fn big_file_a_few_big_writes() -> Result<(), Box<dyn std::error::Error>> {87 fn big_file_a_few_big_writes() -> Result<(), Box<dyn std::error::Error>> {
88 let mut w = DeflateWriter::new(Vec::new());88 let mut w = deflate::Writer::new(Vec::new());
89 let bytes = include_bytes!(89 let bytes = include_bytes!(
90 "../../../../tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"90 "../../../../tests/fixtures/objects/pack/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.pack"
91 );91 );
git-odb/src/zlib/stream/mod.rs+1−4
@@ -1,5 +1,2 @@
1pub mod inflate;
2
3pub mod deflate;1pub mod deflate;
4#[doc(inline)]2pub mod inflate;
5pub use deflate::DeflateWriter;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t