Susumu Yata
null+****@clear*****
Tue May 16 18:27:57 JST 2017
Susumu Yata 2017-05-16 18:27:57 +0900 (Tue, 16 May 2017) New Revision: a3dbf3aafc28ddc272b8d51690742b02db34c122 https://github.com/groonga/grnci/commit/a3dbf3aafc28ddc272b8d51690742b02db34c122 Message: Update v2. Modified files: v2/libgrn/client.go v2/libgrn/handle.go v2/libgrn/libgrn.go v2/request.go Modified: v2/libgrn/client.go (+1 -1) =================================================================== --- v2/libgrn/client.go 2017-05-15 19:04:47 +0900 (a8075b4) +++ v2/libgrn/client.go 2017-05-16 18:27:57 +0900 (e08e8fc) @@ -6,7 +6,7 @@ package libgrn import "C" import "github.com/groonga/grnci/v2" -// Client is associated with a C.grn_ctx. +// Client is a GQTP client. type Client struct { ctx *grnCtx } Modified: v2/libgrn/handle.go (+3 -3) =================================================================== --- v2/libgrn/handle.go 2017-05-15 19:04:47 +0900 (0af4fa1) +++ v2/libgrn/handle.go 2017-05-16 18:27:57 +0900 (ce133dc) @@ -12,7 +12,7 @@ import ( "github.com/groonga/grnci/v2" ) -// Handle is associated with a C.grn_ctx. +// Handle is a handle for a local DB. type Handle struct { ctx *grnCtx db *grnDB @@ -88,7 +88,7 @@ func (h *Handle) send(data []byte) error { return nil } -// recv receives the response to sent data. +// recv receives data. func (h *Handle) recv() ([]byte, error) { var resp *C.char var respLen C.uint @@ -108,7 +108,7 @@ func (h *Handle) Query(req *grnci.Request) (*grnci.Response, error) { if err != nil { return nil, err } - if err := h.send([]byte(cmd)); err != nil { + if err := h.send(cmd); err != nil { respBytes, _ := h.recv() resp, _ := grnci.NewResponse(respBytes) return resp, err Modified: v2/libgrn/libgrn.go (+34 -0) =================================================================== --- v2/libgrn/libgrn.go 2017-05-15 19:04:47 +0900 (b77537f) +++ v2/libgrn/libgrn.go 2017-05-16 18:27:57 +0900 (a984236) @@ -8,6 +8,7 @@ import "C" import ( "errors" "fmt" + "reflect" "sync" "unsafe" ) @@ -89,6 +90,7 @@ func (c *grnCtx) Close() error { return nil } +// TODO func (c *grnCtx) Err() error { if c.ctx.rc == C.GRN_SUCCESS { return nil @@ -96,6 +98,38 @@ func (c *grnCtx) Err() error { return fmt.Errorf("rc = %s: %s", c.ctx.rc, C.GoString(&c.ctx.errbuf[0])) } +// Send sends data. +func (c *grnCtx) Send(data []byte, flags int) error { + var p *C.char + if len(data) != 0 { + p = (*C.char)(unsafe.Pointer(&data[0])) + } + rc := C.grn_rc(C.grn_ctx_send(c.ctx, p, C.uint(len(data)), C.int(flags))) + if (rc != C.GRN_SUCCESS) || (c.ctx.rc != C.GRN_SUCCESS) { + return fmt.Errorf("C.grn_ctx_send failed: rc = %d", rc) + } + return nil +} + +// Recv receives data. +// +// Note that data will be desrtoyed by the next operation on the same context. +func (c *grnCtx) Recv() (data []byte, flags int, err error) { + var cPtr *C.char + var cLen C.uint + var cFlags C.int + rc := C.grn_rc(C.grn_ctx_recv(c.ctx, &cPtr, &cLen, &cFlags)) + if (rc != C.GRN_SUCCESS) || (c.ctx.rc != C.GRN_SUCCESS) { + return nil, 0, fmt.Errorf("C.grn_ctx_recv failed: rc = %s", rc) + } + head := (*reflect.SliceHeader)(unsafe.Pointer(&data)) + head.Data = uintptr(unsafe.Pointer(cPtr)) + head.Len = int(cLen) + head.Cap = int(cLen) + flags = int(cFlags) + return +} + // grnDB is a DB handle. type grnDB struct { obj *C.grn_obj Modified: v2/request.go (+5 -5) =================================================================== --- v2/request.go 2017-05-15 19:04:47 +0900 (f3bc332) +++ v2/request.go 2017-05-16 18:27:57 +0900 (d7bfa2c) @@ -42,13 +42,13 @@ func (r *Request) Check() error { return nil } -// Assemble assembles Command and Arguments into a command string. +// Assemble assembles Command and Arguments into command bytes. // -// The command string format is +// The command format is // Command --Arguments[i].Key 'Arguments[i].Value' ... -func (r *Request) Assemble() (string, error) { +func (r *Request) Assemble() ([]byte, error) { if err := r.Check(); err != nil { - return "", err + return nil, err } size := len(r.Command) for _, arg := range r.Arguments { @@ -78,5 +78,5 @@ func (r *Request) Assemble() (string, error) { buf = append(buf, '\'') } - return string(buf), nil + return buf, nil } -------------- next part -------------- HTML����������������������������...Télécharger