Fixed endless loop bug in LevelDB interleaved spatial query. Simplified code.

This commit is contained in:
Sascha L. Teichmann 2014-09-01 18:26:33 +02:00
parent f4baf63247
commit 77a35e7096
2 changed files with 17 additions and 19 deletions

View File

@ -85,14 +85,22 @@ func EncodeStringToBytes(key int64) ([]byte, error) {
return []byte(strconv.FormatInt(key, 10)), nil return []byte(strconv.FormatInt(key, 10)), nil
} }
func EncodeToBigEndian(key int64) (enc []byte, err error) { func ToBigEndian(key int64) []byte {
enc = make([]byte, 8) enc := make([]byte, 8)
binary.BigEndian.PutUint64(enc, uint64(key)) binary.BigEndian.PutUint64(enc, uint64(key))
return return enc
}
func EncodeToBigEndian(key int64) ([]byte, error) {
return ToBigEndian(key), nil
}
func FromBigEndian(key []byte) int64 {
return int64(binary.BigEndian.Uint64(key))
} }
func DecodeFromBigEndian(key []byte) (int64, error) { func DecodeFromBigEndian(key []byte) (int64, error) {
return int64(binary.BigEndian.Uint64(key)), nil return FromBigEndian(key), nil
} }
func CoordToInterleaved(c Coord) (result int64) { func CoordToInterleaved(c Coord) (result int64) {

View File

@ -336,21 +336,13 @@ func (ldbs *LevelDBSession) interleavedSpatialQuery(first, second []byte, done c
zmin, zmax = order(zmin, zmax) zmin, zmax = order(zmin, zmax)
var ( var (
cub = common.Cuboid{P1: c1, P2: c2} cub = common.Cuboid{P1: c1, P2: c2}
zcode = zmin
err error err error
encodedKey []byte encodedKey []byte
) )
SEEK:
if encodedKey, err = common.EncodeToBigEndian(zcode); err != nil { it.Seek(common.ToBigEndian(zmin))
log.Printf("error encoding key: %s", err)
return
}
it.Seek(encodedKey)
for it.Valid() { for it.Valid() {
if zcode, err = common.DecodeFromBigEndian(it.Key()); err != nil { zcode := common.FromBigEndian(it.Key())
log.Printf("error decoding key: %s", err)
return
}
if zcode > zmax { if zcode > zmax {
break break
@ -366,12 +358,10 @@ func (ldbs *LevelDBSession) interleavedSpatialQuery(first, second []byte, done c
case <-done: case <-done:
return return
} }
it.Next()
} else { } else {
zmin = common.NaiveBigMin(zmin, zmax, zcode) it.Seek(common.ToBigEndian(common.NaiveBigMin(zmin, zmax, zcode)))
goto SEEK
} }
it.Next()
} }
if err = it.GetError(); err != nil { if err = it.GetError(); err != nil {
log.Printf("error while iterating: %s", err) log.Printf("error while iterating: %s", err)