Skip to content

Commit

Permalink
Update v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 14, 2017
1 parent 5380ae8 commit a0f451d
Show file tree
Hide file tree
Showing 16 changed files with 852 additions and 60 deletions.
507 changes: 507 additions & 0 deletions v2/db.go

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions v2/db_test.go
@@ -0,0 +1,82 @@
package grnci

import (
"log"
"testing"
)

func TestDBColumnRemove(t *testing.T) {
client, err := NewHTTPClient("", nil)
if err != nil {
t.Skipf("NewHTTPClient failed: %v", err)
}
db := NewDB(client)
defer db.Close()

result, resp, err := db.ColumnRemove("no_such_table", "no_such_column")
if err != nil {
t.Fatalf("db.ColumnRemove failed: %v", err)
}
log.Printf("result = %#v", result)
log.Printf("resp = %#v", resp)
if err := resp.Err(); err != nil {
log.Printf("error = %#v", err)
}
}

func TestDBStatus(t *testing.T) {
client, err := NewHTTPClient("", nil)
if err != nil {
t.Skipf("NewHTTPClient failed: %v", err)
}
db := NewDB(client)
defer db.Close()

result, resp, err := db.Status()
if err != nil {
t.Fatalf("db.Status failed: %v", err)
}
log.Printf("result = %#v", result)
log.Printf("resp = %#v", resp)
if err := resp.Err(); err != nil {
log.Printf("error = %#v", err)
}
}

func TestDBTruncate(t *testing.T) {
client, err := NewHTTPClient("", nil)
if err != nil {
t.Skipf("NewHTTPClient failed: %v", err)
}
db := NewDB(client)
defer db.Close()

result, resp, err := db.Truncate("no_such_target")
if err != nil {
t.Fatalf("db.Truncate failed: %v", err)
}
log.Printf("result = %#v", result)
log.Printf("resp = %#v", resp)
if err := resp.Err(); err != nil {
log.Printf("error = %#v", err)
}
}

func TestDBTableRemove(t *testing.T) {
client, err := NewHTTPClient("", nil)
if err != nil {
t.Skipf("NewHTTPClient failed: %v", err)
}
db := NewDB(client)
defer db.Close()

result, resp, err := db.TableRemove("no_such_table", false)
if err != nil {
t.Fatalf("db.TableRemove failed: %v", err)
}
log.Printf("result = %#v", result)
log.Printf("resp = %#v", resp)
if err := resp.Err(); err != nil {
log.Printf("error = %#v", err)
}
}
20 changes: 19 additions & 1 deletion v2/gqtp.go
Expand Up @@ -378,7 +378,16 @@ func (c *GQTPConn) Exec(cmd string, body io.Reader) (Response, error) {
return c.execBody(cmd, body)
}

// Query sends a request and receives a response.
// Invoke assembles cmd, params and body into a Request and calls Query.
func (c *GQTPConn) Invoke(cmd string, params map[string]interface{}, body io.Reader) (Response, error) {
req, err := NewRequest(cmd, params, body)
if err != nil {
return nil, err
}
return c.Query(req)
}

// Query calls Exec with req.GQTPRequest and returns the result.
func (c *GQTPConn) Query(req *Request) (Response, error) {
cmd, body, err := req.GQTPRequest()
if err != nil {
Expand Down Expand Up @@ -451,6 +460,15 @@ func (c *GQTPClient) Exec(cmd string, body io.Reader) (Response, error) {
return resp, nil
}

// Invoke assembles cmd, params and body into a Request and calls Query.
func (c *GQTPClient) Invoke(cmd string, params map[string]interface{}, body io.Reader) (Response, error) {
req, err := NewRequest(cmd, params, body)
if err != nil {
return nil, err
}
return c.Query(req)
}

// Query calls Exec with req.GQTPRequest and returns the result.
func (c *GQTPClient) Query(req *Request) (Response, error) {
cmd, body, err := req.GQTPRequest()
Expand Down
14 changes: 14 additions & 0 deletions v2/gqtp_test.go
Expand Up @@ -101,3 +101,17 @@ func TestGQTPClient(t *testing.T) {
}
}
}

func TestGQTPConnHandler(t *testing.T) {
var i interface{} = &GQTPConn{}
if _, ok := i.(Handler); !ok {
t.Fatalf("Failed to cast from *GQTPConn to Handler")
}
}

func TestGQTPClientHandler(t *testing.T) {
var i interface{} = &GQTPClient{}
if _, ok := i.(Handler); !ok {
t.Fatalf("Failed to cast from *GQTPClient to Handler")
}
}
11 changes: 11 additions & 0 deletions v2/handler.go
@@ -0,0 +1,11 @@
package grnci

import "io"

// Handler defines the required methods of DB clients and handles.
type Handler interface {
Exec(cmd string, body io.Reader) (Response, error)
Invoke(cmd string, params map[string]interface{}, body io.Reader) (Response, error)
Query(req *Request) (Response, error)
Close() error
}
29 changes: 21 additions & 8 deletions v2/http.go
Expand Up @@ -363,22 +363,35 @@ func (c *HTTPClient) exec(cmd string, params map[string]string, body io.Reader)
return resp, nil
}

