Skip to content

Commit

Permalink
geo
Browse files Browse the repository at this point in the history
  • Loading branch information
diiyw committed May 20, 2024
1 parent c5d4a20 commit 0d1e7ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 24 additions & 4 deletions geo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ func (n *Nodis) GeoAdd(key string, members ...*geo.Member) (int64, error) {
return v, nil
}

// GeoAddXX adds the specified members to the key only if the member already exists.
func (n *Nodis) GeoAddXX(key string, members ...*geo.Member) (int64, error) {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newZSet)
meta := tx.writeKey(key, nil)
if !meta.isOk() {
return nil
}
for _, member := range members {
v += meta.value.(*zset.SortedSet).ZAddXX(member.Name, float64(member.Hash()))
}
Expand All @@ -37,10 +41,14 @@ func (n *Nodis) GeoAddXX(key string, members ...*geo.Member) (int64, error) {
return v, nil
}

// GeoAddNX adds the specified members to the key only if the member does not already exist.
func (n *Nodis) GeoAddNX(key string, members ...*geo.Member) (int64, error) {
var v int64
_ = n.exec(func(tx *Tx) error {
meta := tx.writeKey(key, n.newZSet)
if meta.isOk() {
return nil
}
for _, member := range members {
v += meta.value.(*zset.SortedSet).ZAddNX(member.Name, float64(member.Hash()))
}
Expand All @@ -53,7 +61,7 @@ func (n *Nodis) GeoDist(key string, member1, member2 string) (float64, error) {
var v float64
err := n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if meta == nil {
if !meta.isOk() {
return nil
}
score1, err := meta.value.(*zset.SortedSet).ZScore(member1)
Expand All @@ -66,7 +74,7 @@ func (n *Nodis) GeoDist(key string, member1, member2 string) (float64, error) {
}
lat1, lng1 := geohash.DecodeInt(uint64(score1))
lat2, lng2 := geohash.DecodeInt(uint64(score2))
v = distance(float64(lat1), float64(lng1), float64(lat2), float64(lng2))
v = distance(lat1, lng1, lat2, lng2)
return nil
})
return v, err
Expand All @@ -86,7 +94,7 @@ func (n *Nodis) GeoHash(key string, members ...string) ([]string, error) {
var v []string
err := n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if meta == nil {
if !meta.isOk() {
return nil
}
for _, member := range members {
Expand Down Expand Up @@ -121,3 +129,15 @@ func (n *Nodis) GeoPos(key string, members ...string) ([]*geo.Member, error) {
})
return v, err
}

func (n *Nodis) GeoRadius(key string, longitude, latitude, radius float64, count int64, desc bool) ([]*geo.Member, error) {
var v []*geo.Member
err := n.exec(func(tx *Tx) error {
meta := tx.readKey(key)
if meta == nil {
return nil
}
return nil
})
return v, err
}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ go 1.21

require (
github.com/gorilla/websocket v1.5.1
github.com/mmcloughlin/geohash v0.10.0
github.com/tidwall/btree v1.7.0
google.golang.org/protobuf v1.34.0
)

require (
github.com/mmcloughlin/geohash v0.10.0 // indirect
golang.org/x/net v0.22.0 // indirect
)
require golang.org/x/net v0.22.0 // indirect

0 comments on commit 0d1e7ca

Please sign in to comment.