From d9b72ffa5485f3c253da22f09ff0a7090de7aa37 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 9 Apr 2022 11:27:14 +0300 Subject: refactor: rename Checksum to Label Label is a clearer and more accurate name for the type now that it is not just a checksum. Also, serialize a Label in tests, rather than using string literals. This is more correct, and we'll be changing serialization later. Sponsored-by: author --- src/index.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/index.rs') diff --git a/src/index.rs b/src/index.rs index 11f3480..5310a44 100644 --- a/src/index.rs +++ b/src/index.rs @@ -1,8 +1,8 @@ //! An on-disk index of chunks for the server. -use crate::checksummer::Checksum; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; +use crate::label::Label; use rusqlite::Connection; use std::path::Path; @@ -74,7 +74,7 @@ impl Index { #[cfg(test)] mod test { - use crate::checksummer::Checksum; + use super::Label; use super::{ChunkId, ChunkMeta, Index}; use std::path::Path; @@ -87,20 +87,20 @@ mod test { #[test] fn remembers_inserted() { let id: ChunkId = "id001".parse().unwrap(); - let sum = Checksum::sha256_from_str_unchecked("abc"); + let sum = Label::sha256(b"abc"); let meta = ChunkMeta::new(&sum); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); idx.insert_meta(id.clone(), meta.clone()).unwrap(); assert_eq!(idx.get_meta(&id).unwrap(), meta); - let ids = idx.find_by_label("abc").unwrap(); + let ids = idx.find_by_label(&format!("{}", sum)).unwrap(); assert_eq!(ids, vec![id]); } #[test] fn does_not_find_uninserted() { let id: ChunkId = "id001".parse().unwrap(); - let sum = Checksum::sha256_from_str_unchecked("abc"); + let sum = Label::sha256(b"abc"); let meta = ChunkMeta::new(&sum); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); @@ -111,19 +111,19 @@ mod test { #[test] fn removes_inserted() { let id: ChunkId = "id001".parse().unwrap(); - let sum = Checksum::sha256_from_str_unchecked("abc"); + let sum = Label::sha256(b"abc"); let meta = ChunkMeta::new(&sum); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); idx.insert_meta(id.clone(), meta).unwrap(); idx.remove_meta(&id).unwrap(); - let ids: Vec = idx.find_by_label("abc").unwrap(); + let ids: Vec = idx.find_by_label(&format!("{}", sum)).unwrap(); assert_eq!(ids, vec![]); } } mod sql { - use super::{Checksum, IndexError}; + use super::{IndexError, Label}; use crate::chunkid::ChunkId; use crate::chunkmeta::ChunkMeta; use log::error; @@ -216,7 +216,7 @@ mod sql { fn row_to_meta(row: &Row) -> rusqlite::Result { let hash: String = row.get("label")?; - let sha256 = Checksum::sha256_from_str_unchecked(&hash); + let sha256 = Label::sha256_from_str_unchecked(&hash); Ok(ChunkMeta::new(&sha256)) } -- cgit v1.2.1 From 82ff782fe85c84c10f1f18c9bd5c2b017bc2f240 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 9 Apr 2022 09:40:59 +0300 Subject: feat! change how chunk labels are serialized Serialized labels now start with a type prefix: a character that says what type of label it is. This isn't strictly required: we _can_ just decide to always use a single type of checksum for all chunks in one backup, for one client, or in the whole repository. However, if it's ever possible to have more than one type, it helps debugging if every checksum, when serialized, is explicit about its type. Change things to use the new serialize method instead of the Display trait for Label. We're primarily serializing labels so they can be stored in a database, and used in URLs, only secondarily showing them to users. Sponsored-by: author --- src/index.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/index.rs') diff --git a/src/index.rs b/src/index.rs index 5310a44..52da2f2 100644 --- a/src/index.rs +++ b/src/index.rs @@ -93,7 +93,7 @@ mod test { let mut idx = new_index(dir.path()); idx.insert_meta(id.clone(), meta.clone()).unwrap(); assert_eq!(idx.get_meta(&id).unwrap(), meta); - let ids = idx.find_by_label(&format!("{}", sum)).unwrap(); + let ids = idx.find_by_label(&sum.serialize()).unwrap(); assert_eq!(ids, vec![id]); } @@ -117,7 +117,7 @@ mod test { let mut idx = new_index(dir.path()); idx.insert_meta(id.clone(), meta).unwrap(); idx.remove_meta(&id).unwrap(); - let ids: Vec = idx.find_by_label(&format!("{}", sum)).unwrap(); + let ids: Vec = idx.find_by_label(&sum.serialize()).unwrap(); assert_eq!(ids, vec![]); } } @@ -216,7 +216,7 @@ mod sql { fn row_to_meta(row: &Row) -> rusqlite::Result { let hash: String = row.get("label")?; - let sha256 = Label::sha256_from_str_unchecked(&hash); + let sha256 = Label::deserialize(&hash).expect("deserialize checksum from database"); Ok(ChunkMeta::new(&sha256)) } -- cgit v1.2.1