Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ version = "0.0.1"
[workspace]
resolver = "2"
members = [
"buffer", "client",
"common",
"engine",
"file",
"log",
"network",
"server",
"transaction",
"buffer",
"client",
"common",
"engine",
"file",
"log",
"network",
"server",
"transaction",
]

[workspace.dependencies]
Expand Down
1 change: 1 addition & 0 deletions buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ version.workspace = true
common = { path = "../common" }
file = { path = "../file" }
log = { path = "../log" }
tracing = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
5 changes: 4 additions & 1 deletion buffer/src/mgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ impl BufferMgr {
Some(buffer) => {
return Ok(buffer);
}
None if start.elapsed() >= MAX_WAIT => return Err(DbError::BufferAbort),
None if start.elapsed() >= MAX_WAIT => {
tracing::debug!("buffer {:?} lock abort", block);
return Err(DbError::BufferAbort);
}
None => {
drop(lock);
thread::sleep(SLEEP)
Expand Down
2 changes: 2 additions & 0 deletions common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum DbError {
ToInt(#[from] std::num::TryFromIntError),
#[error("Unexpected token: {0}")]
UnexpectedToken(String),
#[error("max size ({0}) exceeded: {1}")]
MaxSize(usize, usize),
}

impl DbError {
Expand Down
22 changes: 21 additions & 1 deletion engine/benches/db_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ fn bench_insert(c: &mut Criterion) {
});
}

fn bench_index_insert(c: &mut Criterion) {
let dir = tempdir().unwrap();
let db = SimpleDB::new(dir.path()).unwrap();
let tx = db.get_tx().unwrap();
db.execute(&tx, "CREATE TABLE test(id INT)").unwrap();
db.execute(&tx, "CREATE INDEX test_ids ON test(id)")
.unwrap();
tx.commit().unwrap();
let mut rng = rand::rng();
c.bench_function("index_insert", |b| {
b.iter(|| {
let id = rng.random::<i32>();
db.execute(&tx, &format!("INSERT INTO test(id) VALUES({})", id))
.unwrap();
db.query(&tx, &format!("SELECT id FROM test WHERE id={}", id))
.unwrap();
});
});
}

fn bench_join(c: &mut Criterion) {
let dir = tempdir().unwrap();
let db = SimpleDB::new(dir.path()).unwrap();
Expand Down Expand Up @@ -49,5 +69,5 @@ fn bench_join(c: &mut Criterion) {
});
}

criterion_group!(benches, bench_insert, bench_join);
criterion_group!(benches, bench_insert, bench_join, bench_index_insert);
criterion_main!(benches);
66 changes: 0 additions & 66 deletions engine/src/constant.rs

This file was deleted.

69 changes: 69 additions & 0 deletions engine/src/element.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use common::DbResult;
use common::error::DbError;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Element {
Raw(String),
View(String, String),
Spec(String, String),
}

impl Element {
pub fn raw(value: &str) -> Self {
Self::Raw(value.to_string())
}

pub fn view(source: &str, name: &str) -> Self {
Self::View(source.to_string(), name.to_string())
}

pub fn spec(source: &str, target: &str) -> Self {
Self::Spec(source.to_string(), target.to_string())
}

pub fn as_raw(&self) -> DbResult<&str> {
match self {
Self::Raw(s) => Ok(s),
_ => Err(DbError::InvalidFieldType),
}
}

pub fn len(&self) -> usize {
match self {
Self::Raw(s) => s.len(),
Self::View(source, name) => source.len() + 4 + name.len(),
Self::Spec(source, target) => source.len() + 4 + target.len(),
}
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl std::fmt::Display for Element {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Raw(value) => write!(f, "{}", value),
Self::View(source, name) => write!(f, "{} {}", source, name),
Self::Spec(source, target) => write!(f, "{}.{}", source, target),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn view() {
let view = Element::view("table", "t");
assert_eq!("table t", view.to_string());
}

#[test]
fn spec() {
let spec = Element::spec("t1", "f1");
assert_eq!("t1.f1", spec.to_string());
}
}
34 changes: 17 additions & 17 deletions engine/src/index.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use common::DbResult;

use crate::{constant::Constant, rid::RID};
use crate::{rid::RID, value::Value};

pub mod b_tree;
pub mod btree_dir;
pub mod btree_leaf;
pub mod btree_page;
pub mod dir_entry;

pub trait Index {
fn before_first(&self, key: Constant) -> DbResult<()>;
fn before_first(&self, key: Value) -> DbResult<()>;

fn next(&self) -> DbResult<bool>;

fn get_data_rid(&self) -> DbResult<RID>;

fn insert(&self, value: Constant, rid: RID) -> DbResult<()>;
fn insert(&self, value: Value, rid: RID) -> DbResult<()>;

fn delete(&self, value: Constant, rid: RID) -> DbResult<()>;
fn delete(&self, value: Value, rid: RID) -> DbResult<()>;

fn close(&self) -> DbResult<()>;
}
Expand All @@ -26,29 +22,33 @@ pub trait Index {
mod tests {
use tempfile::tempdir;

use crate::element::Element;
use crate::schema::SchemaBuilder;
use crate::{
SimpleDB,
plan::{Plan, table::TablePlan},
};

#[test]
fn index_retrieval() {
use crate::schema::Schema;
use std::sync::Arc;

let dir = tempdir().unwrap();
let db = SimpleDB::configured(dir.path(), 512, 8).unwrap();
let md = db.metadata_mgr();

let table = "student";

let sid = Element::raw("sid");
let sname = Element::raw("sname");
let majorid = Element::raw("majorid");

let setup_tx = db.get_tx().unwrap();
let schema = Arc::new(Schema::default());
schema.add_int_field("sid".to_string()).unwrap();
schema.add_string_field("sname".to_string(), 16).unwrap();
schema.add_int_field("majorid".to_string()).unwrap();
let schema = SchemaBuilder::default()
.add_int_field(sid.clone())
.add_string_field(sname.clone(), 16)
.add_int_field(majorid.clone())
.build();

md.create_table(table, &schema, &setup_tx).unwrap();
md.create_table(table, schema, &setup_tx).unwrap();
md.create_index("idx_majorid", table, "majorid", &setup_tx)
.unwrap();
setup_tx.commit().unwrap();
Expand All @@ -58,7 +58,7 @@ mod tests {
let _scan = plan.open().unwrap();

let indexes = md.get_index_info(table, &tx).unwrap();
let _index = indexes.get("majorid").unwrap();
let _index = indexes.get(&majorid).unwrap();
tx.commit().unwrap();
}
}
Loading
Loading