// Exec sends a command and returns a response.
// It is the caller's responsibility to close the response.
func (c *HTTPClient) Exec(cmd string, params map[string]string, body io.Reader) (Response, error) {
start := time.Now()
resp, err := c.exec(cmd, params, body)
// Exec assembles cmd and body into a Request and calls Query.
func (c *HTTPClient) Exec(cmd string, body io.Reader) (Response, error) {
req, err := ParseRequest(cmd, body)
if err != nil {
return nil, err
}
return newHTTPResponse(resp, start)
return c.Query(req)
}

// Invoke assembles cmd, params and body into a Request and calls Query.
func (c *HTTPClient) Invoke(cmd string, params map[string]interface{}, body io.Reader) (Response, error) {
req, err := NewRequest(cmd, params, body)
if err != nil {
return nil, err
}
return c.Query(req)
}

// Query calls Exec with req.HTTPRequest and returns the result.
// Query sends a request and receives a response.
// It is the caller's responsibility to close the response.
func (c *HTTPClient) Query(req *Request) (Response, error) {
start := time.Now()
cmd, params, body, err := req.HTTPRequest()
if err != nil {
return nil, err
}
return c.Exec(cmd, params, body)
resp, err := c.exec(cmd, params, body)
if err != nil {
return nil, err
}
return newHTTPResponse(resp, start)
}
13 changes: 8 additions & 5 deletions v2/http_test.go
Expand Up @@ -37,12 +37,8 @@ func TestHTTPClient(t *testing.T) {
if pair.Body != "" {
body = strings.NewReader(pair.Body)
}
req, err := ParseRequest(pair.Command, body)
if err != nil {
t.Fatalf("ParseRequest failed: %v", err)
}
log.Printf("command = %s", pair.Command)
resp, err := client.Query(req)
resp, err := client.Exec(pair.Command, body)
if err != nil {
t.Fatalf("conn.Exec failed: %v", err)
}
Expand All @@ -58,3 +54,10 @@ func TestHTTPClient(t *testing.T) {
}
}
}

func TestHTTPClientHandler(t *testing.T) {
var i interface{} = &HTTPClient{}
if _, ok := i.(Handler); !ok {
t.Fatalf("Failed to cast from *HTTPClient to Handler")
}
}
11 changes: 10 additions & 1 deletion v2/libgrn/client.go
Expand Up @@ -3,7 +3,7 @@ package libgrn
import (
"io"

"github.com/groonga/grnci/v2"
"github.com/s-yata/grnci"
)

const (
Expand Down Expand Up @@ -111,6 +111,15 @@ func (c *Client) Exec(cmd string, body io.Reader) (grnci.Response, error) {
return resp, nil
}

// Invoke assembles cmd, params and body into a grnci.Request and calls Query.
func (c *Client) Invoke(cmd string, params map[string]interface{}, body io.Reader) (grnci.Response, error) {
req, err := grnci.NewRequest(cmd, params, body)
if err != nil {
return nil, err
}
return c.Query(req)
}

// Query calls Exec with req.GQTPRequest and returns the result.
func (c *Client) Query(req *grnci.Request) (grnci.Response, error) {
cmd, body, err := req.GQTPRequest()
Expand Down
9 changes: 9 additions & 0 deletions v2/libgrn/client_test.go
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"strings"
"testing"

"github.com/s-yata/grnci"
)

func TestClientGQTP(t *testing.T) {
Expand Down Expand Up @@ -101,3 +103,10 @@ func TestClientDB(t *testing.T) {
}
}
}

func TestClientHandler(t *testing.T) {
var i interface{} = &Client{}
if _, ok := i.(grnci.Handler); !ok {
t.Fatalf("Failed to cast from *Client to grnci.Handler")
}
}
11 changes: 10 additions & 1 deletion v2/libgrn/conn.go
Expand Up @@ -10,7 +10,7 @@ import (
"time"
"unsafe"

"github.com/groonga/grnci/v2"
"github.com/s-yata/grnci"
)

const (
Expand Down Expand Up @@ -284,6 +284,15 @@ func (c *Conn) Exec(cmd string, body io.Reader) (grnci.Response, error) {
return c.execBody(cmd, body)
}

// Invoke assembles cmd, params and body into a grnci.Request and calls Query.
func (c *Conn) Invoke(cmd string, params map[string]interface{}, body io.Reader) (grnci.Response, error) {
req, err := grnci.NewRequest(cmd, params, body)
if err != nil {
return nil, err
}
return c.Query(req)
}

// Query calls Exec with req.GQTPRequest and returns the result.
func (c *Conn) Query(req *grnci.Request) (grnci.Response, error) {
cmd, body, err := req.GQTPRequest()
Expand Down
9 changes: 9 additions & 0 deletions v2/libgrn/conn_test.go
Expand Up @@ -6,6 +6,8 @@ import (
"log"
"strings"
"testing"

"github.com/s-yata/grnci"
)

func TestConnGQTP(t *testing.T) {
Expand Down Expand Up @@ -101,3 +103,10 @@ func TestConnDB(t *testing.T) {
}
}
}

func TestConnHandler(t *testing.T) {
var i interface{} = &Conn{}
if _, ok := i.(grnci.Handler); !ok {
t.Fatalf("Failed to cast from *Conn to grnci.Handler")
}
}
2 changes: 1 addition & 1 deletion v2/libgrn/libgrn.go
Expand Up @@ -10,7 +10,7 @@ import (
"sync"
"unsafe"

"github.com/groonga/grnci/v2"
"github.com/s-yata/grnci"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion v2/libgrn/response.go
Expand Up @@ -5,7 +5,7 @@ import (
"io/ioutil"
"time"

"github.com/groonga/grnci/v2"
"github.com/s-yata/grnci"
)

// response is a response.
Expand Down

0 comments on commit a0f451d

Please sign in to comment.