Skip to content

Commit

Permalink
Merge pull request #4 from AnjaBruls/master
Browse files Browse the repository at this point in the history
Added InsertBin function for binary data
  • Loading branch information
Jeroen van der Heijden authored Jan 9, 2019
2 parents f9b19c9 + f570e5f commit f151c20
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
19 changes: 18 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/rand"
"reflect"
"time"

qpack "github.com/transceptor-technology/go-qpack"
)

const defaultPingInterval = 30
Expand Down Expand Up @@ -129,6 +131,21 @@ func (client Client) Query(query string, timeout uint16) (interface{}, error) {

// Insert data into a SiriDB database.
func (client Client) Insert(data interface{}, timeout uint16) (interface{}, error) {
var err error
var dataBin []byte

if data != nil {
dataBin, err = qpack.Pack(data)
if err != nil {
return nil, err
}
}
return client.InsertBin(dataBin, timeout)

}

// Insert binary data into a SiriDB database.
func (client Client) InsertBin(data []byte, timeout uint16) (interface{}, error) {
firstTry := true
for {
host := client.pickHost(false)
Expand All @@ -139,7 +156,7 @@ func (client Client) Insert(data interface{}, timeout uint16) (interface{}, erro
}

if host == nil {
return nil, fmt.Errorf("no available conections found")
return nil, fmt.Errorf("no available connections found")
}

res, err := host.conn.Insert(data, timeout)
Expand Down
46 changes: 38 additions & 8 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (conn *Connection) Insert(data interface{}, timeout uint16) (interface{}, e
return conn.Send(CprotoReqInsert, data, timeout)
}

// Insert sends binary data to a SiriDB database.
func (conn *Connection) InsertBin(data []byte, timeout uint16) (interface{}, error) {
return conn.SendBin(CprotoReqInsert, data, timeout)
}

func getResult(respCh chan *Pkg, timeoutCh chan bool) (interface{}, error) {
var result interface{}
var err error
Expand Down Expand Up @@ -135,19 +140,19 @@ func getResult(respCh chan *Pkg, timeoutCh chan bool) (interface{}, error) {
return result, err
}

// Send is used to send bytes
func (conn *Connection) Send(tp uint8, data interface{}, timeout uint16) (interface{}, error) {
func (conn *Connection) increPid() uint16 {
conn.mux.Lock()
pid := conn.pid
b, err := pack(pid, tp, data)

if err != nil {
return nil, err
}
conn.pid++
conn.mux.Unlock()
return pid
}

func (conn *Connection) getRespCh(pid uint16, b []byte, timeout uint16) (interface{}, error) {
respCh := make(chan *Pkg, 1)

conn.mux.Lock()
conn.respMap[pid] = respCh
conn.pid++
conn.mux.Unlock()

conn.buf.conn.Write(b)
Expand All @@ -166,6 +171,31 @@ func (conn *Connection) Send(tp uint8, data interface{}, timeout uint16) (interf
conn.mux.Unlock()

return result, err

}

// Send is used to send bytes
func (conn *Connection) Send(tp uint8, data interface{}, timeout uint16) (interface{}, error) {
pid := conn.increPid()
b, err := pack(pid, tp, data)

if err != nil {
return nil, err
}

return conn.getRespCh(pid, b, timeout)
}

// Send is used to send bytes
func (conn *Connection) SendBin(tp uint8, data []byte, timeout uint16) (interface{}, error) {
pid := conn.increPid()
b, err := packBin(pid, tp, data)

if err != nil {
return nil, err
}

return conn.getRespCh(pid, b, timeout)
}

func niceErr(err error) string {
Expand Down
6 changes: 6 additions & 0 deletions pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func pack(pid uint16, tp uint8, v interface{}) ([]byte, error) {
}
}

return packBin(pid, tp, data)
}

// packbin
func packBin(pid uint16, tp uint8, data []byte) ([]byte, error) {

// create pkg with final size.
pkg := make([]byte, 8+len(data))

Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package siridb

// AppVersion exposes version information
const AppVersion = "1.0.10"
const AppVersion = "1.0.11"

0 comments on commit f151c20

Please sign in to comment.