diff --git a/src/db.rs b/src/db.rs index c3f9b1b..de98e54 100644 --- a/src/db.rs +++ b/src/db.rs @@ -6,11 +6,7 @@ //! exposed for convenient interaction with the database, such as adding //! inserting and returning posts. -use std::{ - fmt, - fmt::{Display, Formatter}, - path::Path, -}; +use std::path::Path; use log::{debug, info}; use serde::{Deserialize, Serialize}; @@ -77,29 +73,6 @@ pub struct Post { pub subject: Option, } -/// Convenience type which facilitates converting an IVec into a String. -/// The IVec type is returned when getting a value from the sled database. -#[derive(Debug, Clone, PartialEq, Eq)] -struct IVecString { - pub bytes: IVec, - pub string: String, -} - -impl Display for IVecString { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{}", self.string) - } -} - -impl From for IVecString { - fn from(bytes: IVec) -> Self { - Self { - string: String::from_utf8_lossy(&bytes).into_owned(), - bytes, - } - } -} - /// An instance of the key-value database and relevant trees. #[allow(dead_code)] #[derive(Clone)] @@ -126,11 +99,11 @@ impl Database { debug!("Opening 'peers' database tree"); let peer_tree = db .open_tree("peers") - .expect("Failed to open database peers tree"); + .expect("Failed to open 'peers' database tree"); debug!("Opening 'posts' database tree"); let post_tree = db .open_tree("posts") - .expect("Failed to open database posts tree"); + .expect("Failed to open 'posts' database tree"); Database { db, @@ -142,14 +115,20 @@ impl Database { /// Add a peer to the database by inserting the public key into the peer /// tree. pub fn add_peer(&self, peer: Peer) -> Result> { + debug!("Serializing peer data for {} to bincode", &peer.public_key); let peer_bytes = bincode::serialize(&peer).unwrap(); + debug!( + "Inserting peer {} into 'peers' database tree", + &peer.public_key + ); self.peer_tree.insert(&peer.public_key, peer_bytes) } /// Remove a peer from the database, as represented by the given public /// key. pub fn remove_peer(&self, public_key: &str) -> Result<()> { + debug!("Removing peer {} from 'peers' database tree", &public_key); self.peer_tree.remove(&public_key).map(|_| ()) } @@ -157,11 +136,18 @@ impl Database { /// The byte value for the matching entry, if found, is deserialized from /// bincode into an instance of the Peer struct. pub fn get_peer(&self, public_key: &str) -> Result> { + debug!( + "Retrieving peer data for {} from 'peers' database tree", + &public_key + ); let peer = self .peer_tree .get(public_key.as_bytes()) .unwrap() - .map(|peer| bincode::deserialize(&peer).unwrap()); + .map(|peer| { + debug!("Deserializing peer data for {} from bincode", &public_key); + bincode::deserialize(&peer).unwrap() + }); Ok(peer) } @@ -170,12 +156,19 @@ impl Database { /// peer entry is deserialized from bincode into an instance of the Peer /// struct. pub fn get_peers(&self) -> Vec { + debug!("Retrieving data for all peers in the 'peers' database tree"); let mut peers = Vec::new(); self.peer_tree .iter() .map(|peer| peer.unwrap()) - .for_each(|peer| peers.push(bincode::deserialize(&peer.1).unwrap())); + .for_each(|peer| { + debug!( + "Deserializing peer data for {} from bincode", + String::from_utf8_lossy(&peer.0).into_owned() + ); + peers.push(bincode::deserialize(&peer.1).unwrap()) + }); peers } @@ -189,8 +182,10 @@ impl Database { /// This method can also be used to update an existing database entry. pub fn add_post(&self, public_key: &str, post: Post) -> Result> { let post_key = format!("{}_{}", public_key, post.key); + debug!("Serializing post data for {} to bincode", &post_key); let post_bytes = bincode::serialize(&post).unwrap(); + debug!("Inserting post {} into 'posts' database tree", &post_key); self.post_tree.insert(post_key.as_bytes(), post_bytes) } @@ -204,11 +199,14 @@ impl Database { for post in posts { let post_key = format!("{}_{}", public_key, post.key); + debug!("Serializing post data for {} to bincode", &post_key); let post_bytes = bincode::serialize(&post).unwrap(); + debug!("Inserting post {} into 'posts' database tree", &post_key); post_batch.insert(post_key.as_bytes(), post_bytes) } + debug!("Applying batch insertion into 'posts' database tree"); self.post_tree.apply_batch(post_batch) } @@ -217,12 +215,19 @@ impl Database { /// each matching entry is deserialized from bincode into an instance of /// the Post struct. pub fn get_posts(&self, public_key: &str) -> Result> { + debug!("Retrieving data for all posts in the 'posts' database tree"); let mut posts = Vec::new(); self.post_tree .scan_prefix(public_key.as_bytes()) .map(|post| post.unwrap()) - .for_each(|post| posts.push(bincode::deserialize(&post.1).unwrap())); + .for_each(|post| { + debug!( + "Deserializing post data for {} from bincode", + String::from_utf8_lossy(&post.0).into_owned() + ); + posts.push(bincode::deserialize(&post.1).unwrap()) + }); posts.sort_by(|a: &Post, b: &Post| b.timestamp.cmp(&a.timestamp)); @@ -235,12 +240,19 @@ impl Database { /// Post struct. pub fn get_post(&self, public_key: &str, msg_id: &str) -> Result> { let post_key = format!("{}_{}", public_key, msg_id); + debug!( + "Retrieving post data for {} from 'posts' database tree", + &post_key + ); let post = self .post_tree .get(post_key.as_bytes()) .unwrap() - .map(|post| bincode::deserialize(&post).unwrap()); + .map(|post| { + debug!("Deserializing post data for {} from bincode", &post_key); + bincode::deserialize(&post).unwrap() + }); Ok(post) } @@ -249,6 +261,7 @@ impl Database { /// key and defined by the given message ID. pub fn remove_post(&self, public_key: &str, msg_id: &str) -> Result<()> { let post_key = format!("{}_{}", public_key, msg_id); + debug!("Removing post {} from 'posts' database tree", &post_key); // .remove() would ordinarily return the value of the deleted entry // as an Option, returning None if the post_key was not found. @@ -260,12 +273,21 @@ impl Database { /// Sum the total number of unread posts for the peer represented by the /// given public key. pub fn get_unread_post_count(&self, public_key: &str) -> u16 { + debug!( + "Counting total number of unread posts for peer {}", + &public_key + ); + let mut unread_post_counter = 0; self.post_tree .scan_prefix(public_key.as_bytes()) .map(|post| post.unwrap()) .for_each(|post| { + debug!( + "Deserializing post data for {} from bincode", + String::from_utf8_lossy(&post.0).into_owned() + ); let deserialized_post: Post = bincode::deserialize(&post.1).unwrap(); if !deserialized_post.read { unread_post_counter += 1