Skip to content

Commit a439c79

Browse files
committed
api: add CRUD module support
This patch provides crud [1] methods as request objects to support CRUD API. The following methods are supported: * `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` 1. https://github.com/tarantool/crud Closes #108
1 parent a22527a commit a439c79

32 files changed

+2650
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1212

1313
- Support pagination (#246)
1414
- A Makefile target to test with race detector (#218)
15+
- Support CRUD API (#108)
1516

1617
### Changed
1718

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ endif
2121

2222
.PHONY: clean
2323
clean:
24-
( cd ./queue; rm -rf .rocks )
24+
( rm -rf queue/.rocks crud/.rocks )
2525
rm -f $(COVERAGE_FILE)
2626

2727
.PHONY: deps
2828
deps: clean
2929
( cd ./queue/testdata; $(TTCTL) rocks install queue 1.2.1 )
30+
( cd ./crud; $(TTCTL) rocks install crud 0.14.1 )
3031

3132
.PHONY: datetime-timezones
3233
datetime-timezones:
@@ -99,6 +100,13 @@ test-settings:
99100
go clean -testcache
100101
go test -tags "$(TAGS)" ./settings/ -v -p 1
101102

103+
.PHONY: test-crud
104+
test-crud:
105+
@echo "Running tests in crud package"
106+
cd ./crud/ && tarantool -e "require('crud')"
107+
go clean -testcache
108+
go test -tags "$(TAGS)" ./crud/ -v -p 1
109+
102110
.PHONY: test-main
103111
test-main:
104112
@echo "Running tests in main package"

crud/common.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
type baseRequest struct {
10+
impl *tarantool.CallRequest
11+
}
12+
13+
func (req *baseRequest) initImpl(methodName string) {
14+
req.impl = tarantool.NewCall17Request(methodName)
15+
}
16+
17+
// Code returns IPROTO code for CRUD request.
18+
func (req *baseRequest) Code() int32 {
19+
return req.impl.Code()
20+
}
21+
22+
// Ctx returns a context of CRUD request.
23+
func (req *baseRequest) Ctx() context.Context {
24+
return req.impl.Ctx()
25+
}
26+
27+
// Async returns is CRUD request expects a response.
28+
func (req *baseRequest) Async() bool {
29+
return req.impl.Async()
30+
}
31+
32+
type spaceRequest struct {
33+
baseRequest
34+
space interface{}
35+
}
36+
37+
func (req *spaceRequest) setSpace(space interface{}) {
38+
req.space = space
39+
}

crud/conditions.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package crud
2+
3+
type Operator string
4+
5+
const (
6+
EQ Operator = "="
7+
LT Operator = "<"
8+
LE Operator = "<="
9+
Gt Operator = ">"
10+
GE Operator = ">="
11+
)
12+
13+
type Condition struct {
14+
// Instruct msgpack to pack this struct as array, so no custom packer
15+
// is needed.
16+
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
17+
Operator Operator
18+
KeyName string
19+
KeyValue interface{}
20+
}

crud/count.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
type CountOpts struct {
10+
Timeout OptUint `crud:"timeout"`
11+
VshardRouter OptString `crud:"vshard_router"`
12+
Mode OptString `crud:"mode"`
13+
PreferReplica OptBool `crud:"prefer_replica"`
14+
Balance OptBool `crud:"balance"`
15+
YieldEvery OptUint `crud:"yield_every"`
16+
BucketId OptUint `crud:"bucket_id"`
17+
ForceMapCall OptBool `crud:"force_map_call"`
18+
Fullscan OptBool `crud:"fullscan"`
19+
}
20+
21+
// CountRequest helps you to create request
22+
// object to call `crud.count` for execution
23+
// by a Connection.
24+
type CountRequest struct {
25+
spaceRequest
26+
conditions interface{}
27+
opts CountOpts
28+
}
29+
30+
// NewCountRequest returns a new empty CountRequest.
31+
func NewCountRequest(space interface{}) *CountRequest {
32+
req := new(CountRequest)
33+
req.initImpl("crud.count")
34+
req.setSpace(space)
35+
req.conditions = []interface{}{}
36+
req.opts = CountOpts{}
37+
return req
38+
}
39+
40+
// Conditions sets the conditions for the CountRequest request.
41+
// Note: default value is nil.
42+
func (req *CountRequest) Conditions(conditions interface{}) *CountRequest {
43+
req.conditions = conditions
44+
return req
45+
}
46+
47+
// Opts sets the options for the CountRequest request.
48+
// Note: default value is nil.
49+
func (req *CountRequest) Opts(opts CountOpts) *CountRequest {
50+
req.opts = opts
51+
return req
52+
}
53+
54+
// Body fills an encoder with the call request body.
55+
func (req *CountRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
56+
req.impl.Args([]interface{}{req.space, req.conditions, convertToMap(req.opts)})
57+
return req.impl.Body(res, enc)
58+
}
59+
60+
// Context sets a passed context to CRUD request.
61+
func (req *CountRequest) Context(ctx context.Context) *CountRequest {
62+
req.impl = req.impl.Context(ctx)
63+
64+
return req
65+
}

crud/delete.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// DeleteRequest helps you to create request
10+
// object to call `crud.delete` for execution
11+
// by a Connection.
12+
type DeleteRequest struct {
13+
spaceRequest
14+
key interface{}
15+
opts SimpleOperationOpts
16+
}
17+
18+
// NewDeleteRequest returns a new empty DeleteRequest.
19+
func NewDeleteRequest(space interface{}) *DeleteRequest {
20+
req := new(DeleteRequest)
21+
req.initImpl("crud.delete")
22+
req.setSpace(space)
23+
req.key = []interface{}{}
24+
req.opts = SimpleOperationOpts{}
25+
return req
26+
}
27+
28+
// Key sets the key for the DeleteRequest request.
29+
// Note: default value is nil.
30+
func (req *DeleteRequest) Key(key interface{}) *DeleteRequest {
31+
req.key = key
32+
return req
33+
}
34+
35+
// Opts sets the options for the DeleteRequest request.
36+
// Note: default value is nil.
37+
func (req *DeleteRequest) Opts(opts SimpleOperationOpts) *DeleteRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *DeleteRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.key, convertToMap(req.opts)})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *DeleteRequest) Context(ctx context.Context) *DeleteRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}

crud/get.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
type GetOpts struct {
10+
Timeout OptUint `crud:"timeout"`
11+
VshardRouter OptString `crud:"vshard_router"`
12+
Fields OptTuple `crud:"fields"`
13+
BucketId OptUint `crud:"bucket_id"`
14+
Mode OptString `crud:"mode"`
15+
PreferReplica OptBool `crud:"prefer_replica"`
16+
Balance OptBool `crud:"balance"`
17+
}
18+
19+
// GetRequest helps you to create request
20+
// object to call `crud.get` for execution
21+
// by a Connection.
22+
type GetRequest struct {
23+
spaceRequest
24+
key interface{}
25+
opts GetOpts
26+
}
27+
28+
// NewGetRequest returns a new empty GetRequest.
29+
func NewGetRequest(space interface{}) *GetRequest {
30+
req := new(GetRequest)
31+
req.initImpl("crud.get")
32+
req.setSpace(space)
33+
req.key = []interface{}{}
34+
req.opts = GetOpts{}
35+
return req
36+
}
37+
38+
// Key sets the key for the GetRequest request.
39+
// Note: default value is nil.
40+
func (req *GetRequest) Key(key interface{}) *GetRequest {
41+
req.key = key
42+
return req
43+
}
44+
45+
// Opts sets the options for the GetRequest request.
46+
// Note: default value is nil.
47+
func (req *GetRequest) Opts(opts GetOpts) *GetRequest {
48+
req.opts = opts
49+
return req
50+
}
51+
52+
// Body fills an encoder with the call request body.
53+
func (req *GetRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
54+
req.impl.Args([]interface{}{req.space, req.key, convertToMap(req.opts)})
55+
return req.impl.Body(res, enc)
56+
}
57+
58+
// Context sets a passed context to CRUD request.
59+
func (req *GetRequest) Context(ctx context.Context) *GetRequest {
60+
req.impl = req.impl.Context(ctx)
61+
62+
return req
63+
}

crud/insert.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package crud
2+
3+
import (
4+
"context"
5+
6+
"github.com/tarantool/go-tarantool"
7+
)
8+
9+
// InsertRequest helps you to create request
10+
// object to call `crud.insert` for execution
11+
// by a Connection.
12+
type InsertRequest struct {
13+
spaceRequest
14+
tuple interface{}
15+
opts SimpleOperationOpts
16+
}
17+
18+
// NewInsertRequest returns a new empty InsertRequest.
19+
func NewInsertRequest(space interface{}) *InsertRequest {
20+
req := new(InsertRequest)
21+
req.initImpl("crud.insert")
22+
req.setSpace(space)
23+
req.tuple = []interface{}{}
24+
req.opts = SimpleOperationOpts{}
25+
return req
26+
}
27+
28+
// Tuple sets the tuple for the InsertRequest request.
29+
// Note: default value is nil.
30+
func (req *InsertRequest) Tuple(tuple interface{}) *InsertRequest {
31+
req.tuple = tuple
32+
return req
33+
}
34+
35+
// Opts sets the options for the insert request.
36+
// Note: default value is nil.
37+
func (req *InsertRequest) Opts(opts SimpleOperationOpts) *InsertRequest {
38+
req.opts = opts
39+
return req
40+
}
41+
42+
// Body fills an encoder with the call request body.
43+
func (req *InsertRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
44+
req.impl.Args([]interface{}{req.space, req.tuple, convertToMap(req.opts)})
45+
return req.impl.Body(res, enc)
46+
}
47+
48+
// Context sets a passed context to CRUD request.
49+
func (req *InsertRequest) Context(ctx context.Context) *InsertRequest {
50+
req.impl = req.impl.Context(ctx)
51+
52+
return req
53+
}
54+
55+
// InsertObjectRequest helps you to create request
56+
// object to call `crud.insert_object` for execution
57+
// by a Connection.
58+
type InsertObjectRequest struct {
59+
spaceRequest
60+
object interface{}
61+
opts SimpleOperationObjectOpts
62+
}
63+
64+
// NewInsertObjectRequest returns a new empty InsertObjectRequest.
65+
func NewInsertObjectRequest(space interface{}) *InsertObjectRequest {
66+
req := new(InsertObjectRequest)
67+
req.initImpl("crud.insert_object")
68+
req.setSpace(space)
69+
req.object = map[string]interface{}{}
70+
req.opts = SimpleOperationObjectOpts{}
71+
return req
72+
}
73+
74+
// Object sets the tuple for the InsertObjectRequest request.
75+
// Note: default value is nil.
76+
func (req *InsertObjectRequest) Object(object interface{}) *InsertObjectRequest {
77+
req.object = object
78+
return req
79+
}
80+
81+
// Opts sets the options for the InsertObjectRequest request.
82+
// Note: default value is nil.
83+
func (req *InsertObjectRequest) Opts(opts SimpleOperationObjectOpts) *InsertObjectRequest {
84+
req.opts = opts
85+
return req
86+
}
87+
88+
// Body fills an encoder with the call request body.
89+
func (req *InsertObjectRequest) Body(res tarantool.SchemaResolver, enc *encoder) error {
90+
req.impl.Args([]interface{}{req.space, req.object, convertToMap(req.opts)})
91+
return req.impl.Body(res, enc)
92+
}
93+
94+
// Context sets a passed context to CRUD request.
95+
func (req *InsertObjectRequest) Context(ctx context.Context) *InsertObjectRequest {
96+
req.impl = req.impl.Context(ctx)
97+
98+
return req
99+
}

0 commit comments

Comments
 (0)