forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
api: add CRUD module support #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Package crud with support of API of Tarantool's CRUD module. | ||
// | ||
// Supported CRUD methods: | ||
// | ||
// - insert | ||
// | ||
// - insert_object | ||
// | ||
// - insert_many | ||
// | ||
// - insert_object_many | ||
// | ||
// - get | ||
// | ||
// - update | ||
// | ||
// - delete | ||
// | ||
// - replace | ||
// | ||
// - replace_object | ||
// | ||
// - replace_many | ||
// | ||
// - replace_object_many | ||
// | ||
// - upsert | ||
// | ||
// - upsert_object | ||
// | ||
// - upsert_many | ||
// | ||
// - upsert_object_many | ||
// | ||
// - select | ||
// | ||
// - min | ||
// | ||
// - max | ||
// | ||
// - truncate | ||
// | ||
// - len | ||
// | ||
// - storage_info | ||
// | ||
// - count | ||
// | ||
// - stats | ||
// | ||
// - unflatten_rows | ||
// | ||
// Since: 1.11.0. | ||
package crud | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tarantool/go-tarantool" | ||
) | ||
|
||
// Tuple is a type to describe tuple for CRUD methods. | ||
type Tuple = []interface{} | ||
|
||
type baseRequest struct { | ||
impl *tarantool.CallRequest | ||
} | ||
|
||
func (req *baseRequest) initImpl(methodName string) { | ||
req.impl = tarantool.NewCall17Request(methodName) | ||
} | ||
|
||
// Code returns IPROTO code for CRUD request. | ||
func (req *baseRequest) Code() int32 { | ||
return req.impl.Code() | ||
} | ||
|
||
// Ctx returns a context of CRUD request. | ||
func (req *baseRequest) Ctx() context.Context { | ||
return req.impl.Ctx() | ||
} | ||
|
||
// Async returns is CRUD request expects a response. | ||
func (req *baseRequest) Async() bool { | ||
return req.impl.Async() | ||
} | ||
|
||
type spaceRequest struct { | ||
baseRequest | ||
space string | ||
} | ||
|
||
func (req *spaceRequest) setSpace(space string) { | ||
req.space = space | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package crud | ||
|
||
// Operator is a type to describe operator of operation. | ||
type Operator string | ||
|
||
const ( | ||
// Eq - comparison operator for "equal". | ||
Eq Operator = "=" | ||
// Lt - comparison operator for "less than". | ||
Lt Operator = "<" | ||
// Le - comparison operator for "less than or equal". | ||
Le Operator = "<=" | ||
// Gt - comparison operator for "greater than". | ||
Gt Operator = ">" | ||
DifferentialOrange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Ge - comparison operator for "greater than or equal". | ||
Ge Operator = ">=" | ||
) | ||
|
||
// Condition describes CRUD condition as a table | ||
// {operator, field-identifier, value}. | ||
type Condition struct { | ||
// Instruct msgpack to pack this struct as array, so no custom packer | ||
// is needed. | ||
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused | ||
Operator Operator | ||
Field interface{} // Field name, field number or index name. | ||
Value interface{} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package crud | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tarantool/go-tarantool" | ||
) | ||
|
||
// CountResult describes result for `crud.count` method. | ||
type CountResult = NumberResult | ||
|
||
// CountOpts describes options for `crud.count` method. | ||
type CountOpts struct { | ||
// Timeout is a `vshard.call` timeout and vshard | ||
// master discovery timeout (in seconds). | ||
Timeout OptUint | ||
// VshardRouter is cartridge vshard group name or | ||
// vshard router instance. | ||
VshardRouter OptString | ||
// 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 | ||
// YieldEvery describes number of tuples processed to yield after. | ||
YieldEvery OptUint | ||
// BucketId is a bucket ID. | ||
BucketId OptUint | ||
// ForceMapCall describes the map call is performed without any | ||
// optimizations even if full primary key equal condition is specified. | ||
ForceMapCall OptBool | ||
// Fullscan describes if a critical log entry will be skipped on | ||
// potentially long count. | ||
Fullscan OptBool | ||
} | ||
|
||
// EncodeMsgpack provides custom msgpack encoder. | ||
func (opts CountOpts) EncodeMsgpack(enc *encoder) error { | ||
const optsCnt = 9 | ||
DifferentialOrange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
options := [optsCnt]option{opts.Timeout, opts.VshardRouter, | ||
opts.Mode, opts.PreferReplica, opts.Balance, | ||
opts.YieldEvery, opts.BucketId, opts.ForceMapCall, | ||
opts.Fullscan} | ||
names := [optsCnt]string{timeoutOptName, vshardRouterOptName, | ||
modeOptName, preferReplicaOptName, balanceOptName, | ||
yieldEveryOptName, bucketIdOptName, forceMapCallOptName, | ||
fullscanOptName} | ||
values := [optsCnt]interface{}{} | ||
|
||
return encodeOptions(enc, options[:], names[:], values[:]) | ||
} | ||
|
||
// CountRequest helps you to create request object to call `crud.count` | ||
// for execution by a Connection. | ||
type CountRequest struct { | ||
spaceRequest | ||
conditions []Condition | ||
opts CountOpts | ||
} | ||
|
||
type countArgs struct { | ||
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused | ||
Space string | ||
Conditions []Condition | ||
Opts CountOpts | ||
} | ||
|
||
// NewCountRequest returns a new empty CountRequest. | ||
func NewCountRequest(space string) *CountRequest { | ||
req := new(CountRequest) | ||
req.initImpl("crud.count") | ||
req.setSpace(space) | ||
req.conditions = nil | ||
req.opts = CountOpts{} | ||
return req | ||
} | ||
|
||
// Conditions sets the conditions for the CountRequest request. | ||
// Note: default value is nil. | ||
func (req *CountRequest) Conditions(conditions []Condition) *CountRequest { | ||
req.conditions = conditions | ||
return req | ||
} | ||
|
||
// Opts sets the options for the CountRequest request. | ||
// Note: default value is nil. | ||
func (req *CountRequest) Opts(opts CountOpts) *CountRequest { | ||
req.opts = opts | ||
return req | ||
} | ||
|
||
// Body fills an encoder with the call request body. | ||
func (req *CountRequest) Body(res tarantool.SchemaResolver, enc *encoder) error { | ||
args := countArgs{Space: req.space, Conditions: req.conditions, Opts: req.opts} | ||
req.impl = req.impl.Args(args) | ||
return req.impl.Body(res, enc) | ||
oleg-jukovec marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Context sets a passed context to CRUD request. | ||
func (req *CountRequest) Context(ctx context.Context) *CountRequest { | ||
req.impl = req.impl.Context(ctx) | ||
|
||
return req | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package crud | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tarantool/go-tarantool" | ||
) | ||
|
||
// DeleteResult describes result for `crud.delete` method. | ||
type DeleteResult = Result | ||
|
||
// DeleteOpts describes options for `crud.delete` method. | ||
type DeleteOpts = SimpleOperationOpts | ||
|
||
// DeleteRequest helps you to create request object to call `crud.delete` | ||
// for execution by a Connection. | ||
type DeleteRequest struct { | ||
spaceRequest | ||
key Tuple | ||
opts DeleteOpts | ||
} | ||
|
||
type deleteArgs struct { | ||
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused | ||
Space string | ||
Key Tuple | ||
Opts DeleteOpts | ||
} | ||
|
||
// NewDeleteRequest returns a new empty DeleteRequest. | ||
func NewDeleteRequest(space string) *DeleteRequest { | ||
req := new(DeleteRequest) | ||
req.initImpl("crud.delete") | ||
req.setSpace(space) | ||
req.key = Tuple{} | ||
req.opts = DeleteOpts{} | ||
return req | ||
} | ||
|
||
// Key sets the key for the DeleteRequest request. | ||
// Note: default value is nil. | ||
func (req *DeleteRequest) Key(key Tuple) *DeleteRequest { | ||
req.key = key | ||
return req | ||
} | ||
|
||
// Opts sets the options for the DeleteRequest request. | ||
// Note: default value is nil. | ||
func (req *DeleteRequest) Opts(opts DeleteOpts) *DeleteRequest { | ||
req.opts = opts | ||
return req | ||
} | ||
|
||
// Body fills an encoder with the call request body. | ||
func (req *DeleteRequest) Body(res tarantool.SchemaResolver, enc *encoder) error { | ||
args := deleteArgs{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 *DeleteRequest) Context(ctx context.Context) *DeleteRequest { | ||
req.impl = req.impl.Context(ctx) | ||
|
||
return req | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.