factbook 0.1.3

A high-speed, self-maintaining fact enricher for data at scale. Point it at reference data and it fetches, verifies, indexes and serves it, keeping itself current. IP geolocation is the batteries-included case.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
// Project:   factbook
// File:      src/table/mod.rs
// Purpose:   Arbitrary tabular sources: fetch, index, look up by key
// Language:  Rust
//
// License:   Apache-2.0
// Copyright: (c) 2026 HYPERI PTY LIMITED

//! A table fetched from wherever the operator names, keyed by whatever reaches
//! its rows.
//!
//! The geo half of this crate is opinionated on purpose: it knows what a city
//! database is and what an ASN database is, and it can only fetch the providers
//! that publish them. Plenty of useful reference data is neither -- an ASN-keyed
//! side table, a JSON list of relays, an internal CSV of customer prefixes --
//! and the only thing those have in common with a GeoLite2 build is how they
//! are acquired.
//!
//! So acquisition is shared and interpretation is stated. A [`TableSource`] is
//! config: a URL, a file name, an encoding, where the column names come from,
//! and which key reaches a row. Everything the geo download path does applies
//! to it unchanged -- the published digest is verified, a body that turns out
//! to be a login page is refused, a replacement a fraction of the size of the
//! copy on disk is refused, and a refusal leaves that copy exactly where it is.
//!
//! # Two surfaces, not one
//!
//! [`GeoIp`](crate::geoip::GeoIp) answers `lookup(ip)`, which a table keyed by
//! an autonomous system number cannot. [`Table`] is the generic surface: it
//! knows about columns and keys and nothing about geography.
//!
//! # A table is held in memory, or it is refused
//!
//! [`Table::ensure`] reads the source into memory and serves it from there. A
//! source whose rows and index outgrow `resident_max_bytes` while they are built
//! is abandoned -- what it had taken is released, and the call fails with
//! [`TableError::OverResidentCeiling`] rather than holding a table the
//! deployment said it did not want held.
//!
//! The ceiling is hit, never predicted. Whether a source fits is not knowable
//! from its size on disk: an encoding, a quoting style and a column count all
//! move the answer.
//!
//! Refusing is the whole of it. Writing an oversized source out as a MaxMind DB
//! and serving it from there is future work, tracked as issue #2.
//!
//! # One key, or several conditions
//!
//! [`Table::get`] and [`Table::all`] answer the enrichment question -- what is
//! filed under this key -- in a hash lookup and a hand-back. That is the common
//! case and the fast one.
//!
//! A host whose lookups arrive as condition objects asks something else: the
//! rows where country is `AU` and status is `active`. [`Table::find`] takes a
//! [`Query`] of those conditions, and an equality on the indexed column still
//! reaches the index rather than walking the table.
//!
//! # Example
//!
//! ```yaml
//! # Fetch a CSV and reach its rows by autonomous system number.
//! url: https://example.net/networks.csv
//! checksum_url: https://example.net/networks.csv.sha256
//! file: networks.csv
//! format: csv
//! index:
//!   column: asn
//! ```
//!
//! ```rust,no_run
//! use factbook::geoip::AutoDownloadConfig;
//! use factbook::table::{Index, Table, TableFormat, TableSource};
//!
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let source = TableSource::new(
//!     "https://example.net/networks.csv",
//!     "networks.csv",
//!     TableFormat::Csv { header: true },
//!     Index::Column("asn".to_string()),
//! );
//!
//! let table = Table::ensure(&source, &AutoDownloadConfig::default()).await?;
//! if let Some(row) = table.get("13335") {
//!     println!("{:?}", row.get("name"));
//! }
//! # Ok(())
//! # }
//! ```

mod config;
mod parse;
mod query;

use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::fmt;
use std::fs;
use std::hash::Hash;
use std::io::{self, BufRead, BufReader};
use std::net::IpAddr;
use std::ops::Range;
use std::path::{Path, PathBuf};
use std::slice;
use std::time::Duration;

use ipnet::IpNet;
use prefix_trie::joint::JointPrefixMap;
use tracing::{debug, warn};

use crate::geoip::AutoDownloadConfig;
use crate::geoip::DatabaseFormat;
use crate::geoip::download::fetch::{self, Archive, Credential, Transfer};
use crate::geoip::download::verify::Guard;
use crate::geoip::download::{self, Attempt, GeoIpDownloadError, SECS_PER_DAY, is_fresh};
use parse::Parsed;

pub use config::{Index, Schema, SourceArchive, TableFormat, TableSource};
pub(crate) use parse::probe;
pub use query::{Condition, Fields, Matched, Matches, Query};

/// Column names an [`Index::Ip`] source is likely to have written, lowercase.
const ADDRESS_COLUMNS: [&str; 5] = ["ip", "ip_address", "ipaddress", "address", "addr"];

/// Column names a CIDR-keyed source is likely to have written, lowercase.
const PREFIX_COLUMNS: [&str; 5] = ["prefix", "network", "cidr", "range", "subnet"];

/// Rows sampled when working out which column holds addresses.
const ADDRESS_SAMPLE: usize = 64;

/// No row is filed under this key.
const NO_ROWS: &[usize] = &[];

/// Bytes one new range costs the prefix index: its trie node, and the branch
/// node a binary trie adds at most once per leaf.
const PREFIX_ENTRY_BYTES: usize =
    2 * size_of::<(IpNet, Option<Vec<usize>>, Option<usize>, Option<usize>)>();

/// One cell of a row. `None` is a value the source did not supply.
type Cell = Option<Box<str>>;

/// Errors raised while provisioning or reading a table.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum TableError {
    /// The file could not be fetched.
    #[error("fetching the table failed: {0}")]
    Fetch(#[from] GeoIpDownloadError),

    /// A filesystem operation failed.
    #[error("IO error: {0}")]
    Io(#[from] io::Error),

    /// The source names somewhere the transfer cannot go.
    #[error("{url} is not a URL that can be fetched: {detail}")]
    BadUrl {
        /// URL as the operator wrote it, minus its userinfo and query values.
        url: String,
        /// What the URL parser refused it for.
        detail: String,
    },

    /// The file name would write outside the data directory.
    #[error("file {file} is not a plain file name")]
    UnsafeFileName {
        /// Name as the operator wrote it.
        file: String,
    },

    /// The file is not the shape its format states.
    #[error("the table is malformed at line {line}: {detail}")]
    Malformed {
        /// Line the record that was refused started on.
        line: usize,
        /// What the reader refused it for.
        detail: String,
    },

    /// The document is not a JSON array of objects.
    #[error("the source is not a JSON array of objects: {detail}")]
    NotAnArrayOfObjects {
        /// What the reader refused it for.
        detail: String,
    },

    /// A CSV with no header row supplies no column names.
    #[error("a CSV with no header row needs schema.named to supply the column names")]
    NamesRequired,

    /// The stated schema names no columns.
    #[error("schema.named names no columns")]
    NoNames,

    /// The file holds no rows.
    #[error("the source holds no rows")]
    Empty,

    /// The index names a column the source does not have.
    #[error("the index names column {column}, which is not one of: {columns}")]
    UnknownColumn {
        /// Column the index asked for.
        column: String,
        /// Columns the source actually has.
        columns: String,
    },

    /// No column holds IP addresses.
    #[error("no column holds IP addresses; name one with index.column instead. columns: {columns}")]
    NoAddressColumn {
        /// Columns the source actually has.
        columns: String,
    },

    /// No column holds CIDR ranges.
    #[error("no column holds CIDR ranges; name one with index.column instead. columns: {columns}")]
    NoPrefixColumn {
        /// Columns the source actually has.
        columns: String,
    },

    /// The rows, or the rows and their index, outgrew the memory the source
    /// was given.
    ///
    /// The read stops here rather than finishing: a source too large for
    /// `resident_max_bytes` is refused, and there is nowhere else for it to go.
    /// Writing one out as a database and serving it from there is future work.
    #[error(
        "the table outgrew the {ceiling}-byte resident ceiling; raise \
         auto_download.resident_max_bytes or point the source at less data"
    )]
    OverResidentCeiling {
        /// Ceiling the read was given, in bytes.
        ceiling: u64,
    },
}

