Skip to content

Commit 73ac62a

Browse files
committed
storage: extend BlobChunkInfoOndisk struct
Signed-off-by: Yan Song <yansong.ys@antfin.com>
1 parent c65f2c1 commit 73ac62a

File tree

1 file changed

+93
-18
lines changed

1 file changed

+93
-18
lines changed

storage/src/meta/mod.rs

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ const BLOB_METADATA_MAX_SIZE: u64 = 0x100_0000u64;
3434
const BLOB_METADTAT_HEADER_SIZE: u64 = 0x1000u64;
3535
const BLOB_METADATA_RESERVED_SIZE: u64 = BLOB_METADTAT_HEADER_SIZE - 44;
3636
const BLOB_METADATA_MAGIC: u32 = 0xb10bb10bu32;
37-
const BLOB_CHUNK_COMP_OFFSET_MASK: u64 = 0xfff_ffff_ffff;
37+
const BLOB_CHUNK_COMP_OFFSET_MASK: u64 = 0xff_ffff_ffff;
3838
const BLOB_CHUNK_UNCOMP_OFFSET_MASK: u64 = 0xfff_ffff_f000;
3939
const BLOB_CHUNK_SIZE_MASK: u64 = 0xf_ffff;
4040
const BLOB_CHUNK_SIZE_SHIFT: u64 = 44;
41+
const BLOB_CHUNK_SIZE_SHIFT_LEFT: u64 = 12;
4142
const FILE_SUFFIX: &str = "blob.meta";
4243
pub const BLOB_FEATURE_4K_ALIGNED: u32 = 0x1;
4344

