Skip to content

Commit 989d426

Browse files
committed
Replace LookupOffset/Decode with RecordKey
1 parent c1c165b commit 989d426

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed

reader.go

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,48 +156,8 @@ func (r *Reader) Lookup(ip netip.Addr) Result {
156156
}
157157
}
158158

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-
175159
var zeroIP = netip.MustParseAddr("::")
176160

177-
// Decode the record at |offset| into |result|. The result value pointed to
178-
// must be a data value that corresponds to a record in the database. This may
179-
// include a struct representation of the data, a map capable of holding the
180-
// data or an empty any value.
181-
//
182-
// If result is a pointer to a struct, the struct need not include a field
183-
// for every value that may be in the database. If a field is not present in
184-
// the structure, the decoder will not decode that field, reducing the time
185-
// required to decode the record.
186-
//
187-
// As a special case, a struct field of type uintptr will be used to capture
188-
// the offset of the value. Decode may later be used to extract the stored
189-
// value from the offset. MaxMind DBs are highly normalized: for example in
190-
// the City database, all records of the same country will reference a
191-
// single representative record for that country. This uintptr behavior allows
192-
// clients to leverage this normalization in their own sub-record caching.
193-
func (r *Reader) Decode(offset uintptr, result any) error {
194-
if r.buffer == nil {
195-
return errors.New("cannot call Decode on a closed database")
196-
}
197-
198-
return Result{decoder: r.decoder, offset: uint(offset)}.Decode(result)
199-
}
200-
201161
func (r *Reader) lookupPointer(ip netip.Addr) (uint, int, error) {
202162
if r.Metadata.IPVersion == 4 && ip.Is6() {
203163
return 0, 0, fmt.Errorf(

result.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ func (r Result) Found() bool {
107107
return r.err == nil && r.offset != notFound
108108
}
109109

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

0 commit comments

Comments
 (0)