Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for buffermgr trait #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/rdbc/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod tests {
},
driver::EmbeddedDriver,
};
use crate::server::config::{self, SimpleDBConfig};

#[test]
fn unit_test() -> Result<()> {
Expand Down Expand Up @@ -74,7 +75,13 @@ mod tests {
];

// driver
let d = EmbeddedDriver::new();
let db_config = SimpleDBConfig {
block_size: 400,
num_of_buffers: 8,
buffer_manager: config::BufferMgr::LRU,
query_planner: config::QueryPlanner::Heuristic,
};
let d = EmbeddedDriver::new(db_config);
// connect database
let mut conn = d.connect("_test/rdbc")?;
// init database
Expand Down
6 changes: 3 additions & 3 deletions src/server/simpledb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub struct SimpleDB {
// managers
fm: Arc<Mutex<FileMgr>>,
lm: Arc<Mutex<LogMgr>>,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
mdm: Option<Arc<Mutex<MetadataMgr>>>,
qp: Option<Arc<Mutex<dyn QueryPlanner>>>,
up: Option<Arc<Mutex<dyn UpdatePlanner>>>,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl SimpleDB {
pub fn log_mgr(&self) -> Arc<Mutex<LogMgr>> {
Arc::clone(&self.lm)
}
pub fn buffer_mgr(&self) -> Arc<Mutex<dyn BufferMgr>> {
pub fn buffer_mgr(&self) -> Arc<Mutex<dyn BufferMgr + Send>> {
Arc::clone(&self.bm)
}
pub fn metadata_mgr(&self) -> Option<Arc<Mutex<MetadataMgr>>> {
Expand Down Expand Up @@ -167,7 +167,7 @@ impl SimpleDB {
));
let lm = Arc::new(Mutex::new(LogMgr::new(Arc::clone(&fm), LOG_FILE).unwrap()));
let (bm_fm, bm_lm, numsbuff) = (Arc::clone(&fm), Arc::clone(&lm), cfg.num_of_buffers);
let bm: Arc<Mutex<dyn BufferMgr>> = match cfg.buffer_manager {
let bm: Arc<Mutex<dyn BufferMgr + Send>> = match cfg.buffer_manager {
config::BufferMgr::Naive => {
Arc::new(Mutex::new(NaiveBufferMgr::new(bm_fm, bm_lm, numsbuff)))
}
Expand Down
13 changes: 10 additions & 3 deletions src/tx/bufferlist.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use std::{
collections::HashMap,
fmt,
sync::{Arc, Mutex},
};

Expand All @@ -9,15 +10,21 @@ use crate::{
file::block_id::BlockId,
};

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct BufferList {
buffers: HashMap<BlockId, Arc<Mutex<Buffer>>>,
pins: Vec<BlockId>,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
}

impl fmt::Debug for BufferList {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "BufferList")
}
}

impl BufferList {
pub fn new(bm: Arc<Mutex<dyn BufferMgr>>) -> Self {
pub fn new(bm: Arc<Mutex<dyn BufferMgr + Send>>) -> Self {
Self {
buffers: HashMap::new(),
pins: vec![],
Expand Down
17 changes: 13 additions & 4 deletions src/tx/recovery/manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use anyhow::Result;
use chrono::NaiveDate;
use core::fmt;
use std::sync::{Arc, Mutex};
use std::{
fmt::Debug,
sync::{Arc, Mutex},
};

use super::logrecord::{
self, checkpoint_record::CheckpointRecord, commit_record::CommitRecord,
Expand Down Expand Up @@ -32,20 +35,26 @@ impl fmt::Display for RecoveryMgrError {
}
}

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct RecoveryMgr {
lm: Arc<Mutex<LogMgr>>,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
tx: Arc<Mutex<Transaction>>,
txnum: i32,
}

impl fmt::Debug for RecoveryMgr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RecoveryMgr(Tx = {})", self.txnum)
}
}

impl RecoveryMgr {
pub fn new(
tx: Arc<Mutex<Transaction>>,
txnum: i32,
lm: Arc<Mutex<LogMgr>>,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
) -> Result<Self> {
StartRecord::write_to_log(Arc::clone(&lm), txnum)?;

Expand Down
13 changes: 10 additions & 3 deletions src/tx/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;
use chrono::NaiveDate;
use std::{
fmt,
sync::{Arc, Mutex},
usize,
};
Expand All @@ -18,27 +19,33 @@ use crate::{

static END_OF_FILE: i32 = -1;

#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Transaction {
// static member (shared by all Transaction)
next_tx_num: Arc<Mutex<i32>>,

recovery_mgr: Option<Arc<Mutex<RecoveryMgr>>>,
concur_mgr: ConcurrencyMgr,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
fm: Arc<Mutex<FileMgr>>,
txnum: i32,
mybuffers: BufferList,
}

impl fmt::Debug for Transaction {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Tx: {}", self.txnum)
}
}

impl Transaction {
pub fn new(
next_tx_num: Arc<Mutex<i32>>,
locktbl: Arc<Mutex<LockTable>>,

fm: Arc<Mutex<FileMgr>>,
lm: Arc<Mutex<LogMgr>>,
bm: Arc<Mutex<dyn BufferMgr>>,
bm: Arc<Mutex<dyn BufferMgr + Send>>,
) -> Result<Self> {
let mut tran = Self {
next_tx_num,
Expand Down