-
Notifications
You must be signed in to change notification settings - Fork 105
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package maxminddb | |
|
||
import ( | ||
"bytes" | ||
"embed" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
@@ -48,6 +49,23 @@ type Metadata struct { | |
RecordSize uint `maxminddb:"record_size"` | ||
} | ||
|
||
func OpenWithEmbedFS(f embed.FS, path string) (*Reader, error) { | ||
data, err := f.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader, err := FromBytes(data) | ||
if err != nil { | ||
_ = munmap(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.