summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2021-01-01 16:21:36 +0200
committerLars Wirzenius <liw@liw.fi>2021-01-01 17:45:04 +0200
commitb5c4df5d31a4624d24171ba70db567327f3aae67 (patch)
treea4420bc238c0c9d5b85d5cdbfdb16c7426066082
parenta7336cfa6cb3f5de69c994a5e61868d49756b1ad (diff)
downloadobnam2-b5c4df5d31a4624d24171ba70db567327f3aae67.tar.gz
refactor: move SQL use into sub-module
This keeps all the SQL related functions closer together, making it easier to make changes to them.
-rw-r--r--src/generation.rs150
1 files changed, 92 insertions, 58 deletions
diff --git a/src/generation.rs b/src/generation.rs
index 7a4b71b..5dc5f97 100644
--- a/src/generation.rs
+++ b/src/generation.rs
@@ -1,7 +1,6 @@
use crate::chunkid::ChunkId;
-use crate::error::ObnamError;
use crate::fsentry::FilesystemEntry;
-use rusqlite::{params, Connection, OpenFlags, Row, Transaction};
+use rusqlite::Connection;
use std::path::Path;
/// A nascent backup generation.
@@ -19,17 +18,7 @@ impl NascentGeneration {
where
P: AsRef<Path>,
{
- let flags = OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE;
- let conn = Connection::open_with_flags(filename, flags)?;
- conn.execute(
- "CREATE TABLE files (fileno INTEGER PRIMARY KEY, json TEXT)",
- params![],
- )?;
- conn.execute(
- "CREATE TABLE chunks (fileno INTEGER, chunkid TEXT)",
- params![],
- )?;
- conn.pragma_update(None, "journal_mode", &"WAL")?;
+ let conn = sql::create_db(filename.as_ref())?;
Ok(Self { conn, fileno: 0 })
}
@@ -40,7 +29,7 @@ impl NascentGeneration {
pub fn insert(&mut self, e: FilesystemEntry, ids: &[ChunkId]) -> anyhow::Result<()> {
let t = self.conn.transaction()?;
self.fileno += 1;
- insert_one(&t, e, self.fileno, ids)?;
+ sql::insert_one(&t, e, self.fileno, ids)?;
t.commit()?;
Ok(())
}
@@ -53,41 +42,13 @@ impl NascentGeneration {
for r in entries {
let (e, ids) = r?;
self.fileno += 1;
- insert_one(&t, e, self.fileno, &ids[..])?;
+ sql::insert_one(&t, e, self.fileno, &ids[..])?;
}
t.commit()?;
Ok(())
}
}
-fn row_to_entry(row: &Row) -> rusqlite::Result<(u64, String)> {
- let fileno: i64 = row.get(row.column_index("fileno")?)?;
- let fileno = fileno as u64;
- let json: String = row.get(row.column_index("json")?)?;
- Ok((fileno, json))
-}
-
-fn insert_one(
- t: &Transaction,
- e: FilesystemEntry,
- fileno: u64,
- ids: &[ChunkId],
-) -> anyhow::Result<()> {
- let fileno = fileno as i64;
- let json = serde_json::to_string(&e)?;
- t.execute(
- "INSERT INTO files (fileno, json) VALUES (?1, ?2)",
- params![fileno, &json],
- )?;
- for id in ids {
- t.execute(
- "INSERT INTO chunks (fileno, chunkid) VALUES (?1, ?2)",
- params![fileno, id],
- )?;
- }
- Ok(())
-}
-
#[cfg(test)]
mod test {
use super::NascentGeneration;
@@ -145,23 +106,98 @@ impl LocalGeneration {
where
P: AsRef<Path>,
{
+ let conn = sql::open_db(filename.as_ref())?;
+ Ok(Self { conn })
+ }
+
+ pub fn file_count(&self) -> anyhow::Result<u32> {
+ Ok(sql::file_count(&self.conn)?)
+ }
+
+ pub fn files(&self) -> anyhow::Result<Vec<(u64, FilesystemEntry)>> {
+ Ok(sql::files(&self.conn)?)
+ }
+
+ pub fn chunkids(&self, fileno: u64) -> anyhow::Result<Vec<ChunkId>> {
+ Ok(sql::chunkids(&self.conn, fileno)?)
+ }
+
+ pub fn get_file(&self, filename: &Path) -> anyhow::Result<Option<FilesystemEntry>> {
+ Ok(sql::get_file(&self.conn, filename)?)
+ }
+
+ pub fn get_fileno(&self, filename: &Path) -> anyhow::Result<Option<u64>> {
+ Ok(sql::get_fileno(&self.conn, filename)?)
+ }
+}
+
+mod sql {
+ use crate::chunkid::ChunkId;
+ use crate::error::ObnamError;
+ use crate::fsentry::FilesystemEntry;
+ use rusqlite::{params, Connection, OpenFlags, Row, Transaction};
+ use std::path::Path;
+
+ pub fn create_db(filename: &Path) -> anyhow::Result<Connection> {
+ let flags = OpenFlags::SQLITE_OPEN_CREATE | OpenFlags::SQLITE_OPEN_READ_WRITE;
+ let conn = Connection::open_with_flags(filename, flags)?;
+ conn.execute(
+ "CREATE TABLE files (fileno INTEGER PRIMARY KEY, json TEXT)",
+ params![],
+ )?;
+ conn.execute(
+ "CREATE TABLE chunks (fileno INTEGER, chunkid TEXT)",
+ params![],
+ )?;
+ conn.pragma_update(None, "journal_mode", &"WAL")?;
+ Ok(conn)
+ }
+
+ pub fn open_db(filename: &Path) -> anyhow::Result<Connection> {
let flags = OpenFlags::SQLITE_OPEN_READ_WRITE;
let conn = Connection::open_with_flags(filename, flags)?;
conn.pragma_update(None, "journal_mode", &"WAL")?;
+ Ok(conn)
+ }
- Ok(Self { conn })
+ pub fn insert_one(
+ t: &Transaction,
+ e: FilesystemEntry,
+ fileno: u64,
+ ids: &[ChunkId],
+ ) -> anyhow::Result<()> {
+ let fileno = fileno as i64;
+ let json = serde_json::to_string(&e)?;
+ t.execute(
+ "INSERT INTO files (fileno, json) VALUES (?1, ?2)",
+ params![fileno, &json],
+ )?;
+ for id in ids {
+ t.execute(
+ "INSERT INTO chunks (fileno, chunkid) VALUES (?1, ?2)",
+ params![fileno, id],
+ )?;
+ }
+ Ok(())
}
- pub fn file_count(&self) -> anyhow::Result<u32> {
- let mut stmt = self.conn.prepare("SELECT count(*) FROM files")?;
+ pub fn row_to_entry(row: &Row) -> rusqlite::Result<(u64, String)> {
+ let fileno: i64 = row.get(row.column_index("fileno")?)?;
+ let fileno = fileno as u64;
+ let json: String = row.get(row.column_index("json")?)?;
+ Ok((fileno, json))
+ }
+
+ pub fn file_count(conn: &Connection) -> anyhow::Result<u32> {
+ let mut stmt = conn.prepare("SELECT count(*) FROM files")?;
let mut iter = stmt.query_map(params![], |row| row.get(0))?;
let count = iter.next().expect("SQL count result");
let count = count?;
Ok(count)
}
- pub fn files(&self) -> anyhow::Result<Vec<(u64, FilesystemEntry)>> {
- let mut stmt = self.conn.prepare("SELECT * FROM files")?;
+ pub fn files(conn: &Connection) -> anyhow::Result<Vec<(u64, FilesystemEntry)>> {
+ let mut stmt = conn.prepare("SELECT * FROM files")?;
let iter = stmt.query_map(params![], |row| row_to_entry(row))?;
let mut files: Vec<(u64, FilesystemEntry)> = vec![];
for x in iter {
@@ -172,11 +208,9 @@ impl LocalGeneration {
Ok(files)
}
- pub fn chunkids(&self, fileno: u64) -> anyhow::Result<Vec<ChunkId>> {
+ pub fn chunkids(conn: &Connection, fileno: u64) -> anyhow::Result<Vec<ChunkId>> {
let fileno = fileno as i64;
- let mut stmt = self
- .conn
- .prepare("SELECT chunkid FROM chunks WHERE fileno = ?1")?;
+ let mut stmt = conn.prepare("SELECT chunkid FROM chunks WHERE fileno = ?1")?;
let iter = stmt.query_map(params![fileno], |row| Ok(row.get(0)?))?;
let mut ids: Vec<ChunkId> = vec![];
for x in iter {
@@ -186,25 +220,25 @@ impl LocalGeneration {
Ok(ids)
}
- pub fn get_file(&self, filename: &Path) -> anyhow::Result<Option<FilesystemEntry>> {
- match self.get_file_and_fileno(filename)? {
+ pub fn get_file(conn: &Connection, filename: &Path) -> anyhow::Result<Option<FilesystemEntry>> {
+ match get_file_and_fileno(conn, filename)? {
None => Ok(None),
Some((_, e)) => Ok(Some(e)),
}
}
- pub fn get_fileno(&self, filename: &Path) -> anyhow::Result<Option<u64>> {
- match self.get_file_and_fileno(filename)? {
+ pub fn get_fileno(conn: &Connection, filename: &Path) -> anyhow::Result<Option<u64>> {
+ match get_file_and_fileno(conn, filename)? {
None => Ok(None),
Some((id, _)) => Ok(Some(id)),
}
}
fn get_file_and_fileno(
- &self,
+ conn: &Connection,
filename: &Path,
) -> anyhow::Result<Option<(u64, FilesystemEntry)>> {
- let files = self.files()?;
+ let files = files(conn)?;
let files: Vec<(u64, FilesystemEntry)> = files
.iter()
.filter(|(_, e)| e.pathbuf() == filename)