Skip to content

Commit

Permalink
Update redb to 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralith committed Mar 29, 2024
1 parent fc1c4af commit c23d0f1
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 57 deletions.
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
56 changes: 23 additions & 33 deletions save/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,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 +84,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 +163,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 +203,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

0 comments on commit c23d0f1

Please sign in to comment.