coip-reader 0.1.0

High-performance read-only reader for COI (.coi) columnar IP databases with projection, LRU caching, and serde support
Documentation
# coip — Columnar IP Database Format (`.coi`)

English | [简体中文]README.zh-CN.md

**`coip`** is a modern, open-source, read-only **columnar IP database format (`.coi`)** with a Rust toolkit and a zero-cgo pure-Go reader. It losslessly stores arbitrary structured IP→Record databases (including any MaxMind `.mmdb` file: GeoIP2/GeoLite2 City, Country, ASN, ISP, Enterprise, DB-IP, IPinfo, or custom CSV/JSONL feeds) at **4.7–4.9× smaller on-disk size**, while providing **microsecond random point-lookups** and compile-once **column projection**.

## Key Features

- **100% Lossless**: Preserves all MMDB fields, numeric widths, IEEE-754 `f64` bit patterns, `None` vs `false`/`0`/`""` optionality, multi-language maps, and IPv6→IPv4 DAG alias semantics.
- **Self-Describing 6-Primitive Schema**: No hardcoded GeoIP fields; supports arbitrary schemas, dynamic inspection, build-time `--keep-fields` / `--languages` pruning, and compile-once **Column Projection**.
- **Two Tuned Profiles**:
  - **`Compact`** (Zstd + `Section 3: ZstdDicts` + `ZeroSkipDelta` / `LocalDictSplit`): **4.75–4.90×** smaller than `.mmdb`.
  - **`UltraFast`** (pure-Rust LZ4): **3.34×** smaller than `.mmdb`, **19.5 µs** cold 5-field lookup, **1.15 µs** warm 5-field lookup (**1.7× faster than `maxminddb`**), and **3.00M QPS** across 8 threads.
- **Zero-C Pure-Rust & Pure-Go Readers**: `coip-reader` defaults to pure-Rust `ruzstd` + `lz4_flex` (optional `zstd-c` feature); [`bindings/go`]bindings/go/ provides a standalone `CGO_ENABLED=0` pure-Go COI v1.0 reader.
- **Open Binary Specification**: Byte-level specification in [`docs/SPEC.md`]docs/SPEC.md ([中文导读]docs/SPEC.zh-CN.md), detailed benchmark report in [`docs/BENCHMARKS.md`]docs/BENCHMARKS.md ([中文压测报告]docs/BENCHMARKS.zh-CN.md), and cross-language golden vectors in [`test-vectors/`]test-vectors/.

## Real-World Benchmark (`GeoIP2-City.mmdb`, 672,963 unique records, 8 languages)

Measured on Intel Core i7-13700 (Windows x64, `coip bench` with `zstd-c`). Full section breakdown, `ruzstd` vs `zstd-c`, and Go benchmarks are in [`docs/BENCHMARKS.md`](docs/BENCHMARKS.md).

| Format / Profile | File Size | Ratio vs MMDB | Cold 5-Field (`serde`) | Warm 5-Field (1T) | Warm 8-Thread QPS |
| --- | ---: | ---: | ---: | ---: | ---: |
| MaxMind `GeoIP2-City.mmdb` (`maxminddb` 5-field) | 104.27 MB | 1.00× | 2.15–2.24 µs | 1.90–1.98 µs | 2.92–3.16M QPS |
| MaxMind `GeoIP2-City.mmdb` (`maxminddb` full) | 104.27 MB | 1.00× | 3.00–3.53 µs | 2.59–2.88 µs | — |
| `coip` **Compact** (Zstd + 4 `ZstdDicts`) | **21.95 MB** | **4.75×** | 103.2 µs | **1.26 µs** (793K QPS) | **2.85M QPS** |
| `coip` **Compact-Max** (`--zstd-level 19`) | **21.30 MB** | **4.90×** | ~103 µs | ~1.26 µs | ~2.85M QPS |
| `coip` **UltraFast** (pure-Rust LZ4) | **31.26 MB** | **3.34×** | **19.46 µs** | **1.15 µs** (871K QPS) | **3.00M QPS** |
| `coip` **Country-only pruned** (`en`, `zh-CN`) | **1.05 MB** | **99.25×** | 27.93 µs | **0.34 µs** (2.91M QPS) | **5.89M QPS** |

## Workspace Layout

