forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/geo_commands.go

279 lines
5.0 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"errors"
)
type GeoCmdable interface {
GeoAdd(ctx context.Context, key string, geoLocation ...*GeoLocation) *IntCmd
2024-02-18 10:42:21 +00:00
GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd
2024-02-18 10:42:21 +00:00
GeoRadius(ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery) *GeoLocationCmd
2024-02-18 10:42:21 +00:00
GeoRadiusStore(ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery) *IntCmd
2024-02-18 10:42:21 +00:00
GeoRadiusByMember(ctx context.Context, key, member string, query *GeoRadiusQuery) *GeoLocationCmd
2024-02-18 10:42:21 +00:00
GeoRadiusByMemberStore(ctx context.Context, key, member string, query *GeoRadiusQuery) *IntCmd
2024-02-18 10:42:21 +00:00
GeoSearch(ctx context.Context, key string, q *GeoSearchQuery) *StringSliceCmd
2024-02-18 10:42:21 +00:00
GeoSearchLocation(ctx context.Context, key string, q *GeoSearchLocationQuery) *GeoSearchLocationCmd
2024-02-18 10:42:21 +00:00
GeoSearchStore(ctx context.Context, key, store string, q *GeoSearchStoreQuery) *IntCmd
2024-02-18 10:42:21 +00:00
GeoDist(ctx context.Context, key string, member1, member2, unit string) *FloatCmd
2024-02-18 10:42:21 +00:00
GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd
}
func (c cmdable) GeoAdd(ctx context.Context, key string, geoLocation ...*GeoLocation) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 2+3*len(geoLocation))
2024-02-18 10:42:21 +00:00
args[0] = "geoadd"
2024-02-18 10:42:21 +00:00
args[1] = key
2024-02-18 10:42:21 +00:00
for i, eachLoc := range geoLocation {
2024-02-18 10:42:21 +00:00
args[2+3*i] = eachLoc.Longitude
2024-02-18 10:42:21 +00:00
args[2+3*i+1] = eachLoc.Latitude
2024-02-18 10:42:21 +00:00
args[2+3*i+2] = eachLoc.Name
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
// GeoRadius is a read-only GEORADIUS_RO command.
2024-02-18 10:42:21 +00:00
func (c cmdable) GeoRadius(
2024-02-18 10:42:21 +00:00
ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery,
2024-02-18 10:42:21 +00:00
) *GeoLocationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewGeoLocationCmd(ctx, query, "georadius_ro", key, longitude, latitude)
2024-02-18 10:42:21 +00:00
if query.Store != "" || query.StoreDist != "" {
2024-02-18 10:42:21 +00:00
cmd.SetErr(errors.New("GeoRadius does not support Store or StoreDist"))
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
// GeoRadiusStore is a writing GEORADIUS command.
2024-02-18 10:42:21 +00:00
func (c cmdable) GeoRadiusStore(
2024-02-18 10:42:21 +00:00
ctx context.Context, key string, longitude, latitude float64, query *GeoRadiusQuery,
2024-02-18 10:42:21 +00:00
) *IntCmd {
2024-02-18 10:42:21 +00:00
args := geoLocationArgs(query, "georadius", key, longitude, latitude)
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
if query.Store == "" && query.StoreDist == "" {
2024-02-18 10:42:21 +00:00
cmd.SetErr(errors.New("GeoRadiusStore requires Store or StoreDist"))
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
// GeoRadiusByMember is a read-only GEORADIUSBYMEMBER_RO command.
2024-02-18 10:42:21 +00:00
func (c cmdable) GeoRadiusByMember(
2024-02-18 10:42:21 +00:00
ctx context.Context, key, member string, query *GeoRadiusQuery,
2024-02-18 10:42:21 +00:00
) *GeoLocationCmd {
2024-02-18 10:42:21 +00:00
cmd := NewGeoLocationCmd(ctx, query, "georadiusbymember_ro", key, member)
2024-02-18 10:42:21 +00:00
if query.Store != "" || query.StoreDist != "" {
2024-02-18 10:42:21 +00:00
cmd.SetErr(errors.New("GeoRadiusByMember does not support Store or StoreDist"))
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
// GeoRadiusByMemberStore is a writing GEORADIUSBYMEMBER command.
2024-02-18 10:42:21 +00:00
func (c cmdable) GeoRadiusByMemberStore(
2024-02-18 10:42:21 +00:00
ctx context.Context, key, member string, query *GeoRadiusQuery,
2024-02-18 10:42:21 +00:00
) *IntCmd {
2024-02-18 10:42:21 +00:00
args := geoLocationArgs(query, "georadiusbymember", key, member)
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
if query.Store == "" && query.StoreDist == "" {
2024-02-18 10:42:21 +00:00
cmd.SetErr(errors.New("GeoRadiusByMemberStore requires Store or StoreDist"))
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoSearch(ctx context.Context, key string, q *GeoSearchQuery) *StringSliceCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 0, 13)
2024-02-18 10:42:21 +00:00
args = append(args, "geosearch", key)
2024-02-18 10:42:21 +00:00
args = geoSearchArgs(q, args)
2024-02-18 10:42:21 +00:00
cmd := NewStringSliceCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoSearchLocation(
2024-02-18 10:42:21 +00:00
ctx context.Context, key string, q *GeoSearchLocationQuery,
2024-02-18 10:42:21 +00:00
) *GeoSearchLocationCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 0, 16)
2024-02-18 10:42:21 +00:00
args = append(args, "geosearch", key)
2024-02-18 10:42:21 +00:00
args = geoSearchLocationArgs(q, args)
2024-02-18 10:42:21 +00:00
cmd := NewGeoSearchLocationCmd(ctx, q, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoSearchStore(ctx context.Context, key, store string, q *GeoSearchStoreQuery) *IntCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 0, 15)
2024-02-18 10:42:21 +00:00
args = append(args, "geosearchstore", store, key)
2024-02-18 10:42:21 +00:00
args = geoSearchArgs(&q.GeoSearchQuery, args)
2024-02-18 10:42:21 +00:00
if q.StoreDist {
2024-02-18 10:42:21 +00:00
args = append(args, "storedist")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoDist(
2024-02-18 10:42:21 +00:00
ctx context.Context, key string, member1, member2, unit string,
2024-02-18 10:42:21 +00:00
) *FloatCmd {
2024-02-18 10:42:21 +00:00
if unit == "" {
2024-02-18 10:42:21 +00:00
unit = "km"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewFloatCmd(ctx, "geodist", key, member1, member2, unit)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoHash(ctx context.Context, key string, members ...string) *StringSliceCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 2+len(members))
2024-02-18 10:42:21 +00:00
args[0] = "geohash"
2024-02-18 10:42:21 +00:00
args[1] = key
2024-02-18 10:42:21 +00:00
for i, member := range members {
2024-02-18 10:42:21 +00:00
args[2+i] = member
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewStringSliceCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c cmdable) GeoPos(ctx context.Context, key string, members ...string) *GeoPosCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 2+len(members))
2024-02-18 10:42:21 +00:00
args[0] = "geopos"
2024-02-18 10:42:21 +00:00
args[1] = key
2024-02-18 10:42:21 +00:00
for i, member := range members {
2024-02-18 10:42:21 +00:00
args[2+i] = member
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewGeoPosCmd(ctx, args...)
2024-02-18 10:42:21 +00:00
_ = c(ctx, cmd)
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}