Browse Source

utilize spatialIndex.GetPositionFromIndexedPoint

main
forest 3 months ago
parent
commit
dd23acd37a
3 changed files with 11 additions and 18 deletions
  1. +5
    -5
      go.mod
  2. +2
    -0
      go.sum
  3. +4
    -13
      main.go

+ 5
- 5
go.mod View File

@ -3,11 +3,11 @@ module git.sequentialread.com/forest/graffiti
go 1.16
require (
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210620162508-4bef09fabeb3 // indirect
git.sequentialread.com/forest/pkg-errors v0.9.2 // indirect
github.com/boltdb/bolt v1.3.1 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/shengdoushi/base58 v1.0.0 // indirect
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390
git.sequentialread.com/forest/pkg-errors v0.9.2
github.com/boltdb/bolt v1.3.1
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/shengdoushi/base58 v1.0.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 // indirect
)

+ 2
- 0
go.sum View File

@ -1,5 +1,7 @@
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210620162508-4bef09fabeb3 h1:n0Wutbh2JvkGb8KDepxzMaaIabieOuoOTWRyb1YxyNc=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210620162508-4bef09fabeb3/go.mod h1:qQXFuJiQIgQIWQ8/viqhsFeoeeFOttqGAF/O5HD/lo4=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390 h1:KjnRL7U1LwnCW9H5WkVukMivibkRG24ZXze3rS9/VpA=
git.sequentialread.com/forest/modular-spatial-index v0.0.0-20210624152442-f9f14923b390/go.mod h1:qQXFuJiQIgQIWQ8/viqhsFeoeeFOttqGAF/O5HD/lo4=
git.sequentialread.com/forest/pkg-errors v0.9.2 h1:j6pwbL6E+TmE7TD0tqRtGwuoCbCfO6ZR26Nv5nest9g=
git.sequentialread.com/forest/pkg-errors v0.9.2/go.mod h1:8TkJ/f8xLWFIAid20aoqgDZcCj9QQt+FU+rk415XO1w=
github.com/boltdb/bolt v1.3.1 h1:JQmyP4ZBrce+ZQu0dY660FMfatumYDLun9hBCUVIkF4=


+ 4
- 13
main.go View File

@ -4,13 +4,11 @@ import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net/http"
"net/url"
"os"
@ -495,14 +493,6 @@ func createPost(post *Post) error {
return err
}
// We append the position to the key so that readers can check the position without decoding the json
xBuffer := make([]byte, 4)
yBuffer := make([]byte, 4)
binary.BigEndian.PutUint32(xBuffer, uint32(post.X+math.MaxInt32>>1))
binary.BigEndian.PutUint32(yBuffer, uint32(post.Y+math.MaxInt32>>1))
spatialKey = append(spatialKey, xBuffer...)
spatialKey = append(spatialKey, yBuffer...)
byAuthorBucket := tx.Bucket([]byte("posts_by_author"))
byAuthorKey := []byte(fmt.Sprintf("%s-%s", post.AuthorId, post.Id))
postByAuthorBytes, err := json.Marshal(&Post{
@ -556,9 +546,10 @@ func getPosts(x, y, radius int) ([][]byte, error) {
cursor := bucket.Cursor()
key, value := cursor.Seek(rng.Start)
for key != nil && bytes.Compare(key, rng.End) < 1 {
// the position is appended to the key
postX := int(binary.BigEndian.Uint32(key[8:8+4])) - (math.MaxInt32 >> 1)
postY := int(binary.BigEndian.Uint32(key[8+4:8+4+4])) - (math.MaxInt32 >> 1)
postX, postY, err := spatialIndex.GetPositionFromIndexedPoint(key)
if err != nil {
return err
}
if postX > xMin && postX < xMax && postY > yMin && postY < yMax {
toReturn = append(toReturn, value)
}


Loading…
Cancel
Save