Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ and this project adheres to
- Filesystem paths now interpret `${name}` as a parameter, even when no
variables are supplied. Undefined parameters and malformed placeholders are
errors; escape a literal `${` as `$${`.
- MMDB output now uses the smallest record size that fits the database when
`record_size` is omitted or 0. Small databases can now use 24-bit records
instead of 28-bit records, and the record size can change as the data grows.
Set `record_size` to keep a fixed size.
- Updated to mmdbwriter/v2 for faster MMDB output with lower memory usage.
- Go 1.27 or later is now required to build from source.
- Progress output now goes to stderr alongside errors and uses structured
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ file = "merged.mmdb"
[output.mmdb]
database_type = "GeoIP2-City"
description = { en = "Merged GeoIP Database" }
record_size = 28
# record_size = 28 # Uncomment to use a fixed record size

[[databases]]
name = "city"
Expand Down Expand Up @@ -336,7 +336,8 @@ output_path = ["location", "longitude"]
- Perfect type preservation from source databases
- Support for nested structures via `output_path`
- Compatible with all MMDB readers (libmaxminddb, etc.)
- Configurable record size (24, 28, or 32 bits)
- Smallest record size that fits the data by default, or a fixed size of 24, 28,
or 32 bits

## Querying Parquet Files

Expand Down
48 changes: 48 additions & 0 deletions cmd/mmdbconvert/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"time"

"github.com/oschwald/maxminddb-golang/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -186,6 +187,53 @@ func TestRunCLISuccess(t *testing.T) {
}
}

func TestRunCLIMMDBRecordSize(t *testing.T) {
tests := []struct {
name string
setting string
recordSize uint
}{
{name: "omitted", recordSize: 24},
{name: "zero", setting: "record_size = 0", recordSize: 24},
{name: "explicit", setting: "record_size = 28", recordSize: 28},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
configPath := filepath.Join(dir, "config.toml")
outputPath := filepath.Join(dir, "output.mmdb")
config := `
[output]
format = "mmdb"
file = "` + filepath.ToSlash(outputPath) + `"

[output.mmdb]
database_type = "Test"
` + tt.setting + `

[[databases]]
name = "city"
path = "` + filepath.ToSlash(testDatabasePath(t)) + `"

[[columns]]
name = "country_code"
database = "city"
path = ["country", "iso_code"]
`
require.NoError(t, os.WriteFile(configPath, []byte(config), 0o600))

var stdout, stderr bytes.Buffer
args := []string{"--quiet", configPath}
require.Equal(t, 0, runCLI(t.Context(), args, &stdout, &stderr, false), stderr.String())

reader, err := maxminddb.Open(outputPath)
require.NoError(t, err)
defer reader.Close()
assert.Equal(t, tt.recordSize, reader.Metadata.RecordSize)
})
}
}