/// Where a table's rows are held.
///
/// One backing today. A source that will not fit in memory is refused rather
/// than moved anywhere else, so this reports rather than chooses -- and it is
/// what a metrics recorder reads to see that a table loaded at all.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum TableBacking {
    /// Read into memory when the table was loaded.
    Resident,
}

impl TableBacking {
    /// Label used in metric series and log fields.
    #[must_use]
    pub const fn label(self) -> &'static str {
        match self {
            Self::Resident => "resident",
        }
    }
}

/// How the rows of a table are reached.
///
/// The address and prefix kinds carry the rows their index could not file --
/// a key cell that is there and does not parse. Those rows are unreachable by
/// address and still reachable by the text they hold, so a
/// [`Query`] narrowed by the index walks them as well.
#[derive(Debug)]
enum Keys {
    /// Filed under the text of a column.
    Text(HashMap<Box<str>, Vec<usize>>),

    /// Filed under a parsed address.
    Address {
        /// Rows, by the address their key cell parsed to.
        filed: HashMap<IpAddr, Vec<usize>>,

        /// Rows whose key cell is there and is not an address.
        unindexed: Vec<usize>,
    },

    /// Filed under a CIDR range, reached by the longest one containing an
    /// address.
    Prefix {
        /// Rows, by the range their key cell parsed to.
        filed: Box<JointPrefixMap<IpNet, Vec<usize>>>,

        /// Rows whose key cell is there and is not a range.
        unindexed: Vec<usize>,
    },
}

