@@ -8,7 +8,6 @@ use crate::graph::FileId;
88use crate :: graph:: Graph ;
99use crate :: graph:: Hash ;
1010use crate :: graph:: Hashes ;
11- use anyhow:: anyhow;
1211use serde_derive:: { Deserialize , Serialize } ;
1312use std:: collections:: HashMap ;
1413use std:: collections:: HashSet ;
@@ -56,10 +55,8 @@ pub struct Writer {
5655
5756impl Writer {
5857 fn new ( ids : IdMap , w : fs:: File ) -> Self {
59- Writer {
60- ids,
61- w : BufWriter :: with_capacity ( IO_BUFFER_SIZE , w) ,
62- }
58+ let w = BufWriter :: with_capacity ( IO_BUFFER_SIZE , w) ;
59+ Writer { ids, w }
6360 }
6461
6562 fn ensure_id ( & mut self , graph : & Graph , file_id : FileId ) -> anyhow:: Result < Id > {
@@ -97,10 +94,15 @@ impl Writer {
9794 }
9895}
9996
100- /// Reads an on-disk database, loading its state into the provided Graph/Hashes.
101- fn read ( mut f : fs:: File , graph : & mut Graph , hashes : & mut Hashes ) -> anyhow:: Result < Writer > {
102- let mut rd = BufReader :: with_capacity ( IO_BUFFER_SIZE , & mut f) ;
103- let mut de = serde_cbor:: Deserializer :: from_reader ( & mut rd) . into_iter ( ) ;
97+ /// Opens or creates an on-disk database, loading its state into the provided Graph.
98+ pub fn open ( path : & str , graph : & mut Graph , hashes : & mut Hashes ) -> anyhow:: Result < Writer > {
99+ let mut f = fs:: OpenOptions :: new ( )
100+ . create ( true )
101+ . read ( true )
102+ . append ( true )
103+ . open ( path) ?;
104+ let mut buf = BufReader :: with_capacity ( IO_BUFFER_SIZE , & mut f) ;
105+ let mut de = serde_cbor:: Deserializer :: from_reader ( & mut buf) . into_iter ( ) ;
104106
105107 let mut ids = IdMap :: new ( ) ;
106108
@@ -152,15 +154,3 @@ enum DbEntry {
152154 hash : Hash ,
153155 } ,
154156}
155-
156- /// Opens or creates an on-disk database, loading its state into the provided Graph.
157- pub fn open ( path : & str , graph : & mut Graph , hashes : & mut Hashes ) -> anyhow:: Result < Writer > {
158- match fs:: OpenOptions :: new ( ) . read ( true ) . append ( true ) . open ( path) {
159- Ok ( f) => read ( f, graph, hashes) ,
160- Err ( err) if err. kind ( ) == std:: io:: ErrorKind :: NotFound => {
161- let f = fs:: File :: create ( path) ?;
162- Ok ( Writer :: new ( IdMap :: new ( ) , f) )
163- }
164- Err ( err) => Err ( anyhow ! ( err) ) ,
165- }
166- }
0 commit comments