Skip to content

Commit 19a0fea

Browse files
committed
changed signature of Delete and DeletePersist
Delete(pfx netip.Prefix) (val V, found bool) DeletePersist(pfx netip.Prefix) (pt *Table[V], val V, found bool) deprecate GetAndDelete in favour of Delete deprecate GetAndDeletePersist in favour of DeletePersist
1 parent 1d55e21 commit 19a0fea

File tree

9 files changed

+66
-87
lines changed

9 files changed

+66
-87
lines changed

README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,21 @@ See the `ExampleLite_concurrent` and `ExampleTable_concurrent` tests for concret
7676
}
7777

7878
func (t *Table[V]) Contains(ip netip.Addr) bool
79-
func (t *Table[V]) Lookup(ip netip.Addr) (val V, ok bool)
79+
func (t *Table[V]) Lookup(ip netip.Addr) (V, bool)
8080

81-
func (t *Table[V]) LookupPrefix(pfx netip.Prefix) (val V, ok bool)
82-
func (t *Table[V]) LookupPrefixLPM(pfx netip.Prefix) (lpm netip.Prefix, val V, ok bool)
81+
func (t *Table[V]) LookupPrefix(pfx netip.Prefix) (V, bool)
82+
func (t *Table[V]) LookupPrefixLPM(pfx netip.Prefix) (netip.Prefix, V, bool)
8383

8484
func (t *Table[V]) Insert(pfx netip.Prefix, val V)
85-
func (t *Table[V]) Delete(pfx netip.Prefix)
8685
func (t *Table[V]) Modify(pfx netip.Prefix, func(V, bool) (V, bool)) (V, bool)
86+
func (t *Table[V]) Delete(pfx netip.Prefix) (V, bool)
87+
func (t *Table[V]) Get(pfx netip.Prefix) (V, bool)
8788

8889
func (t *Table[V]) InsertPersist(pfx netip.Prefix, val V) *Table[V]
89-
func (t *Table[V]) DeletePersist(pfx netip.Prefix) *Table[V]
9090
func (t *Table[V]) ModifyPersist(pfx netip.Prefix, func(val V, ok bool) (V, bool)) (*Table[V], V, bool)
91+
func (t *Table[V]) DeletePersist(pfx netip.Prefix) (*Table[V], V, bool)
9192
func (t *Table[V]) WalkPersist(func(*Table[V], netip.Prefix, V) (*Table[V], bool)) *Table[V]
9293

93-
func (t *Table[V]) Get(pfx netip.Prefix) (val V, ok bool)
94-
func (t *Table[V]) GetAndDelete(pfx netip.Prefix) (val V, ok bool)
95-
func (t *Table[V]) GetAndDeletePersist(pfx netip.Prefix) (pt *Table[V], val V, ok bool)
9694

9795
func (t *Table[V]) Clone() *Table[V]
9896
func (t *Table[V]) Union(o *Table[V])
@@ -151,7 +149,7 @@ Some delegated methods are pointless without a payload.
151149
func (l *Lite) Delete(pfx netip.Prefix)
152150

153151
func (l *Lite) InsertPersist(pfx netip.Prefix) *Lite
154-
func (l *Lite) DeletePersist(pfx netip.Prefix) *Lite
152+
func (l *Lite) DeletePersist(pfx netip.Prefix) (*Lite, bool)
155153
func (l *Lite) WalkPersist(fn func(*Lite, netip.Prefix) (*Lite, bool)) *Lite
156154

157155
func (l *Lite) Clone() *Lite

