Moved redis client from tilemapper to common.

This commit is contained in:
Sascha L. Teichmann 2014-09-13 19:18:12 +02:00
parent fd64d20b76
commit 560afae6c4
2 changed files with 21 additions and 24 deletions

View File

@ -53,9 +53,9 @@ func main() {
address := fmt.Sprintf("%s:%d", host, port) address := fmt.Sprintf("%s:%d", host, port)
var client *Client var client *common.RedisClient
if client, err = NewClient("tcp", address); err != nil { if client, err = common.NewRedisClient("tcp", address); err != nil {
log.Fatalf("Cannot connect to '%s': %s", address, err) log.Fatalf("Cannot connect to '%s': %s", address, err)
} }
defer client.Close() defer client.Close()

View File

@ -2,42 +2,39 @@
// Use of this source code is governed by the MIT license // Use of this source code is governed by the MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
package main package common
import ( import (
"bufio"
"fmt" "fmt"
"net" "net"
"bufio"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
) )
type Client struct { type RedisClient struct {
conn net.Conn conn net.Conn
reader *bufio.Reader reader *bufio.Reader
} }
func NewClient(network, address string) (client *Client, err error) { func NewRedisClient(network, address string) (client *RedisClient, err error) {
var conn net.Conn var conn net.Conn
if conn, err = net.Dial(network, address); err != nil { if conn, err = net.Dial(network, address); err != nil {
return return
} }
client = &Client{conn: conn, reader: bufio.NewReaderSize(conn, 8*1024)} client = &RedisClient{conn: conn, reader: bufio.NewReaderSize(conn, 8*1024)}
return return
} }
func (client *Client) Close() error { func (client *RedisClient) Close() error {
return client.conn.Close() return client.conn.Close()
} }
func (client *Client) writeArray(size int) (err error) { func (client *RedisClient) writeArray(size int) (err error) {
_, err = client.conn.Write([]byte(fmt.Sprintf("*%d\r\n", size))) _, err = client.conn.Write([]byte(fmt.Sprintf("*%d\r\n", size)))
return return
} }
func (client *Client) writeBulkString(data []byte) (err error) { func (client *RedisClient) writeBulkString(data []byte) (err error) {
if _, err = client.conn.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil { if _, err = client.conn.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil {
return return
} }
@ -48,7 +45,7 @@ func (client *Client) writeBulkString(data []byte) (err error) {
return return
} }
func (client *Client) writeHSpatial(p1, p2 int64) (err error) { func (client *RedisClient) writeHSpatial(p1, p2 int64) (err error) {
if err = client.writeArray(4); err != nil { if err = client.writeArray(4); err != nil {
return return
} }
@ -58,14 +55,14 @@ func (client *Client) writeHSpatial(p1, p2 int64) (err error) {
if err = client.writeBulkString([]byte("IGNORE")); err != nil { if err = client.writeBulkString([]byte("IGNORE")); err != nil {
return return
} }
if err = client.writeBulkString(common.StringToBytes(p1)); err != nil { if err = client.writeBulkString(StringToBytes(p1)); err != nil {
return return
} }
err = client.writeBulkString(common.StringToBytes(p2)) err = client.writeBulkString(StringToBytes(p2))
return return
} }
func (client *Client) readLine() (data []byte, err error) { func (client *RedisClient) readLine() (data []byte, err error) {
return client.reader.ReadBytes('\n') return client.reader.ReadBytes('\n')
} }
@ -76,7 +73,7 @@ func isError(line []byte) error {
return nil return nil
} }
func (client *Client) readBulkString(data *[]byte) (size int, err error) { func (client *RedisClient) readBulkString(data *[]byte) (size int, err error) {
var line []byte var line []byte
if line, err = client.readLine(); err != nil { if line, err = client.readLine(); err != nil {
return return
@ -106,15 +103,15 @@ func (client *Client) readBulkString(data *[]byte) (size int, err error) {
return return
} }
func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block)) (err error) { func (client *RedisClient) QueryCuboid(cuboid Cuboid, fn func(*Block)) (err error) {
p1 := common.CoordToPlain(cuboid.P1) p1 := CoordToPlain(cuboid.P1)
p2 := common.CoordToPlain(cuboid.P2) p2 := CoordToPlain(cuboid.P2)
if err = client.writeHSpatial(p1, p2); err != nil { if err = client.writeHSpatial(p1, p2); err != nil {
return return
} }
var ( var (
data = make([]byte, 8*1024) data = make([]byte, 8*1024)
block = common.Block{} block = Block{}
size int size int
key int64 key int64
) )
@ -126,10 +123,10 @@ func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block))
if size <= 0 { if size <= 0 {
break break
} }
if key, err = common.DecodeStringFromBytes(data[0:size]); err != nil { if key, err = DecodeStringFromBytes(data[0:size]); err != nil {
return return
} }
block.Coord = common.PlainToCoord(key) block.Coord = PlainToCoord(key)
if size, err = client.readBulkString(&data); err != nil { if size, err = client.readBulkString(&data); err != nil {
return return
} }