kdb

package module
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2017 License: MIT Imports: 14 Imported by: 7

README

Q driver in Go

Build Status

This is an implementation of kdb+ driver native in Go. It implements Q IPC protocol.

Can be used both as a client(Go program connects to kdb+ process) and as a server(kdb+ connects to Go program). In server mode no execution capabilities are available.

TODO

  • add unix sockets
  • add tls
  • compression

Documentation

Overview

Package kdb implements encoding and decoding of q ipc message format

Index

Constants

View Source
const (
	ASYNC    int = 0
	SYNC     int = 1
	RESPONSE int = 2
)

Request type

View Source
const (
	K0 int8 = 0 // generic type
	//      type bytes qtype     ctype  accessor
	KB int8 = 1  // 1 boolean   char   kG
	UU int8 = 2  // 16 guid     U      kU
	KG int8 = 4  // 1 byte      char   kG
	KH int8 = 5  // 2 short     short  kH
	KI int8 = 6  // 4 int       int    kI
	KJ int8 = 7  // 8 long      long   kJ
	KE int8 = 8  // 4 real      float  kE
	KF int8 = 9  // 8 float     double kF
	KC int8 = 10 // 1 char      char   kC
	KS int8 = 11 // * symbol    char*  kS

	KP int8 = 12 // 8 timestamp long   kJ (nanoseconds from 2000.01.01)
	KM int8 = 13 // 4 month     int    kI (months from 2000.01.01)
	KD int8 = 14 // 4 date      int    kI (days from 2000.01.01)
	KZ int8 = 15 // 8 datetime  double kF (DO NOT USE)
	KN int8 = 16 // 8 timespan  long   kJ (nanoseconds)
	KU int8 = 17 // 4 minute    int    kI
	KV int8 = 18 // 4 second    int    kI
	KT int8 = 19 // 4 time      int    kI (millisecond)

	// table,dict
	XT int8 = 98  //   x->k is XD
	XD int8 = 99  //   kK(x)[0] is keys. kK(x)[1] is values.
	SD int8 = 127 // sorted dict

	// function types
	KFUNC      int8 = 100
	KFUNCUP    int8 = 101 // unary primitive
	KFUNCBP    int8 = 102 // binary primitive
	KFUNCTR    int8 = 103 // ternary (operator)
	KPROJ      int8 = 104 // projection
	KCOMP      int8 = 105 // composition
	KEACH      int8 = 106 // f'
	KOVER      int8 = 107 //	f/
	KSCAN      int8 = 108 // f\
	KPRIOR     int8 = 109 // f':
	KEACHRIGHT int8 = 110 // f/:
	KEACHLEFT  int8 = 111 // f\:
	KDYNLOAD   int8 = 112 // dynamic load

	// error type
	KERR int8 = -128
)

short nil

int nil

long nil

short infinity

int infinity

long infinity

Variables

View Source
var ErrBadHeader = errors.New("Bad header")

msg header is invalid

View Source
var ErrBadMsg = errors.New("Bad Message")

message is malformated or invalid

View Source
var ErrSyncRequest = errors.New("nosyncrequest")

Cannot process sync requests

double nil

View Source
var Wf float64 = math.Inf(1)

double infinity +ve

Functions

func Encode

func Encode(w io.Writer, msgtype int, data *K) (err error)
func compress(b []byte) (dst []byte) {
	i := byte(0)
	var g int32
	//j := int32(len(b))
	f, h0, h, g, head := int32(0), int32(0), int32(0), int32(0), int32(17)
	dst = make([]byte, len(b)/2)
	c := 12
	d := c
	e := len(dst)
	p := 0
	q, r, s0 := 0, 0, 0
	s := int32(8)
	t := int32(len(b))
	a := make([]int32, 256)
	copy(dst[:4], b[:4])
	dst[2] = 1
	//dst[8:]=[]byte(strconv.Itoa(int(j)))
	for ; s < t; i *= 2 {
		if 0 == i {
			if d > e-17 {
				return b
			}
			i = 1
			dst[c] = byte(f)
			c = d
			d++
			f = 0
		}

		h = int(b[s] ^ b[s+1])
		p = a[h]
		g = (s > t-3) || (0 == p) || (0 != (b[s] ^ b[p]))

		if 0 < s0 {
			a[h0] = s0
			s0 = 0
		}
		if g {
			h0 = h
			s0 = s
			dst[d] = b[s]
			d++
			s++
		} else {
			a[h] = s
			f |= int32(i)
			p += 2
			s += 2
			r = s
			q = min32(s+255, t)
			for ; b[p] == b[s] && s < q; s++ {
				p++
			}
			dst[d] = byte(h)
			d++
			dst[d] = byte(s - r)
			d++
		}
	}
	dst[c] = byte(f)
	//dst[4:8]=d
	return dst
}

