-
Notifications
You must be signed in to change notification settings - Fork 92
/
topology.go
185 lines (155 loc) · 4.1 KB
/
topology.go
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rule
import (
"math"
"sort"
"sync"
)
import (
"golang.org/x/exp/slices"
)
// Topology represents the topology of databases and tables.
type Topology struct {
mu sync.RWMutex
dbRender, tbRender func(int) string
idx sync.Map // map[int][]int
}
// Len returns the length of database and table.
func (to *Topology) Len() (dbLen int, tblLen int) {
to.idx.Range(func(_, value any) bool {
dbLen++
tblLen += len(value.([]int))
return true
})
return
}
// SetTopology sets the topology.
func (to *Topology) SetTopology(db int, tables ...int) {
if len(tables) < 1 {
to.idx.Delete(db)
return
}
clone := make([]int, len(tables))
copy(clone, tables)
sort.Ints(clone)
to.idx.Store(db, clone)
}
// SetRender sets the database/table name render.
func (to *Topology) SetRender(dbRender, tbRender func(int) string) {
to.mu.Lock()
to.dbRender, to.tbRender = dbRender, tbRender
to.mu.Unlock()
}
// Render renders the name of database and table from indexes.
func (to *Topology) Render(dbIdx, tblIdx int) (string, string, bool) {
to.mu.RLock()
defer to.mu.RUnlock()
if to.tbRender == nil || to.dbRender == nil {
return "", "", false
}
return to.dbRender(dbIdx), to.tbRender(tblIdx), true
}
func (to *Topology) EnumerateDatabases() []string {
to.mu.RLock()
render := to.dbRender
to.mu.RUnlock()
var keys []string
to.idx.Range(func(key, _ any) bool {
keys = append(keys, render(key.(int)))
return true
})
sort.Strings(keys)
return keys
}
func (to *Topology) Enumerate() DatabaseTables {
to.mu.RLock()
dbRender, tbRender := to.dbRender, to.tbRender
to.mu.RUnlock()
dt := make(DatabaseTables)
to.Each(func(dbIdx, tbIdx int) bool {
d := dbRender(dbIdx)
t := tbRender(tbIdx)
dt[d] = append(dt[d], t)
return true
})
return dt
}
func (to *Topology) Exists(dbIdx, tbIdx int) bool {
exist, ok := to.idx.Load(dbIdx)
if !ok {
return false
}
_, ok = slices.BinarySearch(exist.([]int), tbIdx)
return ok
}
// Each enumerates items in current Topology.
func (to *Topology) Each(onEach func(dbIdx, tbIdx int) (ok bool)) bool {
done := true
to.idx.Range(func(key, value any) bool {
var (
d = key.(int)
v = value.([]int)
)
for _, t := range v {
if !onEach(d, t) {
done = false
return false
}
}
return true
})
return done
}
func (to *Topology) Smallest() (db, tb string, ok bool) {
to.mu.RLock()
dbRender, tbRender := to.dbRender, to.tbRender
to.mu.RUnlock()
smallest := [2]int{math.MaxInt64, math.MaxInt64}
to.idx.Range(func(key, value any) bool {
if d := key.(int); d < smallest[0] {
smallest[0] = d
if t := value.([]int); len(t) > 0 {
smallest[1] = t[0]
}
}
return true
})
if smallest[0] != math.MaxInt64 || smallest[1] != math.MaxInt64 {
db, tb, ok = dbRender(smallest[0]), tbRender(smallest[1]), true
}
return
}
func (to *Topology) Largest() (db, tb string, ok bool) {
to.mu.RLock()
dbRender, tbRender := to.dbRender, to.tbRender
to.mu.RUnlock()
largest := [2]int{math.MinInt64, math.MinInt64}
to.idx.Range(func(key, value any) bool {
if d := key.(int); d > largest[0] {
largest[0] = d
if t := value.([]int); len(t) > 0 {
largest[1] = t[len(t)-1]
}
}
return true
})
if largest[0] != math.MinInt64 || largest[1] != math.MinInt64 {
db, tb, ok = dbRender(largest[0]), tbRender(largest[1]), true
}
return
}