Skip to content

Commit 20eeb70

Browse files
committed
simplify a bit
1 parent bfe009a commit 20eeb70

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/db.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::graph::FileId;
88
use crate::graph::Graph;
99
use crate::graph::Hash;
1010
use crate::graph::Hashes;
11-
use anyhow::anyhow;
1211
use serde_derive::{Deserialize, Serialize};
1312
use std::collections::HashMap;
1413
use std::collections::HashSet;
@@ -56,10 +55,8 @@ pub struct Writer {
5655

5756
impl 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

Comments
 (0)