/// A tabular source, in memory and indexed.
///
/// Rows are kept whole and in file order; the index is a second view of them,
/// so a key that appears twice keeps both rows rather than one silently
/// replacing the other.
///
/// ```
/// use factbook::table::{Index, Schema, Table, TableFormat};
///
/// let csv = "asn,name\n13335,CLOUDFLARENET\n15169,GOOGLE\n";
/// let table = Table::from_reader(
///     csv.as_bytes(),
///     TableFormat::Csv { header: true },
///     &Schema::Auto,
///     &Index::Column("asn".to_string()),
/// )?;
///
/// assert_eq!(table.len(), 2);
/// assert_eq!(table.get("13335").unwrap().get("name"), Some("CLOUDFLARENET"));
/// assert!(table.get("64496").is_none());
/// # Ok::<(), factbook::table::TableError>(())
/// ```
#[derive(Debug)]
pub struct Table {
    /// Column names, in the order a row's cells are stored.
    columns: Vec<String>,

    /// Column the index is built on.
    key_column: usize,

    /// Every row read, in file order.
    rows: Vec<Vec<Cell>>,

    /// Where a key leads.
    keys: Keys,
}

impl Table {
    /// Ensure the source is on disk, downloading when the local copy is missing
    /// or stale, then read it.
    ///
    /// Downloading is non-fatal by the same contract as the geo path: a
    /// transfer that fails over a copy already on disk logs a warning and reads
    /// that copy, because a stale table answers most lookups and no table
    /// answers none. A refusal or a rate limit starts the same cool-down the geo
    /// path keeps, and the copy on disk serves until it passes.
    ///
    /// Reading is not. The rows and their index are held to
    /// `auto.resident_max_bytes`, and a source that outgrows it is abandoned
    /// mid-read or mid-index and refused -- there is
    /// nowhere else for a table to be served from, so a ceiling it crosses is
    /// the end of the load rather than a switch to something slower.
    ///
    /// The read and the index build run on the blocking pool, so a large
    /// source holds no runtime worker while it loads.
    ///
    /// # Errors
    ///
    /// [`TableError::Fetch`] when the download fails or is cooling down and
    /// there is no local copy to fall back to,
    /// [`TableError::OverResidentCeiling`] when the source will not fit, or any
    /// of the read errors of [`Table::load`].
    pub async fn ensure(
        source: &TableSource,
        auto: &AutoDownloadConfig,
    ) -> Result<Self, TableError> {
        source.validate()?;

        let dest = auto.data_dir.join(&source.file);

        if !auto.enabled {
            debug!(file = %dest.display(), "auto-download is off, reading what is on disk");
        } else if is_fresh(&dest, u64::from(auto.max_age_days) * SECS_PER_DAY) {
            debug!(file = %dest.display(), "table is fresh");
        } else {
            download::check_settings(auto, source.http_client.is_some())?;
            let client = fetch::client(
                source.http_client.as_ref(),
                Duration::from_secs(auto.connect_timeout_secs),
                Duration::from_secs(auto.read_timeout_secs),
            )?;

            let transfer = transfer(source, dest.clone());
            let guard = Guard::for_table(source.format, &source.schema, &source.index, auto);

            let asked = download::fingerprint(std::slice::from_ref(&transfer));
            let fetched = download::attempt("table", Some(&dest), asked, || {
                transfer.run_guarded(&client, guard)
            })
            .await;
            match fetched {
                Attempt::Ran(Ok(_)) => {}
                Attempt::Ran(Err(e)) if dest.exists() => warn!(
                    file = %dest.display(),
                    error = %e,
                    "table download failed, reading the copy already on disk"
                ),
                Attempt::Ran(Err(e)) => return Err(e.into()),
                Attempt::Waiting(_) if dest.exists() => {}
                Attempt::Waiting(wait) => {
                    return Err(TableError::Fetch(GeoIpDownloadError::CoolingDown {
                        path: dest.display().to_string(),
                        wait_secs: wait.as_secs(),
                    }));
                }
            }
        }

        let (format, schema, index) = (source.format, source.schema.clone(), source.index.clone());
        let ceiling = auto.resident_max_bytes;
        let table = tokio::task::spawn_blocking(move || {
            Self::read_file(&dest, format, &schema, &index, ceiling)
        })
        .await
        .map_err(io::Error::from)??;

        telemetry::backing(&source.file, table.backing());
        Ok(table)
    }

