forked from EOS-Nation/dfuse-leap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.go
83 lines (66 loc) · 1.73 KB
/
testing.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
package search
import (
"context"
"sort"
"strings"
bsearch "github.com/blevesearch/bleve/search"
"github.com/dfuse-io/search"
"go.uber.org/zap"
)
type testTrxResult struct {
id string
blockNum uint64
}
var TestMatchCollector = func(ctx context.Context, lowBlockNum, highBlockNum uint64, results bsearch.DocumentMatchCollection) (out []search.SearchMatch, err error) {
trxs := make(map[string][]uint16)
var trxList []*testTrxResult
for _, el := range results {
if err := ctx.Err(); err != nil {
return nil, err
}
blockNum, trxID, actionIdx, skip := testExplodeDocumentID(el.ID)
if skip {
continue
}
if blockNum < lowBlockNum || blockNum > highBlockNum {
continue
}
if _, found := trxs[trxID]; !found {
trxList = append(trxList, &testTrxResult{
id: trxID,
blockNum: blockNum,
})
}
trxs[trxID] = append(trxs[trxID], actionIdx)
}
for _, trx := range trxList {
actions := trxs[trx.id]
sort.Slice(actions, func(i, j int) bool { return actions[i] < actions[j] })
out = append(out, &SearchMatch{
TrxIDPrefix: trx.id,
ActionIndexes: actions,
BlockNumber: trx.blockNum,
})
}
return out, nil
}
func testExplodeDocumentID(ref string) (blockNum uint64, trxID string, actionIdx uint16, skip bool) {
var err error
chunks := strings.Split(ref, ":")
chunksCount := len(chunks)
if chunksCount != 3 || chunks[0] == "meta" { // meta, flatten, etc.
skip = true
return
}
blockNum32, err := fromHexUint32(chunks[0])
if err != nil {
zlog.Panic("woah, block num invalid?", zap.Error(err))
}
blockNum = uint64(blockNum32)
trxID = chunks[1]
actionIdx, err = fromHexUint16(chunks[2])
if err != nil {
zlog.Panic("woah, action index invalid?", zap.Error(err))
}
return
}