Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Improve logging for decompression errors

Daoud Clarke3230149c5858
diff --git a/analyse/decompress_errors.py b/analyse/decompress_errors.py
new file mode 100644
index 0000000..231c3cf
--- /dev/null
+++ b/analyse/decompress_errors.py
@@ -0,0 +1,37 @@
+"""
+Create some data, compress it, randomly corrupt it, and then decompress it.
+"""
+from collections import defaultdict
+from random import Random
+
+from zstandard import ZstdCompressor, ZstdDecompressor
+
+random = Random(1)
+
+
+def test_compress_decompress():
+ error_type_counts = defaultdict(int)
+ for i in range(10000):
+ compressor = ZstdCompressor()
+ decompressor = ZstdDecompressor()
+ random_string = (''.join(random.choice('abcdefghijklmnopqrstuvwxyz') for _ in range(4096))).encode('utf8')
+ compressed_data = compressor.compress(random_string)
+
+ # Corrupt the data
+ compressed_data_array = bytearray(compressed_data)
+
+ # Corrupt some random bytes
+ for _ in range(10):
+ compressed_data_array[random.randint(0, len(compressed_data_array) - 1)] = random.randint(0, 255)
+
+ corrupted_data = bytes(compressed_data_array)
+
+ try:
+ decompressed_data = decompressor.decompress(corrupted_data)
+ except Exception as e:
+ error_type_counts[f"{type(e)}: {str(e)}"] += 1
+ continue
+ error_type_counts["none"] += 1
+ print("Counts")
+ for error_type, count in error_type_counts.items():
+ print(f"{error_type}: {count}")
diff --git a/mwmbl/tinysearchengine/indexer.py b/mwmbl/tinysearchengine/indexer.py
index 2b6418c..8b00655 100644
--- a/mwmbl/tinysearchengine/indexer.py
+++ b/mwmbl/tinysearchengine/indexer.py
@@ -193,8 +193,8 @@ class TinyIndex(Generic[T]):
page_data = self.mmap[i * self.page_size + METADATA_SIZE:(i + 1) * self.page_size + METADATA_SIZE]
try:
decompressed_data = self.decompressor.decompress(page_data)
- except ZstdError:
- logger.exception(f"Error decompressing page data, content: {page_data}")
+ except ZstdError as e:
+ logger.exception(f"Error decompressing page {i}: {e}")
return []
return json.loads(decompressed_data.decode('utf8'))