| 1 |
package schema |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"hash/maphash" |
| 6 |
) |
| 7 |
|
| 8 |
// errValuesLength is returned by DecodeValues for keys and values of |
| 9 |
// different lengths. |
| 10 |
var errValuesLength = errors.New("schema: keys and values differ in length") |
| 11 |
|
| 12 |
const ( |
| 13 |
// maxInlinePairs is how many pairs DecodeValues groups in arrays on its |
| 14 |
// own stack; more are grouped in slices allocated for the call. |
| 15 |
maxInlinePairs = 32 |
| 16 |
// maxScanPairs is how many pairs are grouped by comparing each key with |
| 17 |
// the keys before it rather than through a hash table, which for so few |
| 18 |
// costs more to hash than to compare. |
| 19 |
maxScanPairs = 8 |
| 20 |
) |
| 21 |
|
| 22 |
// source is what a decode reads the values of a key from: the map Decode was |
| 23 |
// given, or the pairs DecodeValues was, grouped by key. It is a struct rather |
| 24 |
// than an interface so that a DecodeValues call's index, held on its stack, |
| 25 |
// does not escape through a method call the compiler cannot see into. |
| 26 |
type source struct { |
| 27 |
m map[string][]string |
| 28 |
pairs *pairIndex |
| 29 |
} |
| 30 |
|
| 31 |
// lookup returns the values of key as far as the required-key and default |
| 32 |
// checks read them, which is whether there are any and what the first one is: |
| 33 |
// all of them from a map, and the first alone from pairs, which saves |
| 34 |
// gathering them. It reports whether src has the key. |
| 35 |
func (src *source) lookup(key string) ([]string, bool) { |
| 36 |
if src.pairs != nil { |
| 37 |
if i := src.pairs.find(key); i >= 0 { |
| 38 |
return src.pairs.values[i : i+1 : i+1], true |
| 39 |
} |
| 40 |
return nil, false |
| 41 |
} |
| 42 |
v, ok := src.m[key] |
| 43 |
return v, ok |
| 44 |
} |
| 45 |
|
| 46 |
// hasBytes reports whether src has the key held in key. |
| 47 |
func (src *source) hasBytes(key []byte) bool { |
| 48 |
if src.pairs != nil { |
| 49 |
return src.pairs.findBytes(key) >= 0 |
| 50 |
} |
| 51 |
_, ok := src.m[string(key)] |
| 52 |
return ok |
| 53 |
} |
| 54 |
|
| 55 |
// pairIndex groups key/value pairs by key without building a map: keys are |
| 56 |
// hashed into an open-addressing table that holds the first pair of each |
| 57 |
// key, or for a few pairs compared with the keys before them, and each pair |
| 58 |
// links to the next with the same key. A key's values are then the values of |
| 59 |
// its pairs, which a single pair or a run of adjacent ones hands out as a |
| 60 |
// subslice of values, and any other pattern gathers into a buffer of the |
| 61 |
// caller's. |
| 62 |
// |
| 63 |
// The keys are the request's, so the table hashes them as a Go map would: |
| 64 |
// exactly, as they are grouped, and with a seed drawn per process (see |
| 65 |
// pairSeed), so no choice of keys can pile them onto one slot. |
| 66 |
// |
| 67 |
// DecodeValues sets its slices on it where it lies, and its methods store |
| 68 |
// integers only, into those slices and mask: a slice stored through a pointer |
| 69 |
// to it would count, for the compiler, as a store to the heap, and take the |
| 70 |
// arrays DecodeValues keeps the index in on its stack with it. |
| 71 |
type pairIndex struct { |
| 72 |
keys, values []string |
| 73 |
// table holds, for each key, its first pair plus one, so that 0 marks a |
| 74 |
// free slot; it is a power of two long and at most half full, and nil |
| 75 |
// for maxScanPairs pairs or fewer |
| 76 |
table []int32 |
| 77 |
// next holds, for each pair, the next pair with the same key plus one, |
| 78 |
// or 0 for its key's last pair |
| 79 |
next []int32 |
| 80 |
// last holds, for a key's first pair, the key's last pair, and -1 for |
| 81 |
// every other pair |
| 82 |
last []int32 |
| 83 |
mask uint64 |
| 84 |
} |
| 85 |
|
| 86 |
// pairSeed seeds the hash of pairIndex. A hash an attacker could predict, |
| 87 |
// or one under which distinct keys collide by construction, as keys that |
| 88 |
// differ only in case do under a case-folding hash, would let a request |
| 89 |
// with enough such keys make every insert probe past all the keys before |
| 90 |
// it, quadratic in their number. |
| 91 |
var pairSeed = maphash.MakeSeed() |
| 92 |
|
| 93 |
// pairHash is the hash pairIndex keeps key in, and pairHashBytes the same |
| 94 |
// hash of a key held in a byte slice. |
| 95 |
func pairHash(key string) uint64 { return maphash.String(pairSeed, key) } |
| 96 |
|
| 97 |
func pairHashBytes(key []byte) uint64 { return maphash.Bytes(pairSeed, key) } |
| 98 |
|
| 99 |
// indexSize returns the table length that indexes n pairs. |
| 100 |
func indexSize(n int) int { |
| 101 |
size := 2 |
| 102 |
for size < 2*n { |
| 103 |
size *= 2 |
| 104 |
} |
| 105 |
return size |
| 106 |
} |
| 107 |
|
| 108 |
// index fills in the index of the pairs p.keys[i]=p.values[i], kept in |
| 109 |
// p.table, p.next and p.last, which must be zeroed and sized |
| 110 |
// indexSize(len(p.keys)) (or nil, for maxScanPairs pairs or fewer), |
| 111 |
// len(p.keys) and len(p.keys). |
| 112 |
func (p *pairIndex) index() { |
| 113 |
if p.table == nil { |
| 114 |
p.scan() |
| 115 |
return |
| 116 |
} |
| 117 |
p.mask = uint64(len(p.table) - 1) |
| 118 |
p.build() |
| 119 |
} |
| 120 |
|
| 121 |
// scan fills in the index of p's pairs, few enough to compare each key with |
| 122 |
// the first pairs before it. |
| 123 |
func (p *pairIndex) scan() { |
| 124 |
keys := p.keys |
| 125 |
for i, key := range keys { |
| 126 |
p.last[i] = int32(i) //nolint:gosec // G115 - bounded by the number of pairs |
| 127 |
for first := range i { |
| 128 |
if p.last[first] >= 0 && keys[first] == key { |
| 129 |
p.next[p.last[first]] = int32(i + 1) //nolint:gosec // G115 - bounded by the number of pairs |
| 130 |
p.last[first] = int32(i) //nolint:gosec // G115 - bounded by the number of pairs |
| 131 |
p.last[i] = -1 |
| 132 |
break |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// build fills in the index of p's pairs through the hash table. |
| 139 |
func (p *pairIndex) build() { |
| 140 |
keys := p.keys |
| 141 |
for i, key := range keys { |
| 142 |
for j := pairHash(key) & p.mask; ; j = (j + 1) & p.mask { |
| 143 |
e := p.table[j] |
| 144 |
if e == 0 { |
| 145 |
p.table[j] = int32(i + 1) //nolint:gosec // G115 - bounded by the number of pairs |
| 146 |
p.last[i] = int32(i) //nolint:gosec // G115 - bounded by the number of pairs |
| 147 |
break |
| 148 |
} |
| 149 |
if first := e - 1; keys[first] == key { |
| 150 |
p.next[p.last[first]] = int32(i + 1) //nolint:gosec // G115 - bounded by the number of pairs |
| 151 |
p.last[first] = int32(i) //nolint:gosec // G115 - bounded by the number of pairs |
| 152 |
p.last[i] = -1 |
| 153 |
break |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// first reports whether pair i is the first pair of its key. |
| 160 |
func (p *pairIndex) first(i int) bool { |
| 161 |
return p.last[i] >= 0 |
| 162 |
} |
| 163 |
|
| 164 |
// find returns the first pair of key, or -1 when no pair has it. |
| 165 |
func (p *pairIndex) find(key string) int { |
| 166 |
if p.table == nil { |
| 167 |
for first, k := range p.keys { |
| 168 |
if p.last[first] >= 0 && k == key { |
| 169 |
return first |
| 170 |
} |
| 171 |
} |
| 172 |
return -1 |
| 173 |
} |
| 174 |
for j := pairHash(key) & p.mask; ; j = (j + 1) & p.mask { |
| 175 |
e := p.table[j] |
| 176 |
if e == 0 { |
| 177 |
return -1 |
| 178 |
} |
| 179 |
if first := int(e - 1); p.keys[first] == key { |
| 180 |
return first |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// findBytes is find for a key held in a byte slice. |
| 186 |
func (p *pairIndex) findBytes(key []byte) int { |
| 187 |
if p.table == nil { |
| 188 |
for first, k := range p.keys { |
| 189 |
if p.last[first] >= 0 && k == string(key) { |
| 190 |
return first |
| 191 |
} |
| 192 |
} |
| 193 |
return -1 |
| 194 |
} |
| 195 |
for j := pairHashBytes(key) & p.mask; ; j = (j + 1) & p.mask { |
| 196 |
e := p.table[j] |
| 197 |
if e == 0 { |
| 198 |
return -1 |
| 199 |
} |
| 200 |
if first := int(e - 1); p.keys[first] == string(key) { |
| 201 |
return first |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// group returns the values of the key whose first pair is i, capped at their |
| 207 |
// length: a subslice of values when its pairs are adjacent, else the values |
| 208 |
// appended to scratch. |
| 209 |
func (p *pairIndex) group(i int, scratch []string) []string { |
| 210 |
end := i |
| 211 |
for n := p.next[end]; n != 0; n = p.next[end] { |
| 212 |
if int(n-1) != end+1 { |
| 213 |
for { |
| 214 |
scratch = append(scratch, p.values[i]) |
| 215 |
n := p.next[i] |
| 216 |
if n == 0 { |
| 217 |
return scratch[:len(scratch):len(scratch)] |
| 218 |
} |
| 219 |
i = int(n - 1) |
| 220 |
} |
| 221 |
} |
| 222 |
end++ |
| 223 |
} |
| 224 |
return p.values[i : end+1 : end+1] |
| 225 |
} |