Skip to content

Commit

Permalink
Rename Conn to conn to make it private.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jul 27, 2017
1 parent d7eb680 commit a42011d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 186 deletions.
30 changes: 15 additions & 15 deletions v2/libgrn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
85 changes: 30 additions & 55 deletions v2/libgrn/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,18 +28,18 @@ 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,
ready: true,
}
}

// 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.",
Expand All @@ -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 {
Expand All @@ -118,23 +118,23 @@ 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
}
c.bufSize = n
}

// 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)
}
return c.buf
}

// 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]
Expand All @@ -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
Expand All @@ -160,15 +160,15 @@ 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)
}
return c.execNoBodyDB(cmd)
}

// 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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.",
Expand All @@ -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())
}

0 comments on commit a42011d

Please sign in to comment.