Added right files of the tilemapper.

This commit is contained in:
Sascha L. Teichmann 2014-09-07 21:46:55 +02:00
parent 36df69a6cb
commit 58a553f0da
2 changed files with 203 additions and 0 deletions

139
tilemapper/client.go Normal file
View File

@ -0,0 +1,139 @@
package main
import (
"fmt"
"net"
"bufio"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
type Client struct {
conn net.Conn
reader *bufio.Reader
}
func NewClient(network, address string) (client *Client, 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)}
return
}
func (client *Client) Close() error {
return client.conn.Close()
}
func (client *Client) 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) {
if _, err = client.conn.Write([]byte(fmt.Sprintf("$%d\r\n", len(data)))); err != nil {
return
}
if _, err = client.conn.Write(data); err != nil {
return
}
_, err = client.conn.Write([]byte("\r\n"))
return
}
func (client *Client) writeHSpatial(p1, p2 int64) (err error) {
if err = client.writeArray(4); err != nil {
return
}
if err = client.writeBulkString([]byte("HSPATIAL")); err != nil {
return
}
if err = client.writeBulkString([]byte("IGNORE")); err != nil {
return
}
if err = client.writeBulkString(common.StringToBytes(p1)); err != nil {
return
}
err = client.writeBulkString(common.StringToBytes(p2))
return
}
func (client *Client) readLine() (data []byte, err error) {
return client.reader.ReadBytes('\n')
}
func isError(line []byte) error {
if len(line) > 0 && line[0] == '-' {
return fmt.Errorf("error: %s", line[1:])
}
return nil
}
func (client *Client) readBulkString(data *[]byte) (size int, err error) {
var line []byte
if line, err = client.readLine(); err != nil {
return
}
if err = isError(line); err != nil {
return
}
if _, err = fmt.Sscanf(string(line), "$%d\r\n", &size); err != nil {
return
}
if size <= 0 {
return
}
if cap(*data) < size {
*data = make([]byte, size)
}
for rest := size; rest > 0; {
var n int
if n, err = client.reader.Read((*data)[size-rest : size]); err != nil {
return
}
rest -= n
}
if _, err = client.reader.ReadBytes('\n'); err != nil {
return
}
return
}
func (client *Client) QueryCuboid(cuboid common.Cuboid, fn func(*common.Block)) (err error) {
p1 := common.CoordToPlain(cuboid.P1)
p2 := common.CoordToPlain(cuboid.P2)
if err = client.writeHSpatial(p1, p2); err != nil {
return
}
var (
data = make([]byte, 8*1024)
block = common.Block{}
size int
key int64
)
for {
if size, err = client.readBulkString(&data); err != nil {
return
}
if size <= 0 {
break
}
if key, err = common.DecodeStringFromBytes(data[0:size]); err != nil {
return
}
block.Coord = common.PlainToCoord(key)
if size, err = client.readBulkString(&data); err != nil {
return
}
block.Data = data[0:size]
fn(&block)
}
_ = size
return
}

64
tilemapper/main.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"flag"
"fmt"
"log"
"bitbucket.org/s_l_teichmann/mtredisalize/common"
)
func main() {
var (
port int
host string
x, y, z int
width, height, depth int
)
flag.IntVar(&port, "port", 6379, "port to of mtredisalize server")
flag.StringVar(&host, "host", "localhost", "host to mtredisalize server")
flag.IntVar(&x, "x", -9, "x of query cuboid")
flag.IntVar(&y, "y", -75, "y of query cuboid")
flag.IntVar(&z, "z", -9, "z of query cuboid")
flag.IntVar(&width, "width", 18, "width of query cuboid")
flag.IntVar(&height, "height", 18, "height of query cuboid")
flag.IntVar(&depth, "depth", 150, "depth of query cuboid")
flag.IntVar(&width, "w", 18, "width of query cuboid (shorthand)")
flag.IntVar(&height, "h", 18, "height of query cuboid (shorthand)")
flag.IntVar(&depth, "d", 150, "depth of query cuboid (shorthand)")
flag.Parse()
address := fmt.Sprintf("%s:%d", host, port)
var err error
var client *Client
if client, err = NewClient("tcp", address); err != nil {
log.Fatalf("Cannot connect to '%s': %s", address, err)
}
defer client.Close()
q1x, q1y, q1z := int16(x), int16(y), int16(z)
q2x, q2y, q2z := q1x+int16(width)-1, q1y+int16(depth)-1, q1z+int16(height)-1
c1 := common.Coord{X: q1x, Y: q1y, Z: q1z}
c2 := common.Coord{X: q2x, Y: q2y, Z: q2z}
cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)}
log.Printf("query cuboid: %s", cuboid)
numBlocks := 0
bytesTotal := int64(0)
count := func(block *common.Block) {
numBlocks++
bytesTotal += int64(len(block.Data))
}
if err = client.QueryCuboid(cuboid, count); err != nil {
log.Fatalf("query failed: %s", err)
}
fmt.Printf("num of blocks: %d\n", numBlocks)
fmt.Printf("num of bytes: %d\n", bytesTotal)
}