2014-08-03 15:59:56 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-08-03 01:16:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2014-08-04 15:18:32 +02:00
|
|
|
"io"
|
2014-08-03 01:16:43 +02:00
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RedisConsumer interface {
|
|
|
|
ConsumeInteger(int64) bool
|
|
|
|
ConsumeArray(int64) bool
|
|
|
|
ConsumeSimpleString(string) bool
|
|
|
|
ConsumeBulkString([]byte) bool
|
|
|
|
ConsumeError(error) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisParser struct {
|
|
|
|
reader *bufio.Reader
|
|
|
|
consumer RedisConsumer
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedisParser(reader *bufio.Reader, consumer RedisConsumer) *RedisParser {
|
|
|
|
return &RedisParser{
|
|
|
|
reader: reader,
|
|
|
|
consumer: consumer}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) Parse() {
|
|
|
|
for line := rp.nextLine(); line != nil && rp.dispatch(line); line = rp.nextLine() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) nextLine() []byte {
|
|
|
|
line, err := rp.reader.ReadBytes('\n')
|
|
|
|
if err != nil {
|
2014-08-04 15:18:32 +02:00
|
|
|
if err != io.EOF {
|
|
|
|
rp.consumer.ConsumeError(err)
|
|
|
|
}
|
2014-08-03 01:16:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-03 14:52:24 +02:00
|
|
|
return bytes.TrimRight(line, "\r\n")
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) dispatch(line []byte) bool {
|
|
|
|
if len(line) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
switch line[0] {
|
|
|
|
case '-':
|
|
|
|
return true // ignore errors
|
|
|
|
case ':':
|
|
|
|
return rp.integer(line)
|
|
|
|
case '+':
|
|
|
|
return rp.simpleString(line)
|
|
|
|
case '$':
|
|
|
|
return rp.bulkString(line)
|
|
|
|
case '*':
|
|
|
|
return rp.array(line)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) simpleString(line []byte) bool {
|
|
|
|
return rp.consumer.ConsumeSimpleString(string(line[1:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) integer(line []byte) bool {
|
|
|
|
i, err := strconv.ParseInt(string(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return rp.consumer.ConsumeError(err)
|
|
|
|
}
|
|
|
|
return rp.consumer.ConsumeInteger(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) bulkString(line []byte) bool {
|
|
|
|
var i int64
|
|
|
|
var err error
|
|
|
|
i, err = strconv.ParseInt(string(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return rp.consumer.ConsumeError(err)
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case i < 0:
|
|
|
|
return rp.consumer.ConsumeBulkString(nil)
|
|
|
|
case i == 0:
|
|
|
|
return rp.consumer.ConsumeBulkString([]byte{})
|
|
|
|
default:
|
2014-08-08 15:12:30 +02:00
|
|
|
if i >= 4*1024*1024 { // 4MB should be sufficient
|
|
|
|
return rp.consumer.ConsumeError(
|
|
|
|
fmt.Errorf("Bulk string too large (%d bytes).\n", i))
|
|
|
|
}
|
2014-08-03 01:16:43 +02:00
|
|
|
data := make([]byte, i, i)
|
2014-08-08 13:45:24 +02:00
|
|
|
for rest := i; rest > 0; {
|
|
|
|
var n int
|
|
|
|
if n, err = rp.reader.Read(data[i-rest : i]); err != nil {
|
|
|
|
return rp.consumer.ConsumeError(err)
|
|
|
|
}
|
|
|
|
rest -= int64(n)
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
|
|
|
if _, err = rp.reader.ReadBytes('\n'); err != nil {
|
|
|
|
return rp.consumer.ConsumeError(err)
|
|
|
|
}
|
2014-08-08 13:45:24 +02:00
|
|
|
return rp.consumer.ConsumeBulkString(data)
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rp *RedisParser) array(line []byte) bool {
|
|
|
|
var i int64
|
|
|
|
var err error
|
|
|
|
i, err = strconv.ParseInt(string(line[1:]), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return rp.consumer.ConsumeError(err)
|
|
|
|
}
|
|
|
|
return rp.consumer.ConsumeArray(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
type RedisCommands interface {
|
|
|
|
Hget(hash, key []byte) bool
|
|
|
|
Hset(hash, key, block []byte) bool
|
|
|
|
Multi() bool
|
|
|
|
Exec() bool
|
2014-08-11 14:56:01 +02:00
|
|
|
Hkeys(hash []byte) bool
|
2014-09-01 00:19:47 +02:00
|
|
|
HSpatial(hash, first, second []byte) bool
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type RedisCommandExecutor struct {
|
|
|
|
commands RedisCommands
|
|
|
|
missing int64
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedisCommandExecutor(commands RedisCommands) *RedisCommandExecutor {
|
|
|
|
return &RedisCommandExecutor{
|
|
|
|
commands: commands,
|
|
|
|
missing: 0,
|
|
|
|
args: []interface{}{}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) push(i interface{}) bool {
|
|
|
|
rce.args = append(rce.args, i)
|
|
|
|
rce.missing--
|
|
|
|
if rce.missing <= 0 {
|
|
|
|
rce.missing = 0
|
|
|
|
res := rce.execute()
|
|
|
|
rce.args = []interface{}{}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func asString(i interface{}) string {
|
2014-08-31 19:05:43 +02:00
|
|
|
switch v := i.(type) {
|
2014-08-03 01:16:43 +02:00
|
|
|
case string:
|
2014-08-31 19:05:43 +02:00
|
|
|
return v
|
2014-08-03 01:16:43 +02:00
|
|
|
case []byte:
|
2014-08-31 19:05:43 +02:00
|
|
|
return string(v)
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) execute() bool {
|
|
|
|
l := len(rce.args)
|
|
|
|
if l < 1 {
|
2014-08-07 20:25:48 +02:00
|
|
|
log.Printf("WARN: Too less argument for command.")
|
2014-08-03 01:16:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
cmd := asString(rce.args[0])
|
|
|
|
switch cmd {
|
|
|
|
case "HGET":
|
|
|
|
if l < 3 {
|
2014-08-07 20:25:48 +02:00
|
|
|
log.Println("WARN: Missing argments for HGET.")
|
2014-08-03 01:16:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
hash, ok1 := rce.args[1].([]byte)
|
|
|
|
key, ok2 := rce.args[2].([]byte)
|
|
|
|
if !ok1 || !ok2 {
|
2014-08-07 20:25:48 +02:00
|
|
|
log.Println("WARN: HGET data are not byte slices.")
|
2014-08-03 01:16:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return rce.commands.Hget(hash, key)
|
|
|
|
|
|
|
|
case "HSET":
|
|
|
|
if l < 4 {
|
|
|
|
log.Println("WARN: Missing argments for HSET.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hash, ok1 := rce.args[1].([]byte)
|
|
|
|
key, ok2 := rce.args[2].([]byte)
|
2014-08-03 14:52:24 +02:00
|
|
|
value, ok3 := rce.args[3].([]byte)
|
2014-08-03 01:16:43 +02:00
|
|
|
|
|
|
|
if !ok1 || !ok2 || !ok3 {
|
2014-08-07 20:25:48 +02:00
|
|
|
log.Println("WARN: HSET data are not byte slices.")
|
2014-08-03 01:16:43 +02:00
|
|
|
return false
|
|
|
|
}
|
2014-08-03 14:52:24 +02:00
|
|
|
return rce.commands.Hset(hash, key, value)
|
2014-08-03 01:16:43 +02:00
|
|
|
|
|
|
|
case "MULTI":
|
|
|
|
return rce.commands.Multi()
|
|
|
|
|
|
|
|
case "EXEC":
|
|
|
|
return rce.commands.Exec()
|
2014-08-11 14:56:01 +02:00
|
|
|
|
|
|
|
case "HKEYS":
|
|
|
|
if l < 2 {
|
|
|
|
log.Println("WARN: Missing argments for HKEYS.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hash, ok := rce.args[1].([]byte)
|
|
|
|
if !ok {
|
|
|
|
log.Println("WARN: HKEYS data are not byte slices.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return rce.commands.Hkeys(hash)
|
2014-09-01 00:19:47 +02:00
|
|
|
|
|
|
|
case "HSPATIAL":
|
|
|
|
if l < 4 {
|
|
|
|
log.Println("WARN: Missing argments for HSPATIAL.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
hash, ok1 := rce.args[1].([]byte)
|
|
|
|
first, ok2 := rce.args[2].([]byte)
|
|
|
|
second, ok3 := rce.args[3].([]byte)
|
|
|
|
|
|
|
|
if !ok1 || !ok2 || !ok3 {
|
|
|
|
log.Println("WARN: HSET data are not byte slices.")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return rce.commands.HSpatial(hash, first, second)
|
2014-08-03 01:16:43 +02:00
|
|
|
}
|
2014-08-07 20:25:48 +02:00
|
|
|
log.Printf("WARN: unknown command: '%s'\n", cmd)
|
2014-08-03 01:16:43 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeSimpleString(s string) bool {
|
|
|
|
return rce.push(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeBulkString(data []byte) bool {
|
|
|
|
return rce.push(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeInteger(i int64) bool {
|
|
|
|
return rce.push(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeError(err error) bool {
|
|
|
|
log.Printf("error: %s", err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeArray(i int64) bool {
|
|
|
|
if rce.missing > 0 {
|
|
|
|
log.Println("WARN: Nested arrays are not supported!")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if i < 0 {
|
|
|
|
log.Println("Null arrays are not supported")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
rce.missing = i
|
|
|
|
return true
|
|
|
|
}
|