Go
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore
LICENSE
README.md
buffer.go
client.go
connection.go
connection_test.go
errors.go
host.go
pkg.go
protomap.go
version.go

README.md

Go-SiriDB-Connector

Go Report Card GoDoc

A SiriDB-Connector for the Go language



Installation

Simple install the package to your $GOPATH with the go tool from shell:

$ go get github.com/SiriDB/go-siridb-connector

Make sure Git is installed on your machine and in your system's PATH.

Usage

Go SiriDB Connector can be used to communicate with a single SiriDB server and a more advanced client is provided which can connect to multiple SiriDB servers so queries and inserts are balanced.

Single connection

This is some example code for how to use the Go-SiriDB-Connector as a single connection.

package main

import (
	"fmt"

	"github.com/SiriDB/go-siridb-connector"
)

func example(conn *siridb.Connection, ok chan bool) {
	// make sure the connection will be closed
	defer conn.Close()

	// connect to database 'dbtest' using user 'iris' and password 'siri'
	// this is an example but usually you should do some error handling...
	if err := conn.Connect("iris", "siri", "dbtest"); err == nil {

		// perform a query
		if res, err := conn.Query("list series", 10); err == nil {
			fmt.Printf("Query result: %s\n", res)
		}
	}

	// send to the channel
	ok <- true
}

func main() {
	// create a new connection
	conn := siridb.NewConnection("localhost", 9000)

	// a connection will send output to stdout except when a log channel is used.
	// setup a log channel using:
	//  	conn.LogCh = myLogChannel

	// create a channel
	ok := make(chan bool)

	// run the example
	go example(conn, ok)

	// wait for the channel
	<-ok
}

SiriDB client

And one example for using the client. A client can be used for connecting to multiple siridb servers. Queries and inserts will be send to a random siridb server. When a connection is lost, it will retry to setup the connection each 30 seconds.

package main

import (
	"fmt"

	"github.com/SiriDB/go-siridb-connector"
)

func example(client *siridb.Client, ok chan bool) {
	// make sure the connection will be closed
	defer client.Close()

	client.Connect()

	// IsConnected() returns true if at least one server is connected.
	// Failed connections will retry creating a connection each 30 seconds.
	if client.IsConnected() {
		if res, err := client.Query("list series", 2); err == nil {
			fmt.Printf("Query result: %s\n", res)
		}
	} else {
		fmt.Println("not even a single server is connected...")
	}

	// send to the channel
	ok <- true
}

func main() {
	// create a new client
	client := siridb.NewClient(
		"iris",   // username
		"siri",   // password
		"dbtest", // database
		[][]interface{}{
			{"server1", 9000},
			{"server2", 9000},
		}, // siridb server(s)
		nil, // optional log channel
	)

	// create a channel
	ok := make(chan bool)

	// run the example
	go example(client, ok)

	// wait for the channel
	<-ok
}

Logging

Both a Connection and a Client can send logging to the standard output or to a channel for custom log handling.

For example you can create you own log handler like this:

func printLogs(logCh chan string) {
	for {
		msg := <-logCh
		fmt.Printf("Log: %s\n", msg)
	}
}

And set-up the channel like this:

logCh := make(chan string)
go printLogs(logCh)

If you plan to use the log channel with a Connection you should use the .LogCh property. For example:

conn := siridb.NewConnection(...) // create a new connection
conn.LogCh = logCh // set-up custom log channel

The Client simple accepts the channel as argument. For example:

client := siridb.NewClient(
	"user", 
	"password", 
	"database", 
	[][]interface{}{...}, // array of servers, see SiriDB client for more info
	logCh, // logCh is allowed to be nil for logging to the standard output
)