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    /// Optional: [`CedexTable`](crate::postal::CedexTable). Readers that
56    /// do not know it skip it, so its absence or presence needs no new
57    /// format version.
58    Postal = 18,
59}
60
61impl SectionKind {
62    /// Sections an index may lack.
63    pub const OPTIONAL: [SectionKind; 1] = [Self::Postal];
64
65    pub const ALL: [SectionKind; 19] = [
66        Self::Meta,
67        Self::TokenFst,
68        Self::PostingOffsets,
69        Self::PostingsByDoc,
70        Self::PostingsByScore,
71        Self::PairOffsets,
72        Self::PairBlobs,
73        Self::NgramFst,
74        Self::NgramOffsets,
75        Self::NgramBlobs,
76        Self::GeohashFst,
77        Self::GeohashOffsets,
78        Self::GeohashBlobs,
79        Self::FilterFst,
80        Self::FilterOffsets,
81        Self::FilterBlobs,
82        Self::DocOffsets,
83        Self::DocBlobs,
84        Self::Postal,
85    ];
86}
87
88impl TryFrom<u8> for SectionKind {
89    type Error = u8;
90    fn try_from(v: u8) -> Result<Self, u8> {
91        Self::ALL.iter().copied().find(|k| *k as u8 == v).ok_or(v)
92    }
93}
94
95#[derive(Debug, Clone, Copy)]
96pub struct SectionHeader {
97    pub kind: SectionKind,
98    pub offset: u64,
99    pub length: u64,
100}
101
102/// What an index was built with.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct Meta {
105    pub profile: String,
106    pub profile_version: u32,
107    pub documents: u32,
108    pub tokens: u32,
109}