From b9362d8920c985e5356d55dee7202bf3ec65bdb3 Mon Sep 17 00:00:00 2001 From: daehyun Date: Wed, 11 Jul 2018 23:36:12 +0900 Subject: [PATCH] Compress snapshot files --- Cargo.lock | 11 +++++++++++ sync/Cargo.toml | 1 + sync/src/lib.rs | 1 + sync/src/snapshot/service.rs | 13 +++++++++---- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cf5210c504..ba485dff40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,6 +444,7 @@ dependencies = [ "patricia-trie 0.1.0", "rand 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "rlp 0.2.1", + "snap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", "triehash 0.1.0", ] @@ -1702,6 +1703,15 @@ name = "smallvec" version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "snap" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "snappy-sys" version = "0.1.0" @@ -2390,6 +2400,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum smallvec 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c8cbcd6df1e117c2210e13ab5109635ad68a929fcbb8964dc965b76cb5ee013" "checksum smallvec 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4f357e8cd37bf8822e1b964e96fd39e2cb5a0424f8aaa284ccaccc2162411c" "checksum smallvec 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "03dab98ab5ded3a8b43b2c80751194608d0b2aa0f1d46cf95d1c35e192844aa7" +"checksum snap 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "174451758f7045084ae92070f18e5d8e5c53a716f4172a9c6b17ce03e7b82573" "checksum snappy-sys 0.1.0 (git+https://github.com/paritytech/rust-snappy)" = "" "checksum socket2 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "06dc9f86ee48652b7c80f3d254e3b9accb67a928c562c64d10d7b016d3d98dab" "checksum spmc 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "cd1f11d1fb5fd41834e55ce0b85a186efbf2f2afd9fdb09e2c8d72f9bff1ad1a" diff --git a/sync/Cargo.toml b/sync/Cargo.toml index ef0abf2e43..efc4ff4efb 100644 --- a/sync/Cargo.toml +++ b/sync/Cargo.toml @@ -18,6 +18,7 @@ parking_lot = "0.5" patricia-trie = { path = "../util/patricia_trie" } rand = "0.5.3" rlp = { path = "../util/rlp" } +snap = "0.2" time = "0.1" triehash = { path = "../util/triehash" } diff --git a/sync/src/lib.rs b/sync/src/lib.rs index 035d49ad82..6309faeb22 100644 --- a/sync/src/lib.rs +++ b/sync/src/lib.rs @@ -30,6 +30,7 @@ extern crate patricia_trie as trie; extern crate rand; #[cfg_attr(test, macro_use)] extern crate rlp; +extern crate snap; extern crate time; extern crate triehash; diff --git a/sync/src/snapshot/service.rs b/sync/src/snapshot/service.rs index 9f0d5c0a14..f653d49e15 100644 --- a/sync/src/snapshot/service.rs +++ b/sync/src/snapshot/service.rs @@ -25,6 +25,7 @@ use ctypes::H256; use kvdb::KeyValueDB; use rlp::{decode as rlp_decode, RlpStream}; +use snap; use trie::{Node, OwnedNode}; use super::error::Error; @@ -90,7 +91,8 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), } { - let mut file = File::create(path.join("head"))?; + let file = File::create(path.join("head"))?; + let mut snappy = snap::Writer::new(file); let mut stream = RlpStream::new(); stream.begin_unbounded_list(); @@ -101,12 +103,14 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), } stream.complete_unbounded_list(); - file.write(&stream.drain())?; + snappy.write(&stream.drain())?; } for (grandchild, _) in &grandchildren { let nodes = enumerate_subtree(&db, grandchild)?; - let mut file = File::create(path.join(format!("{:x}", grandchild)))?; + let file = File::create(path.join(format!("{:x}", grandchild)))?; + let mut snappy = snap::Writer::new(file); + let mut stream = RlpStream::new(); stream.begin_unbounded_list(); for (key, value) in nodes { @@ -115,7 +119,8 @@ fn write_snapshot(db: Arc, path: PathBuf, root: &H256) -> Result<(), stream.append(&value); } stream.complete_unbounded_list(); - file.write(&stream.drain())?; + + snappy.write(&stream.drain())?; } Ok(())