-
Notifications
You must be signed in to change notification settings - Fork 105
Add LookupNetwork method #59
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
Conversation
I think this is a remanent of the code before we did unmarshaling ourselves and all of the decode methods had the same signature.
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.
Great!
reader.go
Outdated
// result value to Decode into. | ||
func (r *Reader) Lookup(ipAddress net.IP, result interface{}) error { | ||
// Lookup retrieves the database record for ip and stores it in the value | ||
// pointed to be result. If result is nil or not a pointer, an error is |
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.
"by result" I think. LookupNetwork()'s comment below as well.
reader.go
Outdated
func (r *Reader) cidr(ip net.IP, prefixLength int) *net.IPNet { | ||
// This is necessary as the node that the IPv4 start is at may | ||
// be at a bit depth that is less that 96, i.e., ipv4Start points | ||
// to a leaf node. |
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.
Not sure I get this. It'd be in a database with no IPv4 IPs?
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.
Kind of. It could "have" them in the sense that lookups for them would return a result, but it would be the same result for all of them. For instance, if there was a record at ::/8
.
pointer, prefixLength, ip, err := r.lookupPointer(ip) | ||
|
||
network = r.cidr(ip, prefixLength) | ||
if pointer == 0 || err != nil { |
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.
If this is 0 or an error, do we need to call cidr()?
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.
My thought was that it would be useful to know the network size for empty networks as well as networks with records. I believe the legacy reader did return this. For errors, it might be less useful, although it could help in debugging a bad database.
Previously, we were benchmarking the assert.NoError call, which isn't particularly cheap.
when calculating LookupNetwork. Arguably, it might be cleaner to not set ipv4Start when there is no IPv4 subtree, although that would also introduce additional complications and it would make those lookups slower.
23fc4df
to
27395cf
Compare
This replaces the runtime check on record size and unrolls the node calculation. Before: ``` goos: linux goarch: amd64 pkg: github.com/oschwald/maxminddb-golang BenchmarkLookup-8 1000000 15928 ns/op BenchmarkLookupNetwork-8 1000000 16638 ns/op BenchmarkCountryCode-8 10000000 1244 ns/op PASS ok github.com/oschwald/maxminddb-golang 46.654s ``` After: ``` goos: linux goarch: amd64 pkg: github.com/oschwald/maxminddb-golang BenchmarkLookup-8 1000000 14918 ns/op BenchmarkLookupNetwork-8 1000000 15681 ns/op BenchmarkCountryCode-8 20000000 1137 ns/op PASS ok github.com/oschwald/maxminddb-golang 54.967s ```
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.
🎉
Closes #36 and #41.
This PR addes a
LookupNetwork
method. This behaves similarly toLookup
except that it returns the network associated with the record in the database and an ok boolean indicating whether a record was found or not.This also includes some minor cleanup and adds
golangci-lint
to Travis.Benchmarks from the branch:
Benchmarks of master + the
err
handling change: