Skip to content

Instantly share code, notes, and snippets.

@chaudum
Last active September 23, 2024 13:53
Show Gist options
  • Save chaudum/df70d63c1da5537338a424040cfc141f to your computer and use it in GitHub Desktop.
Save chaudum/df70d63c1da5537338a424040cfc141f to your computer and use it in GitHub Desktop.
Binary format of Loki bloom blocks
# Binary format
## Tar Archive
+--------------------------------------------------------------------------------------------------------+
| Compression (optional) |
| +----------------------------------------------------------------------------------------------------+ |
| | Tar Archive | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | `series` file | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | `blooms` file | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
+--------------------------------------------------------------------------------------------------------+
## `series` file:
+--------------------------------------------------------------------------------------------------------+
| BlockIndex |
| +----------------------------------------------------------------------------------------------------+ |
| | BlockOptions | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | Schema | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | Magic Number (4 bytes) | Version (1 byte) | Encoding (1 byte) | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | SeriesPageSize (8 bytes) | BloomPageSize (8 bytes) | BlockSize (8 bytes) | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | Headers | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | Count (uvarint) | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | SeriesPageHeaderWithOffset (repeated) | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | Offset (uvarint) | Len (uvarint) | DecomressedLen (uvarint) | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | SeriesHeader | | | |
| | | | +----------------------------------------------------------------------------------------+ | | | |
| | | | | NumSeries (uvarint) | Bounds (uvarint, uvarint) | From (varint) | Through (varint) | | | | |
| | | | +----------------------------------------------------------------------------------------+ | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | Pages | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | SeriesWithMeta | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | Fingerprint (uvarint, delta-encoded) | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | NumOffsets (uvarint) | Offset (repeated) | ... | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | NumChunks (uvarint) | Chunk (repeated) | ... | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | NumFields (uvarint) | Field (repeated) | ... | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | HeaderOffset (8 bytes) | Checksum (4 bytes) | |
| +----------------------------------------------------------------------------------------------------+ |
+--------------------------------------------------------------------------------------------------------+
## `blooms` file:
+--------------------------------------------------------------------------------------------------------+
| BloomBlock |
| +----------------------------------------------------------------------------------------------------+ |
| | Schema | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | Magic Number (4 bytes) | Version (1 byte) | Encoding (1 byte) | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | Headers | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | Count (uvarint) | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | BloomPageHeader (repeated) | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | N (uvarint) | Offset (uvarint) | Len (uvarint) | DecomressedLen (uvarint) | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | Pages | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | Bloom | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | Len (uvarint) | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | | | ScalableBloomFilter | | | |
| | | | +----------------------------------------------------------------------------------------+ | | | |
| | | | | xxxx | | | | |
| | | | +----------------------------------------------------------------------------------------+ | | | |
| | | +--------------------------------------------------------------------------------------------+ | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| | | ... | | |
| | +------------------------------------------------------------------------------------------------+ | |
| +----------------------------------------------------------------------------------------------------+ |
| | HeaderOffset (8 bytes) | Checksum (4 bytes) | |
| +----------------------------------------------------------------------------------------------------+ |
+--------------------------------------------------------------------------------------------------------+
@chaudum
Copy link
Author

chaudum commented Sep 23, 2024

Appendix

  • Magic Number is not actually part of the Schema struct, but is encoded/decoded as part of it.
  • Headers and Pages are not an actual structs, but are containers for better visualisation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment