This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
135 lines (122 loc) · 2.59 KB
/
functions.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
package dockage
import (
"hash/fnv"
"strings"
"github.com/dgraph-io/badger"
"github.com/fatih/structs"
)
func prepdoc(doc interface{}) (resID []byte, resRev *structs.Field, reserr error) {
ins := new(inspector)
ins.inspect(doc)
if ins.id == "" {
reserr = ErrNoID
return
}
sid := ins.id
if strings.ContainsAny(sid, specials) {
reserr = ErrInvalidID
return
}
resID = []byte(sid)
if ins.rev == nil {
reserr = ErrNoRev
return
}
resRev = ins.rev
return
}
func fnvhash(v []byte) []byte {
h := fnv.New64a()
h.Write(v)
return h.Sum(nil)
}
func pat4View(s ...string) string {
return viewsp + strings.Join(s, viewsp)
}
func pat4Key(s ...string) string {
return keysp + strings.Join(s, keysp)
}
func pat4Sys(s ...string) string {
return syssp + strings.Join(s, syssp)
}
func getlimits(params Q) (skip, limit int, applySkip, applyLimit bool) {
skip = params.Skip
limit = params.Limit
var ()
if skip > 0 {
applySkip = true
}
if limit <= 0 && !params.Count {
limit = 100
}
if limit > 0 {
applyLimit = true
}
return
}
func stopWords(params Q, forIndexedKeys ...bool) (start, end, prefix []byte) {
if params.View == "" {
start = []byte(pat4Key(string(params.Start)))
if len(params.End) > 0 {
end = []byte(pat4Key(string(params.End)))
}
if len(params.Prefix) > 0 {
prefix = []byte(pat4Key(string(params.Prefix)))
} else {
prefix = start
}
} else {
name := string(fnvhash([]byte(params.View)))
domain := viewx2k
if len(forIndexedKeys) > 0 && forIndexedKeys[0] {
domain = viewk2x
}
pfx := pat4View(name + domain)
start = []byte(pfx + pat4View(string(params.Start)))
if len(params.End) > 0 {
end = []byte(pfx + pat4View(string(params.End)))
}
if len(params.Prefix) > 0 {
prefix = []byte(pfx + pat4View(string(params.Prefix)))
} else {
prefix = []byte(pfx)
}
}
return
}
func itrFunc(txn *badger.Txn,
opt badger.IteratorOptions,
start, prefix []byte,
bodyFunc func(itr interface{ Item() *badger.Item }) error) error {
itr := txn.NewIterator(opt)
defer itr.Close()
for itr.Seek(start); itr.ValidForPrefix(prefix); itr.Next() {
if err := bodyFunc(itr); err != nil {
return err
}
}
return nil
}
type inspector struct {
id string
rev *structs.Field
}
func (ins *inspector) inspect(v interface{}) {
list := structs.Fields(v)
ins.recinspect(list...)
}
func (ins *inspector) recinspect(fields ...*structs.Field) {
for _, fl := range fields {
if fl.IsEmbedded() {
ins.recinspect(fl.Fields()...)
continue
}
dok := fl.Tag("json")
switch dok {
case "id":
ins.id = fl.Value().(string)
case "rev":
ins.rev = fl
}
}
}