example_lite_concurrent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func ExampleLite_concurrent() {
6464

6565
// batch of deletes
6666
for _, pfx := range examplePrefixes {
67-
tbl = tbl.DeletePersist(pfx)
67+
tbl, _ = tbl.DeletePersist(pfx)
6868
}
6969

7070
liteAtomicPtr.Store(tbl)

example_table_concurrent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func ExampleTable_concurrent() {
8484

8585
// batch of deletes
8686
for _, pfx := range examplePrefixes {
87-
tbl = tbl.DeletePersist(pfx)
87+
tbl, _, _ = tbl.DeletePersist(pfx)
8888
}
8989

9090
tblAtomicPtr.Store(tbl)

lite.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
// The following methods are pointless without a payload:
1616
// - Lookup (use Contains)
1717
// - Get (use Exists)
18-
// - GetAndDelete
19-
// - GetAndDeletePersist
2018
// - Modify
2119
// - ModifyPersist
2220
type Lite struct {
@@ -48,15 +46,16 @@ func (l *Lite) InsertPersist(pfx netip.Prefix) *Lite {
4846
}
4947

5048
// Delete is a wrapper for the underlying table.
51-
func (l *Lite) Delete(pfx netip.Prefix) {
52-
l.Table.Delete(pfx)
49+
func (l *Lite) Delete(pfx netip.Prefix) bool {
50+
_, found := l.Table.Delete(pfx)
51+
return found
5352
}
5453

5554
// DeletePersist is an adapter for the underlying table.
56-
func (l *Lite) DeletePersist(pfx netip.Prefix) *Lite {
57-
tbl := l.Table.DeletePersist(pfx)
55+
func (l *Lite) DeletePersist(pfx netip.Prefix) (*Lite, bool) {
56+
tbl, _, found := l.Table.DeletePersist(pfx)
5857
//nolint:govet // copy of *tbl is here by intention
59-
return &Lite{*tbl}
58+
return &Lite{*tbl}, found
6059
}
6160

6261
// WalkPersist is an adapter for the underlying table.

lite_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func TestLiteInvalid(t *testing.T) {
7676
}
7777
}(testname)
7878

79-
_ = tbl1.DeletePersist(zeroPfx)
79+
_, _ = tbl1.DeletePersist(zeroPfx)
8080
})
8181

8282
testname = "Contains"
@@ -160,7 +160,7 @@ func TestLiteDeletePersist(t *testing.T) {
160160
// must not panic
161161
tbl := new(Lite)
162162
checkLiteNumNodes(t, tbl, 0)
163-
tbl = tbl.DeletePersist(randomPrefix(prng))
163+
tbl, _ = tbl.DeletePersist(randomPrefix(prng))
164164
checkLiteNumNodes(t, tbl, 0)
165165
})
166166

@@ -172,7 +172,7 @@ func TestLiteDeletePersist(t *testing.T) {
172172

173173
tbl.Insert(mpp("10.0.0.0/8"))
174174
checkLiteNumNodes(t, tbl, 1)
175-
tbl = tbl.DeletePersist(mpp("10.0.0.0/8"))
175+
tbl, _ = tbl.DeletePersist(mpp("10.0.0.0/8"))
176176
checkLiteNumNodes(t, tbl, 0)
177177
})
178178

@@ -185,7 +185,7 @@ func TestLiteDeletePersist(t *testing.T) {
185185
tbl.Insert(mpp("192.168.0.1/32"))
186186
checkLiteNumNodes(t, tbl, 1)
187187

188-
tbl = tbl.DeletePersist(mpp("192.168.0.1/32"))
188+
tbl, _ = tbl.DeletePersist(mpp("192.168.0.1/32"))
189189
checkLiteNumNodes(t, tbl, 0)
190190
})
191191

@@ -199,7 +199,7 @@ func TestLiteDeletePersist(t *testing.T) {
199199
tbl.Insert(mpp("192.180.0.1/32"))
200200
checkLiteNumNodes(t, tbl, 2)
201201

202-
tbl = tbl.DeletePersist(mpp("192.180.0.1/32"))
202+
tbl, _ = tbl.DeletePersist(mpp("192.180.0.1/32"))
203203
checkLiteNumNodes(t, tbl, 1)
204204
})
205205

@@ -215,7 +215,7 @@ func TestLiteDeletePersist(t *testing.T) {
215215

216216
checkLiteNumNodes(t, tbl, 2)
217217

218-
tbl = tbl.DeletePersist(mpp("192.180.0.1/32"))
218+
tbl, _ = tbl.DeletePersist(mpp("192.180.0.1/32"))
219219
checkLiteNumNodes(t, tbl, 2)
220220
})
221221

@@ -231,7 +231,7 @@ func TestLiteDeletePersist(t *testing.T) {
231231

232232
checkLiteNumNodes(t, tbl, 2)
233233

234-
tbl = tbl.DeletePersist(mpp("192.180.0.1/32"))
234+
tbl, _ = tbl.DeletePersist(mpp("192.180.0.1/32"))
235235
checkLiteNumNodes(t, tbl, 2)
236236
})
237237

@@ -244,7 +244,7 @@ func TestLiteDeletePersist(t *testing.T) {
244244
tbl.Insert(mpp("192.168.0.1/32"))
245245
checkLiteNumNodes(t, tbl, 1)
246246

247-
tbl = tbl.DeletePersist(mpp("200.0.0.0/32"))
247+
tbl, _ = tbl.DeletePersist(mpp("200.0.0.0/32"))
248248
checkLiteNumNodes(t, tbl, 1)
249249
})
250250

@@ -259,7 +259,7 @@ func TestLiteDeletePersist(t *testing.T) {
259259
tbl.Insert(mpp("192.168.0.0/22"))
260260
checkLiteNumNodes(t, tbl, 3)
261261

262-
tbl = tbl.DeletePersist(mpp("192.168.0.0/22"))
262+
tbl, _ = tbl.DeletePersist(mpp("192.168.0.0/22"))
263263
checkLiteNumNodes(t, tbl, 1)
264264
})
265265