    /// Read and index the file at `path`, refusing past `ceiling` bytes.
    /// Blocking: it reads, parses and indexes the whole file.
    pub(crate) fn read_file(
        path: &Path,
        format: TableFormat,
        schema: &Schema,
        index: &Index,
        ceiling: u64,
    ) -> Result<Self, TableError> {
        #[cfg(test)]
        crate::geoip::download::testkit::checkpoint(path);

        let parsed = parse::read(
            BufReader::new(fs::File::open(path)?),
            format,
            schema,
            ceiling,
        )?;
        Self::build(parsed, index, ceiling)
    }

    /// Read a table off disk.
    ///
    /// Held in memory whatever its size. The ceiling belongs to
    /// [`Table::ensure`], which is the call that reads the setting.
    ///
    /// # Errors
    ///
    /// [`TableError::Io`] when the file cannot be read, and the reader's own
    /// errors -- [`TableError::Malformed`], [`TableError::NotAnArrayOfObjects`],
    /// [`TableError::NamesRequired`], [`TableError::Empty`] -- when its contents
    /// are not the table the source states.
    pub fn load(
        path: &Path,
        format: TableFormat,
        schema: &Schema,
        index: &Index,
    ) -> Result<Self, TableError> {
        Self::read_file(path, format, schema, index, u64::MAX)
    }

    /// Read a table from bytes already in hand.
    ///
    /// # Errors
    ///
    /// The errors of [`Table::load`], other than the ones that come from
    /// opening a file.
    pub fn from_reader(
        reader: impl BufRead,
        format: TableFormat,
        schema: &Schema,
        index: &Index,
    ) -> Result<Self, TableError> {
        Self::build(
            parse::read(reader, format, schema, u64::MAX)?,
            index,
            u64::MAX,
        )
    }

