-
Notifications
You must be signed in to change notification settings - Fork 22
/
positions.go
188 lines (165 loc) · 5.8 KB
/
positions.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.DATANODE file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package sqlstore
import (
"context"
"fmt"
"strings"
"code.vegaprotocol.io/vega/datanode/entities"
"code.vegaprotocol.io/vega/datanode/metrics"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"github.com/georgysavva/scany/pgxscan"
)
var positionsOrdering = TableOrdering{
ColumnOrdering{Name: "vega_time", Sorting: ASC},
ColumnOrdering{Name: "party_id", Sorting: ASC},
ColumnOrdering{Name: "market_id", Sorting: ASC},
}
type Positions struct {
*ConnectionSource
batcher MapBatcher[entities.PositionKey, entities.Position]
}
func NewPositions(connectionSource *ConnectionSource) *Positions {
a := &Positions{
ConnectionSource: connectionSource,
batcher: NewMapBatcher[entities.PositionKey, entities.Position](
"positions",
entities.PositionColumns),
}
return a
}
func (ps *Positions) Flush(ctx context.Context) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "Flush")()
return ps.batcher.Flush(ctx, ps.Connection)
}
func (ps *Positions) Add(ctx context.Context, p entities.Position) error {
ps.batcher.Add(p)
return nil
}
func (ps *Positions) GetByMarketAndParty(ctx context.Context,
marketIDRaw string,
partyIDRaw string,
) (entities.Position, error) {
var (
position = entities.Position{}
marketID = entities.MarketID(marketIDRaw)
partyID = entities.PartyID(partyIDRaw)
)
defer metrics.StartSQLQuery("Positions", "GetByMarketAndParty")()
return position, ps.wrapE(pgxscan.Get(ctx, ps.Connection, &position,
`SELECT * FROM positions_current WHERE market_id=$1 AND party_id=$2`,
marketID, partyID))
}
func (ps *Positions) GetByMarketAndParties(ctx context.Context, marketIDRaw string, partyIDsRaw []string) ([]entities.Position, error) {
marketID := entities.MarketID(marketIDRaw)
partyIDs := make([]interface{}, 0, len(partyIDsRaw))
in := make([]string, 0, len(partyIDsRaw))
bindNum := 2
for _, p := range partyIDsRaw {
partyIDs = append(partyIDs, entities.PartyID(p))
in = append(in, fmt.Sprintf("$%d", bindNum))
bindNum++
}
bind := make([]interface{}, 0, len(in)+1)
// set all bind vars
bind = append(bind, marketID)
bind = append(bind, partyIDs...)
positions := []entities.Position{}
// build the query
q := fmt.Sprintf(`SELECT * FROM positions_current WHERE market_id = $1 AND party_id IN (%s)`, strings.Join(in, ", "))
err := pgxscan.Select(ctx, ps.Connection, &positions, q, bind...)
return positions, err
}
func (ps *Positions) GetByMarket(ctx context.Context, marketID string) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetByMarket")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current WHERE market_id=$1`,
entities.MarketID(marketID))
return positions, err
}
func (ps *Positions) GetByParty(ctx context.Context, partyID string) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetByParty")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current WHERE party_id=$1`,
entities.PartyID(partyID))
return positions, err
}
func stringToPartyID(s ...string) [][]byte {
partyIDs := make([][]byte, 0, len(s))
for _, v := range s {
if v == "" {
continue
}
id := entities.PartyID(v)
bs, err := id.Bytes()
if err != nil {
continue
}
partyIDs = append(partyIDs, bs)
}
return partyIDs
}
func stringToMarketID(s ...string) [][]byte {
marketIDs := make([][]byte, 0, len(s))
for _, v := range s {
if v == "" {
continue
}
id := entities.MarketID(v)
bs, err := id.Bytes()
if err != nil {
continue
}
marketIDs = append(marketIDs, bs)
}
return marketIDs
}
func (ps *Positions) GetByPartyConnection(ctx context.Context, partyIDRaw []string, marketIDRaw []string, pagination entities.CursorPagination) ([]entities.Position, entities.PageInfo, error) {
var (
args []interface{}
pageInfo entities.PageInfo
query = `select * from positions_current`
where string
partyID = stringToPartyID(partyIDRaw...)
marketID = stringToMarketID(marketIDRaw...)
err error
)
if len(partyID) > 0 && len(marketID) == 0 {
where = fmt.Sprintf(" where party_id = ANY(%s::bytea[])", nextBindVar(&args, partyID))
} else if len(partyID) > 0 && len(marketID) > 0 {
where = fmt.Sprintf(" where party_id = ANY(%s::bytea[]) and market_id = ANY(%s::bytea[])", nextBindVar(&args, partyID), nextBindVar(&args, marketID))
} else if len(partyID) == 0 && len(marketID) > 0 {
where = fmt.Sprintf(" where market_id = ANY(%s::bytea[])", nextBindVar(&args, marketID))
}
if where != "" {
query = fmt.Sprintf("%s %s", query, where)
}
query, args, err = PaginateQuery[entities.PositionCursor](query, args, positionsOrdering, pagination)
if err != nil {
return nil, pageInfo, err
}
var positions []entities.Position
if err = pgxscan.Select(ctx, ps.Connection, &positions, query, args...); err != nil {
return nil, pageInfo, err
}
positions, pageInfo = entities.PageEntities[*v2.PositionEdge](positions, pagination)
return positions, pageInfo, nil
}
func (ps *Positions) GetAll(ctx context.Context) ([]entities.Position, error) {
defer metrics.StartSQLQuery("Positions", "GetAll")()
positions := []entities.Position{}
err := pgxscan.Select(ctx, ps.Connection, &positions,
`SELECT * FROM positions_current`)
return positions, err
}