add readme, now the driver is ok!
This commit is contained in:
parent
7dd0faf24b
commit
95120c77b1
6 changed files with 145 additions and 29 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
42
readme.md
Normal file
42
readme.md
Normal file
|
|
@ -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)
|
||||
}
|
||||
```
|
||||
11
sonic/actions.go
Normal file
11
sonic/actions.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package sonic
|
||||
|
||||
type Action string
|
||||
|
||||
const (
|
||||
Consolidate Action = "consolidate"
|
||||
)
|
||||
|
||||
func IsActionValid(action Action) bool {
|
||||
return action == Consolidate
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue