Skip to content

Commit

Permalink
use a single set and encode stuff in map value
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Feb 1, 2025
1 parent 852f39c commit 4aca336
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 41 deletions.
46 changes: 15 additions & 31 deletions internal/origins/radix.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (t *Tree) Insert(keyPattern string, scheme string, port int) {
n.add(scheme, port, hasLeadingAsterisk)
return
}
if n.wSet.Contains(scheme, port) {
if n.set.Contains(scheme, port, hasLeadingAsterisk) {
return
}
child := n.edges[labelToChild]
Expand Down Expand Up @@ -91,11 +91,11 @@ func (t *Tree) Contains(k string, scheme string, port int) bool {
for {
label, ok := lastByte(k)
if !ok {
return n.set.Contains(scheme, port) || n.set.Contains(scheme, WildcardPort)
return n.set.Contains(scheme, port, false) || n.set.Contains(scheme, WildcardPort, false)
}

// k is not empty; check wildcard edge
if n.wSet.Contains(scheme, port) || n.wSet.Contains(scheme, WildcardPort) {
if n.set.Contains(scheme, port, true) || n.set.Contains(scheme, WildcardPort, true) {
return true
}

Expand Down Expand Up @@ -158,9 +158,6 @@ type node struct {
edges edges
// values in this node
set SchemePorts
// values in the "conceptual" child node down the wildcard edge
// that stems from this node
wSet SchemePorts
}

type pair struct {
Expand All @@ -169,20 +166,17 @@ type pair struct {
}

func (n *node) add(scheme string, port int, toWildcardSet bool) {
var set *SchemePorts
if toWildcardSet {
set = &n.wSet
} else {
set = &n.set
if port == WildcardPort || n.set == nil {
n.set = NewSchemePorts(scheme, port, toWildcardSet)
return
}
if port == WildcardPort || *set == nil {
*set = NewSchemePorts(scheme, port)
if !toWildcardSet && n.set.Contains(scheme, WildcardPort, false) { // nothing to do
return
}
if set.Contains(scheme, WildcardPort) { // nothing to do
if toWildcardSet && n.set.Contains(scheme, WildcardPort, true) { // nothing to do
return
}
set.Add(scheme, port)
n.set.Add(scheme, port, toWildcardSet)
}

func (n *node) upsertEdge(label byte, child *node) {
Expand All @@ -200,8 +194,12 @@ type edges = map[byte]*node
func (n *node) Elems(dst *[]string, suf string) {
suf = n.suf + suf
for scheme, ports := range n.set {
for port := range ports {
for port, i := range ports {
var s string
var prefix string
if i&2 == 2 {
prefix = "*"
}
switch port {
case WildcardPort:
s = suf + ":*"
Expand All @@ -210,21 +208,7 @@ func (n *node) Elems(dst *[]string, suf string) {
default:
s = suf + ":" + strconv.Itoa(port)
}
*dst = append(*dst, scheme+"://"+s)
}
}
for scheme, ports := range n.wSet {
for port := range ports {
var s string
switch port {
case WildcardPort:
s = "*" + suf + ":*"
case 0:
s = "*" + suf
default:
s = "*" + suf + ":" + strconv.Itoa(port)
}
*dst = append(*dst, scheme+"://"+s)
*dst = append(*dst, scheme+"://"+prefix+s)
}
}
for _, child := range n.edges {
Expand Down
26 changes: 16 additions & 10 deletions internal/origins/schemeports.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package origins

type SchemePorts map[string]map[int]struct{} // TODO: replace struct by int: 1 means exact match, 2 means arbitrary subs, 3 means both
type SchemePorts map[string]map[int]int // TODO: replace struct by int: 0: nothing, 1 means exact match, 2 means arbitrary subs, 3 means both
// TODO: nil ports for scheme means all ports allowed?

func NewSchemePorts(scheme string, port int) SchemePorts {
func NewSchemePorts(scheme string, port int, wildcardSubs bool) SchemePorts {
set := make(SchemePorts)
set.Add(scheme, port)
set.Add(scheme, port, wildcardSubs)
return set
}

func (s SchemePorts) Add(scheme string, port int) {
var m map[int]struct{}
func (s SchemePorts) Add(scheme string, port int, wildcardSubs bool) {
var m map[int]int
if m = s[scheme]; m == nil {
m = make(map[int]struct{})
m = make(map[int]int)
}
m[port] = struct{}{}
var i int
if wildcardSubs {
i = 2
} else {
i = 1
}
m[port] = m[port] | i
s[scheme] = m
}

func (s SchemePorts) Contains(scheme string, port int) bool {
func (s SchemePorts) Contains(scheme string, port int, wildcardSubs bool) bool {
ports, found := s[scheme]
if !found {
return false
}
_, found = ports[port]
return found
v := ports[port]
return !wildcardSubs && v&1 == 1 || wildcardSubs && v&2 == 2
}

0 comments on commit 4aca336

Please sign in to comment.