forked from Onyx-Protocol/Onyx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs.go
131 lines (109 loc) · 3.43 KB
/
outputs.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
package query
import (
"context"
"encoding/json"
"fmt"
"math"
"strings"
"github.com/lib/pq"
"chain/core/query/filter"
"chain/errors"
)
var defaultOutputsAfter = OutputsAfter{
lastBlockHeight: math.MaxInt64,
lastTxPos: math.MaxUint32,
lastIndex: math.MaxUint32,
}
type OutputsAfter struct {
lastBlockHeight uint64
lastTxPos uint32
lastIndex uint32
}
func (cur OutputsAfter) String() string {
return fmt.Sprintf("%d:%d:%d", cur.lastBlockHeight, cur.lastTxPos, cur.lastIndex)
}
func DecodeOutputsAfter(str string) (c *OutputsAfter, err error) {
var lastBlockHeight, lastTxPos, lastIndex uint64
_, err = fmt.Sscanf(str, "%d:%d:%d", &lastBlockHeight, &lastTxPos, &lastIndex)
if err != nil {
return c, errors.Wrap(ErrBadAfter, err.Error())
}
if lastBlockHeight > math.MaxInt64 ||
lastTxPos > math.MaxUint32 ||
lastIndex > math.MaxUint32 {
return nil, errors.Wrap(ErrBadAfter)
}
return &OutputsAfter{
lastBlockHeight: lastBlockHeight,
lastTxPos: uint32(lastTxPos),
lastIndex: uint32(lastIndex),
}, nil
}
func (ind *Indexer) Outputs(ctx context.Context, p filter.Predicate, vals []interface{}, timestampMS uint64, after *OutputsAfter, limit int) ([]interface{}, *OutputsAfter, error) {
if len(vals) != p.Parameters {
return nil, nil, ErrParameterCountMismatch
}
expr, err := filter.AsSQL(p, "data", vals)
if err != nil {
return nil, nil, err
}
queryStr, queryArgs := constructOutputsQuery(expr, timestampMS, after, limit)
rows, err := ind.db.Query(ctx, queryStr, queryArgs...)
if err != nil {
return nil, nil, err
}
defer rows.Close()
var newAfter = defaultOutputsAfter
if after != nil {
newAfter = *after
}
outputs := make([]interface{}, 0, limit)
for rows.Next() {
var (
blockHeight uint64
txPos uint32
index uint32
data []byte
)
err = rows.Scan(&blockHeight, &txPos, &index, &data)
if err != nil {
return nil, nil, err
}
outputs = append(outputs, (*json.RawMessage)(&data))
newAfter.lastBlockHeight = blockHeight
newAfter.lastTxPos = txPos
newAfter.lastIndex = index
}
err = rows.Err()
if err != nil {
return nil, nil, err
}
return outputs, &newAfter, nil
}
func constructOutputsQuery(expr filter.SQLExpr, timestampMS uint64, after *OutputsAfter, limit int) (string, []interface{}) {
// TODO(jackson): refactor to use bytes.Buffer for consistency
// with the other construct(...)Query functions.
sql := fmt.Sprintf("SELECT block_height, tx_pos, output_index, data FROM %s", pq.QuoteIdentifier("annotated_outputs"))
vals := make([]interface{}, 0, 4+len(expr.Values))
vals = append(vals, expr.Values...)
vals = append(vals, timestampMS)
timestampValIndex := len(vals)
where := strings.TrimSpace(expr.SQL)
timespanExpr := fmt.Sprintf("timespan @> $%d::int8", timestampValIndex)
if where == "" {
where = timespanExpr
} else {
where = fmt.Sprintf("(%s) AND %s", where, timespanExpr)
}
if after != nil {
vals = append(vals, after.lastBlockHeight)
lastBlockHeightValIndex := len(vals)
vals = append(vals, after.lastTxPos)
lastTxPosValIndex := len(vals)
vals = append(vals, after.lastIndex)
lastIndexValIndex := len(vals)
where = fmt.Sprintf("%s AND (block_height, tx_pos, output_index) < ($%d, $%d, $%d)", where, lastBlockHeightValIndex, lastTxPosValIndex, lastIndexValIndex)
}
sql += fmt.Sprintf(" WHERE %s ORDER BY block_height DESC, tx_pos DESC, output_index DESC LIMIT %d", where, limit)
return sql, vals
}