@@ -195,16 +196,26 @@ impl BlobChunkInfoOndisk {
195196
/// Get compressed size of the chunk.
196197
#[inline]
197198
pub fn compressed_size(&self) -> u32 {
198-
(self.comp_info >> BLOB_CHUNK_SIZE_SHIFT) as u32 + 1
199+
// (self.comp_info >> BLOB_CHUNK_SIZE_SHIFT) as u32 + 1
200+
201+
let bit20 = self.comp_info >> BLOB_CHUNK_SIZE_SHIFT;
202+
let bit4 = (self.comp_info & 0xf0000000000) >> 20;
203+
(bit4 | bit20) as u32 + 1
199204
}
200205

201206
/// Set compressed size of the chunk.
202207
#[inline]
203208
pub fn set_compressed_size(&mut self, size: u32) {
204209
let size = size as u64;
205-
debug_assert!(size > 0 && size <= BLOB_CHUNK_SIZE_MASK + 1);
206-
self.comp_info &= !(BLOB_CHUNK_SIZE_MASK << BLOB_CHUNK_SIZE_SHIFT);
207-
self.comp_info |= ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
210+
// debug_assert!(size > 0 && size <= BLOB_CHUNK_SIZE_MASK + 1);
211+
// self.comp_info &= !(BLOB_CHUNK_SIZE_MASK << BLOB_CHUNK_SIZE_SHIFT);
212+
// self.comp_info |= ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
213+
214+
let bit20 = ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
215+
let bit4 = ((size - 1) & 0xf0_0000) << 20;
216+
let bit40 = self.comp_info & BLOB_CHUNK_COMP_OFFSET_MASK;
217+
218+
self.comp_info = bit20 | bit4 | bit40;
208219
}
209220

210221
/// Get compressed end of the chunk.
@@ -230,16 +241,24 @@ impl BlobChunkInfoOndisk {
230241
/// Get uncompressed end of the chunk.
231242
#[inline]
232243
pub fn uncompressed_size(&self) -> u32 {
233-
(self.uncomp_info >> BLOB_CHUNK_SIZE_SHIFT) as u32 + 1
244+
let bit4 = (self.uncomp_info & 0xf00) << BLOB_CHUNK_SIZE_SHIFT_LEFT;
245+
let bit20 = self.uncomp_info >> BLOB_CHUNK_SIZE_SHIFT;
246+
(bit4 | bit20) as u32 + 1
234247
}
235248

236249
/// Set uncompressed end of the chunk.
237250
#[inline]
238251
pub fn set_uncompressed_size(&mut self, size: u32) {
239252
let size = size as u64;
240-
debug_assert!(size != 0 && size <= BLOB_CHUNK_SIZE_MASK + 1);
241-
self.uncomp_info &= !(BLOB_CHUNK_SIZE_MASK << BLOB_CHUNK_SIZE_SHIFT);
242-
self.uncomp_info |= ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
253+
// debug_assert!(size != 0 && size <= BLOB_CHUNK_SIZE_MASK + 1);
254+
// self.uncomp_info &= !(BLOB_CHUNK_SIZE_MASK << BLOB_CHUNK_SIZE_SHIFT);
255+
// self.uncomp_info |= ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
256+
257+
let bit20 = ((size - 1) & BLOB_CHUNK_SIZE_MASK) << BLOB_CHUNK_SIZE_SHIFT;
258+
let bit32 = self.uncomp_info & BLOB_CHUNK_UNCOMP_OFFSET_MASK;
259+
let bit12 = ((size - 1) & 0xf0_0000) >> BLOB_CHUNK_SIZE_SHIFT_LEFT;
260+
261+
self.uncomp_info = bit20 | bit32 | bit12;
243262
}
244263

245264
/// Get uncompressed size of the chunk.
@@ -808,22 +827,78 @@ mod tests {
808827
assert_eq!(chunk.uncompressed_size(), 1);
809828
assert_eq!(chunk.aligned_uncompressed_end(), 0x1000);
810829

830+
// chunk.set_compressed_offset(0x1000);
831+
// chunk.set_compressed_size(0x100);
832+
// assert_eq!(chunk.compressed_offset(), 0x1000);
833+
// assert_eq!(chunk.compressed_size(), 0x100);
834+
// assert_eq!(chunk.compressed_end(), 0x1100);
835+
// chunk.set_uncompressed_offset(0x2000);
836+
// chunk.set_uncompressed_size(0x100);
837+
// assert_eq!(chunk.uncompressed_offset(), 0x2000);
838+
// assert_eq!(chunk.uncompressed_size(), 0x100);
839+
// assert_eq!(chunk.uncompressed_end(), 0x2100);
840+
// assert_eq!(chunk.aligned_uncompressed_end(), 0x3000);
841+
// assert!(!chunk.is_compressed());
842+
843+
// chunk.set_uncompressed_size(0x200);
844+
// assert_eq!(chunk.uncompressed_size(), 0x200);
845+
// assert!(chunk.is_compressed());
846+
847+
// chunk.set_uncompressed_size(0x100000);
848+
// assert_eq!(chunk.uncompressed_size(), 0x100000);
849+
850+
// chunk.set_uncompressed_size(0x100000 + 0x100);
851+
// assert_eq!(chunk.uncompressed_size(), 0x100000 + 0x100);
852+
811853
chunk.set_compressed_offset(0x1000);
854+
assert_eq!(chunk.compressed_offset(), 0x1000);
855+
812856
chunk.set_compressed_size(0x100);
857+
assert_eq!(chunk.compressed_size(), 0x100);
858+
859+
chunk.set_compressed_offset(0x1000);
813860
assert_eq!(chunk.compressed_offset(), 0x1000);
861+
862+
chunk.set_compressed_size(0x100);
814863
assert_eq!(chunk.compressed_size(), 0x100);
815-
assert_eq!(chunk.compressed_end(), 0x1100);
816-
chunk.set_uncompressed_offset(0x2000);
864+
865+
chunk.set_uncompressed_offset(0x1000);
866+
assert_eq!(chunk.uncompressed_offset(), 0x1000);
867+
817868
chunk.set_uncompressed_size(0x100);
818-
assert_eq!(chunk.uncompressed_offset(), 0x2000);
819869
assert_eq!(chunk.uncompressed_size(), 0x100);
820-
assert_eq!(chunk.uncompressed_end(), 0x2100);
821-
assert_eq!(chunk.aligned_uncompressed_end(), 0x3000);
822-
assert!(!chunk.is_compressed());
823870

824-
chunk.set_uncompressed_size(0x200);
825-
assert_eq!(chunk.uncompressed_size(), 0x200);
826-
assert!(chunk.is_compressed());
871+
chunk.set_uncompressed_offset(0x1000);
872+
assert_eq!(chunk.uncompressed_offset(), 0x1000);
873+
874+
chunk.set_uncompressed_size(0x100);
875+
assert_eq!(chunk.uncompressed_size(), 0x100);
876+
877+
//
878+
879+
chunk.set_compressed_offset(0x100000 + 0x100);
880+
assert_eq!(chunk.compressed_offset(), 0x100000 + 0x100);
881+
882+
chunk.set_compressed_size(0x100000 + 0x100);
883+
assert_eq!(chunk.compressed_size(), 0x100000 + 0x100);
884+
885+
chunk.set_compressed_offset(0x100000 + 0x100);
886+
assert_eq!(chunk.compressed_offset(), 0x100000 + 0x100);
887+
888+
chunk.set_compressed_size(0x100000 + 0x100);
889+
assert_eq!(chunk.compressed_size(), 0x100000 + 0x100);
890+
891+
chunk.set_uncompressed_offset(0x100000 + 0x100);
892+
assert_eq!(chunk.uncompressed_offset(), 0x100000);
893+
894+
chunk.set_uncompressed_size(0x100000 + 0x100);
895+
assert_eq!(chunk.uncompressed_size(), 0x100000 + 0x100);
896+
897+
chunk.set_uncompressed_offset(0x100000 + 0x100);
898+
assert_eq!(chunk.uncompressed_offset(), 0x100000);
899+
900+
chunk.set_uncompressed_size(0x100000 + 0x100);
901+
assert_eq!(chunk.uncompressed_size(), 0x100000 + 0x100);
827902
}
828903

829904
#[test]

0 commit comments

Comments
 (0)