Skip to main content

geocoder_core/index/
format.rs

1//! Binary layout of an index file.
2//!
3//! ```text
4//! [0..8]   magic  b"GEOCODER"
5//! [8..12]  format version, u32 le
6//! [12..16] section count, u32 le
7//! [16..]   section headers: kind u8, offset u64 le, length u64 le
8//! [...]    sections, each aligned to 8 bytes
9//! ```
10//!
11//! Sections are read in place from the memory map, except the FSTs.
12//! Offsets tables hold `count + 1` little-endian u64, entry `i` spanning
13//! `offsets[i]..offsets[i + 1]`.
14
15use serde::{Deserialize, Serialize};
16
17pub const MAGIC: &[u8; 8] = b"GEOCODER";
18pub const VERSION: u32 = 2;
19pub const HEADER_SIZE: usize = 16;
20pub const SECTION_HEADER_SIZE: usize = 17;
21
22#[repr(u8)]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
24pub enum SectionKind {
25    /// JSON [`Meta`].
26    Meta = 0,
27    /// `fst::Map`: token → TokenId.
28    TokenFst = 1,
29    /// Offsets, in entries, into the two postings sections.
30    PostingOffsets = 2,
31    /// Per token, `(doc u32, boost f32)` sorted by doc.
32    PostingsByDoc = 3,
33    /// Per token, `doc u32` sorted like Redis `ZREVRANGE`.
34    PostingsByScore = 4,
35    /// Offsets, in bytes, into `PairBlobs`, per TokenId.
36    PairOffsets = 5,
37    /// Serialized RoaringBitmap<TokenId> of co-occurring tokens.
38    PairBlobs = 6,
39    /// `fst::Map`: edge ngram → entry, then offsets and bitmaps of TokenId.
40    NgramFst = 7,
41    NgramOffsets = 8,
42    NgramBlobs = 9,
43    /// `fst::Map`: geohash → entry, then offsets and bitmaps of DocId.
44    GeohashFst = 10,
45    GeohashOffsets = 11,
46    GeohashBlobs = 12,
47    /// `fst::Map`: `key|value` → entry, then offsets and bitmaps of DocId.
48    FilterFst = 13,
49    FilterOffsets = 14,
50    FilterBlobs = 15,
51    /// Offsets, in bytes, into `DocBlobs`, per DocId.
52    DocOffsets = 16,
53    /// zlib-compressed JSON documents.
54    DocBlobs = 17,
55}
56
57impl SectionKind {
58    pub const ALL: [SectionKind; 18] = [
59        Self::Meta,
60        Self::TokenFst,
61        Self::PostingOffsets,
62        Self::PostingsByDoc,
63        Self::PostingsByScore,
64        Self::PairOffsets,
65        Self::PairBlobs,
66        Self::NgramFst,
67        Self::NgramOffsets,
68        Self::NgramBlobs,
69        Self::GeohashFst,
70        Self::GeohashOffsets,
71        Self::GeohashBlobs,
72        Self::FilterFst,
73        Self::FilterOffsets,
74        Self::FilterBlobs,
75        Self::DocOffsets,
76        Self::DocBlobs,
77    ];
78}
79
80impl TryFrom<u8> for SectionKind {
81    type Error = u8;
82    fn try_from(v: u8) -> Result<Self, u8> {
83        Self::ALL.iter().copied().find(|k| *k as u8 == v).ok_or(v)
84    }
85}
86
87#[derive(Debug, Clone, Copy)]
88pub struct SectionHeader {
89    pub kind: SectionKind,
90    pub offset: u64,
91    pub length: u64,
92}
93
94/// What an index was built with.
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Meta {
97    pub profile: String,
98    pub profile_version: u32,
99    pub documents: u32,
100    pub tokens: u32,
101}