summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-05-03 09:38:34 +0300
committerLars Wirzenius <liw@liw.fi>2022-05-03 09:59:07 +0300
commitaad50a0827b5c74229153395f01e0eafdd64edf6 (patch)
treeb8665053f47f25f3d6864c409a3bda85146704d1
parent0b6adba069cd1300079c822b14832d690595cfb4 (diff)
downloadobnam2-aad50a0827b5c74229153395f01e0eafdd64edf6.tar.gz
refactor: add a type for plain integers we store in a database
This will make it easier to change later, if need be. We may want to do that for various reasons, such as to save space. We may also want to change things to only use integer types that SQLite can handle: u64 is currently not well handled by our database layer. However, as this is a refactor, there's no change or fix to that. FileId is now explicitly a database integer. This doesn't break anything, for now, as the underlying integer type is still u64. Also, change a couple of places where it will matter if DbInt changes away from u64, and disable warnings for harmless conversions that may cause warnings depending on what type DbInt has. Sponsored-by: author
-rw-r--r--src/cmd/restore.rs2
-rw-r--r--src/cmd/show_gen.rs5
-rw-r--r--src/db.rs27
-rw-r--r--src/dbgen.rs6
4 files changed, 23 insertions, 17 deletions
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs
index 4a637da..223d481 100644
--- a/src/cmd/restore.rs
+++ b/src/cmd/restore.rs
@@ -297,7 +297,7 @@ fn path_to_cstring(path: &Path) -> CString {
fn create_progress_bar(file_count: FileId, verbose: bool) -> ProgressBar {
let progress = if verbose {
- ProgressBar::new(file_count)
+ ProgressBar::new(file_count as u64)
} else {
ProgressBar::hidden()
};
diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs
index 98c57fc..f47a07b 100644
--- a/src/cmd/show_gen.rs
+++ b/src/cmd/show_gen.rs
@@ -3,6 +3,7 @@
use crate::chunk::ClientTrust;
use crate::client::BackupClient;
use crate::config::ClientConfig;
+use crate::db::DbInt;
use crate::error::ObnamError;
use crate::fsentry::FilesystemKind;
use crate::generation::GenId;
@@ -66,7 +67,7 @@ impl ShowGeneration {
#[derive(Debug, Default, Serialize)]
struct Output {
generation_id: String,
- file_count: u64,
+ file_count: DbInt,
file_bytes: String,
file_bytes_raw: u64,
db_bytes: String,
@@ -81,7 +82,7 @@ impl Output {
}
}
- fn file_count(mut self, n: u64) -> Self {
+ fn file_count(mut self, n: DbInt) -> Self {
self.file_count = n;
self
}
diff --git a/src/db.rs b/src/db.rs
index b4de399..ab638a9 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -353,13 +353,16 @@ impl Column {
}
}
+/// Type of plain integers that can be stored.
+pub type DbInt = u64;
+
/// A value in a named column.
#[derive(Debug)]
pub enum Value<'a> {
/// An integer primary key.
- PrimaryKey(&'a str, u64),
+ PrimaryKey(&'a str, DbInt),
/// An integer.
- Int(&'a str, u64),
+ Int(&'a str, DbInt),
/// A text string.
Text(&'a str, &'a str),
/// A binary string.
@@ -381,12 +384,12 @@ impl<'a> Value<'a> {
}
/// Create an integer primary key value.
- pub fn primary_key(name: &'a str, value: u64) -> Self {
+ pub fn primary_key(name: &'a str, value: DbInt) -> Self {
Self::PrimaryKey(name, value)
}
/// Create an integer value.
- pub fn int(name: &'a str, value: u64) -> Self {
+ pub fn int(name: &'a str, value: DbInt) -> Self {
Self::Int(name, value)
}
@@ -406,6 +409,7 @@ impl<'a> Value<'a> {
}
}
+#[allow(clippy::useless_conversion)]
impl<'a> ToSql for Value<'a> {
// The trait defines to_sql to return a Result. However, for our
// particular case, to_sql can't ever fail. We only store values
@@ -438,9 +442,9 @@ impl<'a> ToSql for Value<'a> {
/// Like a Value, but owns the data.
pub enum OwnedValue {
/// An integer primary key.
- PrimaryKey(String, u64),
+ PrimaryKey(String, DbInt),
/// An integer.
- Int(String, u64),
+ Int(String, DbInt),
/// A text string.
Text(String, String),
/// A binary string.
@@ -462,6 +466,7 @@ impl From<&Value<'_>> for OwnedValue {
}
impl ToSql for OwnedValue {
+ #[allow(clippy::useless_conversion)]
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput> {
use rusqlite::types::Value;
let v = match self {
@@ -547,7 +552,7 @@ mod test {
use std::path::Path;
use tempfile::tempdir;
- fn get_bar(row: &rusqlite::Row) -> Result<u64, rusqlite::Error> {
+ fn get_bar(row: &rusqlite::Row) -> Result<DbInt, rusqlite::Error> {
row.get("bar")
}
@@ -566,12 +571,12 @@ mod test {
Database::open(file).unwrap()
}
- fn insert(db: &mut Database, value: u64) {
+ fn insert(db: &mut Database, value: DbInt) {
let table = table();
db.insert(&table, &[Value::int("bar", value)]).unwrap();
}
- fn values(db: Database) -> Vec<u64> {
+ fn values(db: Database) -> Vec<DbInt> {
let table = table();
let mut rows = db.all_rows(&table, &get_bar).unwrap();
let iter = rows.iter().unwrap();
@@ -606,7 +611,7 @@ mod test {
#[test]
fn inserts_many_rows() {
- const N: u64 = 1000;
+ const N: DbInt = 1000;
let tmp = tempdir().unwrap();
let filename = tmp.path().join("test.db");
@@ -618,7 +623,7 @@ mod test {
let db = open_db(&filename);
let values = values(db);
- assert_eq!(values.len() as u64, N);
+ assert_eq!(values.len() as DbInt, N);
let mut expected = vec![];
for i in 0..N {
diff --git a/src/dbgen.rs b/src/dbgen.rs
index 8e5ece5..0053d4a 100644
--- a/src/dbgen.rs
+++ b/src/dbgen.rs
@@ -2,7 +2,7 @@
use crate::backup_reason::Reason;
use crate::chunkid::ChunkId;
-use crate::db::{Column, Database, DatabaseError, SqlResults, Table, Value};
+use crate::db::{Column, Database, DatabaseError, DbInt, SqlResults, Table, Value};
use crate::fsentry::FilesystemEntry;
use crate::genmeta::{GenerationMeta, GenerationMetaError};
use crate::label::LabelChecksumKind;
@@ -28,8 +28,8 @@ pub const DEFAULT_SCHEMA_MAJOR: VersionComponent = V0_0::MAJOR;
/// Major schema versions supported by this version of Obnam.
pub const SCHEMA_MAJORS: &[VersionComponent] = &[0, 1];
-/// An identifier for a file in a generation.
-pub type FileId = u64;
+/// An integer identifier for a file in a generation.
+pub type FileId = DbInt;
/// Possible errors from using generation databases.
#[derive(Debug, thiserror::Error)]