-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathterms.go
139 lines (119 loc) · 3.89 KB
/
terms.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
// Copyright 2020 dfuse Platform Inc.
//
// Licensed 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 search
import (
"fmt"
"regexp"
"strings"
)
type IndexedTerms struct {
Receiver bool
Account bool
Action bool
Auth bool
Scheduled bool
Status bool
Notif bool
Input bool
Event bool
RAMConsumed bool
RAMReleased bool
DBTable bool
DBKey bool
Base map[string]bool
Data map[string]bool
}
type fieldCategory int
const (
fieldCategoryBase fieldCategory = iota
fieldCategoryData
)
var splitTermRegexp = regexp.MustCompile("(,|\\s+)")
// DefaultIndexedTerms holds terms to index from last hosted dfuse infra
const DefaultIndexedTerms = "receiver, account, action, auth, scheduled, status, notif, input, event, ram.consumed, ram.released, db.key, db.table, data.account, data.active, data.active_key, data.actor, data.amount, data.auth, data.authority, data.bid, data.bidder, data.canceler, data.creator, data.executer, data.from, data.is_active, data.is_priv, data.isproxy, data.issuer, data.level, data.location, data.maximum_supply, data.name, data.newname, data.owner, data.parent, data.payer, data.permission, data.producer, data.producer_key, data.proposal_name, data.proposal_hash, data.proposer, data.proxy, data.public_key, data.producers, data.quant, data.quantity, data.ram_payer, data.receiver, data.requested, data.requirement, data.symbol, data.threshold, data.to, data.transfer, data.voter, data.voter_name, data.weight, data.abi, data.code"
func NewIndexedTerms(specs string) (out *IndexedTerms, err error) {
if specs == "*" || specs == "" {
specs = DefaultIndexedTerms
}
out = &IndexedTerms{
Base: map[string]bool{},
Data: map[string]bool{},
}
terms := splitTermRegexp.Split(specs, -1)
for _, term := range terms {
term = strings.TrimSpace(term)
if term == "" {
continue
}
category := fieldCategoryBase
switch term {
case "receiver":
out.Receiver = true
case "account":
out.Account = true
case "action":
out.Action = true
case "auth":
out.Auth = true
case "scheduled":
out.Scheduled = true
case "status":
out.Status = true
case "notif":
out.Notif = true
case "input":
out.Input = true
case "event":
out.Event = true
case "ram.consumed":
out.RAMConsumed = true
case "ram.released":
out.RAMReleased = true
case "db.table":
out.DBTable = true
case "db.key":
out.DBKey = true
default:
if strings.HasPrefix(term, "data.") {
category = fieldCategoryData
out.Data[out.NormalizeDataField(term)] = true
} else {
return nil, fmt.Errorf("invalid indexed term specs %q: unknown field %q", specs, term)
}
}
if category == fieldCategoryBase {
out.Base[term] = true
}
}
return out, nil
}
func (t *IndexedTerms) IsIndexed(fieldName string) bool {
if t.Base[fieldName] {
return true
}
if strings.HasPrefix(fieldName, "data.") {
return t.Data[t.NormalizeDataField(fieldName)]
}
return strings.HasPrefix(fieldName, "event.")
}
// NormalizeDataTerm extracts the the first child element from the data name (i.e. from `data.first.second.third`
// to `first` where `data` is the parent name, `first.second.third` is the child hierarchy and `first`
// is the first child element of `data`).
func (t *IndexedTerms) NormalizeDataField(fieldName string) string {
segments := strings.Split(fieldName, ".")
if len(segments) < 2 {
return fieldName
}
return segments[1]
}