    /// Column names, in the order a row's cells are stored.
    #[must_use]
    pub fn columns(&self) -> &[String] {
        &self.columns
    }

    /// Column the rows are indexed by.
    ///
    /// Worth reporting because [`Index::Ip`] locates its own column, so this is
    /// how a deployment sees which one it settled on.
    #[must_use]
    pub fn key_column(&self) -> &str {
        &self.columns[self.key_column]
    }

    /// Whether rows are reached by an address or by the text of a column.
    ///
    /// A lookup that does not match answers nothing rather than failing, so a
    /// source whose `index` was edited from a column to a prefix builds, reports
    /// its full [`len`](Self::len), and returns `None` from every
    /// [`get`](Self::get). This is how a caller tells the two apart.
    #[must_use]
    pub const fn keyed_by_address(&self) -> bool {
        matches!(self.keys, Keys::Address { .. } | Keys::Prefix { .. })
    }

    /// Where the rows are held.
    ///
    /// One answer today, and reported rather than configured. The same answer
    /// reaches a metrics recorder as `enrichment_table_backing`.
    #[must_use]
    pub const fn backing(&self) -> TableBacking {
        TableBacking::Resident
    }

    /// How many rows the table holds.
    #[must_use]
    pub fn len(&self) -> usize {
        self.rows.len()
    }

