|
| 1 | +package crud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/tarantool/go-tarantool" |
| 7 | +) |
| 8 | + |
| 9 | +// CountOpts describes options for `crud.count` method. |
| 10 | +type CountOpts struct { |
| 11 | + // Timeout is a `vshard.call` timeout and vshard |
| 12 | + // master discovery timeout (in seconds). |
| 13 | + Timeout OptUint |
| 14 | + // VshardRouter is cartridge vshard group name or |
| 15 | + // vshard router instance. |
| 16 | + VshardRouter OptString |
| 17 | + // Mode is a parameter with `write`/`read` possible values, |
| 18 | + // if `write` is specified then operation is performed on master. |
| 19 | + Mode OptString |
| 20 | + // PreferReplica is a parameter to specify preferred target |
| 21 | + // as one of the replicas. |
| 22 | + PreferReplica OptBool |
| 23 | + // Balance is a parameter to use replica according to vshard |
| 24 | + // load balancing policy. |
| 25 | + Balance OptBool |
| 26 | + // YieldEvery describes number of tuples processed to yield after. |
| 27 | + YieldEvery OptUint |
| 28 | + // BucketId is a bucket ID. |
| 29 | + BucketId OptUint |
| 30 | + // ForceMapCall describes the map call is performed without any |
| 31 | + // optimizations even if full primary key equal condition is specified. |
| 32 | + ForceMapCall OptBool |
| 33 | + // Fullscan describes if a critical log entry will be skipped on |
| 34 | + // potentially long count. |
| 35 | + Fullscan OptBool |
| 36 | +} |
| 37 | + |
| 38 | +// EncodeMsgpack provides custom msgpack encoder. |
| 39 | +func (opts CountOpts) EncodeMsgpack(enc *encoder) error { |
| 40 | + const optsCnt = 9 |
| 41 | + |
| 42 | + options := [optsCnt]option{opts.Timeout, opts.VshardRouter, |
| 43 | + opts.Mode, opts.PreferReplica, opts.Balance, |
| 44 | + opts.YieldEvery, opts.BucketId, opts.ForceMapCall, |
| 45 | + opts.Fullscan} |
| 46 | + names := [optsCnt]string{timeoutOptName, vshardRouterOptName, |
| 47 | + modeOptName, preferReplicaOptName, balanceOptName, |
| 48 | + yieldEveryOptName, bucketIdOptName, forceMapCallOptName, |
| 49 | + fullscanOptName} |
| 50 | + values := [optsCnt]interface{}{} |
| 51 | + |
| 52 | + return encodeOptions(enc, options[:], names[:], values[:]) |
| 53 | +} |
| 54 | + |
| 55 | +// CountRequest helps you to create request object to call `crud.count` |
| 56 | +// for execution by a Connection. |
| 57 | +type CountRequest struct { |
| 58 | + spaceRequest |
| 59 | + conditions []Condition |
| 60 | + opts CountOpts |
| 61 | +} |
| 62 | + |
| 63 | +type countArgs struct { |
| 64 | + _msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused |
| 65 | + Space string |
| 66 | + Conditions []Condition |
| 67 | + Opts CountOpts |
| 68 | +} |
| 69 | + |
| 70 | +// NewCountRequest returns a new empty CountRequest. |
| 71 | +func NewCountRequest(space string) *CountRequest { |
| 72 | + req := new(CountRequest) |
| 73 | + req.initImpl("crud.count") |
| 74 | + req.setSpace(space) |
| 75 | + req.conditions = nil |
| 76 | + req.opts = CountOpts{} |
| 77 | + return req |
| 78 | +} |
| 79 | + |
| 80 | +// Conditions sets the conditions for the CountRequest request. |
| 81 | +// Note: default value is nil. |
| 82 | +func (req *CountRequest) Conditions(conditions []Condition) *CountRequest { |
| 83 | + req.conditions = conditions |
| 84 | + return req |
| 85 | +} |
| 86 | + |
| 87 | +// Opts sets the options for the CountRequest request. |
| 88 | +// Note: default value is nil. |
| 89 | +func (req *CountRequest) Opts(opts CountOpts) *CountRequest { |
| 90 | + req.opts = opts |
| 91 | + return req |
| 92 | +} |
| 93 | + |
| 94 | +// Body fills an encoder with the call request body. |
| 95 | +func (req *CountRequest) Body(res tarantool.SchemaResolver, enc *encoder) error { |
| 96 | + args := countArgs{Space: req.space, Conditions: req.conditions, Opts: req.opts} |
| 97 | + req.impl = req.impl.Args(args) |
| 98 | + return req.impl.Body(res, enc) |
| 99 | +} |
| 100 | + |
| 101 | +// Context sets a passed context to CRUD request. |
| 102 | +func (req *CountRequest) Context(ctx context.Context) *CountRequest { |
| 103 | + req.impl = req.impl.Context(ctx) |
| 104 | + |
| 105 | + return req |
| 106 | +} |
0 commit comments