@@ -270,7 +270,7 @@ func TestLiteDeletePersist(t *testing.T) {
270270

271271
tbl.Insert(mpp("0.0.0.0/0"))
272272
tbl.Insert(mpp("::/0"))
273-
tbl = tbl.DeletePersist(mpp("0.0.0.0/0"))
273+
tbl, _ = tbl.DeletePersist(mpp("0.0.0.0/0"))
274274

275275
checkLiteNumNodes(t, tbl, 1)
276276
})
@@ -284,10 +284,10 @@ func TestLiteDeletePersist(t *testing.T) {
284284
tbl.Insert(mpp("10.20.0.0/17"))
285285
checkLiteNumNodes(t, tbl, 2)
286286

287-
tbl = tbl.DeletePersist(mpp("10.20.0.0/17"))
287+
tbl, _ = tbl.DeletePersist(mpp("10.20.0.0/17"))
288288
checkLiteNumNodes(t, tbl, 1)
289289

290-
tbl = tbl.DeletePersist(mpp("10.10.0.0/17"))
290+
tbl, _ = tbl.DeletePersist(mpp("10.10.0.0/17"))
291291
checkLiteNumNodes(t, tbl, 0)
292292
})
293293
}
@@ -837,7 +837,8 @@ func TestLiteWalkPersist(t *testing.T) {
837837
"fd00::/8",
838838
},
839839
fn: func(pl *Lite, pfx netip.Prefix) (*Lite, bool) {
840-
return pl.DeletePersist(pfx), true // remove everything
840+
prt, _ := pl.DeletePersist(pfx)
841+
return prt, true // remove everything
841842
},
842843
wantRemain: []string{},
843844
},
@@ -849,7 +850,7 @@ func TestLiteWalkPersist(t *testing.T) {
849850
},
850851
fn: func(pl *Lite, pfx netip.Prefix) (*Lite, bool) {
851852
if pfx.Addr().Is4() {
852-
pl = pl.DeletePersist(pfx)
853+
pl, _ = pl.DeletePersist(pfx)
853854
}
854855
return pl, true
855856
},

persist_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestDeletePersistTable(t *testing.T) {
123123

124124
clone := orig
125125
for _, pfx := range pfxs {
126-
clone = clone.DeletePersist(pfx)
126+
clone, _, _ = clone.DeletePersist(pfx)
127127

128128
// Deleted prefix should be absent in clone
129129
_, ok := clone.Get(pfx)
@@ -198,7 +198,7 @@ func TestDeletePersistLite(t *testing.T) {
198198

199199
clone := orig
200200
for i, pfx := range pfxs {
201-
clone = clone.DeletePersist(pfx)
201+
clone, _ = clone.DeletePersist(pfx)
202202

203203
// test for existence
204204
if ok := orig.Exists(pfx); !ok {

table.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,14 @@ func (t *Table[V]) Modify(pfx netip.Prefix, cb func(val V, found bool) (_ V, del
418418
panic("unreachable")
419419
}
420420

421-
// Delete removes pfx from the tree, pfx does not have to be present.
422-
func (t *Table[V]) Delete(pfx netip.Prefix) {
423-
_, _ = t.getAndDelete(pfx)
421+
// Deprecated: use [Table.Delete] instead.
422+
func (t *Table[V]) GetAndDelete(pfx netip.Prefix) (val V, found bool) {
423+
return t.Delete(pfx)
424424
}
425425

426-
// GetAndDelete deletes the prefix and returns the associated payload for prefix and true,
426+
// Delete the prefix and returns the associated payload for prefix and true if found
427427
// or the zero value and false if prefix is not set in the routing table.
428-
func (t *Table[V]) GetAndDelete(pfx netip.Prefix) (val V, ok bool) {
429-
return t.getAndDelete(pfx)
430-
}
431-
432-
func (t *Table[V]) getAndDelete(pfx netip.Prefix) (val V, existed bool) {
428+
func (t *Table[V]) Delete(pfx netip.Prefix) (val V, found bool) {
433429
if !pfx.IsValid() {
434430
return
435431
}
@@ -459,8 +455,8 @@ func (t *Table[V]) getAndDelete(pfx netip.Prefix) (val V, existed bool) {
459455

460456
if depth == maxDepth {
461457
// try to delete prefix in trie node
462-
val, existed = n.prefixes.DeleteAt(art.PfxToIdx(octet, lastBits))
463-
if !existed {
458+
val, found = n.prefixes.DeleteAt(art.PfxToIdx(octet, lastBits))
459+
if !found {
464460
return
465461
}
466462

0 commit comments

Comments
 (0)