Encode data to ipc format as msgtype(sync/async/response) to specified writer

func HandleClientConnection

func HandleClientConnection(conn net.Conn)

process clients requests

func UnmarshalDict

func UnmarshalDict(t Dict, v interface{}) error

unmarshall dict to struct

func UnmarshalDictToMap

func UnmarshalDictToMap(t Dict, v interface{}) error

Unmarshall Q dictionary to map[string]{}interface

func UnmarshalTable

func UnmarshalTable(t Table, v interface{}) (interface{}, error)

Types

type Attr

type Attr int8

Vector attributes

const (
	NONE Attr = iota
	SORTED
	UNIQUE
	PARTED
	GROUPED
)

type Dict

type Dict struct {
	Key   *K
	Value *K
}

Dictionary: ordered key->value mapping. Key and Value should be slices of the same length

func (Dict) String

func (d Dict) String() string

type Function

type Function struct {
	Namespace string
	Body      string
}

Struct that represents q function

type K

type K struct {
	Type int8
	Attr Attr
	Data interface{}
}

func Atom

func Atom(t int8, x interface{}) *K

func Decode

func Decode(src *bufio.Reader) (data *K, msgtype int, e error)

Decodes data from src in q ipc format.

func Error

func Error(x error) *K

func Float

func Float(x float64) *K

func Int

func Int(x int32) *K

func Long

func Long(x int64) *K

func NewDict

func NewDict(k, v *K) *K

func NewFunc

func NewFunc(ctx, body string) *K

func NewList

func NewList(x ...*K) *K

func NewTable

func NewTable(cols []string, data []*K) *K

func Real

func Real(x float32) *K

func Symbol

func Symbol(x string) *K

func SymbolV

func SymbolV(x []string) *K

func (*K) Index

func (k *K) Index(i int) interface{}

func (*K) Len

func (k *K) Len() int

func (K) String

func (k K) String() string

type KDBConn

type KDBConn struct {
	Host string
	Port string
	// contains filtered or unexported fields
}

KDBConn establishes connection and communicates using Q IPC protocol

func DialKDB

func DialKDB(host string, port int, auth string) (*KDBConn, error)

Connect to host:port using supplies user:password. Wait until connected

func DialKDBTimeout

func DialKDBTimeout(host string, port int, auth string, timeout time.Duration) (*KDBConn, error)

Connect to host:port using supplied user:password. Wait timeout for connection

func (*KDBConn) AsyncCall

func (c *KDBConn) AsyncCall(cmd string, args ...*K) (err error)

Make asynchronous request to server

func (*KDBConn) Call

func (c *KDBConn) Call(cmd string, args ...*K) (data *K, err error)

Make synchronous call to server similar to h(func;arg1;arg2;...)

func (*KDBConn) Close

func (c *KDBConn) Close() error

Close connection to the server

func (*KDBConn) ReadMessage

func (c *KDBConn) ReadMessage() (data *K, msgtype int, e error)

Read complete message from connection

func (*KDBConn) Response

func (c *KDBConn) Response(data *K) (err error)

Send response to asynchronous request

func (*KDBConn) WriteMessage

func (c *KDBConn) WriteMessage(msgtype int, data *K) (err error)

Write data in Q IPC format

type Minute

type Minute time.Time

kdb minute type

func (Minute) String

func (m Minute) String() string

type Month

type Month int32

kdb month

func (Month) String

func (m Month) String() string

type Second

type Second time.Time

kdb second hh:mm:ss

func (Second) String

func (s Second) String() string

type Table

type Table struct {
	Columns []string
	Data    []*K
}

Table

func (*Table) Index

func (tbl *Table) Index(i int) Dict

func (Table) String

func (tbl Table) String() string

type Time

type Time time.Time

kdb time hh:mm:ss.SSS

func (Time) String

func (t Time) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL