refactor: Take impl Into<String> Db names and possibly avoid some allocations

Also remove some explicit clones that SNAFU will take care of
pull/24376/head
Carol (Nichols || Goulding) 2020-10-14 10:00:41 -04:00
parent 4faf5f04dc
commit 083e6947df
1 changed files with 9 additions and 8 deletions

View File

@ -225,16 +225,17 @@ pub struct Db {
impl Db { impl Db {
/// New creates a new in-memory only write buffer database /// New creates a new in-memory only write buffer database
pub fn new(name: &str) -> Self { pub fn new(name: impl Into<String>) -> Self {
Self { Self {
name: name.to_string(), name: name.into(),
..Default::default() ..Default::default()
} }
} }
/// Create a new DB that will create and use the Write Ahead Log /// Create a new DB that will create and use the Write Ahead Log
/// (WAL) directory `wal_dir` /// (WAL) directory `wal_dir`
pub async fn try_with_wal(name: &str, wal_dir: &mut PathBuf) -> Result<Self> { pub async fn try_with_wal(name: impl Into<String>, wal_dir: &mut PathBuf) -> Result<Self> {
let name = name.into();
wal_dir.push(&name); wal_dir.push(&name);
if let Err(e) = std::fs::create_dir(wal_dir.clone()) { if let Err(e) = std::fs::create_dir(wal_dir.clone()) {
match e.kind() { match e.kind() {
@ -251,14 +252,14 @@ impl Db {
let wal_builder = WalBuilder::new(wal_dir.clone()); let wal_builder = WalBuilder::new(wal_dir.clone());
let wal_details = start_wal_sync_task(wal_builder) let wal_details = start_wal_sync_task(wal_builder)
.await .await
.context(OpeningWal { database: name })?; .context(OpeningWal { database: &name })?;
wal_details wal_details
.write_metadata() .write_metadata()
.await .await
.context(OpeningWal { database: name })?; .context(OpeningWal { database: &name })?;
Ok(Self { Ok(Self {
name: name.to_string(), name,
wal_details: Some(wal_details), wal_details: Some(wal_details),
..Default::default() ..Default::default()
}) })
@ -345,7 +346,7 @@ impl Database for Db {
if let Some(wal) = &self.wal_details { if let Some(wal) = &self.wal_details {
wal.write_and_sync(data).await.context(WritingWal { wal.write_and_sync(data).await.context(WritingWal {
database: self.name.clone(), database: &self.name,
})?; })?;
} }
@ -369,7 +370,7 @@ impl Database for Db {
wal.write_and_sync(write.data.clone()) wal.write_and_sync(write.data.clone())
.await .await
.context(WritingWal { .context(WritingWal {
database: self.name.clone(), database: &self.name,
})?; })?;
} }