/
namespace_range.go
297 lines (261 loc) · 8.2 KB
/
namespace_range.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
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
package main
import (
"fmt"
"strings"
"sort"
"math/big"
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
)
const namespaceKind = "__namespace__"
type (
// NamespaceRange represents a namespace range
NamespaceRange struct {
Start string `json:"start"`
End string `json:"end"`
}
byStart []*NamespaceRange
)
var (
lexDistance []*big.Int
namespaceCharacters string
maxNamespaceLength int
namespaceBatchSize int
minNamespace string
maxNamespace string
)
func init() {
setupConstants("-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz", 100, 50)
}
func setupConstants(alphabet string, maxLength, batchSize int) {
namespaceCharacters = alphabet
maxNamespaceLength = maxLength
minNamespace = ""
maxNamespace = strings.Repeat(alphabet[len(alphabet)-1:], maxLength)
namespaceBatchSize = batchSize
lexDistance = make([]*big.Int, maxLength)
lexDistance[0] = big.NewInt(1)
length := big.NewInt(int64(len(alphabet)))
for i := 1; i < maxLength; i++ {
temp := new(big.Int)
temp.Mul(lexDistance[i-1], length)
temp.Add(temp, big.NewInt(1))
lexDistance[i] = temp
}
}
func newNamespaceRange(start, end string) *NamespaceRange {
if start == "" {
start = minNamespace
}
if end == "" {
end = maxNamespace
}
if start > end {
// error
}
return &NamespaceRange{
Start: start,
End: end,
}
}
func (s byStart) Len() int { return len(s) }
func (s byStart) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byStart) Less(i, j int) bool { return s[i].Start < s[j].Start }
// IsSingleNamespace returns True if the namespace range only includes a single namespace
func (n *NamespaceRange) IsSingleNamespace() bool {
return n.Start == n.End
}
// Split splits the NamespaceRange into two nearly equal-sized ranges
// If this NamespaceRange contains a single namespace then a list containing
// this NamespaceRange is returned. Otherwise a two-element list containing
// two NamespaceRanges whose total range is identical to this
// NamespaceRange's is returned.
func (n *NamespaceRange) Split() (*NamespaceRange, *NamespaceRange) {
if n.IsSingleNamespace() {
return n, nil
}
midPoint := new(big.Int)
midPoint.Add(namespaceToOrd(n.Start), namespaceToOrd(n.End))
midPoint.Div(midPoint, big.NewInt(2))
left := newNamespaceRange(n.Start, ordToNamespace(midPoint, 0))
right := newNamespaceRange(ordToNamespace(midPoint.Add(midPoint, big.NewInt(1)), 0), n.End)
return left, right
}
// WithStartAfter returns a copy of this NamespaceRange with a new start
func (n *NamespaceRange) WithStartAfter(afterNamespace string) *NamespaceRange {
temp := new(big.Int)
namespaceStart := ordToNamespace(temp.Add(namespaceToOrd(afterNamespace), big.NewInt(1)), 0)
return newNamespaceRange(namespaceStart, n.End)
}
// MakeDatastoreQuery returns a datastore.Query that generates all namespaces in the range
func (n *NamespaceRange) MakeDatastoreQuery(c context.Context, start string) *datastore.Query {
q := datastore.NewQuery(namespaceKind)
if n.Start != "" {
q = q.Filter("__key__ >=", datastore.NewKey(c, namespaceKind, n.Start, 0, nil))
}
q = q.Filter("__key__ <=", datastore.NewKey(c, namespaceKind, n.End, 0, nil))
q = q.Order("__key__")
q = q.KeysOnly()
if start != "" {
cursor, _ := datastore.DecodeCursor(start)
q = q.Start(cursor)
}
return q
}
// NormalizedStart returns a NamespaceRange with leading non-existant namespaces removed
// A copy of this NamespaceRange whose namespace_start is adjusted to exclude
// the portion of the range that contains no actual namespaces in the
// datastore. None is returned if the NamespaceRange contains no actual
// namespaces in the datastore.
func (n *NamespaceRange) NormalizedStart(c context.Context) *NamespaceRange {
q := n.MakeDatastoreQuery(c, "")
namespaceAfterKey, _ := q.Limit(1).GetAll(c, nil)
// fmt.Printf("NormalizedStart %s %#v\n", n.Start, namespaceAfterKey)
if len(namespaceAfterKey) == 0 {
return nil
}
return newNamespaceRange(namespaceAfterKey[0].Namespace(), n.End)
}
// Convert a namespace ordinal to a namespace string
func ordToNamespace(n *big.Int, maxLength int) string {
if n.Int64() == 0 {
return ""
}
if maxLength == 0 {
maxLength = maxNamespaceLength
}
maxLength--
length := lexDistance[maxLength]
tmp := new(big.Int)
tmp.Sub(n, big.NewInt(1))
index := new(big.Int)
index.Div(tmp, length)
mod := new(big.Int)
mod.Mod(tmp, length)
return namespaceCharacters[index.Int64():index.Int64() + 1] + ordToNamespace(mod, maxLength)
}
// Converts a namespace string into an int representing its lexographic order
func namespaceToOrd(namespace string) *big.Int {
n := new(big.Int)
for i, c := range namespace {
pos := strings.IndexRune(namespaceCharacters, c)
tmp := new(big.Int)
ld := lexDistance[maxNamespaceLength - i - 1]
tmp.Mul(ld, big.NewInt(int64(pos)))
n.Add(n, tmp)
n.Add(n, big.NewInt(1))
}
return n
}
func getNamespaces(c context.Context, limit int) ([]string, error) {
q := datastore.NewQuery(namespaceKind).Limit(limit).KeysOnly()
keys, err := q.GetAll(c, nil)
if err != nil {
return nil, err
}
names := make([]string, 0, len(keys))
for _, k := range keys {
names = append(names, k.StringID())
}
return names, nil
}
// Splits the complete NamespaceRange into n equally-sized NamespaceRanges.
func namespaceSplit(c context.Context, n int, contiguous, canQuery bool) ([]*NamespaceRange, error) {
if n < 1 {
return nil, fmt.Errorf("n must be >= 1")
}
nsRanges := []*NamespaceRange{}
if canQuery {
if contiguous {
nsRange := newNamespaceRange("", "")
nsRange = nsRange.NormalizedStart(c)
nsRanges = append(nsRanges, nsRange)
} else {
namespaces, err := getNamespaces(c, n + 1)
// fmt.Println(namespaces)
if err != nil || len(namespaces) == 0 {
return nsRanges, nil
}
if len(namespaces) < n {
// If we have less actual namespaces than number of NamespaceRanges
// to return, then just return the list of those namespaces.
for _, ns := range namespaces {
nsRanges = append(nsRanges, newNamespaceRange(ns, ns))
}
sort.Sort(byStart(nsRanges))
return nsRanges, nil
}
nsRanges = append(nsRanges, newNamespaceRange(namespaces[0], ""))
}
} else {
nsRanges = append(nsRanges, newNamespaceRange("", ""))
}
//for _, nsRange := range nsRanges {
// fmt.Printf("start '%s' end '%s'\n", nsRange.Start, nsRange.End)
//}
singles := []*NamespaceRange{}
for len(nsRanges) > 0 && (len(nsRanges) + len(singles) < n) {
// fmt.Printf("\nranges: %d\n", len(nsRanges))
// for _, nsRange := range nsRanges {
// fmt.Printf("%#v\n", nsRange)
// }
nsRange := nsRanges[0]
nsRanges = append(nsRanges[:0], nsRanges[1:]...)
// nsRanges = nsRanges[1:len(nsRanges)]
// nsRanges, nsRanges[len(nsRanges)-1] = nsRanges[1:], nil
if nsRange.IsSingleNamespace() {
singles = append(singles, nsRange)
} else {
left, right := nsRange.Split()
// fmt.Printf("\nleft %#v\nright %#v\n", left, right)
if right != nil {
if canQuery {
right = right.NormalizedStart(c)
}
if right != nil {
nsRanges = append(nsRanges, right)
}
}
nsRanges = append(nsRanges, left)
}
}
nsRanges = append(nsRanges, singles...)
sort.Sort(byStart(nsRanges))
// fmt.Printf("\nsorted: %d\n", len(nsRanges))
// for _, nsRange := range nsRanges {
// fmt.Printf("%#v\n", nsRange)
// }
if contiguous {
if len(nsRanges) == 0 {
// This condition is possible if every namespace was deleted after the
// first call to ns_range.normalized_start().
nsRanges = []*NamespaceRange{newNamespaceRange("", "")}
return nsRanges, nil
}
continuousRanges := []*NamespaceRange{}
for i := 0; i < len(nsRanges); i++ {
// fmt.Printf("\ncontinuous: %d\n", len(nsRanges))
// for _, nsRange := range nsRanges {
// fmt.Printf("%#v\n", nsRange)
// }
var start string
if i == 0 {
start = minNamespace
} else {
start = nsRanges[i].Start
}
var end string
if i == len(nsRanges) - 1 {
end = maxNamespace
} else {
tmp := new(big.Int)
tmp.Sub(namespaceToOrd(nsRanges[i+1].Start), big.NewInt(1))
end = ordToNamespace(tmp, 0)
}
// fmt.Printf("start %s end %s\n", start, end)
continuousRanges = append(continuousRanges, newNamespaceRange(start, end))
}
return continuousRanges, nil
}
return nsRanges, nil
}