2019-04-19 07:55:03 +02:00
|
|
|
package serv
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"encoding/hex"
|
2019-09-05 06:09:56 +02:00
|
|
|
"io"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2019-07-29 07:13:33 +02:00
|
|
|
|
|
|
|
"github.com/cespare/xxhash/v2"
|
2019-09-05 06:09:56 +02:00
|
|
|
"github.com/dosco/super-graph/jsn"
|
2019-07-29 07:13:33 +02:00
|
|
|
)
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
func mkkey(h *xxhash.Digest, k1 string, k2 string) uint64 {
|
|
|
|
h.WriteString(k1)
|
|
|
|
h.WriteString(k2)
|
|
|
|
v := h.Sum64()
|
|
|
|
h.Reset()
|
2019-04-19 07:55:03 +02:00
|
|
|
|
2019-05-13 01:27:26 +02:00
|
|
|
return v
|
2019-04-19 07:55:03 +02:00
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
func gqlHash(b string, vars []byte) string {
|
|
|
|
b = strings.TrimSpace(b)
|
2019-07-29 07:13:33 +02:00
|
|
|
h := sha1.New()
|
2019-07-30 07:38:05 +02:00
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
s, e := 0, 0
|
2019-07-30 07:38:05 +02:00
|
|
|
space := []byte{' '}
|
|
|
|
|
|
|
|
var b0, b1 byte
|
2019-07-29 07:13:33 +02:00
|
|
|
|
|
|
|
for {
|
2019-07-30 07:38:05 +02:00
|
|
|
if ws(b[e]) {
|
|
|
|
for e < len(b) && ws(b[e]) {
|
2019-07-29 07:13:33 +02:00
|
|
|
e++
|
|
|
|
}
|
2019-07-30 07:38:05 +02:00
|
|
|
if e < len(b) {
|
|
|
|
b1 = b[e]
|
|
|
|
}
|
|
|
|
if al(b0) && al(b1) {
|
|
|
|
h.Write(space)
|
|
|
|
}
|
2019-07-29 07:13:33 +02:00
|
|
|
} else {
|
2019-07-30 07:38:05 +02:00
|
|
|
s = e
|
|
|
|
for e < len(b) && ws(b[e]) == false {
|
|
|
|
e++
|
|
|
|
}
|
|
|
|
if e != 0 {
|
|
|
|
b0 = b[(e - 1)]
|
|
|
|
}
|
2019-09-05 06:09:56 +02:00
|
|
|
io.WriteString(h, strings.ToLower(b[s:e]))
|
2019-07-30 07:38:05 +02:00
|
|
|
}
|
|
|
|
if e >= len(b) {
|
|
|
|
break
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 06:09:56 +02:00
|
|
|
if vars == nil {
|
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := jsn.Keys([]byte(vars))
|
|
|
|
|
|
|
|
sort.Slice(fields, func(i, j int) bool {
|
|
|
|
return bytes.Compare(fields[i], fields[j]) == -1
|
|
|
|
})
|
|
|
|
|
|
|
|
for i := range fields {
|
|
|
|
h.Write(fields[i])
|
|
|
|
}
|
|
|
|
|
2019-07-29 07:13:33 +02:00
|
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ws(b byte) bool {
|
2019-07-30 07:38:05 +02:00
|
|
|
return b == ' ' || b == '\n' || b == '\t' || b == ','
|
|
|
|
}
|
|
|
|
|
|
|
|
func al(b byte) bool {
|
|
|
|
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
|
2019-07-29 07:13:33 +02:00
|
|
|
}
|