Skip to content

Instantly share code, notes, and snippets.

@Bouncner
Created August 19, 2022 15:25
Show Gist options
  • Save Bouncner/bf348c4c23f824d42494d0a3a77a3883 to your computer and use it in GitHub Desktop.
Save Bouncner/bf348c4c23f824d42494d0a3a77a3883 to your computer and use it in GitHub Desktop.
diff --git a/src/lib/storage/lz4_segment.cpp b/src/lib/storage/lz4_segment.cpp
index 7fa821329..cd675c2b8 100644
--- a/src/lib/storage/lz4_segment.cpp
+++ b/src/lib/storage/lz4_segment.cpp
@@ -333,7 +333,9 @@ std::pair<pmr_string, size_t> LZ4Segment<pmr_string>::decompress(const ChunkOffs
* If the previously decompressed block was a different block than the one accessed now, overwrite it with the now
* decompressed block.
*/
- if (!cached_block_index || start_block != *cached_block_index) {
+ if (!cached_block_index) {
+ _decompress_block_to_bytes(start_block, cached_block);
+ } else if (start_block != *cached_block_index) {
_decompress_block_to_bytes(start_block, cached_block);
}
@@ -387,9 +389,24 @@ std::pair<pmr_string, size_t> LZ4Segment<pmr_string>::decompress(const ChunkOffs
for (auto block_index = start_block; block_index <= end_block; ++block_index) {
// Only decompress the current block if it's not cached.
- if (!(use_caching && block_index == *cached_block_index)) {
+
+ if (!use_caching) {
_decompress_block_to_bytes(block_index, cached_block);
new_cached_block_index = block_index;
+ } else {
+
+ if (cached_block_index) {
+ if (*cached_block_index >= start_block) {
+ if (*cached_block_index <= end_offset) {
+ if (block_index != *cached_block_index) {
+ _decompress_block_to_bytes(block_index, cached_block);
+ new_cached_block_index = block_index;
+ }
+ }
+ }
+ }
+
+
}
// Set the offset for the end of the string.
@@ -402,12 +419,24 @@ std::pair<pmr_string, size_t> LZ4Segment<pmr_string>::decompress(const ChunkOffs
* If the cached block is not the start block, the data is retrieved from the copy.
*/
auto partial_result = pmr_string{};
- if (use_caching && block_index == *cached_block_index && block_index != start_block) {
- const auto start_offset_it =
- cached_block_copy.cbegin() + static_cast<CachedBlockDifferenceType>(block_start_offset);
- const auto end_offset_it = cached_block_copy.cbegin() + static_cast<CachedBlockDifferenceType>(block_end_offset);
- partial_result = pmr_string{start_offset_it, end_offset_it};
- } else {
+ auto hit = false;
+ if (cached_block_index) {
+ if (*cached_block_index >= start_block) {
+ if (*cached_block_index <= end_offset) {
+ if (block_index == *cached_block_index) {
+ if (block_index != start_block) {
+ const auto start_offset_it =
+ cached_block_copy.cbegin() + static_cast<CachedBlockDifferenceType>(block_start_offset);
+ const auto end_offset_it = cached_block_copy.cbegin() + static_cast<CachedBlockDifferenceType>(block_end_offset);
+ partial_result = pmr_string{start_offset_it, end_offset_it};
+ hit = true;
+ }
+ }
+ }
+ }
+ }
+
+ if (!hit) {
const auto start_offset_it = cached_block.cbegin() + static_cast<CachedBlockDifferenceType>(block_start_offset);
const auto end_offset_it = cached_block.cbegin() + static_cast<CachedBlockDifferenceType>(block_end_offset);
partial_result = pmr_string{start_offset_it, end_offset_it};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment