forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
api: add support of a batch insert request #411
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
oleg-jukovec
merged 4 commits into
master
from
dmyger/gh-399-add-support-IPROTO_INSERT_ARROW
Oct 11, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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,56 @@ | ||
package arrow | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
// Arrow MessagePack extension type. | ||
const arrowExtId = 8 | ||
|
||
// Arrow struct wraps a raw arrow data buffer. | ||
type Arrow struct { | ||
data []byte | ||
} | ||
|
||
// MakeArrow returns a new arrow.Arrow object that contains | ||
// wrapped a raw arrow data buffer. | ||
func MakeArrow(arrow []byte) (Arrow, error) { | ||
return Arrow{arrow}, nil | ||
} | ||
|
||
// Raw returns a []byte that contains Arrow raw data. | ||
func (a Arrow) Raw() []byte { | ||
return a.data | ||
} | ||
|
||
func arrowDecoder(d *msgpack.Decoder, v reflect.Value, extLen int) error { | ||
arrow := Arrow{ | ||
data: make([]byte, extLen), | ||
} | ||
n, err := d.Buffered().Read(arrow.data) | ||
if err != nil { | ||
return fmt.Errorf("arrowDecoder: can't read bytes on Arrow decode: %w", err) | ||
} | ||
if n < extLen || n != len(arrow.data) { | ||
return fmt.Errorf("arrowDecoder: unexpected end of stream after %d Arrow bytes", n) | ||
} | ||
|
||
v.Set(reflect.ValueOf(arrow)) | ||
return nil | ||
} | ||
|
||
func arrowEncoder(e *msgpack.Encoder, v reflect.Value) ([]byte, error) { | ||
arr, ok := v.Interface().(Arrow) | ||
if !ok { | ||
return []byte{}, fmt.Errorf("arrowEncoder: not an Arrow type") | ||
} | ||
return arr.data, nil | ||
} | ||
|
||
func init() { | ||
msgpack.RegisterExtDecoder(arrowExtId, Arrow{}, arrowDecoder) | ||
msgpack.RegisterExtEncoder(arrowExtId, Arrow{}, arrowEncoder) | ||
} | ||
oleg-jukovec marked this conversation as resolved.
Show resolved
Hide resolved
|
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,100 @@ | ||
package arrow_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tarantool/go-tarantool/v2/arrow" | ||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
var longArrow, _ = hex.DecodeString("ffffffff70000000040000009effffff0400010004000000" + | ||
"b6ffffff0c00000004000000000000000100000004000000daffffff140000000202" + | ||
"000004000000f0ffffff4000000001000000610000000600080004000c0010000400" + | ||
"080009000c000c000c0000000400000008000a000c00040006000800ffffffff8800" + | ||
"0000040000008affffff0400030010000000080000000000000000000000acffffff" + | ||
"01000000000000003400000008000000000000000200000000000000000000000000" + | ||
"00000000000000000000000000000800000000000000000000000100000001000000" + | ||
"0000000000000000000000000a00140004000c0010000c0014000400060008000c00" + | ||
"00000000000000000000") | ||
|
||
var tests = []struct { | ||
name string | ||
arr []byte | ||
enc []byte | ||
}{ | ||
{ | ||
"abc", | ||
[]byte{'a', 'b', 'c'}, | ||
[]byte{0xc7, 0x3, 0x8, 'a', 'b', 'c'}, | ||
}, | ||
{ | ||
"empty", | ||
[]byte{}, | ||
[]byte{0xc7, 0x0, 0x8}, | ||
}, | ||
{ | ||
"one", | ||
[]byte{1}, | ||
[]byte{0xd4, 0x8, 0x1}, | ||
}, | ||
{ | ||
"long", | ||
longArrow, | ||
[]byte{ | ||
0xc8, 0x1, 0x10, 0x8, 0xff, 0xff, 0xff, 0xff, 0x70, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, | ||
0x0, 0x9e, 0xff, 0xff, 0xff, 0x4, 0x0, 0x1, 0x0, 0x4, 0x0, 0x0, 0x0, 0xb6, 0xff, 0xff, | ||
0xff, 0xc, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, | ||
0x4, 0x0, 0x0, 0x0, 0xda, 0xff, 0xff, 0xff, 0x14, 0x0, 0x0, 0x0, 0x2, 0x2, 0x0, 0x0, | ||
0x4, 0x0, 0x0, 0x0, 0xf0, 0xff, 0xff, 0xff, 0x40, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, | ||
0x61, 0x0, 0x0, 0x0, 0x6, 0x0, 0x8, 0x0, 0x4, 0x0, 0xc, 0x0, 0x10, 0x0, 0x4, 0x0, 0x8, | ||
0x0, 0x9, 0x0, 0xc, 0x0, 0xc, 0x0, 0xc, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x8, 0x0, | ||
0xa, 0x0, 0xc, 0x0, 0x4, 0x0, 0x6, 0x0, 0x8, 0x0, 0xff, 0xff, 0xff, 0xff, 0x88, 0x0, | ||
0x0, 0x0, 0x4, 0x0, 0x0, 0x0, 0x8a, 0xff, 0xff, 0xff, 0x4, 0x0, 0x3, 0x0, 0x10, 0x0, | ||
0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xac, 0xff, 0xff, | ||
0xff, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x34, 0x0, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x14, 0x0, | ||
0x4, 0x0, 0xc, 0x0, 0x10, 0x0, 0xc, 0x0, 0x14, 0x0, 0x4, 0x0, 0x6, 0x0, 0x8, 0x0, 0xc, | ||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, | ||
}, | ||
}, | ||
} | ||
|
||
func TestEncodeArrow(t *testing.T) { | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
buf := bytes.NewBuffer([]byte{}) | ||
enc := msgpack.NewEncoder(buf) | ||
|
||
arr, err := arrow.MakeArrow(tt.arr) | ||
require.NoError(t, err) | ||
|
||
err = enc.Encode(arr) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tt.enc, buf.Bytes()) | ||
}) | ||
|
||
} | ||
} | ||
|
||
func TestDecodeArrow(t *testing.T) { | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
buf := bytes.NewBuffer(tt.enc) | ||
dec := msgpack.NewDecoder(buf) | ||
|
||
var arr arrow.Arrow | ||
err := dec.Decode(&arr) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tt.arr, arr.Raw()) | ||
}) | ||
} | ||
} | ||
oleg-jukovec marked this conversation as resolved.
Show resolved
Hide resolved
|
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,61 @@ | ||
// Run Tarantool Enterprise Edition instance before example execution: | ||
// | ||
// Terminal 1: | ||
// $ cd arrow | ||
// $ TEST_TNT_WORK_DIR=$(mktemp -d -t 'tarantool.XXX') tarantool testdata/config-memcs.lua | ||
// | ||
// Terminal 2: | ||
// $ go test -v example_test.go | ||
package arrow_test | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/tarantool/go-tarantool/v2" | ||
"github.com/tarantool/go-tarantool/v2/arrow" | ||
) | ||
|
||
var arrowBinData, _ = hex.DecodeString("ffffffff70000000040000009effffff0400010004000000" + | ||
"b6ffffff0c00000004000000000000000100000004000000daffffff140000000202" + | ||
"000004000000f0ffffff4000000001000000610000000600080004000c0010000400" + | ||
"080009000c000c000c0000000400000008000a000c00040006000800ffffffff8800" + | ||
"0000040000008affffff0400030010000000080000000000000000000000acffffff" + | ||
"01000000000000003400000008000000000000000200000000000000000000000000" + | ||
"00000000000000000000000000000800000000000000000000000100000001000000" + | ||
"0000000000000000000000000a00140004000c0010000c0014000400060008000c00" + | ||
"00000000000000000000") | ||
|
||
func Example() { | ||
dialer := tarantool.NetDialer{ | ||
Address: "127.0.0.1:3013", | ||
User: "test", | ||
Password: "test", | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) | ||
client, err := tarantool.Connect(ctx, dialer, tarantool.Opts{}) | ||
cancel() | ||
if err != nil { | ||
log.Fatalf("Failed to connect: %s", err) | ||
} | ||
|
||
arr, err := arrow.MakeArrow(arrowBinData) | ||
if err != nil { | ||
log.Fatalf("Failed prepare Arrow data: %s", err) | ||
} | ||
|
||
req := arrow.NewInsertRequest("testArrow", arr) | ||
|
||
resp, err := client.Do(req).Get() | ||
if err != nil { | ||
log.Fatalf("Failed insert Arrow: %s", err) | ||
} | ||
if len(resp) > 0 { | ||
log.Fatalf("Unexpected response") | ||
} else { | ||
fmt.Printf("Batch arrow inserted") | ||
} | ||
} |
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,93 @@ | ||
package arrow | ||
|
||
import ( | ||
"context" | ||
"io" | ||
|
||
"github.com/tarantool/go-iproto" | ||
"github.com/tarantool/go-tarantool/v2" | ||
"github.com/vmihailenco/msgpack/v5" | ||
) | ||
|
||
// INSERT Arrow request. | ||
// | ||
// FIXME: replace with iproto.IPROTO_INSERT_ARROW when iproto will released. | ||
// https://github.com/tarantool/go-tarantool/issues/412 | ||
const iprotoInsertArrowType = iproto.Type(17) | ||
|
||
// The data in Arrow format. | ||
// | ||
// FIXME: replace with iproto.IPROTO_ARROW when iproto will released. | ||
// https://github.com/tarantool/go-tarantool/issues/412 | ||
const iprotoArrowKey = iproto.Key(0x36) | ||
|
||
// InsertRequest helps you to create an insert request object for execution | ||
// by a Connection. | ||
type InsertRequest struct { | ||
arrow Arrow | ||
space interface{} | ||
ctx context.Context | ||
} | ||
|
||
// NewInsertRequest returns a new InsertRequest. | ||
func NewInsertRequest(space interface{}, arrow Arrow) *InsertRequest { | ||
return &InsertRequest{ | ||
space: space, | ||
arrow: arrow, | ||
} | ||
} | ||
|
||
// Type returns a IPROTO_INSERT_ARROW type for the request. | ||
func (r *InsertRequest) Type() iproto.Type { | ||
return iprotoInsertArrowType | ||
} | ||
|
||
// Async returns false to the request return a response. | ||
func (r *InsertRequest) Async() bool { | ||
return false | ||
} | ||
|
||
// Ctx returns a context of the request. | ||
func (r *InsertRequest) Ctx() context.Context { | ||
return r.ctx | ||
} | ||
|
||
// Context sets a passed context to the request. | ||
// | ||
// Pay attention that when using context with request objects, | ||
// the timeout option for Connection does not affect the lifetime | ||
// of the request. For those purposes use context.WithTimeout() as | ||
// the root context. | ||
func (r *InsertRequest) Context(ctx context.Context) *InsertRequest { | ||
r.ctx = ctx | ||
return r | ||
} | ||
|
||
// Arrow sets the arrow for insertion the insert arrow request. | ||
// Note: default value is nil. | ||
func (r *InsertRequest) Arrow(arrow Arrow) *InsertRequest { | ||
r.arrow = arrow | ||
return r | ||
} | ||
|
||
// Body fills an msgpack.Encoder with the insert arrow request body. | ||
func (r *InsertRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error { | ||
if err := enc.EncodeMapLen(2); err != nil { | ||
return err | ||
} | ||
if err := tarantool.EncodeSpace(res, enc, r.space); err != nil { | ||
return err | ||
} | ||
if err := enc.EncodeUint(uint64(iprotoArrowKey)); err != nil { | ||
return err | ||
} | ||
return enc.Encode(r.arrow) | ||
} | ||
|
||
// Response creates a response for the InsertRequest. | ||
func (r *InsertRequest) Response( | ||
header tarantool.Header, | ||
body io.Reader, | ||
) (tarantool.Response, error) { | ||
return tarantool.DecodeBaseResponse(header, body) | ||
} |
Oops, something went wrong.
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.