From 9176e858d524a34958ae77fd701582d291d26504 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 23 Oct 2020 20:19:04 +0000 Subject: [PATCH] A bit of database code cleanup. --- src/db.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index fb426c7..c3fdca7 100644 --- a/src/db.rs +++ b/src/db.rs @@ -3,6 +3,8 @@ use sled::transaction::abort; use sled::transaction::{TransactionError, TransactionalTree, UnabortableTransactionError}; use sled::{Db, Tree}; use std::collections::HashMap; +use std::path::Path; +use std::str; use thiserror::Error; use zerocopy::byteorder::I32; use zerocopy::{AsBytes, LayoutVerified}; @@ -103,8 +105,8 @@ fn convert_i32(raw_value: &[u8]) -> Result { } } -/// Atomically alter the count of variables in the database, by the -/// given amount. Count cannot go below 0. +/// Use a transaction to atomically alter the count of variables in +/// the database by the given amount. Count cannot go below 0. fn alter_room_variable_count( variables: &TransactionalTree, room_id: &str, @@ -127,7 +129,7 @@ fn alter_room_variable_count( } impl Database { - pub fn new>(path: P) -> Result { + pub fn new>(path: P) -> Result { let db = sled::open(path)?; let variables = db.open_tree("variables")?; let rooms = db.open_tree("rooms")?; @@ -152,16 +154,15 @@ impl Database { .scan_prefix(prefix) .map(|entry| match entry { Ok((key, raw_value)) => { - //Strips room and username from key, leaving - //behind name. - let variable_name = std::str::from_utf8(&key[prefix_len..])?; + //Strips room and username from key, leaving behind name. + let variable_name = str::from_utf8(&key[prefix_len..])?; Ok((variable_name.to_owned(), convert_i32(&raw_value)?)) } Err(e) => Err(e.into()), }) .collect(); - //Convert_I32 to hash map. Can we do this in the first mapping + //Convert I32 to hash map. Can we do this in the first mapping //step instead? For some reason this is faster. variables.map(|entries| entries.into_iter().collect()) }