From c096b8bbf10af5bd193838c2d50f1a6d92b096f7 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 1 Sep 2014 18:39:42 +0200 Subject: [PATCH] Simplified code of non interleaved spatial query in LevelDB backend. --- common/coords.go | 6 +++++- leveldb.go | 20 ++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/common/coords.go b/common/coords.go index e29e480..2aa95f0 100644 --- a/common/coords.go +++ b/common/coords.go @@ -80,9 +80,13 @@ func DecodeStringFromBytes(key []byte) (pos int64, err error) { return strconv.ParseInt(string(key), 10, 64) } +func StringToBytes(key int64) []byte { + return []byte(strconv.FormatInt(key, 10)) +} + // Encode a block pos to byte slice. func EncodeStringToBytes(key int64) ([]byte, error) { - return []byte(strconv.FormatInt(key, 10)), nil + return StringToBytes(key), nil } func ToBigEndian(key int64) []byte { diff --git a/leveldb.go b/leveldb.go index 7e655c3..ee1e08d 100644 --- a/leveldb.go +++ b/leveldb.go @@ -269,20 +269,17 @@ func (ldbs *LevelDBSession) plainSpatialQuery(first, second []byte, done chan st for a.Y = c1.Y; a.Y <= c2.Y; a.Y++ { b.Y = a.Y from, to := order(common.CoordToPlain(a), common.CoordToPlain(b)) - var encodedFrom []byte - if encodedFrom, err = common.EncodeStringToBytes(from); err != nil { - log.Printf("encoding key failed: %s", err) - return - } - it.Seek(encodedFrom) - for it.Valid() { - key := it.Key() - var decodedKey int64 - if decodedKey, err = common.DecodeStringFromBytes(key); err != nil { + it.Seek(common.StringToBytes(from)) + for ; it.Valid(); it.Next() { + var ( + key = it.Key() + pos int64 + ) + if pos, err = common.DecodeStringFromBytes(key); err != nil { log.Printf("decoding key failed: %s", err) return } - if decodedKey > to { + if pos > to { break } select { @@ -290,7 +287,6 @@ func (ldbs *LevelDBSession) plainSpatialQuery(first, second []byte, done chan st case <-done: return } - it.Next() } if err = it.GetError(); err != nil { log.Printf("iterating failed: %s", err)