1个不稳定版本
使用旧的Rust 2015
0.0.5 | 2018年9月14日 |
---|
#180 在 数据库实现
145KB
3K SLoC
v11
v11
是一个面向数据设计的数据库引擎。基本思想是通过直接操作特定问题所需的数据来最小化浪费的CPU缓存。在第一层,这是通过将数据存储在列式表中("数组的结构")来实现的。在第二层,这是通过应用数据库规范化 技术来实现的。
此crate仍在开发中。
v11
使用Tracker
确保一致性:当删除一行时,任何依赖于该行的依赖项都会有机会做出反应。
#[macro_use] extern crate v11_macros;
#[macro_use] extern crate v11;
use v11::Universe;
use v11::event;
domain! { EXAMPLE }
table! {
#[kind = "consistent"]
#[row_derive(Clone, Debug)]
[EXAMPLE/ships] {
name: [String; VecCol<String>],
cargo: [u8; VecCol<u8>],
}
}
table! {
#[kind = "consistent"]
#[row_derive(Debug)]
[EXAMPLE/sailors] {
#[foreign_auto]
#[index]
ship: [ships::RowId; VecCol<ships::RowId>],
name: [String; VecCol<String>],
}
}
fn main() {
EXAMPLE.register();
ships::register();
sailors::register();
let universe = &Universe::new(&[EXAMPLE]);
let boaty_mcboatface = {
let mut ships = ships::write(universe);
let mont_blanc = ships.push(ships::Row {
name: "SS Mont-Blanc".into(),
cargo: 11,
});
let lusitania = ships.push(ships::Row {
name: "RMS Lusitania".into(),
cargo: 237,
});
let titanic = ships.push(ships::Row {
name: "RMS Titanic".into(),
cargo: 42,
});
let boaty_mcboatface = ships.push(ships::Row {
name: "Boaty McBoatface".into(),
cargo: 24,
});
let mut sailors = sailors::write(universe);
sailors.push(sailors::Row {
ship: titanic,
name: "Alice".into(),
});
sailors.push(sailors::Row {
ship: boaty_mcboatface,
name: "Bob".into(),
});
sailors.push(sailors::Row {
ship: lusitania,
name: "Charles".into(),
});
sailors.push(sailors::Row {
ship: mont_blanc,
name: "Dave".into(),
});
sailors.close();
ships.flush(universe, event::CREATE);
boaty_mcboatface
};
show(universe);
{
let mut ships = ships::write(universe);
println!();
println!("The Boaty McBoatface is sinking! Oh, the humanity!");
println!();
ships.delete(boaty_mcboatface);
ships.flush(universe, event::DELETE);
}
show(universe);
}
fn show(universe: &Universe) {
let ships = ships::read(universe);
let sailors = sailors::read(universe);
for ship in ships.iter() {
println!("{:?} = {:?}", ship, ships.get_row(ship));
}
for sailor in sailors.iter() {
println!("{:?} = {:?}", sailor, sailors.get_row_ref(sailor));
}
}
这是输出
ships[0] = Row { name: "SS Mont-Blanc", cargo: 11 }
ships[1] = Row { name: "RMS Lusitania", cargo: 237 }
ships[2] = Row { name: "RMS Titanic", cargo: 42 }
ships[3] = Row { name: "Boaty McBoatface", cargo: 24 }
sailors[0] = RowRef { ship: ships[2], name: "Alice" }
sailors[1] = RowRef { ship: ships[3], name: "Bob" }
sailors[2] = RowRef { ship: ships[1], name: "Charles" }
sailors[3] = RowRef { ship: ships[0], name: "Dave" }
The Boaty McBoatface is sinking! Oh, the humanity!
ships[0] = Row { name: "SS Mont-Blanc", cargo: 11 }
ships[1] = Row { name: "RMS Lusitania", cargo: 237 }
ships[2] = Row { name: "RMS Titanic", cargo: 42 }
sailors[0] = RowRef { ship: ships[2], name: "Alice" }
sailors[2] = RowRef { ship: ships[1], name: "Charles" }
sailors[3] = RowRef { ship: ships[0], name: "Dave" }
行索引是强类型,所以ships.cargo[sailor]
是编译器错误。
依赖关系
~11MB
~211K SLoC