-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathclient.go
154 lines (129 loc) · 5.22 KB
/
client.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
package query
import (
"context"
"time"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/closer"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/query/options"
"github.com/ydb-platform/ydb-go-sdk/v3/retry/budget"
"github.com/ydb-platform/ydb-go-sdk/v3/trace"
)
type (
// Executor is an interface for execute queries
Executor interface {
// Exec execute query without result
//
// Exec used by default:
// - DefaultTxControl
Exec(ctx context.Context, sql string, opts ...ExecuteOption) error
// Query execute query with result
//
// Exec used by default:
// - DefaultTxControl
Query(ctx context.Context, sql string, opts ...ExecuteOption) (Result, error)
// QueryResultSet execute query and take the exactly single materialized result set from result
//
// Exec used by default:
// - DefaultTxControl
QueryResultSet(ctx context.Context, sql string, opts ...ExecuteOption) (ClosableResultSet, error)
// QueryRow execute query and take the exactly single row from exactly single result set from result
//
// Exec used by default:
// - DefaultTxControl
QueryRow(ctx context.Context, sql string, opts ...ExecuteOption) (Row, error)
}
// Client defines API of query client
Client interface {
Executor
// Do provide the best effort for execute operation.
//
// Do implements internal busy loop until one of the following conditions is met:
// - deadline was canceled or deadlined
// - retry operation returned nil as error
//
// Warning: if context without deadline or cancellation func than Do can run indefinitely.
Do(ctx context.Context, op Operation, opts ...DoOption) error
// DoTx provide the best effort for execute transaction.
//
// DoTx implements internal busy loop until one of the following conditions is met:
// - deadline was canceled or deadlined
// - retry operation returned nil as error
//
// DoTx makes auto selector (with TransactionSettings, by default - SerializableReadWrite), commit and
// rollback (on error) of transaction.
//
// If op TxOperation returns nil - transaction will be committed
// If op TxOperation return non nil - transaction will be rollback
// Warning: if context without deadline or cancellation func than DoTx can run indefinitely
DoTx(ctx context.Context, op TxOperation, opts ...DoTxOption) error
// Exec execute query without result
//
// Exec used by default:
// - DefaultTxControl
Exec(ctx context.Context, sql string, opts ...ExecuteOption) error
// Query execute query with materialized result
//
// Warning: the large result from query will be materialized and can happened to "OOM killed" problem
//
// Exec used by default:
// - DefaultTxControl
Query(ctx context.Context, sql string, opts ...ExecuteOption) (Result, error)
// QueryResultSet is a helper which read all rows from first result set in result
//
// Warning: the large result set from query will be materialized and can happened to "OOM killed" problem
QueryResultSet(ctx context.Context, sql string, opts ...ExecuteOption) (ClosableResultSet, error)
// QueryRow is a helper which read only one row from first result set in result
//
// ReadRow returns error if result contains more than one result set or more than one row
QueryRow(ctx context.Context, sql string, opts ...ExecuteOption) (Row, error)
// ExecuteScript starts long executing script with polling results later
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
ExecuteScript(
ctx context.Context, sql string, ttl time.Duration, ops ...ExecuteOption,
) (*options.ExecuteScriptOperation, error)
// FetchScriptResults fetching the script results
//
// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
FetchScriptResults(
ctx context.Context, opID string, opts ...options.FetchScriptOption,
) (*options.FetchScriptResult, error)
}
)
func WithFetchToken(fetchToken string) options.FetchScriptOption {
return options.WithFetchToken(fetchToken)
}
func WithResultSetIndex(resultSetIndex int64) options.FetchScriptOption {
return options.WithResultSetIndex(resultSetIndex)
}
func WithRowsLimit(rowsLimit int64) options.FetchScriptOption {
return options.WithRowsLimit(rowsLimit)
}
type (
// Operation is the interface that holds an operation for retry.
// if Operation returns not nil - operation will retry
// if Operation returns nil - retry loop will break
Operation func(ctx context.Context, s Session) error
// TxOperation is the interface that holds an operation for retry.
// if TxOperation returns not nil - operation will retry
// if TxOperation returns nil - retry loop will break
TxOperation func(ctx context.Context, tx TxActor) error
ClosableSession interface {
closer.Closer
Session
}
DoOption = options.DoOption
DoTxOption = options.DoTxOption
)
func WithIdempotent() options.RetryOptionsOption {
return options.WithIdempotent()
}
func WithTrace(t *trace.Query) options.TraceOption {
return options.WithTrace(t)
}
func WithLabel(lbl string) options.LabelOption {
return options.WithLabel(lbl)
}
// WithRetryBudget creates option with external budget
func WithRetryBudget(b budget.Budget) options.RetryOptionsOption {
return options.WithRetryBudget(b)
}