Susumu Yata
null+****@clear*****
Thu Jul 27 12:19:25 JST 2017
Susumu Yata 2017-07-27 12:19:25 +0900 (Thu, 27 Jul 2017) New Revision: a42011da4ee245bb32b319c86bc6236be1486168 https://github.com/groonga/grnci/commit/a42011da4ee245bb32b319c86bc6236be1486168 Message: Rename Conn to conn to make it private. Removed files: v2/libgrn/conn_test.go Modified files: v2/libgrn/client.go v2/libgrn/conn.go v2/libgrn/db_test.go v2/libgrn/response.go Modified: v2/libgrn/client.go (+15 -15) =================================================================== --- v2/libgrn/client.go 2017-07-27 11:52:44 +0900 (863ad86) +++ v2/libgrn/client.go 2017-07-27 12:19:25 +0900 (d0ee702) @@ -25,8 +25,8 @@ func NewClientOptions() *ClientOptions { // Client is a thread-safe GQTP client or DB handle. type Client struct { addr string - baseConn *Conn - idleConns chan *Conn + baseConn *conn + idleConns chan *conn } // DialClient returns a new Client connected to a GQTP server. @@ -35,16 +35,16 @@ func DialClient(addr string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - conn, err := Dial(addr) + cn, err := dial(addr) if err != nil { return nil, err } c := &Client{ addr: addr, - idleConns: make(chan *Conn, options.MaxIdleConns), + idleConns: make(chan *conn, options.MaxIdleConns), } - c.idleConns <- conn - conn.client = c + c.idleConns <- cn + cn.client = c return c, nil } @@ -53,13 +53,13 @@ func OpenClient(path string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - conn, err := Open(path) + cn, err := open(path) if err != nil { return nil, err } return &Client{ - baseConn: conn, - idleConns: make(chan *Conn, options.MaxIdleConns), + baseConn: cn, + idleConns: make(chan *conn, options.MaxIdleConns), }, nil } @@ -68,13 +68,13 @@ func CreateClient(path string, options *ClientOptions) (*Client, error) { if options == nil { options = NewClientOptions() } - conn, err := Create(path) + cn, err := create(path) if err != nil { return nil, err } return &Client{ - baseConn: conn, - idleConns: make(chan *Conn, options.MaxIdleConns), + baseConn: cn, + idleConns: make(chan *conn, options.MaxIdleConns), }, nil } @@ -104,13 +104,13 @@ Loop: // exec sends a command and receives a response. func (c *Client) exec(cmd string, body io.Reader) (grnci.Response, error) { - var conn *Conn + var conn *conn var err error select { case conn = <-c.idleConns: default: if c.baseConn == nil { - conn, err = Dial(c.addr) + conn, err = dial(c.addr) if err != nil { return nil, err } @@ -123,7 +123,7 @@ func (c *Client) exec(cmd string, body io.Reader) (grnci.Response, error) { conn.client = c } } - resp, err := conn.exec(cmd, body) + resp, err := conn.Exec(cmd, body) if err != nil { conn.Close() return nil, err Modified: v2/libgrn/conn.go (+30 -55) =================================================================== --- v2/libgrn/conn.go 2017-07-27 11:52:44 +0900 (df94f28) +++ v2/libgrn/conn.go 2017-07-27 12:19:25 +0900 (58c0226) @@ -17,8 +17,8 @@ const ( defaultBufferSize = 1 << 16 // Default buffer size ) -// Conn is a thread-unsafe GQTP client or DB handle. -type Conn struct { +// conn is a thread-unsafe GQTP client or DB handle. +type conn struct { client *Client // Owner client if available ctx *grnCtx // C.grn_ctx db *grnDB // C.grn_obj @@ -28,9 +28,9 @@ type Conn struct { broken bool // Whether or not the connection is broken } -// newConn returns a new Conn. -func newConn(ctx *grnCtx, db *grnDB) *Conn { - return &Conn{ +// newConn returns a new conn. +func newConn(ctx *grnCtx, db *grnDB) *conn { + return &conn{ ctx: ctx, db: db, bufSize: defaultBufferSize, @@ -38,8 +38,8 @@ func newConn(ctx *grnCtx, db *grnDB) *Conn { } } -// Dial returns a new Conn connected to a GQTP server. -func Dial(addr string) (*Conn, error) { +// dial returns a new conn connected to a GQTP server. +func dial(addr string) (*conn, error) { a, err := grnci.ParseGQTPAddress(addr) if err != nil { return nil, err @@ -59,8 +59,8 @@ func Dial(addr string) (*Conn, error) { return newConn(ctx, nil), nil } -// Open opens an existing DB and returns a new Conn as its handle. -func Open(path string) (*Conn, error) { +// open opens an existing DB and returns a new conn as its handle. +func open(path string) (*conn, error) { ctx, err := newGrnCtx() if err != nil { return nil, err @@ -73,8 +73,8 @@ func Open(path string) (*Conn, error) { return newConn(ctx, db), nil } -// Create creates a new DB and returns a new Conn as its handle. -func Create(path string) (*Conn, error) { +// create creates a new DB and returns a new conn as its handle. +func create(path string) (*conn, error) { ctx, err := newGrnCtx() if err != nil { return nil, err @@ -87,8 +87,8 @@ func Create(path string) (*Conn, error) { return newConn(ctx, db), nil } -// Dup duplicates the Conn if it is a DB handle. -func (c *Conn) Dup() (*Conn, error) { +// Dup duplicates the conn if it is a DB handle. +func (c *conn) Dup() (*conn, error) { if c.db == nil { return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ "error": "GQTP clients do not support Dup.", @@ -101,8 +101,8 @@ func (c *Conn) Dup() (*Conn, error) { return newConn(ctx, c.db), nil } -// Close closes the Conn. -func (c *Conn) Close() error { +// Close closes the conn. +func (c *conn) Close() error { var err error if c.db != nil { if e := c.db.Close(c.ctx); e != nil { @@ -118,7 +118,7 @@ func (c *Conn) Close() error { } // SetBufferSize updates the size of the copy buffer. -func (c *Conn) SetBufferSize(n int) { +func (c *conn) SetBufferSize(n int) { if n <= 0 || n > maxChunkSize { n = defaultBufferSize } @@ -126,7 +126,7 @@ func (c *Conn) SetBufferSize(n int) { } // getBuffer returns the copy buffer. -func (c *Conn) getBuffer() []byte { +func (c *conn) getBuffer() []byte { if len(c.buf) != c.bufSize { c.buf = make([]byte, c.bufSize) } @@ -134,7 +134,7 @@ func (c *Conn) getBuffer() []byte { } // execNoBodyGQTP sends a command and receives a response. -func (c *Conn) execNoBodyGQTP(cmd string) (grnci.Response, error) { +func (c *conn) execNoBodyGQTP(cmd string) (grnci.Response, error) { name := strings.TrimLeft(cmd, " \t\r\n") if idx := strings.IndexAny(name, " \t\r\n"); idx != -1 { name = name[:idx] @@ -150,7 +150,7 @@ func (c *Conn) execNoBodyGQTP(cmd string) (grnci.Response, error) { } // execNoBodyDB executes a command and receives a response. -func (c *Conn) execNoBodyDB(cmd string) (grnci.Response, error) { +func (c *conn) execNoBodyDB(cmd string) (grnci.Response, error) { if err := c.ctx.Send([]byte(cmd), flagTail); err != nil { data, flags, _ := c.ctx.Recv() return newDBResponse(c, data, flags, err), nil @@ -160,7 +160,7 @@ func (c *Conn) execNoBodyDB(cmd string) (grnci.Response, error) { } // execNoBody sends a command without body and receives a response. -func (c *Conn) execNoBody(cmd string) (grnci.Response, error) { +func (c *conn) execNoBody(cmd string) (grnci.Response, error) { if c.db == nil { return c.execNoBodyGQTP(cmd) } @@ -168,7 +168,7 @@ func (c *Conn) execNoBody(cmd string) (grnci.Response, error) { } // execBodyGQTP sends a command and receives a response. -func (c *Conn) execBodyGQTP(cmd string, body io.Reader) (grnci.Response, error) { +func (c *conn) execBodyGQTP(cmd string, body io.Reader) (grnci.Response, error) { name := strings.TrimLeft(cmd, " \t\r\n") if idx := strings.IndexAny(name, " \t\r\n"); idx != -1 { name = name[:idx] @@ -215,7 +215,7 @@ func (c *Conn) execBodyGQTP(cmd string, body io.Reader) (grnci.Response, error) } // execBodyDB sends a command and receives a response. -func (c *Conn) execBodyDB(cmd string, body io.Reader) (grnci.Response, error) { +func (c *conn) execBodyDB(cmd string, body io.Reader) (grnci.Response, error) { if err := c.ctx.Send([]byte(cmd), 0); err != nil { data, flags, _ := c.ctx.Recv() return newDBResponse(c, data, flags, err), nil @@ -252,15 +252,20 @@ func (c *Conn) execBodyDB(cmd string, body io.Reader) (grnci.Response, error) { } // execBody sends a command with body and receives a response. -func (c *Conn) execBody(cmd string, body io.Reader) (grnci.Response, error) { +func (c *conn) execBody(cmd string, body io.Reader) (grnci.Response, error) { if c.db == nil { return c.execBodyGQTP(cmd, body) } return c.execBodyDB(cmd, body) } -// exec sends a command and receives a response. -func (c *Conn) exec(cmd string, body io.Reader) (grnci.Response, error) { +// Exec sends a command and receives a response. +func (c *conn) Exec(cmd string, body io.Reader) (grnci.Response, error) { + if c.broken { + return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ + "error": "The connection is broken.", + }) + } if !c.ready { return nil, grnci.NewError(grnci.OperationError, map[string]interface{}{ "error": "The connection is not ready to send a command.", @@ -278,33 +283,3 @@ func (c *Conn) exec(cmd string, body io.Reader) (grnci.Response, error) { } return c.execBody(cmd, body) } - -// Exec parses cmd, reassembles it and calls Query. -// The Conn must not be used until the response is closed. -func (c *Conn) Exec(cmd string, body io.Reader) (grnci.Response, error) { - command, err := grnci.ParseCommand(cmd) - if err != nil { - return nil, err - } - command.SetBody(body) - return c.Query(command) -} - -// Invoke assembles name, params and body into a command and calls Query. -func (c *Conn) Invoke(name string, params map[string]interface{}, body io.Reader) (grnci.Response, error) { - cmd, err := grnci.NewCommand(name, params) - if err != nil { - return nil, err - } - cmd.SetBody(body) - return c.Query(cmd) -} - -// Query sends a command and receives a response. -// It is the caller's responsibility to close the response. -func (c *Conn) Query(cmd *grnci.Command) (grnci.Response, error) { - if err := cmd.Check(); err != nil { - return nil, err - } - return c.exec(cmd.String(), cmd.Body()) -} Deleted: v2/libgrn/conn_test.go (+0 -112) 100644 =================================================================== --- v2/libgrn/conn_test.go 2017-07-27 11:52:44 +0900 (95904c8) +++ /dev/null @@ -1,112 +0,0 @@ -package libgrn - -import ( - "testing" - - "github.com/groonga/grnci/v2" -) - -// func TestConnGQTP(t *testing.T) { -// type Pair struct { -// Command string -// Body string -// } -// pairs := []Pair{ -// // Pair{"no_such_command", ""}, -// Pair{"status", ""}, -// Pair{`table_create Tbl TABLE_PAT_KEY ShortText`, ""}, -// Pair{`column_create Tbl Col COLUMN_SCALAR Int32`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test"]]'`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, ""}, -// Pair{"load --table Tbl", `[["_key"],["test"]]`}, -// Pair{"load --table Tbl", `[["_key"],["test" invalid_format]]`}, -// Pair{"select --table Tbl", ""}, -// Pair{"dump", ""}, -// } - -// conn, err := Dial("") -// if err != nil { -// t.Skipf("Dial failed: %v", err) -// } -// defer conn.Close() - -// for _, pair := range pairs { -// var body io.Reader -// if pair.Body != "" { -// body = strings.NewReader(pair.Body) -// } -// log.Printf("command = %s", pair.Command) -// resp, err := conn.Exec(pair.Command, body) -// if err != nil { -// t.Fatalf("conn.Exec failed: %v", err) -// } -// result, err := ioutil.ReadAll(resp) -// if err != nil { -// t.Fatalf("ioutil.ReadAll failed: %v", err) -// } -// log.Printf("start = %v, elapsed = %v", resp.Start(), resp.Elapsed()) -// log.Printf("result = %s", result) -// if err := resp.Err(); err != nil { -// log.Printf("err = %v", err) -// } -// if err := resp.Close(); err != nil { -// t.Fatalf("resp.Close failed: %v", err) -// } -// } -// } - -// func TestConnDB(t *testing.T) { -// type Pair struct { -// Command string -// Body string -// } -// pairs := []Pair{ -// // Pair{"no_such_command", ""}, -// Pair{"status", ""}, -// Pair{`table_create Tbl TABLE_PAT_KEY ShortText`, ""}, -// Pair{`column_create Tbl Col COLUMN_SCALAR Int32`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test"]]'`, ""}, -// Pair{`load --table Tbl --values '[["_key"],["test" invalid_format]]'`, ""}, -// Pair{"load --table Tbl", `[["_key"],["test"]]`}, -// Pair{"load --table Tbl", `[["_key"],["test" invalid_format]]`}, -// Pair{"select --table Tbl", ""}, -// Pair{"dump", ""}, -// } - -// conn, err := Open("/tmp/db/db") -// if err != nil { -// t.Skipf("Open failed: %v", err) -// } -// defer conn.Close() - -// for _, pair := range pairs { -// var body io.Reader -// if pair.Body != "" { -// body = strings.NewReader(pair.Body) -// } -// log.Printf("command = %s", pair.Command) -// resp, err := conn.Exec(pair.Command, body) -// if err != nil { -// t.Fatalf("conn.Exec failed: %v", err) -// } -// result, err := ioutil.ReadAll(resp) -// if err != nil { -// t.Fatalf("ioutil.ReadAll failed: %v", err) -// } -// log.Printf("start = %v, elapsed = %v", resp.Start(), resp.Elapsed()) -// log.Printf("result = %s", result) -// if err := resp.Err(); err != nil { -// log.Printf("err = %v", err) -// } -// if err := resp.Close(); err != nil { -// t.Fatalf("resp.Close failed: %v", err) -// } -// } -// } - -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") - } -} Modified: v2/libgrn/db_test.go (+1 -1) =================================================================== --- v2/libgrn/db_test.go 2017-07-27 11:52:44 +0900 (21fc135) +++ v2/libgrn/db_test.go 2017-07-27 12:19:25 +0900 (a7893b0) @@ -20,7 +20,7 @@ func makeDB(t *testing.T) (db *grnci.DB, dir string) { if err != nil { t.Fatalf("ioutil.TempDir failed: %v", err) } - conn, err := Create(filepath.Join(dir, "db")) + conn, err := CreateClient(filepath.Join(dir, "db"), nil) if err != nil { os.RemoveAll(dir) t.Fatalf("Open failed: %v", err) Modified: v2/libgrn/response.go (+3 -3) =================================================================== --- v2/libgrn/response.go 2017-07-27 11:52:44 +0900 (209ad86) +++ v2/libgrn/response.go 2017-07-27 12:19:25 +0900 (4863c93) @@ -10,7 +10,7 @@ import ( // response is a response. type response struct { - conn *Conn + conn *conn left []byte flags byte err error @@ -18,7 +18,7 @@ type response struct { } // newGQTPResponse returns a new GQTP response. -func newGQTPResponse(conn *Conn, name string, data []byte, flags byte, err error) *response { +func newGQTPResponse(conn *conn, name string, data []byte, flags byte, err error) *response { return &response{ conn: conn, left: data, @@ -28,7 +28,7 @@ func newGQTPResponse(conn *Conn, name string, data []byte, flags byte, err error } // newDBResponse returns a new DB response. -func newDBResponse(conn *Conn, data []byte, flags byte, err error) *response { +func newDBResponse(conn *conn, data []byte, flags byte, err error) *response { return &response{ conn: conn, left: data, -------------- next part -------------- HTML����������������������������...Download