-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnode.go
More file actions
651 lines (602 loc) · 20.1 KB
/
Copy pathnode.go
File metadata and controls
651 lines (602 loc) · 20.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package mmdbwriter
import (
"errors"
"fmt"
"github.com/maxmind/mmdbwriter/v2/inserter"
"github.com/maxmind/mmdbwriter/v2/internal/treeaddr"
"github.com/maxmind/mmdbwriter/v2/mmdbtype"
)
type recordType byte
const (
recordTypeEmpty recordType = iota
recordTypeData
recordTypeNode
recordTypeAlias
recordTypeFixedNode
recordTypeReserved
recordTypePath
)
type record struct {
value valueRef
// nodeIndex indexes Tree node blocks for node-like records and Tree.paths
// for compressed-path records.
nodeIndex nodeIndex
recordType recordType
}
// each node contains two records.
type node struct {
children [2]record
}
type compressedPath struct {
ip [16]byte
record record
endDepth int
}
type nodeIndex uint32
const (
rootNodeIndex nodeIndex = 0
noNodeIndex = ^nodeIndex(0)
nodeBlockSize = 1024
)
// insertRecord carries the state for one insert call. Most fields are fixed for
// the life of the value. These are not:
//
// - ip, prefixLen, and insertedAs4, which insertPrepared re-targets for every
// subnet of a range, on every insert path.
// - splitDepth, which insertPrepared resets for each subnet and traversal
// sets when it splits a record.
// - the memo fields, which resolve updates as it goes.
//
// The memo is only used for pure inserters. It is keyed on the existing value
// alone, which is sound only because the resolver and value are fixed: reusing an
// insertRecord across inserts with a different value would return results
// computed from the previous one.
//
// The memo owns one store reference per key and per non-nil result. Every
// caller must run releaseResolved once insertion finishes. It releases the
// memo references and the reference interned for the inserted value itself.
// memoFirst and memoResult hold the single entry until a second distinct key
// promotes both into memo, which clears them.
//
// Fields are ordered widest first. The struct is one allocation per insert
// call, and this layout keeps it inside the 128-byte size class.
type insertRecord struct {
resolver insertResolver
store *valueStore
tree *Tree
// valueView is the value handed to the inserter as its new-value argument:
// the caller's value as passed for the insert entry points, or a
// store-materialized view when the insert began from an interned
// reference, as when loading.
valueView mmdbtype.DataType
// callerValue is the caller's object for a direct insert. Its identity is
// registered only after the insert succeeds, so a failed insert cannot
// serve stale data if the caller mutates and retries the object.
callerValue mmdbtype.DataType
memo map[valueRef]valueRef
prefixLen int
ip [16]byte
insertedNode nodeIndex
value valueRef
memoFirst valueRef
memoResult valueRef
recordType recordType
// insertedAs4 records the address family that the tree-space ip cannot
// encode. A netip.Prefix field would carry it directly, but at 32 bytes
// it pushes the struct into the next size class, while a pointer to a
// prepared Metadata escapes to the heap once per insertRange call. The
// flag fits in padding, and rebuilding the prefix per record measures at
// about 4ns.
//
// Only the metadata path reads it, but every path maintains it, so it can
// never hold a value from an earlier insert.
insertedAs4 bool
// splitDepth is the depth of the record a split chain started from, which
// is the extent that record had before this insertion. Tree depths are at
// most 128, and zero is the sentinel for no active split.
//
// One field is enough for the whole walk, because a split chain and a
// descent deeper than prefixLen are never live at the same time. Splitting
// happens only at a record shallower than prefixLen, and the chain then
// descends through a single child until it resolves at exactly prefixLen.
// Records deeper than prefixLen resolve at their own depth and are never
// split. Threading the depth through every recursive call would buy
// nothing.
splitDepth uint8
memoSet bool
}
// resolveValue returns the reference for a record and whether the caller owns
// it. With no inserter the reference interned when the insert began is reused
// directly: the store canonicalizes by content, so an equal existing value is
// the same reference and replaceDataRecord makes the assignment a no-op.
func (iRec *insertRecord) resolveValue(
existing valueRef,
existingDepth int,
) (valueRef, bool, error) {
if !iRec.resolver.hasFunc() {
return iRec.value, false, nil
}
return iRec.resolve(existing, existingDepth)
}
// resolve returns a reference and whether the caller owns it. A pure
// inserter's memo owns one reference to each non-nil result until insertion
// finishes. A metadata-aware Func is not memoized, so a newly interned
// reference is transferred directly to the target record.
func (iRec *insertRecord) resolve(
existing valueRef,
existingDepth int,
) (valueRef, bool, error) {
if iRec.resolver.pure != nil {
if iRec.memo != nil {
if value, ok := iRec.memo[existing]; ok {
return value, false, nil
}
} else if iRec.memoSet && iRec.memoFirst == existing {
return iRec.memoResult, false, nil
}
}
var result mmdbtype.DataType
var err error
if iRec.resolver.pure != nil {
result, err = iRec.resolver.pure(
iRec.store.materialize(existing),
iRec.valueView,
)
} else {
insertedNetwork, metadataErr := treeaddr.PrefixFromInsertIP(
iRec.ip,
iRec.prefixLen,
iRec.tree.treeDepth,
iRec.insertedAs4,
)
if metadataErr != nil {
return nilValueRef, false, fmt.Errorf(
"creating inserted network metadata: %w",
metadataErr,
)
}
metadata := inserter.Metadata{
InsertedNetwork: insertedNetwork,
ExistingDepth: existingDepth,
TreeDepth: iRec.tree.treeDepth,
}
if existingDepth == iRec.prefixLen {
// ip is masked at prefixLen and the walk has not descended past
// it, so no masking is needed.
metadata.ExistingAddr = iRec.ip
} else {
// Shallower records sit in a split chain, which leaves ip's deeper
// bits zero. Deeper records carry the descent path insertNode
// wrote, and masking drops the bits past this record.
metadata.ExistingAddr = maskedTreeAddr(iRec.ip, existingDepth)
}
result, err = iRec.resolver.withMetadata(
iRec.store.materialize(existing),
iRec.valueView,
metadata,
)
}
if err != nil {
return nilValueRef, false, err
}
if result == nil {
if iRec.resolver.pure != nil {
iRec.rememberResolved(existing, nilValueRef)
}
return nilValueRef, false, nil
}
// A result equal to the existing value interns to the existing reference,
// as intern canonicalizes by content, and the assignment in
// replaceDataRecord then becomes a no-op. Interning is wire-exact, so
// values that differ only in a float sign bit or NaN payload get a new
// reference and replace the old one.
value, err := iRec.store.intern(result)
if err != nil {
return nilValueRef, false, err
}
if iRec.resolver.pure != nil {
iRec.rememberResolved(existing, value)
return value, false, nil
}
return value, true, nil
}
func (iRec *insertRecord) rememberResolved(existing, result valueRef) {
// The memo owns a reference to each key. Without it, a released key ref
// could be recycled for a new value and produce a false memo hit.
iRec.store.retain(existing)
// Keep the common one-result case allocation-free. A second distinct
// existing value promotes the first entry into the map.
if iRec.memo == nil && !iRec.memoSet {
iRec.memoFirst = existing
iRec.memoResult = result
iRec.memoSet = true
return
}
if iRec.memo == nil {
iRec.memo = map[valueRef]valueRef{
iRec.memoFirst: iRec.memoResult,
}
// Clear the single-entry fields at promotion, so a reader that
// forgets to check the map first reads nil instead of a stale hit.
iRec.memoFirst = nilValueRef
iRec.memoResult = nilValueRef
iRec.memoSet = false
}
iRec.memo[existing] = result
}
// releaseResolved releases every reference the insertRecord owns: the value
// interned when the insert began, the memoized inserter results, and the memo
// keys. It detaches every field before releasing anything, so a release panic
// cannot make the deferred second call retry the same reference and mask the
// original failure. A second call is otherwise a no-op. That lets callers both
// defer it for panic safety and call it explicitly before the audit runs.
func (iRec *insertRecord) releaseResolved() {
value := iRec.value
memo := iRec.memo
memoFirst := iRec.memoFirst
memoResult := iRec.memoResult
memoSet := iRec.memoSet
iRec.value = nilValueRef
iRec.memo = nil
iRec.memoFirst = nilValueRef
iRec.memoResult = nilValueRef
iRec.memoSet = false
iRec.store.release(value)
if memo != nil {
for key, value := range memo {
iRec.store.release(key)
iRec.store.release(value)
}
} else if memoSet {
iRec.store.release(memoFirst)
iRec.store.release(memoResult)
}
}
// replaceDataRecord is the guarded mutation path for a record's value: it
// releases the old value exactly once and stores the new one. The owned flag
// is resolve's ownership handoff. When the caller already owns the incoming
// reference, the record adopts it; otherwise the record retains its own. A
// value equal to the old one leaves the record untouched, releasing the
// incoming reference if it was owned.
func (iRec *insertRecord) replaceDataRecord(
r *record,
value valueRef,
owned bool,
) {
oldValue := r.value
r.nodeIndex = iRec.insertedNode
if value == nilValueRef {
r.recordType = recordTypeEmpty
r.value = nilValueRef
iRec.store.release(oldValue)
return
}
r.recordType = recordTypeData
if oldValue != value {
if !owned {
iRec.store.retain(value)
}
r.value = value
iRec.store.release(oldValue)
return
}
if owned {
iRec.store.release(value)
}
}
func newNodeIndex(index int) nodeIndex {
if index < 0 {
panic("node index is negative")
}
if uint64(index) >= uint64(noNodeIndex) {
panic("node index exceeds usable range")
}
return nodeIndex(index)
}
func (t *Tree) newNode(children [2]record) nodeIndex {
index := newNodeIndex(t.nodeCountAllocated)
if t.nodeCountAllocated == len(t.nodeBlocks)*nodeBlockSize {
t.nodeBlocks = append(t.nodeBlocks, make([]node, nodeBlockSize))
}
// Node blocks are never reallocated, which keeps node pointers stable while
// insertion allocates more nodes. Dead nodes are not reclaimed.
t.nodeCountAllocated++
*t.nodeAt(index) = node{children: children}
return index
}
func (t *Tree) nodeAt(index nodeIndex) *node {
return &t.nodeBlocks[int(index)/nodeBlockSize][int(index)%nodeBlockSize]
}
// newPath stores a compressed path for a sparse insertion. This avoids
// allocating one node per remaining bit until a later insert reaches the path
// or finalize expands it. Path entries are not reclaimed after materialization.
func (t *Tree) newPath(ip [16]byte, endDepth int, record record) nodeIndex {
index := newNodeIndex(len(t.paths))
t.paths = append(t.paths, compressedPath{
ip: ip,
endDepth: endDepth,
record: record,
})
return index
}
// materializePath expands a compressed path into ordinary nodes starting at
// startDepth. The caller replaces the path record with the returned record.
func (t *Tree) materializePath(startDepth int, path compressedPath) record {
child := path.record
for depth := path.endDepth - 1; depth >= startDepth; depth-- {
var children [2]record
children[bitAt(path.ip, depth)] = child
child = record{
nodeIndex: t.newNode(children),
recordType: recordTypeNode,
}
}
return child
}
func (iRec *insertRecord) insertNode(
index nodeIndex,
currentDepth int,
) error {
newDepth := currentDepth + 1
node := iRec.tree.nodeAt(index)
// Check if we are inside the network already
if newDepth > iRec.prefixLen {
// Data already exists for the network so insert into all the children.
// Identical child records are merged as recursion unwinds.
//
// Record which child we take in iRec.ip. Navigation no longer reads
// these bits once the walk is deeper than prefixLen, so they can carry
// the descent path that metadata needs to report a record more
// specific than the insert. Both branches write the bit, because a
// sibling subtree may have left it set. Nothing restores it:
// maskedTreeAddr drops every bit past the record's own depth,
// PrefixFromInsertIP masks at prefixLen, and insertPrepared overwrites
// ip for the next subnet of a range.
setBitAt(&iRec.ip, currentDepth, 0)
err := iRec.insertRecord(&node.children[0], newDepth)
if err != nil {
return err
}
setBitAt(&iRec.ip, currentDepth, 1)
return iRec.insertRecord(&node.children[1], newDepth)
}
// We haven't reached the network yet.
pos := bitAt(iRec.ip, currentDepth)
return iRec.insertRecord(&node.children[pos], newDepth)
}
func (iRec *insertRecord) insertRecord(
r *record,
newDepth int,
) error {
switch r.recordType {
case recordTypeNode:
err := iRec.insertNode(r.nodeIndex, newDepth)
if err != nil {
return iRec.mergeChildrenAfterError(r, err)
}
return iRec.maybeMergeChildren(r)
case recordTypeFixedNode:
return iRec.insertNode(r.nodeIndex, newDepth)
case recordTypePath:
path := iRec.tree.paths[r.nodeIndex]
// materializePath moves the path record's value ownership into the
// expanded nodes. Zero the dead slot, so an accidental later read
// fails loudly instead of double-counting the moved reference.
iRec.tree.paths[r.nodeIndex].record = record{}
*r = iRec.tree.materializePath(newDepth, path)
return iRec.insertRecord(r, newDepth)
case recordTypeEmpty, recordTypeData:
if newDepth >= iRec.prefixLen {
if iRec.recordType == recordTypeData {
existingDepth := newDepth
if iRec.splitDepth != 0 {
existingDepth = int(iRec.splitDepth)
}
value, owned, err := iRec.resolveValue(r.value, existingDepth)
if err != nil {
return err
}
iRec.replaceDataRecord(r, value, owned)
} else {
// This mirrors replaceDataRecord's release-then-overwrite for
// a non-data target. It stays inline because the split case
// below transfers the old reference instead of releasing it,
// so a shared helper would cover only part of the pattern.
oldValue := r.value
r.nodeIndex = iRec.insertedNode
r.recordType = iRec.recordType
r.value = nilValueRef
iRec.store.release(oldValue)
}
return nil
}
if r.recordType == recordTypeEmpty && iRec.recordType == recordTypeData {
// newDepth is the record's own extent, with no splitDepth check.
// Only a data record splits, so a split chain never descends into
// an empty record and splitDepth is necessarily zero here.
value, owned, err := iRec.resolveValue(nilValueRef, newDepth)
if err != nil {
return err
}
if value == nilValueRef {
return nil
}
if !owned {
iRec.store.retain(value)
}
r.nodeIndex = iRec.tree.newPath(iRec.ip, iRec.prefixLen, record{
value: value,
recordType: recordTypeData,
})
r.recordType = recordTypePath
return nil
}
// We are splitting this record so we create two duplicate child
// records.
if iRec.splitDepth == 0 {
iRec.splitDepth = uint8(newDepth) //nolint:gosec // Tree depths cannot exceed 128.
}
if r.recordType == recordTypeData {
iRec.store.retain(r.value)
}
r.nodeIndex = iRec.tree.newNode([2]record{*r, *r})
r.value = nilValueRef
r.recordType = recordTypeNode
err := iRec.insertNode(r.nodeIndex, newDepth)
if err != nil {
return iRec.mergeChildrenAfterError(r, err)
}
return iRec.maybeMergeChildren(r)
case recordTypeReserved:
if iRec.prefixLen >= newDepth {
return newReservedNetworkError(iRec.ip, newDepth, iRec.prefixLen, iRec.tree.treeDepth)
}
// We are inserting a network that contains a reserved network. Leave
// the reserved record as it is, and do not report it to an inserter.
return nil
case recordTypeAlias:
if iRec.prefixLen < newDepth {
// Do nothing. We are inserting a network that contains an aliased
// network. We silently ignore.
return nil
}
// attempting to insert _into_ an aliased network
return newAliasedNetworkError(iRec.ip, newDepth, iRec.prefixLen, iRec.tree.treeDepth)
default:
return fmt.Errorf("inserting into record type %d is not implemented", r.recordType)
}
}
func (iRec *insertRecord) mergeChildrenAfterError(r *record, insertErr error) error {
mergeErr := iRec.maybeMergeChildren(r)
if mergeErr == nil {
return insertErr
}
// Name the source, so a log reader can tell a failure to restore the record
// boundaries from the insert failure that triggered the restore.
return errors.Join(insertErr, fmt.Errorf(
"restoring record boundaries after insert failure: %w",
mergeErr,
))
}
func (iRec *insertRecord) maybeMergeChildren(r *record) error {
// Check to see if the children are the same and can be merged.
// Use pointer access to avoid copying the record struct; this is
// called from every node-level insert, so the copies add up across
// millions of inserts.
node := iRec.tree.nodeAt(r.nodeIndex)
child0 := &node.children[0]
child1 := &node.children[1]
if child0.recordType != child1.recordType {
return nil
}
switch child0.recordType {
// Node-like and compressed-path records can't be merged by record equality.
case recordTypeFixedNode, recordTypeNode, recordTypePath:
return nil
case recordTypeEmpty, recordTypeReserved:
r.recordType = child0.recordType
r.nodeIndex = noNodeIndex
return nil
case recordTypeData:
// The store keeps exactly one live node per wire-equal value, so
// reference equality here is value equality.
if child0.value != child1.value {
return nil
}
// Children have same data and can be merged
r.recordType = recordTypeData
r.value = child0.value
iRec.store.release(child1.value)
r.nodeIndex = noNodeIndex
return nil
default:
return fmt.Errorf("merging record type %d is not implemented", child0.recordType)
}
}
func (t *Tree) getNode(
index nodeIndex,
ip [16]byte,
depth int,
) (int, record) {
n := t.nodeAt(index)
r := n.children[bitAt(ip, depth)]
depth++
return t.getRecord(r, ip, depth)
}
func (t *Tree) getRecord(
r record,
ip [16]byte,
depth int,
) (int, record) {
if r.recordType == recordTypePath {
path := t.paths[r.nodeIndex]
for pathDepth := depth; pathDepth < path.endDepth; pathDepth++ {
if bitAt(ip, pathDepth) != bitAt(path.ip, pathDepth) {
return pathDepth + 1, record{}
}
}
return t.getRecord(path.record, ip, path.endDepth)
}
switch r.recordType {
case recordTypeNode, recordTypeAlias, recordTypeFixedNode:
return t.getNode(r.nodeIndex, ip, depth)
default:
return depth, r
}
}
func (t *Tree) expandPaths(index nodeIndex, currentDepth int) {
n := t.nodeAt(index)
for i := range 2 {
child := &n.children[i]
recordDepth := currentDepth + 1
switch child.recordType {
case recordTypePath:
path := t.paths[child.nodeIndex]
// Zero the dead slot, as in insertRecord's path case.
t.paths[child.nodeIndex].record = record{}
*child = t.materializePath(recordDepth, path)
if child.recordType == recordTypeNode {
t.expandPaths(child.nodeIndex, recordDepth)
}
case recordTypeNode, recordTypeFixedNode:
t.expandPaths(child.nodeIndex, recordDepth)
case recordTypeEmpty, recordTypeData, recordTypeAlias, recordTypeReserved:
}
}
}
// finalizeNode assigns node numbers depth-first. expandPaths must run before
// this so compressed paths cannot be confused with node indexes.
func (t *Tree) finalizeNode(index nodeIndex, currentNum int) int {
n := t.nodeAt(index)
t.nodeNumbers[index] = currentNum
currentNum++
for i := range 2 {
switch n.children[i].recordType {
case recordTypeFixedNode,
recordTypeNode:
currentNum = t.finalizeNode(n.children[i].nodeIndex, currentNum)
case recordTypePath:
panic("compressed path found after expandPaths")
default:
}
}
return currentNum
}
func bitAt(ip [16]byte, depth int) byte {
return (ip[depth/8] >> (7 - (depth % 8))) & 1
}
// setBitAt sets the bit at depth in ip to bit, which must be 0 or 1.
func setBitAt(ip *[16]byte, depth int, bit byte) {
shift := 7 - (depth % 8)
mask := byte(1) << shift
ip[depth/8] = ip[depth/8]&^mask | bit<<shift
}
func maskedTreeAddr(ip [16]byte, depth int) [16]byte {
byteIndex := depth / 8
if remainingBits := depth % 8; remainingBits != 0 {
ip[byteIndex] &= byte(0xff << (8 - remainingBits))
byteIndex++
}
clear(ip[byteIndex:])
return ip
}