mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
Moved redis client from tilemapper to common.
This commit is contained in:
parent
fd64d20b76
commit
560afae6c4
|
@ -53,9 +53,9 @@ func main() {
|
|||
|
||||
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)
|
||||
}
|
||||
defer client.Close()
|
||||
|
|
|
@ -2,42 +2,39 @@
|
|||
// Use of this source code is governed by the MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"bufio"
|
||||
|
||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
type RedisClient struct {
|
||||
conn net.Conn
|
||||
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
|
||||
if conn, err = net.Dial(network, address); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client = &Client{conn: conn, reader: bufio.NewReaderSize(conn, 8*1024)}
|
||||
client = &RedisClient{conn: conn, reader: bufio.NewReaderSize(conn, 8*1024)}
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) Close() error {
|
||||
func (client *RedisClient) Close() error {
|
||||
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)))
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
@ -48,7 +45,7 @@ func (client *Client) writeBulkString(data []byte) (err error) {
|
|||
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 {
|
||||
return
|
||||
}
|
||||
|
@ -58,14 +55,14 @@ func (client *Client) writeHSpatial(p1, p2 int64) (err error) {
|
|||
if err = client.writeBulkString([]byte("IGNORE")); err != nil {
|
||||
return
|
||||
}
|
||||
if err = client.writeBulkString(common.StringToBytes(p1)); err != nil {
|
||||
if err = client.writeBulkString(StringToBytes(p1)); err != nil {
|
||||
return
|
||||
}
|
||||
err = client.writeBulkString(common.StringToBytes(p2))
|
||||
err = client.writeBulkString(StringToBytes(p2))
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) readLine() (data []byte, err error) {
|
||||
func (client *RedisClient) readLine() (data []byte, err error) {
|
||||
return client.reader.ReadBytes('\n')
|
||||
}
|
||||
|
||||
|
@ -76,7 +73,7 @@ func isError(line []byte) error {
|
|||
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
|
||||
if line, err = client.readLine(); err != nil {
|
||||
return
|
||||
|
@ -106,15 +103,15 @@ func (client *Client) readBulkString(data *[]byte) (size int, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block)) (err error) {
|
||||
p1 := common.CoordToPlain(cuboid.P1)
|
||||
p2 := common.CoordToPlain(cuboid.P2)
|
||||
func (client *RedisClient) QueryCuboid(cuboid Cuboid, fn func(*Block)) (err error) {
|
||||
p1 := CoordToPlain(cuboid.P1)
|
||||
p2 := CoordToPlain(cuboid.P2)
|
||||
if err = client.writeHSpatial(p1, p2); err != nil {
|
||||
return
|
||||
}
|
||||
var (
|
||||
data = make([]byte, 8*1024)
|
||||
block = common.Block{}
|
||||
block = Block{}
|
||||
size int
|
||||
key int64
|
||||
)
|
||||
|
@ -126,10 +123,10 @@ func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block))
|
|||
if size <= 0 {
|
||||
break
|
||||
}
|
||||
if key, err = common.DecodeStringFromBytes(data[0:size]); err != nil {
|
||||
if key, err = DecodeStringFromBytes(data[0:size]); err != nil {
|
||||
return
|
||||
}
|
||||
block.Coord = common.PlainToCoord(key)
|
||||
block.Coord = PlainToCoord(key)
|
||||
if size, err = client.readBulkString(&data); err != nil {
|
||||
return
|
||||
}
|
Loading…
Reference in New Issue
Block a user