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

Update redb to 2.0 #373

Merged
merged 1 commit into from
Mar 29, 2024
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
2 changes: 1 addition & 1 deletion save/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
prost = "0.12.2"
redb = "1.0"
redb = "2.0"
thiserror = "1.0.38"
zstd = { package = "zstd-safe", version = "7.1.0", default-features = false, features = ["std", "experimental"] }

Expand Down
7 changes: 3 additions & 4 deletions save/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn save(c: &mut Criterion) {
.collect::<Vec<u128>>();
(file, save, node_ids)
},
|(_file, mut save, node_ids)| {
|(_file, save, node_ids)| {
let mut tx = save.write().unwrap();
let mut writer = tx.get().unwrap();
for i in node_ids {
Expand All @@ -49,7 +49,7 @@ fn save(c: &mut Criterion) {
b.iter_batched(
|| {
let file = tempfile::NamedTempFile::new().unwrap();
let mut save = Save::open(file.path(), 12).unwrap();
let save = Save::open(file.path(), 12).unwrap();
let node_ids = (&mut rng)
.sample_iter(rand::distributions::Standard)
.take(count as usize)
Expand All @@ -66,8 +66,7 @@ fn save(c: &mut Criterion) {
(file, save, node_ids)
},
|(_file, save, node_ids)| {
let read = save.read().unwrap();
let mut read = read.get().unwrap();
let mut read = save.read().unwrap();
for i in node_ids {
black_box(read.get_voxel_node(i).unwrap().unwrap());
}
Expand Down
61 changes: 25 additions & 36 deletions save/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Save {
let meta = {
let tx = db.begin_read().map_err(redb::Error::from)?;
// Intermediate variable to make borrowck happy
let meta = match tx.open_table(META_TABLE) {
match tx.open_table(META_TABLE) {
Ok(meta) => {
let Some(value) = meta.get(&[][..]).map_err(redb::Error::from)? else {
return Err(OpenError::MissingMeta);
Expand All @@ -39,8 +39,7 @@ impl Save {
defaults
}
Err(e) => return Err(OpenError::Db(DbError(e.into()))),
};
meta
}
};
Ok(Self { meta, db })
}
Expand All @@ -50,12 +49,18 @@ impl Save {
&self.meta
}

pub fn read(&self) -> Result<ReaderGuard<'_>, DbError> {
pub fn read(&self) -> Result<Reader, DbError> {
let tx = self.db.begin_read().map_err(redb::Error::from)?;
Ok(ReaderGuard { tx })
Ok(Reader {
voxel_nodes: tx.open_table(VOXEL_NODE_TABLE)?,
entity_nodes: tx.open_table(ENTITY_NODE_TABLE)?,
characters: tx.open_table(CHARACTERS_BY_NAME_TABLE)?,
dctx: dctx(),
accum: Vec::new(),
})
}

pub fn write(&mut self) -> Result<WriterGuard<'_>, DbError> {
pub fn write(&self) -> Result<WriterGuard, DbError> {
let tx = self.db.begin_write().map_err(redb::Error::from)?;
Ok(WriterGuard { tx })
}
Expand All @@ -78,38 +83,22 @@ fn init_meta_table(db: &Database, value: &Meta) -> Result<(), redb::Error> {
Ok(())
}

pub struct ReaderGuard<'a> {
tx: redb::ReadTransaction<'a>,
}

impl ReaderGuard<'_> {
pub fn get(&self) -> Result<Reader<'_>, DbError> {
Ok(Reader {
voxel_nodes: self.tx.open_table(VOXEL_NODE_TABLE)?,
entity_nodes: self.tx.open_table(ENTITY_NODE_TABLE)?,
characters: self.tx.open_table(CHARACTERS_BY_NAME_TABLE)?,
dctx: dctx(),
accum: Vec::new(),
})
}
}

fn dctx() -> zstd::DCtx<'static> {
let mut dctx = zstd::DCtx::create();
dctx.set_parameter(zstd::DParameter::Format(zstd::FrameFormat::Magicless))
.unwrap();
dctx
}

pub struct Reader<'a> {
voxel_nodes: redb::ReadOnlyTable<'a, u128, &'static [u8]>,
entity_nodes: redb::ReadOnlyTable<'a, u128, &'static [u8]>,
characters: redb::ReadOnlyTable<'a, &'static str, &'static [u8]>,
pub struct Reader {
voxel_nodes: redb::ReadOnlyTable<u128, &'static [u8]>,
entity_nodes: redb::ReadOnlyTable<u128, &'static [u8]>,
characters: redb::ReadOnlyTable<&'static str, &'static [u8]>,
dctx: zstd::DCtx<'static>,
accum: Vec<u8>,
}

impl Reader<'_> {
impl Reader {
pub fn get_voxel_node(&mut self, node_id: u128) -> Result<Option<VoxelNode>, GetError> {
let Some(node) = self.voxel_nodes.get(&node_id)? else {
return Ok(None);
Expand Down Expand Up @@ -173,12 +162,12 @@ fn decompress(
}
}

pub struct WriterGuard<'a> {
tx: redb::WriteTransaction<'a>,
pub struct WriterGuard {
tx: redb::WriteTransaction,
}

impl<'a> WriterGuard<'a> {
pub fn get(&mut self) -> Result<Writer<'a, '_>, DbError> {
impl WriterGuard {
pub fn get(&mut self) -> Result<Writer<'_>, DbError> {
Ok(Writer {
voxel_nodes: self
.tx
Expand Down Expand Up @@ -213,16 +202,16 @@ fn cctx() -> zstd::CCtx<'static> {
cctx
}

pub struct Writer<'save, 'guard> {
voxel_nodes: redb::Table<'save, 'guard, u128, &'static [u8]>,
entity_nodes: redb::Table<'save, 'guard, u128, &'static [u8]>,
characters: redb::Table<'save, 'guard, &'static str, &'static [u8]>,
pub struct Writer<'guard> {
voxel_nodes: redb::Table<'guard, u128, &'static [u8]>,
entity_nodes: redb::Table<'guard, u128, &'static [u8]>,
characters: redb::Table<'guard, &'static str, &'static [u8]>,
cctx: zstd::CCtx<'static>,
plain: Vec<u8>,
compressed: Vec<u8>,
}

impl Writer<'_, '_> {
impl Writer<'_> {
pub fn put_voxel_node(&mut self, node_id: u128, state: &VoxelNode) -> Result<(), DbError> {
prepare(&mut self.cctx, &mut self.plain, &mut self.compressed, state);
self.voxel_nodes.insert(node_id, &*self.compressed)?;
Expand Down
2 changes: 1 addition & 1 deletion save/tests/heavy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rand::{rngs::SmallRng, Rng, SeedableRng};
fn write() {
let mut rng = SmallRng::from_entropy();
let file = tempfile::NamedTempFile::new().unwrap();
let mut save = Save::open(file.path(), 12).unwrap();
let save = Save::open(file.path(), 12).unwrap();
let node = save::VoxelNode {
chunks: vec![save::Chunk {
vertex: 0,
Expand Down
20 changes: 4 additions & 16 deletions save/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn persist_meta() {
#[test]
fn persist_node() {
let file = tempfile::NamedTempFile::new().unwrap();
let mut save = Save::open(file.path(), 12).unwrap();
let save = Save::open(file.path(), 12).unwrap();
let node = save::VoxelNode {
chunks: vec![save::Chunk {
vertex: 0,
Expand All @@ -31,20 +31,14 @@ fn persist_node() {
writer_guard.commit().unwrap();
assert_eq!(
node,
save.read()
.unwrap()
.get()
.unwrap()
.get_voxel_node(0)
.unwrap()
.unwrap()
save.read().unwrap().get_voxel_node(0).unwrap().unwrap()
);
}

#[test]
fn persist_character() {
let file = tempfile::NamedTempFile::new().unwrap();
let mut save = Save::open(file.path(), 12).unwrap();
let save = Save::open(file.path(), 12).unwrap();
let mut writer_guard = save.write().unwrap();
let mut writer = writer_guard.get().unwrap();
let mut rng = SmallRng::from_entropy();
Expand All @@ -61,12 +55,6 @@ fn persist_character() {
let save = Save::open(file.path(), 12).unwrap();
assert_eq!(
ch,
save.read()
.unwrap()
.get()
.unwrap()
.get_character("asdf")
.unwrap()
.unwrap()
save.read().unwrap().get_character("asdf").unwrap().unwrap()
);
}
3 changes: 1 addition & 2 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ impl Sim {
}

fn load_all_voxels(&mut self, save: &save::Save) -> anyhow::Result<()> {
let read_guard = save.read()?;
let mut read = read_guard.get()?;
let mut read = save.read()?;
for node_hash in read.get_all_voxel_node_ids()? {
let Some(voxel_node) = read.get_voxel_node(node_hash)? else {
continue;
Expand Down
Loading