-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor sync package and update dependencies and optimize code
- Loading branch information
Showing
23 changed files
with
418 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package nodis | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/diiyw/nodis/ds/zset" | ||
"github.com/diiyw/nodis/geo" | ||
"github.com/mmcloughlin/geohash" | ||
) | ||
|
||
var ( | ||
earthRadius = 6372797.560856 | ||
dr = math.Pi / 180.0 | ||
) | ||
|
||
func (n *Nodis) GeoAdd(key string, members ...*geo.Member) (int64, error) { | ||
var v int64 | ||
_ = n.exec(func(tx *Tx) error { | ||
meta := tx.writeKey(key, n.newZSet) | ||
for _, member := range members { | ||
v += meta.value.(*zset.SortedSet).ZAdd(member.Name, float64(member.Hash())) | ||
} | ||
return nil | ||
}) | ||
return v, nil | ||
} | ||
|
||
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) | ||
for _, member := range members { | ||
v += meta.value.(*zset.SortedSet).ZAddXX(member.Name, float64(member.Hash())) | ||
} | ||
return nil | ||
}) | ||
return v, nil | ||
} | ||
|
||
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) | ||
for _, member := range members { | ||
v += meta.value.(*zset.SortedSet).ZAddNX(member.Name, float64(member.Hash())) | ||
} | ||
return nil | ||
}) | ||
return v, nil | ||
} | ||
|
||
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 { | ||
return nil | ||
} | ||
score1, err := meta.value.(*zset.SortedSet).ZScore(member1) | ||
if err != nil { | ||
return err | ||
} | ||
score2, err := meta.value.(*zset.SortedSet).ZScore(member2) | ||
if err != nil { | ||
return err | ||
} | ||
lat1, lng1 := geohash.DecodeInt(uint64(score1)) | ||
lat2, lng2 := geohash.DecodeInt(uint64(score2)) | ||
v = distance(float64(lat1), float64(lng1), float64(lat2), float64(lng2)) | ||
return nil | ||
}) | ||
return v, err | ||
} | ||
|
||
// distance computes the distance between two given coordinates in meter | ||
func distance(latitude1, longitude1, latitude2, longitude2 float64) float64 { | ||
radLat1 := latitude1 * dr | ||
radLat2 := latitude2 * dr | ||
a := radLat1 - radLat2 | ||
b := longitude1*dr - longitude2*dr | ||
return 2 * earthRadius * math.Asin(math.Sqrt(math.Pow(math.Sin(a/2), 2)+ | ||
math.Cos(radLat1)*math.Cos(radLat2)*math.Pow(math.Sin(b/2), 2))) | ||
} | ||
|
||
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 { | ||
return nil | ||
} | ||
for _, member := range members { | ||
score, err := meta.value.(*zset.SortedSet).ZScore(member) | ||
if err != nil { | ||
return err | ||
} | ||
lat, lng := geohash.DecodeInt(uint64(score)) | ||
v = append(v, geohash.Encode(lat, lng)) | ||
} | ||
return nil | ||
}) | ||
return v, err | ||
} | ||
|
||
func (n *Nodis) GeoPos(key string, members ...string) ([]*geo.Member, error) { | ||
var v []*geo.Member | ||
err := n.exec(func(tx *Tx) error { | ||
meta := tx.readKey(key) | ||
if meta == nil { | ||
return nil | ||
} | ||
for _, member := range members { | ||
score, err := meta.value.(*zset.SortedSet).ZScore(member) | ||
if err != nil { | ||
return err | ||
} | ||
lat, lng := geohash.DecodeInt(uint64(score)) | ||
v = append(v, &geo.Member{Name: member, Latitude: lat, Longitude: lng}) | ||
} | ||
return nil | ||
}) | ||
return v, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package geo | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/mmcloughlin/geohash" | ||
) | ||
|
||
type Member struct { | ||
Longitude float64 | ||
Latitude float64 | ||
Name string | ||
} | ||
|
||
func (m *Member) Hash() uint64 { | ||
return geohash.EncodeInt(m.Latitude, m.Longitude) | ||
} | ||
|
||
func Parse(name, lat, lon string) (*Member, error) { | ||
l, err := strconv.ParseFloat(lat, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
n, err := strconv.ParseFloat(lon, 64) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if l < -90 || l > 90 || n < -180 || n > 180 { | ||
return nil, fmt.Errorf("ERR invalid longitude,latitude pair %s,%s", lat, lon) | ||
} | ||
return &Member{ | ||
Latitude: l, | ||
Longitude: n, | ||
Name: name, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package utils | ||
package strings | ||
|
||
import "unsafe" | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.