    /// Whether the table holds no rows.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Every row, in file order.
    #[must_use]
    pub fn rows(&self) -> Rows<'_> {
        Rows {
            table: self,
            positions: Positions::All(0..self.rows.len()),
        }
    }

    /// The first row filed under a text key.
    ///
    /// Answers nothing on a table keyed by address, which
    /// [`keyed_by_address`](Self::keyed_by_address) reports.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<Row<'_>> {
        self.positions_of(key).first().map(|&at| self.row(at))
    }

    /// Every row filed under a text key.
    #[must_use]
    pub fn all(&self, key: &str) -> Rows<'_> {
        Rows {
            table: self,
            positions: Positions::Listed(self.positions_of(key).iter()),
        }
    }

    /// The first row filed under an address.
    ///
    /// Answers nothing on a table keyed by a column, which
    /// [`keyed_by_address`](Self::keyed_by_address) reports.
    #[must_use]
    pub fn get_by_address(&self, address: IpAddr) -> Option<Row<'_>> {
        self.positions_at(address).first().map(|&at| self.row(at))
    }

    /// Every row filed under an address.
    #[must_use]
    pub fn all_by_address(&self, address: IpAddr) -> Rows<'_> {
        Rows {
            table: self,
            positions: Positions::Listed(self.positions_at(address).iter()),
        }
    }

    /// Index parsed rows by the key the source states, refusing once the rows,
    /// the column names and the index together cost more than `ceiling` bytes.
    fn build(parsed: Parsed, index: &Index, ceiling: u64) -> Result<Self, TableError> {
        let Parsed {
            columns,
            mut rows,
            held,
        } = parsed;

        // The parse charged one slot per row, not the capacity the vector
        // doubled into.
        rows.shrink_to_fit();
        let mut budget = Budget { held, ceiling };
        for column in &columns {
            budget.charge(column.len() + size_of::<String>())?;
        }

        let key_column = match index {
            Index::Column(name) => columns
                .iter()
                .position(|column| column == name)
                .ok_or_else(|| TableError::UnknownColumn {
                    column: name.clone(),
                    columns: columns.join(", "),
                })?,
            Index::Ip => keyed_column(&columns, &rows, &ADDRESS_COLUMNS, |text| {
                as_address(text).is_some()
            })
            .ok_or_else(|| TableError::NoAddressColumn {
                columns: columns.join(", "),
            })?,
            Index::Prefix => keyed_column(&columns, &rows, &PREFIX_COLUMNS, |text| {
                as_prefix(text).is_some()
            })
            .ok_or_else(|| TableError::NoPrefixColumn {
                columns: columns.join(", "),
            })?,
        };

        let mut reachable = 0;
        let keys = match index {
            Index::Column(_) => {
                let mut filed: HashMap<Box<str>, Vec<usize>> = HashMap::new();
                for (at, row) in rows.iter().enumerate() {
                    if let Some(key) = cell(row, key_column) {
                        budget.file(&mut filed, Box::from(key), key.len(), at)?;
                        reachable += 1;
                    }
                }
                Keys::Text(filed)
            }
            Index::Ip => {
                let mut filed: HashMap<IpAddr, Vec<usize>> = HashMap::new();
                let mut unindexed = Vec::new();
                for (at, row) in rows.iter().enumerate() {
                    let Some(text) = cell(row, key_column) else {
                        continue;
                    };
                    if let Some(address) = as_address(text) {
                        budget.file(&mut filed, address, 0, at)?;
                        reachable += 1;
                    } else {
                        budget.push(&mut unindexed, at)?;
                    }
                }
                Keys::Address { filed, unindexed }
            }
            Index::Prefix => {
                let mut filed: JointPrefixMap<IpNet, Vec<usize>> = JointPrefixMap::default();
                let mut unindexed = Vec::new();
                for (at, row) in rows.iter().enumerate() {
                    let Some(text) = cell(row, key_column) else {
                        continue;
                    };
                    if let Some(prefix) = as_prefix(text) {
                        // A repeated range keeps every row, the same as a
                        // repeated text key does.
                        if let Some(list) = filed.get_mut(&prefix) {
                            budget.push(list, at)?;
                        } else {
                            budget.charge(PREFIX_ENTRY_BYTES)?;
                            let mut list = Vec::new();
                            budget.push(&mut list, at)?;
                            filed.insert(prefix, list);
                        }
                        reachable += 1;
                    } else {
                        budget.push(&mut unindexed, at)?;
                    }
                }
                Keys::Prefix {
                    filed: Box::new(filed),
                    unindexed,
                }
            }
        };

        // Rows whose key is missing or unparseable are kept and unreachable,
        // which is worth saying once rather than losing silently.
        if reachable < rows.len() {
            debug!(
                column = columns[key_column].as_str(),
                rows = rows.len(),
                unreachable = rows.len() - reachable,
                "some table rows carry no key"
            );
        }

        Ok(Self {
            columns,
            key_column,
            rows,
            keys,
        })
    }

    /// Where a text key leads.
    fn positions_of(&self, key: &str) -> &[usize] {
        match &self.keys {
            Keys::Text(filed) => filed.get(key).map_or(NO_ROWS, Vec::as_slice),
            Keys::Address { .. } | Keys::Prefix { .. } => NO_ROWS,
        }
    }

    /// Where an address leads.
    ///
    /// An exact index answers the address itself; a prefix index answers the
    /// most specific range that contains it, so a `/24` wins over the `/8`
    /// around it.
    fn positions_at(&self, address: IpAddr) -> &[usize] {
        match &self.keys {
            Keys::Address { filed, .. } => filed.get(&address).map_or(NO_ROWS, Vec::as_slice),
            Keys::Prefix { filed, .. } => filed
                .get_lpm(&IpNet::from(address))
                .map_or(NO_ROWS, |(_, rows)| rows.as_slice()),
            Keys::Text(_) => NO_ROWS,
        }
    }

    /// The row at a position.
    fn row(&self, at: usize) -> Row<'_> {
        Row {
            table: self,
            cells: &self.rows[at],
        }
    }
}

