Skip to content

Commit bc5bcfa

Browse files
committed
Compression re-added
1 parent ec90cd0 commit bc5bcfa

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/database/chunks.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::utils::error::Error;
99
use crate::utils::hash::hash;
1010
use crate::world::chunkformat::Chunk;
1111

12+
use crate::utils::binary_utils::{bzip_compress, bzip_decompress};
1213
use bincode::config::standard;
1314
use bincode::{decode_from_slice, encode_to_vec};
1415

@@ -25,7 +26,9 @@ impl Database {
2526
// Attempt to fetch chunk from table
2627
if let Ok(data) = database.get(&ro_tx, key) {
2728
Ok(data.map(|encoded_chunk| {
28-
let chunk: (Chunk, usize) = decode_from_slice(encoded_chunk, standard())
29+
let decompressed =
30+
bzip_decompress(&encoded_chunk).expect("Failed to decompress chunk");
31+
let chunk: (Chunk, usize) = decode_from_slice(&*decompressed, standard())
2932
.expect("Failed to decode chunk from database");
3033
chunk.0
3134
}))
@@ -45,10 +48,11 @@ impl Database {
4548

4649
// Encode chunk
4750
let encoded_chunk = encode_to_vec(chunk, standard()).expect("Failed to encode chunk");
51+
let compressed = bzip_compress(&encoded_chunk).expect("Failed to compress chunk");
4852
let key = hash((chunk.dimension.as_ref().unwrap(), chunk.x_pos, chunk.z_pos));
4953

5054
// Insert chunk
51-
let res = database.put(&mut rw_tx, &key, &encoded_chunk);
55+
let res = database.put(&mut rw_tx, &key, &compressed);
5256
rw_tx.commit().map_err(|err| {
5357
Error::DatabaseError(format!("Unable to commit changes to database: {err}"))
5458
})?;
@@ -76,14 +80,14 @@ impl Database {
7680
for chunk in chunks {
7781
// Encode chunk
7882
let encoded_chunk = encode_to_vec(chunk, standard()).expect("Failed to encode chunk");
83+
84+
let compressed = bzip_compress(&encoded_chunk).expect("Failed to compress chunk");
7985
let key = hash((chunk.dimension.as_ref().unwrap(), chunk.x_pos, chunk.z_pos));
8086

8187
// Insert chunk
82-
database
83-
.put(&mut rw_tx, &key, &encoded_chunk)
84-
.map_err(|err| {
85-
Error::DatabaseError(format!("Failed to insert or update chunk: {err}"))
86-
})?;
88+
database.put(&mut rw_tx, &key, &compressed).map_err(|err| {
89+
Error::DatabaseError(format!("Failed to insert or update chunk: {err}"))
90+
})?;
8791
}
8892

8993
// Commit changes
@@ -237,8 +241,7 @@ impl Database {
237241
Ok(true)
238242
// Else check persistent database and load it into cache
239243
} else {
240-
let res = spawn_blocking(move || Self::get_chunk_from_database(&db, &key))
241-
.await?;
244+
let res = spawn_blocking(move || Self::get_chunk_from_database(&db, &key)).await?;
242245

243246
// WARNING: The previous logic was to order the chunk to be loaded into cache whether it existed or not.
244247
// This has been replaced by directly loading the queried chunk into cache
@@ -281,9 +284,7 @@ impl Database {
281284
// Insert new chunk state into persistent database
282285
let chunk = value.clone();
283286
let db = self.db.clone();
284-
spawn_blocking(move || Self::insert_chunk_into_database(&db, &chunk))
285-
.await
286-
.unwrap()?;
287+
spawn_blocking(move || Self::insert_chunk_into_database(&db, &chunk)).await??;
287288

288289
// Insert new chunk state into cache
289290
self.cache.insert(key, value).await;

0 commit comments

Comments
 (0)