Skip to content

Commit

Permalink
perf: use arc in config struct for performance cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
peeeuzin committed Jun 8, 2024
1 parent 50eacaf commit cc1ebb7
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/collection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub enum TransactionStatus {
pub struct Collection<T: Sync + Send + Clone + Debug + Serialize + DeserializeOwned + 'static> {
memtable: Memtable<T>,
storage: Storage,
config: config::DustDataConfig,
config: Arc<config::DustDataConfig>,
pub wal: Wal,
}

Expand All @@ -118,7 +118,7 @@ type Storage = Arc<RwLock<storage::Storage>>;
type Wal = Arc<RwLock<wal::Wal>>;

impl<T: Sync + Send + Clone + Debug + Serialize + 'static + DeserializeOwned> Collection<T> {
pub fn new(config: config::DustDataConfig) -> Self {
pub fn new(config: Arc<config::DustDataConfig>) -> Self {
let storage = Arc::new(RwLock::new(storage::Storage::new(config.clone()).unwrap()));
let wal = Arc::new(RwLock::new(wal::Wal::new(config.clone()).unwrap()));

Expand Down
3 changes: 2 additions & 1 deletion src/collection/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap;
use std::fmt::Debug;
use std::io::{prelude::*, SeekFrom};
use std::sync::Arc;
use std::{fs, path};

use super::config;
Expand All @@ -24,7 +25,7 @@ pub struct StorageTupleEntry<T> {
}

impl Storage {
pub fn new(config: config::DustDataConfig) -> Result<Self> {
pub fn new(config: Arc<config::DustDataConfig>) -> Result<Self> {
let storage_path = config.data_path.join("data");

std::fs::create_dir_all(&storage_path).ok();
Expand Down
5 changes: 3 additions & 2 deletions src/collection/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::collections::BTreeMap;
use std::fmt::Debug;
use std::io::{Read, Seek, SeekFrom, Write};
use std::ops::RangeBounds;
use std::sync::Arc;
use std::{fs, path};

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -92,13 +93,13 @@ impl LogFile {

#[derive(Debug)]
pub struct Wal {
config: config::DustDataConfig,
config: Arc<config::DustDataConfig>,
current_file: LogFile,
pub index: WALIndex,
}

impl Wal {
pub fn new(config: config::DustDataConfig) -> Result<Self> {
pub fn new(config: Arc<config::DustDataConfig>) -> Result<Self> {
let log_path = config.data_path.join(&config.wal.log_path);

fs::create_dir_all(&log_path).ok();
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use fs2::FileExt;
use serde::{de::DeserializeOwned, Serialize};
use std::fmt::Debug;
use std::fs;
use std::sync::Arc;

#[derive(Debug, Clone, Default)]
pub struct DustData {
Expand Down Expand Up @@ -99,7 +100,7 @@ impl DustData {
let mut config = self.config.clone();
config.data_path.push(name);

collection::Collection::new(config)
collection::Collection::new(Arc::new(config))
}

pub fn drop_collection(&self, name: &str) -> Result<()> {
Expand Down

0 comments on commit cc1ebb7

Please sign in to comment.