forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathget.go
113 lines (98 loc) · 3.23 KB
/
get.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
package crud
import (
"context"
"github.com/vmihailenco/msgpack/v5"
"github.com/tarantool/go-tarantool/v2"
)
// GetOpts describes options for `crud.get` method.
type GetOpts struct {
// Timeout is a `vshard.call` timeout and vshard
// master discovery timeout (in seconds).
Timeout OptFloat64
// VshardRouter is cartridge vshard group name or
// vshard router instance.
VshardRouter OptString
// Fields is field names for getting only a subset of fields.
Fields OptTuple
// BucketId is a bucket ID.
BucketId OptUint
// Mode is a parameter with `write`/`read` possible values,
// if `write` is specified then operation is performed on master.
Mode OptString
// PreferReplica is a parameter to specify preferred target
// as one of the replicas.
PreferReplica OptBool
// Balance is a parameter to use replica according to vshard
// load balancing policy.
Balance OptBool
// FetchLatestMetadata guarantees the up-to-date metadata (space format)
// in first return value, otherwise it may not take into account
// the latest migration of the data format. Performance overhead is up to 15%.
// Disabled by default.
FetchLatestMetadata OptBool
}
// EncodeMsgpack provides custom msgpack encoder.
func (opts GetOpts) EncodeMsgpack(enc *msgpack.Encoder) error {
const optsCnt = 8
names := [optsCnt]string{timeoutOptName, vshardRouterOptName,
fieldsOptName, bucketIdOptName, modeOptName,
preferReplicaOptName, balanceOptName, fetchLatestMetadataOptName}
values := [optsCnt]interface{}{}
exists := [optsCnt]bool{}
values[0], exists[0] = opts.Timeout.Get()
values[1], exists[1] = opts.VshardRouter.Get()
values[2], exists[2] = opts.Fields.Get()
values[3], exists[3] = opts.BucketId.Get()
values[4], exists[4] = opts.Mode.Get()
values[5], exists[5] = opts.PreferReplica.Get()
values[6], exists[6] = opts.Balance.Get()
values[7], exists[7] = opts.FetchLatestMetadata.Get()
return encodeOptions(enc, names[:], values[:], exists[:])
}
// GetRequest helps you to create request object to call `crud.get`
// for execution by a Connection.
type GetRequest struct {
spaceRequest
key Tuple
opts GetOpts
}
type getArgs struct {
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
Space string
Key Tuple
Opts GetOpts
}
// MakeGetRequest returns a new empty GetRequest.
func MakeGetRequest(space string) GetRequest {
req := GetRequest{}
req.impl = newCall("crud.get")
req.space = space
req.opts = GetOpts{}
return req
}
// Key sets the key for the GetRequest request.
// Note: default value is nil.
func (req GetRequest) Key(key Tuple) GetRequest {
req.key = key
return req
}
// Opts sets the options for the GetRequest request.
// Note: default value is nil.
func (req GetRequest) Opts(opts GetOpts) GetRequest {
req.opts = opts
return req
}
// Body fills an encoder with the call request body.
func (req GetRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
if req.key == nil {
req.key = []interface{}{}
}
args := getArgs{Space: req.space, Key: req.key, Opts: req.opts}
req.impl = req.impl.Args(args)
return req.impl.Body(res, enc)
}
// Context sets a passed context to CRUD request.
func (req GetRequest) Context(ctx context.Context) GetRequest {
req.impl = req.impl.Context(ctx)
return req
}