2015-07-26 16:33:29 +02:00
|
|
|
// Copyright 2014, 2015 by Sascha L. Teichmann
|
2014-08-03 15:59:56 +02:00
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-08-03 09:20:47 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"log"
|
|
|
|
"net"
|
2015-05-27 16:48:51 +02:00
|
|
|
"strconv"
|
2014-08-03 09:20:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
redisOk = []byte("+OK\r\n")
|
2015-05-26 18:12:55 +02:00
|
|
|
redisPong = []byte("+PONG\r\n")
|
2014-08-03 11:25:25 +02:00
|
|
|
redisError = []byte("-ERR\r\n")
|
2014-08-03 09:20:47 +02:00
|
|
|
redisNoSuchBlock = []byte("$-1\r\n")
|
|
|
|
redisCrnl = []byte("\r\n")
|
|
|
|
redisEmptyArray = []byte("*0\r\n")
|
|
|
|
redisQueued = []byte("+QUEUED\r\n")
|
2015-05-26 19:25:26 +02:00
|
|
|
redisTrue = []byte(":1\r\n")
|
|
|
|
redisFalse = []byte(":0\r\n")
|
2014-08-03 09:20:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Connection struct {
|
2015-03-14 19:01:00 +01:00
|
|
|
conn net.Conn
|
|
|
|
session Session
|
|
|
|
maxBulkStringSize int64
|
|
|
|
boolArray []bool
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2015-03-14 19:01:00 +01:00
|
|
|
func NewConnection(conn net.Conn, session Session, maxBulkStringSize int64) *Connection {
|
2014-08-03 09:20:47 +02:00
|
|
|
return &Connection{
|
2015-03-14 19:01:00 +01:00
|
|
|
conn: conn,
|
|
|
|
session: session,
|
|
|
|
maxBulkStringSize: maxBulkStringSize,
|
|
|
|
boolArray: []bool{}}
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Run() {
|
2014-08-06 01:11:41 +02:00
|
|
|
defer func() {
|
|
|
|
c.session.Close()
|
|
|
|
c.conn.Close()
|
|
|
|
}()
|
2014-08-03 09:20:47 +02:00
|
|
|
rce := NewRedisCommandExecutor(c)
|
2014-08-08 13:45:24 +02:00
|
|
|
r := bufio.NewReaderSize(c.conn, 8*1024)
|
2015-03-14 19:01:00 +01:00
|
|
|
parser := NewRedisParser(r, rce, c.maxBulkStringSize)
|
2014-08-03 09:20:47 +02:00
|
|
|
parser.Parse()
|
2014-08-04 15:18:32 +02:00
|
|
|
log.Println("client disconnected")
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2015-06-02 20:10:56 +02:00
|
|
|
func logError(err error) bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: %s\n", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Hget(hash, key []byte) bool {
|
2014-08-03 11:25:25 +02:00
|
|
|
|
2014-08-03 09:20:47 +02:00
|
|
|
var err error
|
|
|
|
var data []byte
|
2014-08-06 01:11:41 +02:00
|
|
|
if data, err = c.session.Fetch(hash, key); err != nil {
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
2014-08-03 11:25:25 +02:00
|
|
|
|
2014-08-03 09:20:47 +02:00
|
|
|
return c.writeBlock(data)
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:25:25 +02:00
|
|
|
func (c *Connection) Hset(hash, key, data []byte) bool {
|
2014-08-03 09:20:47 +02:00
|
|
|
|
|
|
|
var err error
|
2014-08-03 11:25:25 +02:00
|
|
|
var exists bool
|
2014-08-06 01:11:41 +02:00
|
|
|
if exists, err = c.session.Store(hash, key, data); err != nil {
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 01:11:41 +02:00
|
|
|
if c.session.InTransaction() {
|
2014-08-03 11:25:25 +02:00
|
|
|
c.boolArray = append(c.boolArray, exists)
|
2014-08-03 09:20:47 +02:00
|
|
|
return c.writeQueued()
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeBool(exists)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Multi() bool {
|
2014-08-06 01:11:41 +02:00
|
|
|
if c.session.InTransaction() {
|
2014-08-03 09:20:47 +02:00
|
|
|
log.Println("WARN: Already running transaction.")
|
|
|
|
} else {
|
2014-08-06 01:11:41 +02:00
|
|
|
if err := c.session.BeginTransaction(); err != nil {
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeError(err)
|
|
|
|
}
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
return c.writeOk()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) Exec() bool {
|
2014-08-06 01:11:41 +02:00
|
|
|
if !c.session.InTransaction() {
|
2014-08-03 09:20:47 +02:00
|
|
|
return c.writeEmptyArray()
|
|
|
|
}
|
2014-08-03 11:25:25 +02:00
|
|
|
arr := c.boolArray
|
|
|
|
c.boolArray = []bool{}
|
2014-08-06 01:11:41 +02:00
|
|
|
if err := c.session.CommitTransaction(); err != nil {
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
2014-08-03 11:25:25 +02:00
|
|
|
return c.writeBoolArray(arr)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-11 14:56:01 +02:00
|
|
|
func (c *Connection) Hkeys(hash []byte) bool {
|
2014-08-31 19:21:58 +02:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
n int
|
|
|
|
keys chan []byte
|
|
|
|
done = make(chan struct{})
|
|
|
|
)
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
if keys, n, err = c.session.AllKeys(hash, done); err != nil {
|
2014-08-11 14:56:01 +02:00
|
|
|
return c.writeError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
return c.writeEmptyArray()
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:48:51 +02:00
|
|
|
if _, err := c.conn.Write(redisLength('*', n)); err != nil {
|
2015-06-02 20:10:56 +02:00
|
|
|
return logError(err)
|
2014-08-11 14:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for key := range keys {
|
2014-09-01 00:19:47 +02:00
|
|
|
if err = c.writeBulkString(key); err != nil {
|
2015-06-02 20:10:56 +02:00
|
|
|
return logError(err)
|
2014-09-01 00:19:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-05-26 18:12:55 +02:00
|
|
|
func (c *Connection) Ping() bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
_, err := c.conn.Write(redisPong)
|
|
|
|
return logError(err)
|
2015-05-26 18:12:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-01 00:19:47 +02:00
|
|
|
func (c *Connection) HSpatial(hash, first, second []byte) bool {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
blocks chan Block
|
|
|
|
done = make(chan struct{})
|
|
|
|
)
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
if blocks, err = c.session.SpatialQuery(hash, first, second, done); err != nil {
|
|
|
|
return c.writeError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for block := range blocks {
|
|
|
|
if err = c.writeBulkString(block.Key); err != nil {
|
2015-06-02 20:10:56 +02:00
|
|
|
return logError(err)
|
2014-09-01 00:19:47 +02:00
|
|
|
}
|
|
|
|
if err = c.writeBulkString(block.Data); err != nil {
|
2015-06-02 20:10:56 +02:00
|
|
|
return logError(err)
|
2014-08-11 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-01 00:19:47 +02:00
|
|
|
|
2015-06-21 13:17:10 +02:00
|
|
|
return logError(c.writeBulkString(nil))
|
2014-08-11 14:56:01 +02:00
|
|
|
}
|
|
|
|
|
2014-08-03 11:25:25 +02:00
|
|
|
func (c *Connection) writeError(err error) bool {
|
|
|
|
logError(err)
|
2015-06-21 13:17:10 +02:00
|
|
|
_, err = c.conn.Write(redisError)
|
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) writeEmptyArray() bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
_, err := c.conn.Write(redisEmptyArray)
|
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-26 19:25:26 +02:00
|
|
|
func (c *Connection) writeBool(b bool) bool {
|
|
|
|
var err error
|
2014-08-03 11:25:25 +02:00
|
|
|
if b {
|
2015-05-26 19:25:26 +02:00
|
|
|
_, err = c.conn.Write(redisTrue)
|
|
|
|
} else {
|
|
|
|
_, err = c.conn.Write(redisFalse)
|
2014-08-03 11:25:25 +02:00
|
|
|
}
|
2015-06-21 13:17:10 +02:00
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
2015-05-27 16:48:51 +02:00
|
|
|
func redisLength(prefix byte, s int) []byte {
|
|
|
|
buf := append(make([]byte, 0, 16), prefix)
|
|
|
|
return append(strconv.AppendInt(buf, int64(s), 10), '\r', '\n')
|
|
|
|
}
|
|
|
|
|
2014-08-03 11:25:25 +02:00
|
|
|
func (c *Connection) writeBoolArray(arr []bool) bool {
|
2015-05-27 16:48:51 +02:00
|
|
|
if _, err := c.conn.Write(redisLength('*', len(arr))); err != nil {
|
2015-06-02 20:10:56 +02:00
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
2014-08-03 11:25:25 +02:00
|
|
|
for _, b := range arr {
|
|
|
|
if !c.writeBool(b) {
|
2014-08-03 09:20:47 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) writeOk() bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
_, err := c.conn.Write(redisOk)
|
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) writeQueued() bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
_, err := c.conn.Write(redisQueued)
|
|
|
|
return logError(err)
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) writeBlock(data []byte) bool {
|
2015-06-21 13:17:10 +02:00
|
|
|
return logError(c.writeBulkString(data))
|
2014-08-03 09:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Connection) writeBulkString(data []byte) (err error) {
|
|
|
|
con := c.conn
|
|
|
|
if data == nil {
|
|
|
|
_, err = con.Write(redisNoSuchBlock)
|
|
|
|
} else {
|
2015-05-27 16:48:51 +02:00
|
|
|
if _, err = con.Write(redisLength('$', len(data))); err != nil {
|
2014-08-03 09:20:47 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = con.Write(data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = con.Write(redisCrnl)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|