From b33897df044a55fd15c43072bed4f99812342f52 Mon Sep 17 00:00:00 2001 From: Bertrand Paquet Date: Mon, 21 Nov 2016 21:30:09 -0800 Subject: [PATCH] Fix some points of #1 --- README.md | 7 ++++++- commands.go | 10 +++++----- license.txt | 13 +++++++++++++ redis.go | 7 +++---- test/config.json | 3 +-- test/config_expanded_map.json | 11 +++++++++++ test/run.sh | 17 +++++++++++++++++ writers.go | 16 ++++++++-------- 8 files changed, 64 insertions(+), 20 deletions(-) create mode 100644 license.txt create mode 100644 test/config_expanded_map.json create mode 100755 test/run.sh diff --git a/README.md b/README.md index e1dc382..26cada6 100644 --- a/README.md +++ b/README.md @@ -134,12 +134,17 @@ Please feel free to open an issue if you discover problems. Tests are only integration tests, and are written in PHP. -## undocumented functions +## Undocumented functions * Statsd statistics * Write back * Backward compatibility +## License + +[Apache 2 license](license.txt) + + diff --git a/commands.go b/commands.go index fea5ac0..b00ed6a 100644 --- a/commands.go +++ b/commands.go @@ -2,7 +2,6 @@ package main import ( "strconv" - "reflect" "strings" "encoding/base64" @@ -171,10 +170,10 @@ func array_pop(wf write_func, ctx *context, args [][]byte, f string) (error) { } x := rec.([]interface{})[0] // backward compat - t := reflect.TypeOf(x).Kind() - if t == reflect.Int { + switch x.(type) { + case int: return WriteByteArray(wf, []byte(strconv.Itoa(x.(int)))) - } else if t == reflect.String { + case string: s := x.(string) if strings.HasPrefix(s, "__64__") { bytes, err := base64.StdEncoding.DecodeString(s[6:]) @@ -185,8 +184,9 @@ func array_pop(wf write_func, ctx *context, args [][]byte, f string) (error) { } return WriteByteArray(wf, []byte(s)) // end of backward compat + default: + return WriteByteArray(wf, x.([]byte)) } - return WriteByteArray(wf, x.([]byte)) } func cmd_RPOP(wf write_func, ctx *context, args [][]byte) (error) { diff --git a/license.txt b/license.txt new file mode 100644 index 0000000..096bcfe --- /dev/null +++ b/license.txt @@ -0,0 +1,13 @@ +Copyright 2016 Bertrand Paquet + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/redis.go b/redis.go index d50ef82..8f14188 100644 --- a/redis.go +++ b/redis.go @@ -14,7 +14,6 @@ import ( "errors" "math/rand" "time" - "reflect" "runtime/pprof" as "github.com/aerospike/aerospike-client-go" @@ -78,15 +77,15 @@ func ExpandedMapHandlers() (map[string]handler) { } func getIntFromJson(x interface{}) (int) { - if reflect.TypeOf(x).Kind() == reflect.String { + switch x.(type) { + case string: v, err := strconv.Atoi(x.(string)) if err != nil { panic(err) } return v - } else { - return int(x.(float64)) } + return int(x.(float64)) } func DisplayExpandedMapCacheStat(ctx *context) { diff --git a/test/config.json b/test/config.json index 7998c89..0bed6d6 100644 --- a/test/config.json +++ b/test/config.json @@ -5,7 +5,6 @@ "sets": [{ "proto": "tcp", "listen": "0.0.0.0:6379", - "set": "redis", - "expanded_map": 1 + "set": "redis" }] } \ No newline at end of file diff --git a/test/config_expanded_map.json b/test/config_expanded_map.json new file mode 100644 index 0000000..7998c89 --- /dev/null +++ b/test/config_expanded_map.json @@ -0,0 +1,11 @@ +{ + "aerospike_ips": [ + "192.168.56.80" + ], + "sets": [{ + "proto": "tcp", + "listen": "0.0.0.0:6379", + "set": "redis", + "expanded_map": 1 + }] +} \ No newline at end of file diff --git a/test/run.sh b/test/run.sh new file mode 100755 index 0000000..a9f9cbe --- /dev/null +++ b/test/run.sh @@ -0,0 +1,17 @@ +#!/bin/bash -e + +pkill aerodis || true + +echo "Standard test" +../aerodis --config_file config.json & +sleep 3 +php test.php +pkill aerodis || true +sleep 3 + +echo "Expanded map test" +../aerodis --config_file config_expanded_map.json & +sleep 3 +php test.php +pkill aerodis || true +sleep 3 diff --git a/writers.go b/writers.go index 9dcdbce..1d57fc5 100644 --- a/writers.go +++ b/writers.go @@ -3,7 +3,6 @@ package main import ( "log" "strconv" - "reflect" "encoding/base64" "strings" "bytes" @@ -35,8 +34,8 @@ func WriteArray(wf write_func, array []interface{}) error { } for _, e := range array { // backward compat - t := reflect.TypeOf(e).Kind() - if t == reflect.String { + switch e.(type) { + case string: s := e.(string) if strings.HasPrefix(s, "__64__") { bytes, err := base64.StdEncoding.DecodeString(s[6:]) @@ -53,7 +52,7 @@ func WriteArray(wf write_func, array []interface{}) error { return err } } - } else { + default: // end of backward compat err := WriteByteArray(wf, e.([]byte)) if err != nil { @@ -69,11 +68,11 @@ func WriteLine(wf write_func, s string) error { } func WriteValue(wf write_func, x interface{}) error { - t := reflect.TypeOf(x).Kind() - if t == reflect.Int { + switch x.(type) { + case int: return WriteByteArray(wf, []byte(strconv.Itoa(x.(int)))) // backward compat - } else if t == reflect.String { + case string: s := x.(string) if strings.HasPrefix(s, "__64__") { bytes, err := base64.StdEncoding.DecodeString(s[6:]) @@ -85,8 +84,9 @@ func WriteValue(wf write_func, x interface{}) error { return WriteByteArray(wf, []byte(s)) } // end of backward compat + default: + return WriteByteArray(wf, x.([]byte)) } - return WriteByteArray(wf, x.([]byte)) } func WriteBin(wf write_func, rec * as.Record, bin_name string, nil_value string) error {