Skip to content

Commit 457aad7

Browse files
committed
Replace LookupOffset with RecordOffset
1 parent a60828e commit 457aad7

File tree

3 files changed

+33
-43
lines changed

3 files changed

+33
-43
lines changed

reader.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ import (
99
"reflect"
1010
)
1111

12-
const (
13-
// NotFound is returned by LookupOffset when a matched root record offset
14-
// cannot be found.
15-
NotFound = ^uintptr(0)
16-
17-
dataSectionSeparatorSize = 16
18-
)
12+
const dataSectionSeparatorSize = 16
1913

2014
var metadataStartMarker = []byte("\xAB\xCD\xEFMaxMind.com")
2115

@@ -156,24 +150,6 @@ func (r *Reader) Lookup(ip netip.Addr) Result {
156150
}
157151
}
158152

159-
// LookupOffset maps an argument net.IP to a corresponding record offset in the
160-
// database. NotFound is returned if no such record is found, and a record may
161-
// otherwise be extracted by passing the returned offset to Decode. LookupOffset
162-
// is an advanced API, which exists to provide clients with a means to cache
163-
// previously-decoded records.
164-
func (r *Reader) LookupOffset(ip netip.Addr) (uintptr, error) {
165-
if r.buffer == nil {
166-
return 0, errors.New("cannot call LookupOffset on a closed database")
167-
}
168-
pointer, _, err := r.lookupPointer(ip)
169-
if pointer == 0 || err != nil {
170-
return NotFound, err
171-
}
172-
return r.resolveDataPointer(pointer)
173-
}
174-
175-
var zeroIP = netip.MustParseAddr("::")
176-
177153
// Decode the record at |offset| into |result|. The result value pointed to
178154
// must be a data value that corresponds to a record in the database. This may
179155
// include a struct representation of the data, a map capable of holding the
@@ -198,6 +174,8 @@ func (r *Reader) Decode(offset uintptr, result any) error {
198174
return Result{decoder: r.decoder, offset: uint(offset)}.Decode(result)
199175
}
200176

177+
var zeroIP = netip.MustParseAddr("::")
178+
201179
func (r *Reader) lookupPointer(ip netip.Addr) (uint, int, error) {
202180
if r.Metadata.IPVersion == 4 && ip.Is6() {
203181
return 0, 0, fmt.Errorf(

reader_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,19 @@ func TestDecoder(t *testing.T) {
300300

301301
{
302302
// Directly lookup and decode.
303-
var result TestType
304-
require.NoError(t, reader.Lookup(netip.MustParseAddr("::1.1.1.0")).Decode(&result))
305-
verify(result)
303+
var testV TestType
304+
require.NoError(t, reader.Lookup(netip.MustParseAddr("::1.1.1.0")).Decode(&testV))
305+
verify(testV)
306306
}
307307
{
308308
// Lookup record offset, then Decode.
309-
var result TestType
310-
offset, err := reader.LookupOffset(netip.MustParseAddr("::1.1.1.0"))
311-
require.NoError(t, err)
312-
assert.NotEqual(t, NotFound, offset)
309+
var testV TestType
310+
result := reader.Lookup(netip.MustParseAddr("::1.1.1.0"))
311+
require.NoError(t, result.Err())
312+
require.True(t, result.Found())
313313

314-
require.NoError(t, reader.Decode(offset, &result))
315-
verify(result)
314+
require.NoError(t, reader.Decode(result.RecordOffset(), &testV))
315+
verify(testV)
316316
}
317317

318318
require.NoError(t, reader.Close())
@@ -548,9 +548,9 @@ func TestNestedOffsetDecode(t *testing.T) {
548548
db, err := Open(testFile("GeoIP2-City-Test.mmdb"))
549549
require.NoError(t, err)
550550

551-
off, err := db.LookupOffset(netip.MustParseAddr("81.2.69.142"))
552-
assert.NotEqual(t, NotFound, off)
553-
require.NoError(t, err)
551+
result := db.Lookup(netip.MustParseAddr("81.2.69.142"))
552+
require.NoError(t, result.Err())
553+
require.True(t, result.Found())
554554

555555
var root struct {
556556
CountryOffset uintptr `maxminddb:"country"`
@@ -563,7 +563,7 @@ func TestNestedOffsetDecode(t *testing.T) {
563563
TimeZoneOffset uintptr `maxminddb:"time_zone"`
564564
} `maxminddb:"location"`
565565
}
566-
require.NoError(t, db.Decode(off, &root))
566+
require.NoError(t, db.Decode(result.RecordOffset(), &root))
567567
assert.InEpsilon(t, 51.5142, root.Location.Latitude, 1e-10)
568568

569569
var longitude float64
@@ -671,14 +671,15 @@ func TestUsingClosedDatabase(t *testing.T) {
671671
require.NoError(t, err)
672672
require.NoError(t, reader.Close())
673673

674-
var recordInterface any
675674
addr := netip.MustParseAddr("::")
675+
676+
result := reader.Lookup(addr)
677+
assert.Equal(t, "cannot call Lookup on a closed database", result.Err().Error())
678+
679+
var recordInterface any
676680
err = reader.Lookup(addr).Decode(recordInterface)
677681
assert.Equal(t, "cannot call Lookup on a closed database", err.Error())
678682

679-
_, err = reader.LookupOffset(addr)
680-
assert.Equal(t, "cannot call LookupOffset on a closed database", err.Error())
681-
682683
err = reader.Decode(0, recordInterface)
683684
assert.Equal(t, "cannot call Decode on a closed database", err.Error())
684685
}

result.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (r Result) DecodePath(v any, path ...any) error {
9595
}
9696

9797
// Err provides a way to check whether there was an error during the lookup
98-
// without clling Result.Decode. If there was an error, it will also be
98+
// without calling Result.Decode. If there was an error, it will also be
9999
// returned from Result.Decode.
100100
func (r Result) Err() error {
101101
return r.err
@@ -107,6 +107,17 @@ func (r Result) Found() bool {
107107
return r.err == nil && r.offset != notFound
108108
}
109109

110+
// RecordOffset returns the offset of the record in the database. This can be
111+
// passed to ReaderDecode. It can also be used as a unique identifier for the
112+
// data record in the particular database to cache the data record across
113+
// lookups. Note that while the offset uniquely identifies the data record,
114+
// other data in Result may differ between lookups. The offset is only valid
115+
// for the current database version. If you update the database file, you must
116+
// invalidate any cache associated with the previous version.
117+
func (r Result) RecordOffset() uintptr {
118+
return uintptr(r.offset)
119+
}
120+
110121
// Network returns the netip.Prefix representing the network associated with
111122
// the data record in the database.
112123
func (r Result) Network() netip.Prefix {

0 commit comments

Comments
 (0)