From aad50a0827b5c74229153395f01e0eafdd64edf6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 May 2022 09:38:34 +0300 Subject: 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 --- src/db.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/db.rs') 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 { 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 { + fn get_bar(row: &rusqlite::Row) -> Result { 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 { + fn values(db: Database) -> Vec { 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 { -- cgit v1.2.1 From 856d32c448a87245234315462d2190c5a9aab549 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 May 2022 10:05:28 +0300 Subject: feat! only store signed 64-bit plain integers in database This is a breaking change, but allows to store the largest signed 64-bit integers in SQLite and get it back. Sponsored-by: author --- src/db.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index ab638a9..46ba16a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -354,7 +354,7 @@ impl Column { } /// Type of plain integers that can be stored. -pub type DbInt = u64; +pub type DbInt = i64; /// A value in a named column. #[derive(Debug)] @@ -631,4 +631,17 @@ mod test { } assert_eq!(values, expected); } + + #[test] + fn round_trips_int_max() { + let tmp = tempdir().unwrap(); + let filename = tmp.path().join("test.db"); + let mut db = create_db(&filename); + insert(&mut db, DbInt::MAX); + db.close().unwrap(); + + let db = open_db(&filename); + let values = values(db); + assert_eq!(values, vec![DbInt::MAX]); + } } -- cgit v1.2.1 From 3d50e1497dd07929655636cfea0c48ccadd3ab8e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 3 May 2022 10:44:26 +0300 Subject: test: add test for storing, retrieving u64::MAX values in JSON The test passes. We create a FilesystemEntry with a length field containing u64::MAX, store that into a generation, and read it back. This works. The entry is serialised into JSON for storing in SQLite, and this proves we can handle any u64 value in an entry. serde_json deals with it fine, and we don't need to worry about it. Sponsored-by: author --- src/db.rs | 1 - 1 file changed, 1 deletion(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index 46ba16a..da24e96 100644 --- a/src/db.rs +++ b/src/db.rs @@ -631,7 +631,6 @@ mod test { } assert_eq!(values, expected); } - #[test] fn round_trips_int_max() { let tmp = tempdir().unwrap(); -- cgit v1.2.1