mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
241 lines
5.0 KiB
Go
241 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"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 {
|
|
rp.consumer.ConsumeError(err)
|
|
return nil
|
|
}
|
|
line = bytes.TrimRight(line, "\r\n")
|
|
log.Printf("%q", line)
|
|
return line
|
|
}
|
|
|
|
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:
|
|
data := make([]byte, i, i)
|
|
var n int
|
|
if n, err = rp.reader.Read(data); err != nil {
|
|
return rp.consumer.ConsumeError(err)
|
|
}
|
|
if _, err = rp.reader.ReadBytes('\n'); err != nil {
|
|
return rp.consumer.ConsumeError(err)
|
|
}
|
|
return rp.consumer.ConsumeBulkString(data[0:n])
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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 {
|
|
switch i.(type) {
|
|
case string:
|
|
return i.(string)
|
|
case []byte:
|
|
return string(i.([]byte))
|
|
}
|
|
return fmt.Sprintf("%s", i)
|
|
}
|
|
|
|
func (rce *RedisCommandExecutor) execute() bool {
|
|
l := len(rce.args)
|
|
if l < 1 {
|
|
log.Printf("WARN: Too less argument for command")
|
|
return false
|
|
}
|
|
cmd := asString(rce.args[0])
|
|
switch cmd {
|
|
case "HGET":
|
|
if l < 3 {
|
|
log.Println("WARN: Missing argments for HGET")
|
|
return false
|
|
}
|
|
hash, ok1 := rce.args[1].([]byte)
|
|
key, ok2 := rce.args[2].([]byte)
|
|
if !ok1 || !ok2 {
|
|
log.Println("WARN HGET data are not byte slices.")
|
|
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)
|
|
block, ok3 := rce.args[3].([]byte)
|
|
|
|
if !ok1 || !ok2 || !ok3 {
|
|
log.Printf("WARM HSET data are not byte slices,")
|
|
return false
|
|
}
|
|
return rce.commands.Hset(hash, key, block)
|
|
|
|
case "MULTI":
|
|
return rce.commands.Multi()
|
|
|
|
case "EXEC":
|
|
return rce.commands.Exec()
|
|
}
|
|
log.Printf("WARN unkown command: %s", cmd)
|
|
return false
|
|
}
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeSimpleString(s string) bool {
|
|
log.Printf("simple string: %s", s)
|
|
return rce.push(s)
|
|
}
|
|
|
|
func shorten(data []byte) string {
|
|
if len(data) > 10 {
|
|
return fmt.Sprintf("%.10q...", data)
|
|
}
|
|
return fmt.Sprintf("%q", data)
|
|
}
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeBulkString(data []byte) bool {
|
|
s := shorten(data)
|
|
log.Printf("buld string: len = %d: %s", len(data), s)
|
|
return rce.push(data)
|
|
}
|
|
|
|
func (rce *RedisCommandExecutor) ConsumeInteger(i int64) bool {
|
|
log.Printf("integer: %d", i)
|
|
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 {
|
|
log.Printf("array: %d", i)
|
|
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
|
|
}
|