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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/expectedsh/go-sonic/sonic"
|
"github.com/expectedsh/go-sonic/sonic"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
connection := &sonic.Connection{
|
|
||||||
Host: "localhost",
|
ingester, err := sonic.NewIngester("localhost", 1491, "SecretPassword")
|
||||||
Port: 1491,
|
if err != nil {
|
||||||
Password: "SecretPassword",
|
panic(err)
|
||||||
Channel: sonic.Ingest,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e := connection.Connect()
|
// I will ignore all errors for demonstration purposes
|
||||||
if e != nil {
|
|
||||||
panic(e)
|
_ = 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}
|
results, _ := search.Query("movies", "general", "man", 10, 0)
|
||||||
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(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"
|
"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
|
Host string
|
||||||
Port int
|
Port int
|
||||||
Password string
|
Password string
|
||||||
|
|
@ -23,9 +27,20 @@ type Connection struct {
|
||||||
closed bool
|
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) {
|
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))
|
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 {
|
if c.closed {
|
||||||
return "", ClosedError
|
return "", ClosedError
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +90,7 @@ func (c *Connection) read() (string, error) {
|
||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Connection) write(str string) error {
|
func (c Driver) write(str string) error {
|
||||||
if c.closed {
|
if c.closed {
|
||||||
return ClosedError
|
return ClosedError
|
||||||
}
|
}
|
||||||
|
|
@ -83,34 +98,50 @@ func (c Connection) write(str string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Quit() error {
|
func (c *Driver) Quit() error {
|
||||||
err := c.write("QUIT")
|
err := c.write("QUIT")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// should get ENDED
|
// should get ENDED
|
||||||
_, err = c.read()
|
_, err = c.read()
|
||||||
c.clean()
|
c.clean()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Connection) Ping() error {
|
func (c Driver) Ping() error {
|
||||||
err := c.write("PING")
|
err := c.write("PING")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err write")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// should get PONG
|
// should get PONG
|
||||||
_, err = c.read()
|
_, err = c.read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err read")
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
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.closed = true
|
||||||
_ = c.conn.Close()
|
_ = c.conn.Close()
|
||||||
c.conn = nil
|
c.conn = nil
|
||||||
|
|
@ -28,7 +28,23 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type IngesterChannel struct {
|
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) {
|
func (i IngesterChannel) Push(collection, bucket, object, text string) (err error) {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,23 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type SearchChannel struct {
|
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) {
|
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