driver is functional

This commit is contained in:
alexisvisco 2019-03-25 19:42:23 +01:00
parent df036ec680
commit 7dd0faf24b
3 changed files with 90 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"github.com/expectedsh/go-sonic/sonic" "github.com/expectedsh/go-sonic/sonic"
"time"
) )
func main() { func main() {
@ -20,6 +21,10 @@ func main() {
channel := sonic.IngesterChannel{Connection: connection} channel := sonic.IngesterChannel{Connection: connection}
c, e := channel.Count("test", "default", "captain") c, e := channel.Count("test", "default", "captain")
fmt.Println("waiting")
time.Sleep(time.Second * 10)
e = channel.Ping()
//c, e = channel.Count("test", "default", "captain")
fmt.Println(c, e) fmt.Println(c, e)
} }

View file

@ -5,10 +5,13 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"net" "net"
"strings" "strings"
) )
var ClosedError = errors.New("sonic connection is closed")
type Connection struct { type Connection struct {
Host string Host string
Port int Port int
@ -17,6 +20,7 @@ type Connection struct {
reader *bufio.Reader reader *bufio.Reader
conn net.Conn conn net.Conn
closed bool
} }
func (c *Connection) Connect() error { func (c *Connection) Connect() error {
@ -45,12 +49,21 @@ func (c *Connection) Connect() error {
} }
} }
func (c Connection) read() (string, error) { func (c *Connection) read() (string, error) {
if c.closed {
return "", ClosedError
}
buffer := bytes.Buffer{} buffer := bytes.Buffer{}
for { for {
line, isPrefix, err := c.reader.ReadLine() line, isPrefix, err := c.reader.ReadLine()
buffer.Write(line) buffer.Write(line)
if err != nil || !isPrefix { if err != nil {
if err == io.EOF {
c.clean()
}
return "", err
}
if !isPrefix {
break break
} }
} }
@ -63,6 +76,43 @@ func (c Connection) read() (string, error) {
} }
func (c Connection) write(str string) error { func (c Connection) write(str string) error {
if c.closed {
return ClosedError
}
_, err := c.conn.Write([]byte(str + "\r\n")) _, err := c.conn.Write([]byte(str + "\r\n"))
return err return err
} }
func (c *Connection) Quit() error {
err := c.write("QUIT")
if err != nil {
return err
}
// should get ENDED
_, err = c.read()
c.clean()
return err
}
func (c *Connection) Ping() error {
err := c.write("PING")
if err != nil {
fmt.Println("err write")
return err
}
// should get PONG
_, err = c.read()
if err != nil {
fmt.Println("err read")
return err
}
return nil
}
func (c *Connection) clean() {
c.closed = true
_ = c.conn.Close()
c.conn = nil
c.reader = nil
}

View file

@ -85,13 +85,43 @@ func buildCountQuery(bucket, object string) string {
} }
func (i IngesterChannel) FlushCollection(collection string) (err error) { func (i IngesterChannel) FlushCollection(collection string) (err error) {
panic("implement me") err = i.write(fmt.Sprintf("%s %s", flushc, collection))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
} }
func (i IngesterChannel) FlushBucket(collection, bucket string) (err error) { func (i IngesterChannel) FlushBucket(collection, bucket string) (err error) {
panic("implement me") err = i.write(fmt.Sprintf("%s %s %s", flushb, collection, bucket))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
} }
func (i IngesterChannel) FlushObject(collection, bucket, object string) (err error) { func (i IngesterChannel) FlushObject(collection, bucket, object string) (err error) {
panic("implement me") err = i.write(fmt.Sprintf("%s %s %s %s", flusho, collection, bucket, object))
if err != nil {
return err
}
// sonic should sent OK
_, err = i.read()
if err != nil {
return err
}
return nil
} }