| Path | Description |
| --- | --- |
| [`docs/SPEC.md`]docs/SPEC.md / [`docs/SPEC.zh-CN.md`]docs/SPEC.zh-CN.md | Canonical COI v1.0 binary format specification (EN & ZH-CN) |
| [`docs/BENCHMARKS.md`]docs/BENCHMARKS.md / [`docs/BENCHMARKS.zh-CN.md`]docs/BENCHMARKS.zh-CN.md | Compression evolution, section breakdown, latency/QPS & architecture analysis |
| [`crates/coip-format`]crates/coip-format | Header, Footer, Section Directory, Schema, Varint, CRC32C (`no_std` + `alloc`) |
| [`crates/coip-builder`]crates/coip-builder | Generic `.coi` builder, lossless `.mmdb` importer, LPM resolver, pruning, CSV/JSONL import |
| [`crates/coip-reader`]crates/coip-reader | `mmap` / `&[u8]` / `File` reader, sharded LRU caches, Column Projection, streaming `serde` |
| [`crates/coip-cli`]crates/coip-cli | CLI: `coip convert \| import \| export \| diff \| lookup \| query \| inspect \| verify \| bench` |
| [`bindings/go`]bindings/go | Standalone pure-Go (`CGO_ENABLED=0`) COI v1.0 reader ([中文文档]bindings/go/README.zh-CN.md) |
| [`test-vectors/`]test-vectors | Cross-language golden `.coi` test vectors (`Compact` + `UltraFast`) and expected JSON |
| [`benches/`]benches | Criterion benchmark suite comparing `coip-reader` vs `maxminddb` |

## CLI Quick Start

```bash
# Convert an MMDB file to .coi (Compact Zstd + ZstdDicts profile, 4.75x smaller)
cargo run --release -p coip-cli -- convert GeoIP2-City.mmdb GeoIP2-City.coi

# Convert with UltraFast profile (pure-Rust LZ4, 19.5 µs cold / 1.15 µs warm, 3.34x smaller)
cargo run --release -p coip-cli -- convert GeoIP2-City.mmdb GeoIP2-City-fast.coi --profile ultrafast

# Build-time field/language pruning or .coi -> .coi repacking (no .mmdb needed!)
cargo run --release -p coip-cli -- convert GeoIP2-City.coi GeoIP2-Country.coi \
  --keep-fields continent,country,registered_country --languages en,zh-CN

# Import any custom JSONL or CSV IP dataset (supports overlapping LPM CIDRs & IP ranges)
cargo run --release -p coip-cli -- import custom_ips.csv custom.coi --profile compact

# Export a .coi database as canonical CIDR + JSONL (with optional projection)
cargo run --release -p coip-cli -- export GeoIP2-City.coi --fields country.iso_code,location.time_zone --limit 100

# Diff two .coi databases (exact dual-stack IP additions, removals, and changes)
cargo run --release -p coip-cli -- diff GeoIP2-City-old.coi GeoIP2-City-new.coi

# Inspect schema, dictionaries, ZstdDicts, and section sizes
cargo run --release -p coip-cli -- inspect GeoIP2-City.coi

# Lookup / query an IP (full record, projected fields, pure --json, or Nushell-style --table)
cargo run --release -p coip-cli -- lookup GeoIP2-City.coi 8.8.8.8
cargo run --release -p coip-cli -- query GeoIP2-City.coi 8.8.8.8 \
  --fields country.iso_code,location.latitude,location.longitude,location.time_zone --json
cargo run --release -p coip-cli -- query GeoIP2-City.coi 8.8.8.8 --table

# Exhaustively verify 100% field-by-field lossless equivalence against source MMDB
cargo run --release -p coip-cli -- verify GeoIP2-City.mmdb GeoIP2-City.coi

# Benchmark cold / warm / multi-threaded concurrent lookups vs maxminddb
cargo run --release -p coip-cli -- bench GeoIP2-City.mmdb GeoIP2-City.coi
```

## Rust Library Example

See runnable examples in [`crates/coip-reader/examples/basic_lookup.rs`](crates/coip-reader/examples/basic_lookup.rs) and [`crates/coip-builder/examples/custom_db.rs`](crates/coip-builder/examples/custom_db.rs):

```bash
cargo run -p coip-builder --example custom_db
cargo run -p coip-reader --example basic_lookup
```

## Pure-Go Reader (`bindings/go`)

```go
import coip "github.com/netkn001/coip/bindings/go"

r, err := coip.Open("GeoIP2-City.coi")
if err != nil {
    log.Fatal(err)
}
defer r.Close()

proj, _ := coip.CompileProjection("country.iso_code", "location.latitude", "location.longitude")
rec, found, err := r.LookupProjectedString("8.8.8.8", proj)
```

See [`bindings/go/README.md`](bindings/go/README.md) ([简体中文](bindings/go/README.zh-CN.md)) for full Go documentation.

## License

MIT License — see [`LICENSE`](LICENSE).