@@ -9,6 +9,7 @@ use crate::utils::error::Error;
9
9
use crate :: utils:: hash:: hash;
10
10
use crate :: world:: chunkformat:: Chunk ;
11
11
12
+ use crate :: utils:: binary_utils:: { bzip_compress, bzip_decompress} ;
12
13
use bincode:: config:: standard;
13
14
use bincode:: { decode_from_slice, encode_to_vec} ;
14
15
@@ -25,7 +26,9 @@ impl Database {
25
26
// Attempt to fetch chunk from table
26
27
if let Ok ( data) = database. get ( & ro_tx, key) {
27
28
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 ( ) )
29
32
. expect ( "Failed to decode chunk from database" ) ;
30
33
chunk. 0
31
34
} ) )
@@ -45,10 +48,11 @@ impl Database {
45
48
46
49
// Encode chunk
47
50
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" ) ;
48
52
let key = hash ( ( chunk. dimension . as_ref ( ) . unwrap ( ) , chunk. x_pos , chunk. z_pos ) ) ;
49
53
50
54
// Insert chunk
51
- let res = database. put ( & mut rw_tx, & key, & encoded_chunk ) ;
55
+ let res = database. put ( & mut rw_tx, & key, & compressed ) ;
52
56
rw_tx. commit ( ) . map_err ( |err| {
53
57
Error :: DatabaseError ( format ! ( "Unable to commit changes to database: {err}" ) )
54
58
} ) ?;
@@ -76,14 +80,14 @@ impl Database {
76
80
for chunk in chunks {
77
81
// Encode chunk
78
82
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" ) ;
79
85
let key = hash ( ( chunk. dimension . as_ref ( ) . unwrap ( ) , chunk. x_pos , chunk. z_pos ) ) ;
80
86
81
87
// 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
+ } ) ?;
87
91
}
88
92
89
93
// Commit changes
@@ -237,8 +241,7 @@ impl Database {
237
241
Ok ( true )
238
242
// Else check persistent database and load it into cache
239
243
} 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 ?;
242
245
243
246
// WARNING: The previous logic was to order the chunk to be loaded into cache whether it existed or not.
244
247
// This has been replaced by directly loading the queried chunk into cache
@@ -281,9 +284,7 @@ impl Database {
281
284
// Insert new chunk state into persistent database
282
285
let chunk = value. clone ( ) ;
283
286
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 ??;
287
288
288
289
// Insert new chunk state into cache
289
290
self . cache . insert ( key, value) . await ;
0 commit comments