-
Notifications
You must be signed in to change notification settings - Fork 20
/
filter.go
85 lines (74 loc) · 2.14 KB
/
filter.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
package fields
import (
"fmt"
sdk "github.com/algorand/go-algorand-sdk/v2/types"
)
// Operation an operation like "any" or "all" for boolean logic
type Operation string
const anyFieldOperation Operation = "any"
const allFieldOperation Operation = "all"
const noneFieldOperation Operation = "none"
// ValidFieldOperation returns true if the input is a valid operation
func ValidFieldOperation(input string) bool {
if input != string(anyFieldOperation) && input != string(allFieldOperation) && input != string(noneFieldOperation) {
return false
}
return true
}
// Filter an object that combines field searches with a boolean operator
type Filter struct {
Op Operation
Searchers []*Searcher
OmitGroup bool
}
func (f Filter) matches(txn *sdk.SignedTxnWithAD) (bool, error) {
numMatches := 0
for _, fs := range f.Searchers {
b, err := fs.search(txn)
if err != nil {
return false, err
}
if b {
numMatches++
}
}
switch f.Op {
case noneFieldOperation:
return numMatches == 0, nil
case anyFieldOperation:
return numMatches > 0, nil
case allFieldOperation:
return numMatches == len(f.Searchers), nil
default:
return false, fmt.Errorf("unknown operation: %s", f.Op)
}
}
// SearchAndFilter searches through the block data and applies the operation to the results
func (f Filter) SearchAndFilter(payset []sdk.SignedTxnInBlock) ([]sdk.SignedTxnInBlock, error) {
var result []sdk.SignedTxnInBlock
firstGroupIdx := 0
for i := 0; i < len(payset); i++ {
if payset[firstGroupIdx].Txn.Group != payset[i].Txn.Group {
firstGroupIdx = i
}
match, err := f.matches(&payset[i].SignedTxnWithAD)
if err != nil {
return nil, err
}
if match {
// if txn.Group is set and omit group is false
if payset[i].Txn.Group != (sdk.Digest{}) && !f.OmitGroup {
j := firstGroupIdx
// append all txns with same group ID
for ; j < len(payset) && payset[j].Txn.Group == payset[firstGroupIdx].Txn.Group; j++ {
result = append(result, payset[j])
}
// skip txns that are already added, set i to the index of the last added txn
i = j - 1
} else {
result = append(result, payset[i])
}
}
}
return result, nil
}