impl TableSource {
    /// Check the source can be acted on, before anything is downloaded.
    ///
    /// This is the config-load check: a URL nothing can fetch, a file name that
    /// would write outside the data directory, or a headerless CSV that names
    /// no columns is reported here rather than part way through a transfer.
    ///
    /// # Errors
    ///
    /// [`TableError::BadUrl`], [`TableError::UnsafeFileName`],
    /// [`TableError::NamesRequired`] or [`TableError::NoNames`], naming the
    /// field to fix.
    pub fn validate(&self) -> Result<(), TableError> {
        reqwest::Url::parse(&self.url).map_err(|e| TableError::BadUrl {
            url: fetch::loggable(&self.url),
            detail: e.to_string(),
        })?;

        // A source states a file name, never a path: it is written into a
        // directory the operator chose, and it may not climb out of it.
        if Path::new(&self.file).file_name() != Some(self.file.as_ref()) {
            return Err(TableError::UnsafeFileName {
                file: self.file.clone(),
            });
        }

        match (&self.format, &self.schema) {
            (TableFormat::Csv { header: false }, Schema::Auto) => Err(TableError::NamesRequired),
            (_, Schema::Named(named)) if named.is_empty() => Err(TableError::NoNames),
            _ => Ok(()),
        }
    }
}

/// One row of a table, borrowed from it.
#[derive(Clone, Copy)]
pub struct Row<'a> {
    /// Table the row belongs to, which is what names its cells.
    table: &'a Table,

    /// Cells of this row, as wide as the table's columns.
    cells: &'a [Cell],
}

impl<'a> Row<'a> {
    /// The value under a column name.
    #[must_use]
    pub fn get(&self, column: &str) -> Option<&'a str> {
        self.at(self.table.position_of(column)?)
    }

    /// The value at a column position.
    #[must_use]
    pub fn at(&self, column: usize) -> Option<&'a str> {
        cell(self.cells, column)
    }

    /// The value this row is indexed by.
    #[must_use]
    pub fn key(&self) -> Option<&'a str> {
        self.at(self.table.key_column)
    }

    /// Column names, in the order the cells are stored.
    #[must_use]
    pub fn columns(&self) -> &'a [String] {
        &self.table.columns
    }
}

impl fmt::Debug for Row<'_> {
    /// Prints the row's own cells rather than the table behind it, which every
    /// row would otherwise carry a copy of.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut map = f.debug_map();
        for (column, value) in self.table.columns.iter().zip(self.cells) {
            map.entry(column, &value.as_deref());
        }
        map.finish()
    }
}

/// Which rows a [`Rows`] walks.
#[derive(Debug)]
enum Positions<'a> {
    /// The rows filed under one key.
    Listed(slice::Iter<'a, usize>),

    /// Every row, in file order.
    All(Range<usize>),
}

/// Rows of a table, in file order.
#[derive(Debug)]
pub struct Rows<'a> {
    /// Table the rows belong to.
    table: &'a Table,

    /// Positions still to walk.
    positions: Positions<'a>,
}

impl<'a> Iterator for Rows<'a> {
    type Item = Row<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let at = match &mut self.positions {
            Positions::Listed(listed) => *listed.next()?,
            Positions::All(all) => all.next()?,
        };
        Some(self.table.row(at))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.positions {
            Positions::Listed(listed) => listed.size_hint(),
            Positions::All(all) => all.size_hint(),
        }
    }
}

impl ExactSizeIterator for Rows<'_> {}

/// The transfer that fetches one table source.
fn transfer(source: &TableSource, dest: PathBuf) -> Transfer {
    Transfer {
        url: source.url.clone(),
        // A user-supplied source states one URL: there is no publication month
        // to fall back behind.
        fallback_url: None,
        checksum_url: source.checksum_url.clone(),
        dest,
        archive: match source.archive {
            SourceArchive::Raw => Archive::Raw,
            SourceArchive::Gzip => Archive::Gzip,
        },
        format: match source.format {
            TableFormat::Csv { .. } => DatabaseFormat::Csv,
            TableFormat::Json => DatabaseFormat::Json,
        },
        credential: Credential::None,
    }
}

/// The value at a position, absent when the source did not supply one.
fn cell(cells: &[Cell], at: usize) -> Option<&str> {
    cells.get(at).and_then(Option::as_deref)
}

/// A cell as an address, when it is one.
fn as_address(text: &str) -> Option<IpAddr> {
    text.parse().ok()
}

/// A cell as a CIDR range, when it is one.
///
/// A bare address is accepted as the range holding only itself, because
/// publishers mix single hosts into a prefix list.
fn as_prefix(text: &str) -> Option<IpNet> {
    text.parse()
        .ok()
        .or_else(|| as_address(text).map(IpNet::from))
}

