diff --git a/cmd/main/main.go b/cmd/main/main.go index fbd9d30..bb6f271 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -3,28 +3,28 @@ package main import ( "fmt" "github.com/expectedsh/go-sonic/sonic" - "time" ) func main() { - connection := &sonic.Connection{ - Host: "localhost", - Port: 1491, - Password: "SecretPassword", - Channel: sonic.Ingest, + + ingester, err := sonic.NewIngester("localhost", 1491, "SecretPassword") + if err != nil { + panic(err) } - e := connection.Connect() - if e != nil { - panic(e) + // I will ignore all errors for demonstration purposes + + _ = ingester.Push("movies", "general", "id:6ab56b4kk3", "Star wars") + _ = ingester.Push("movies", "general", "id:5hg67f8dg5", "Spider man") + _ = ingester.Push("movies", "general", "id:1m2n3b4vf6", "Batman") + _ = ingester.Push("movies", "general", "id:68d96h5h9d0", "This is another movie") + + search, err := sonic.NewSearch("localhost", 1491, "SecretPassword") + if err != nil { + panic(err) } - channel := sonic.IngesterChannel{Connection: connection} - 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") + results, _ := search.Query("movies", "general", "man", 10, 0) - fmt.Println(c, e) + fmt.Println(results) } diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..7f4a4e8 --- /dev/null +++ b/readme.md @@ -0,0 +1,42 @@ +## Go client for the sonic search backend + +This package implement all commands to work with sonic. If there is one missing, open an issue ! :) + +### Install + +`go get github.com/expectedsh/go-sonic` + +### Example + +```go +package main + +import ( + "fmt" + "github.com/expectedsh/go-sonic/sonic" +) + +func main() { + + ingester, err := sonic.NewIngester("localhost", 1491, "SecretPassword") + if err != nil { + panic(err) + } + + // I will ignore all errors for demonstration purposes + + _ = ingester.Push("movies", "general", "id:6ab56b4kk3", "Star wars") + _ = ingester.Push("movies", "general", "id:5hg67f8dg5", "Spider man") + _ = ingester.Push("movies", "general", "id:1m2n3b4vf6", "Batman") + _ = ingester.Push("movies", "general", "id:68d96h5h9d0", "This is another movie") + + search, err := sonic.NewSearch("localhost", 1491, "SecretPassword") + if err != nil { + panic(err) + } + + results, _ := search.Query("movies", "general", "man", 10, 0) + + fmt.Println(results) +} +``` \ No newline at end of file diff --git a/sonic/actions.go b/sonic/actions.go new file mode 100644 index 0000000..906b38b --- /dev/null +++ b/sonic/actions.go @@ -0,0 +1,11 @@ +package sonic + +type Action string + +const ( + Consolidate Action = "consolidate" +) + +func IsActionValid(action Action) bool { + return action == Consolidate +} diff --git a/sonic/connector.go b/sonic/driver.go similarity index 60% rename from sonic/connector.go rename to sonic/driver.go index b6fdedc..8e183ea 100644 --- a/sonic/connector.go +++ b/sonic/driver.go @@ -10,9 +10,13 @@ import ( "strings" ) -var ClosedError = errors.New("sonic connection is closed") +var ( + ClosedError = errors.New("sonic connection is closed") + InvalidChanName = errors.New("invalid channel name") + InvalidActionName = errors.New("invalid action name") +) -type Connection struct { +type Driver struct { Host string Port int Password string @@ -23,9 +27,20 @@ type Connection struct { closed bool } -func (c *Connection) Connect() error { +func NewControl(host string, port int, password string) (*Driver, error) { + driver := &Driver{ + Host: host, + Port: port, + Password: password, + Channel: Ingest, + } + + return driver, driver.connect() +} + +func (c *Driver) connect() error { if !IsChannelValid(c.Channel) { - return errors.New("invalid channel name") + return InvalidChanName } conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", c.Host, c.Port)) @@ -49,7 +64,7 @@ func (c *Connection) Connect() error { } } -func (c *Connection) read() (string, error) { +func (c *Driver) read() (string, error) { if c.closed { return "", ClosedError } @@ -75,7 +90,7 @@ func (c *Connection) read() (string, error) { return str, nil } -func (c Connection) write(str string) error { +func (c Driver) write(str string) error { if c.closed { return ClosedError } @@ -83,34 +98,50 @@ func (c Connection) write(str string) error { return err } -func (c *Connection) Quit() error { +func (c *Driver) 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 { +func (c Driver) 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() { +func (c Driver) Trigger(action Action) error { + if IsActionValid(action) { + return InvalidActionName + } + err := c.write(fmt.Sprintf("TRIGGER %s", action)) + if err != nil { + return err + } + + // should get OK + _, err = c.read() + if err != nil { + return err + } + return nil +} + +func (c *Driver) clean() { c.closed = true _ = c.conn.Close() c.conn = nil diff --git a/sonic/ingester.go b/sonic/ingester.go index 5808b15..d0288a8 100644 --- a/sonic/ingester.go +++ b/sonic/ingester.go @@ -28,7 +28,23 @@ const ( ) type IngesterChannel struct { - *Connection + *Driver +} + +func NewIngester(host string, port int, password string) (Ingestable, error) { + driver := &Driver{ + Host: host, + Port: port, + Password: password, + Channel: Ingest, + } + err := driver.connect() + if err != nil { + return nil, err + } + return IngesterChannel{ + Driver: driver, + }, nil } func (i IngesterChannel) Push(collection, bucket, object, text string) (err error) { diff --git a/sonic/search.go b/sonic/search.go index 6b3a347..af3b161 100644 --- a/sonic/search.go +++ b/sonic/search.go @@ -18,7 +18,23 @@ const ( ) type SearchChannel struct { - *Connection + *Driver +} + +func NewSearch(host string, port int, password string) (Searchable, error) { + driver := &Driver{ + Host: host, + Port: port, + Password: password, + Channel: Search, + } + err := driver.connect() + if err != nil { + return nil, err + } + return SearchChannel{ + Driver: driver, + }, nil } func (s SearchChannel) Query(collection, bucket, term string, limit, offset int) (results []string, err error) {