Skip to content

feat: Reader support open db from embed.fs #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package maxminddb

import (
"bytes"
"embed"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -48,6 +49,23 @@ type Metadata struct {
RecordSize uint `maxminddb:"record_size"`
}

func OpenWithEmbedFS(f embed.FS, path string) (*Reader, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need documentation and the name is somewhat awkward.

data, err := f.ReadFile(path)
if err != nil {
return nil, err
}

reader, err := FromBytes(data)
if err != nil {
_ = munmap(data)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no memory map. It doesn't make sense to cal this.

return nil, err
}

reader.hasMappedFile = true
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are loading the file into memory, not memory mapping it.

runtime.SetFinalizer(reader, (*Reader).Close)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point is setting a finalizer here.

return reader, nil
}

// Open takes a string path to a MaxMind DB file and returns a Reader
// structure or an error. The database file is opened using a memory map
// on supported platforms. On platforms without memory map support, such
Expand Down
19 changes: 19 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package maxminddb

import (
"embed"
"errors"
"fmt"
"math/big"
Expand All @@ -16,6 +17,9 @@ import (
"github.com/stretchr/testify/require"
)

//go:embed test-data/test-data/*
var f embed.FS

func TestReader(t *testing.T) {
for _, recordSize := range []uint{24, 28, 32} {
for _, ipVersion := range []uint{4, 6} {
Expand All @@ -24,7 +28,10 @@ func TestReader(t *testing.T) {
ipVersion,
recordSize,
)
// log.Printf("Checking %s", fileName)

t.Run(fileName, func(t *testing.T) {
// reader, err := Open(testFile(fileName))
reader, err := Open(testFile(fileName))
require.NoError(t, err, "unexpected error while opening database: %v", err)
checkMetadata(t, reader, ipVersion, recordSize)
Expand All @@ -35,6 +42,18 @@ func TestReader(t *testing.T) {
checkIpv6(t, reader)
}
})

t.Run(fileName, func(t *testing.T) {
reader, err := OpenWithEmbedFS(f, testFile(fileName))
require.NoError(t, err, "unexpected error while opening database: %v", err)
checkMetadata(t, reader, ipVersion, recordSize)

if ipVersion == 4 {
checkIpv4(t, reader)
} else {
checkIpv6(t, reader)
}
})
}
}
}
Expand Down