← First Pair Library

22 Zone data as an indexed semantic model

Zone is more than a parser for tinydns text. It is the semantic index used by authoritative answers:

pub struct Zone {
    records: BTreeMap<Name, Vec<Record>>,
    metadata: BTreeMap<Name, Vec<RecordMetadata>>,
    authoritative: BTreeSet<Name>,
    delegations: BTreeSet<Name>,
    locations: Vec<(Vec<u8>, [u8; 2])>,
    current_metadata: RecordMetadata,
    default_serial: u32,
    nodes: BTreeSet<Name>,
    unqualified_nodes: BTreeSet<Name>,
}

The maps hold records and djbdns location metadata. The sets encode facts that would otherwise require scans: zone apexes, delegation cuts, all existing nodes, and nodes that exist independently of location-qualified records. This all-node index is why the optimized NXDOMAIN benchmark is about eleven times faster than the earlier scan-based implementation.

The type also prevents semantic drift. Parsing validates CNAME exclusivity once. Lookup returns a Lookup enum, so absence cannot collapse into one null result:

Wildcard processing uses the nodes index to find the closest encloser. Delegation processing uses the explicit delegations set. Transfer processing walks the same model while excluding child-zone contents. One representation therefore supplies ordinary answers, negative answers, wildcards, referrals, and AXFR without five subtly different interpretations of a zone.