/// Which column a source is keyed by.
///
/// A conventional name is preferred, and a source that uses none is sampled
/// instead, because the field is named differently by every publisher. The
/// sampling policy is shared by both key kinds and changes for both at once.
fn keyed_column(
    columns: &[String],
    rows: &[Vec<Cell>],
    names: &[&str],
    parses: fn(&str) -> bool,
) -> Option<usize> {
    let holds = |at: usize| {
        let mut seen = 0;

        for row in rows.iter().take(ADDRESS_SAMPLE) {
            let Some(text) = cell(row, at) else {
                continue;
            };
            if !parses(text) {
                return false;
            }
            seen += 1;
        }

        seen > 0
    };

    let conventional = columns
        .iter()
        .position(|column| names.contains(&column.to_ascii_lowercase().as_str()));

    if let Some(at) = conventional
        && holds(at)
    {
        return Some(at);
    }

    (0..columns.len()).find(|&at| holds(at))
}

/// Bytes a table may hold while its index is built, and what it holds so far.
///
/// An estimate on the same footing as the parse's: what each structure asks
/// the allocator for, with no account of how the allocator rounds it.
struct Budget {
    /// Bytes charged so far.
    held: u64,

    /// Bytes the table may cost before the build is abandoned.
    ceiling: u64,
}

impl Budget {
    /// Add `bytes`, refusing once the total passes the ceiling.
    fn charge(&mut self, bytes: usize) -> Result<(), TableError> {
        let bytes = u64::try_from(bytes).unwrap_or(u64::MAX);
        self.held = self.held.saturating_add(bytes);
        if self.held > self.ceiling {
            return Err(TableError::OverResidentCeiling {
                ceiling: self.ceiling,
            });
        }
        Ok(())
    }

    /// Append a row position, charging whatever capacity the list grows by.
    fn push(&mut self, list: &mut Vec<usize>, at: usize) -> Result<(), TableError> {
        let before = list.capacity();
        list.push(at);
        self.charge((list.capacity() - before) * size_of::<usize>())
    }

    /// File a row position under `key`, charging the `key_bytes` it holds on
    /// the heap, the list it lands in and the buckets the map grows by.
    fn file<K: Hash + Eq>(
        &mut self,
        filed: &mut HashMap<K, Vec<usize>>,
        key: K,
        key_bytes: usize,
        at: usize,
    ) -> Result<(), TableError> {
        let slots = filed.capacity();
        let list = match filed.entry(key) {
            Entry::Occupied(entry) => entry.into_mut(),
            Entry::Vacant(entry) => {
                self.charge(key_bytes)?;
                entry.insert(Vec::new())
            }
        };
        self.push(list, at)?;

        // A bucket holds one entry and one control byte, and the map keeps an
        // eighth of its buckets empty.
        let bucket = size_of::<(K, Vec<usize>)>() + 1;
        self.charge((filed.capacity() - slots) * bucket * 8 / 7)
    }
}

/// The backing each table got, through the facade.
///
/// On the same footing as the enricher's own backing gauge: it is emitted once
/// per table per load, so it costs nothing a lookup can feel.
#[cfg(feature = "metrics")]
mod telemetry {
    use super::TableBacking;

    /// Value of the `type` label on every series.
    const TYPE: &str = "table";

    /// Report where a loaded table's rows are.
    pub(super) fn backing(file: &str, chosen: TableBacking) {
        metrics::gauge!(
            "enrichment_table_backing",
            "type" => TYPE,
            "file" => file.to_string(),
            "backing" => chosen.label(),
        )
        .set(1.0);
    }
}

/// Backing emission, compiled out.
#[cfg(not(feature = "metrics"))]
mod telemetry {
    use super::TableBacking;

    /// Report where a loaded table's rows are.
    pub(super) const fn backing(_file: &str, _chosen: TableBacking) {}
}

#[cfg(test)]
mod tests;