func TestRunCLIArgumentErrors(t *testing.T) {
configPath, outputPath := writeTestConfig(t, testDatabasePath(t))
tests := []struct {
Expand Down
9 changes: 6 additions & 3 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,17 @@ When `format = "mmdb"`, you can specify MMDB-specific options:
database_type = "GeoIP2-City" # Database type (required)
description = { en = "Custom Database", de = "Benutzerdefinierte Datenbank" } # Descriptions by language
languages = ["en", "de"] # List of languages (auto-populated from description if omitted)
record_size = 28 # Record size: 24, 28, or 32 (default: 28)
record_size = 0 # Record size: 24, 28, or 32, or 0 for the smallest that fits (default: 0)
include_reserved_networks = false # Include reserved networks (default: false)
```

**Notes:**

- `database_type` is required for MMDB output
- `languages` is auto-populated from `description` keys if not specified
- If `record_size` is 0 or omitted, mmdbconvert uses the smallest record size
that fits the data. The size can change between runs as the data grows. Set
`record_size` to 24, 28, or 32 to keep a fixed size.
- Split IPv4/IPv6 files are not supported for MMDB output (must use single
`file`)
- Network columns are not used for MMDB output (data is written by prefix)
Expand Down Expand Up @@ -722,7 +725,7 @@ file = "nested.mmdb"
[output.mmdb]
database_type = "GeoIP2-City"
description = { en = "Nested Structure Example", de = "Beispiel für verschachtelte Strukturen" }
record_size = 28
# record_size = 28 # Uncomment to use a fixed record size
include_reserved_networks = false

[[databases]]
Expand Down Expand Up @@ -772,7 +775,7 @@ file = "enterprise-with-anonymous.mmdb"
[output.mmdb]
database_type = "GeoIP2-Enterprise"
description = { en = "Enterprise + Anonymous IP Merged" }
record_size = 28
# record_size = 28 # Uncomment to use a fixed record size

[[databases]]
name = "enterprise"
Expand Down
2 changes: 1 addition & 1 deletion examples/mmdb-copy-entire-db.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ file = "city-with-anonymous.mmdb"
[output.mmdb]
database_type = "GeoIP2-City"
description = { en = "City + Anonymous IP Merged" }
record_size = 28
# record_size = 28 # Uncomment to use a fixed record size

[[databases]]
name = "city"
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/maxmind/mmdbconvert
go 1.27.0

require (
github.com/maxmind/mmdbwriter/v2 v2.0.0-20260929214414-aa874fe6bc9e
github.com/maxmind/mmdbwriter/v2 v2.0.0-20261026204648-34b590caf65a
github.com/oschwald/maxminddb-golang/v2 v2.8.0
github.com/parquet-go/parquet-go v0.32.0
github.com/pelletier/go-toml/v2 v2.4.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/klauspost/compress v1.18.7 h1:aUyZsS4kH3QTKurYhAOwAHxllVPnOthb3vPfnF1Ehjw=
github.com/klauspost/compress v1.18.7/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/maxmind/mmdbwriter/v2 v2.0.0-20260929214414-aa874fe6bc9e h1:tFFxMqYvIZ6Z4sdOf+KlKHeMQ7Y4PA/bCSOH6s5XWyY=
github.com/maxmind/mmdbwriter/v2 v2.0.0-20260929214414-aa874fe6bc9e/go.mod h1:wgHnsdUYfQeQZI6xWn4Ix7zt1b9/Wi5Q9JJMFa1jZDk=
github.com/maxmind/mmdbwriter/v2 v2.0.0-20261026204648-34b590caf65a h1:rE1bP01/Y3dfgbpq1igvaMEGxnMMsx6ejOjGHystUEA=
github.com/maxmind/mmdbwriter/v2 v2.0.0-20261026204648-34b590caf65a/go.mod h1:gWKq4T3yvU8hWtq8VXpehRDVcNfq4hB8esLwFgeSGAI=
github.com/oschwald/maxminddb-golang/v2 v2.8.0 h1:enP6KMn9sCV4bRAg3LfHMaU2qjOP2xqY8LjatJMwufU=
github.com/oschwald/maxminddb-golang/v2 v2.8.0/go.mod h1:kvJXAdsCahFxSWHiigYomcbP0qnEtsL929Mv98Hz8dM=
github.com/parquet-go/bitpack v1.0.0 h1:AUqzlKzPPXf2bCdjfj4sTeacrUwsT7NlcYDMUQxPcQA=
Expand Down
13 changes: 4 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type MMDBConfig struct {
DatabaseType string `toml:"database_type"` // Database type (e.g., "GeoIP2-City")
Description map[string]string `toml:"description"` // Descriptions by language
Languages []string `toml:"languages"` // List of languages (auto-populated from description if empty)
RecordSize *int `toml:"record_size"` // 24, 28, or 32 (default: 28)
RecordSize int `toml:"record_size"` // 24, 28, or 32, or 0 for the smallest that fits (default: 0)
IncludeReservedNetworks *bool `toml:"include_reserved_networks"` // Include reserved networks (default: false)
}

Expand Down Expand Up @@ -250,9 +250,6 @@ func applyDefaults(config *Config) {

// MMDB defaults
if config.Output.Format == OutputFormatMMDB {
if config.Output.MMDB.RecordSize == nil {
config.Output.MMDB.RecordSize = new(28)
}
if config.Output.MMDB.IncludeReservedNetworks == nil {
config.Output.MMDB.IncludeReservedNetworks = new(false)
}
Expand Down Expand Up @@ -347,11 +344,9 @@ func validate(config *Config, data []byte) error {
return errors.New("output.mmdb.database_type is required for MMDB output")
}

if config.Output.MMDB.RecordSize != nil {
rs := *config.Output.MMDB.RecordSize
if rs != 24 && rs != 28 && rs != 32 {
return fmt.Errorf("output.mmdb.record_size must be 24, 28, or 32, got %d", rs)
}
rs := config.Output.MMDB.RecordSize
if rs != 0 && rs != 24 && rs != 28 && rs != 32 {
return fmt.Errorf("output.mmdb.record_size must be 0, 24, 28, or 32, got %d", rs)
}

// Reject split files for MMDB
Expand Down
56 changes: 56 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -509,6 +510,30 @@ path = ["country", "iso_code"]
}
},
},
{
name: "MMDB record size 0 chooses the size automatically",
toml: `
[output]
format = "mmdb"
file = "output.mmdb"

[output.mmdb]
database_type = "Test"
record_size = 0

[[databases]]
name = "geo"
path = "/path/to/geo.mmdb"

[[columns]]
name = "country"
database = "geo"
path = ["country", "iso_code"]
`,
validate: func(t *testing.T, cfg *Config) {
assert.Zero(t, cfg.Output.MMDB.RecordSize)
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -691,6 +716,28 @@ path = ["country", "iso_code"]
`,
expectError: "either output.file must be set or both output.ipv4_file and output.ipv6_file must be provided",
},
{
name: "invalid MMDB record size",
toml: `
[output]
format = "mmdb"
file = "output.mmdb"

[output.mmdb]
database_type = "Test"
record_size = 26

[[databases]]
name = "geo"
path = "/path/to/geo.mmdb"

[[columns]]
name = "country"
database = "geo"
path = ["country", "iso_code"]
`,
expectError: "output.mmdb.record_size must be 0, 24, 28, or 32, got 26",
},
{
name: "no databases",
toml: `
Expand Down Expand Up @@ -1297,6 +1344,15 @@ func TestApplyDefaults(t *testing.T) {
}
},
},
{
name: "MMDB record size is left unset",
input: Config{
Output: OutputConfig{Format: "mmdb"},
},
validate: func(t *testing.T, cfg *Config) {
assert.Zero(t, cfg.Output.MMDB.RecordSize)
},
},
{
name: "Parquet bucket size defaults",
input: Config{
Expand Down
2 changes: 1 addition & 1 deletion internal/writer/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestEndToEnd_MMDBExport(t *testing.T) {
Format: config.OutputFormatMMDB,
MMDB: config.MMDBConfig{
DatabaseType: source.Metadata().DatabaseType,
RecordSize: &recordSize,
RecordSize: recordSize,
IncludeReservedNetworks: &includeReserved,
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/writer/mmdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewMMDBWriter(output io.Writer, cfg *config.Config, ipVersion int) (*MMDBWr
DatabaseType: cfg.Output.MMDB.DatabaseType,
Description: cfg.Output.MMDB.Description,
Languages: cfg.Output.MMDB.Languages,
RecordSize: *cfg.Output.MMDB.RecordSize,
RecordSize: cfg.Output.MMDB.RecordSize,
IPVersion: ipVersion,
IncludeReservedNetworks: *cfg.Output.MMDB.IncludeReservedNetworks,
})
Expand Down
30 changes: 28 additions & 2 deletions internal/writer/mmdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ import (
"github.com/maxmind/mmdbconvert/internal/config"
)

func TestMMDBWriter_AutoRecordSize(t *testing.T) {
includeReserved := false
cfg := &config.Config{
Output: config.OutputConfig{
MMDB: config.MMDBConfig{
DatabaseType: "AutoRecordSize-Test",
IncludeReservedNetworks: &includeReserved,
},
},
Columns: []config.Column{{Name: "count"}},
}
var output bytes.Buffer
writer, err := NewMMDBWriter(&output, cfg, 4)
require.NoError(t, err)
require.NoError(t, writer.WriteRow(
netip.MustParsePrefix("1.2.3.0/24"),
[]mmdbtype.DataType{mmdbtype.Uint32(1)},
))
require.NoError(t, writer.Flush())

reader, err := maxminddb.OpenBytes(output.Bytes())
require.NoError(t, err)
defer reader.Close()
assert.EqualValues(t, 24, reader.Metadata.RecordSize)
}

func TestMMDBWriter_RoundTrip(t *testing.T) {
tests := []struct {
name string
Expand All @@ -36,7 +62,7 @@ func TestMMDBWriter_RoundTrip(t *testing.T) {
DatabaseType: "RoundTrip-Test",
Description: map[string]string{"en": "Round trip test"},
Languages: []string{"en"},
RecordSize: &recordSize,
RecordSize: recordSize,
IncludeReservedNetworks: &includeReserved,
},
},
Expand Down Expand Up @@ -102,7 +128,7 @@ func TestMMDBWriter_InvalidNetworks(t *testing.T) {
cfg := &config.Config{
Output: config.OutputConfig{
MMDB: config.MMDBConfig{
RecordSize: &recordSize,
RecordSize: recordSize,
IncludeReservedNetworks: &includeReserved,
},
},
Expand Down
Loading