command.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. package redis
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "gopkg.in/redis.v5/internal"
  9. "gopkg.in/redis.v5/internal/pool"
  10. "gopkg.in/redis.v5/internal/proto"
  11. )
  12. var (
  13. _ Cmder = (*Cmd)(nil)
  14. _ Cmder = (*SliceCmd)(nil)
  15. _ Cmder = (*StatusCmd)(nil)
  16. _ Cmder = (*IntCmd)(nil)
  17. _ Cmder = (*DurationCmd)(nil)
  18. _ Cmder = (*BoolCmd)(nil)
  19. _ Cmder = (*StringCmd)(nil)
  20. _ Cmder = (*FloatCmd)(nil)
  21. _ Cmder = (*StringSliceCmd)(nil)
  22. _ Cmder = (*BoolSliceCmd)(nil)
  23. _ Cmder = (*StringStringMapCmd)(nil)
  24. _ Cmder = (*StringIntMapCmd)(nil)
  25. _ Cmder = (*ZSliceCmd)(nil)
  26. _ Cmder = (*ScanCmd)(nil)
  27. _ Cmder = (*ClusterSlotsCmd)(nil)
  28. )
  29. type Cmder interface {
  30. args() []interface{}
  31. arg(int) string
  32. name() string
  33. readReply(*pool.Conn) error
  34. setErr(error)
  35. readTimeout() *time.Duration
  36. Err() error
  37. fmt.Stringer
  38. }
  39. func setCmdsErr(cmds []Cmder, e error) {
  40. for _, cmd := range cmds {
  41. cmd.setErr(e)
  42. }
  43. }
  44. func writeCmd(cn *pool.Conn, cmds ...Cmder) error {
  45. cn.Wb.Reset()
  46. for _, cmd := range cmds {
  47. if err := cn.Wb.Append(cmd.args()); err != nil {
  48. return err
  49. }
  50. }
  51. _, err := cn.Write(cn.Wb.Bytes())
  52. return err
  53. }
  54. func cmdString(cmd Cmder, val interface{}) string {
  55. var ss []string
  56. for _, arg := range cmd.args() {
  57. ss = append(ss, fmt.Sprint(arg))
  58. }
  59. s := strings.Join(ss, " ")
  60. if err := cmd.Err(); err != nil {
  61. return s + ": " + err.Error()
  62. }
  63. if val != nil {
  64. switch vv := val.(type) {
  65. case []byte:
  66. return s + ": " + string(vv)
  67. default:
  68. return s + ": " + fmt.Sprint(val)
  69. }
  70. }
  71. return s
  72. }
  73. func cmdFirstKeyPos(cmd Cmder, info *CommandInfo) int {
  74. switch cmd.name() {
  75. case "eval", "evalsha":
  76. if cmd.arg(2) != "0" {
  77. return 3
  78. } else {
  79. return -1
  80. }
  81. }
  82. if info == nil {
  83. internal.Logf("info for cmd=%s not found", cmd.name())
  84. return -1
  85. }
  86. return int(info.FirstKeyPos)
  87. }
  88. //------------------------------------------------------------------------------
  89. type baseCmd struct {
  90. _args []interface{}
  91. err error
  92. _readTimeout *time.Duration
  93. }
  94. func (cmd *baseCmd) Err() error {
  95. if cmd.err != nil {
  96. return cmd.err
  97. }
  98. return nil
  99. }
  100. func (cmd *baseCmd) args() []interface{} {
  101. return cmd._args
  102. }
  103. func (cmd *baseCmd) arg(pos int) string {
  104. if pos < 0 || pos >= len(cmd._args) {
  105. return ""
  106. }
  107. s, _ := cmd._args[pos].(string)
  108. return s
  109. }
  110. func (cmd *baseCmd) name() string {
  111. if len(cmd._args) > 0 {
  112. // Cmd name must be lower cased.
  113. s := internal.ToLower(cmd.arg(0))
  114. cmd._args[0] = s
  115. return s
  116. }
  117. return ""
  118. }
  119. func (cmd *baseCmd) readTimeout() *time.Duration {
  120. return cmd._readTimeout
  121. }
  122. func (cmd *baseCmd) setReadTimeout(d time.Duration) {
  123. cmd._readTimeout = &d
  124. }
  125. func (cmd *baseCmd) setErr(e error) {
  126. cmd.err = e
  127. }
  128. func newBaseCmd(args []interface{}) baseCmd {
  129. if len(args) > 0 {
  130. // Cmd name is expected to be in lower case.
  131. args[0] = internal.ToLower(args[0].(string))
  132. }
  133. return baseCmd{_args: args}
  134. }
  135. //------------------------------------------------------------------------------
  136. type Cmd struct {
  137. baseCmd
  138. val interface{}
  139. }
  140. func NewCmd(args ...interface{}) *Cmd {
  141. return &Cmd{
  142. baseCmd: baseCmd{_args: args},
  143. }
  144. }
  145. func (cmd *Cmd) Val() interface{} {
  146. return cmd.val
  147. }
  148. func (cmd *Cmd) Result() (interface{}, error) {
  149. return cmd.val, cmd.err
  150. }
  151. func (cmd *Cmd) String() string {
  152. return cmdString(cmd, cmd.val)
  153. }
  154. func (cmd *Cmd) readReply(cn *pool.Conn) error {
  155. cmd.val, cmd.err = cn.Rd.ReadReply(sliceParser)
  156. if cmd.err != nil {
  157. return cmd.err
  158. }
  159. if b, ok := cmd.val.([]byte); ok {
  160. // Bytes must be copied, because underlying memory is reused.
  161. cmd.val = string(b)
  162. }
  163. return nil
  164. }
  165. //------------------------------------------------------------------------------
  166. type SliceCmd struct {
  167. baseCmd
  168. val []interface{}
  169. }
  170. func NewSliceCmd(args ...interface{}) *SliceCmd {
  171. return &SliceCmd{
  172. baseCmd: baseCmd{_args: args},
  173. }
  174. }
  175. func (cmd *SliceCmd) Val() []interface{} {
  176. return cmd.val
  177. }
  178. func (cmd *SliceCmd) Result() ([]interface{}, error) {
  179. return cmd.val, cmd.err
  180. }
  181. func (cmd *SliceCmd) String() string {
  182. return cmdString(cmd, cmd.val)
  183. }
  184. func (cmd *SliceCmd) readReply(cn *pool.Conn) error {
  185. var v interface{}
  186. v, cmd.err = cn.Rd.ReadArrayReply(sliceParser)
  187. if cmd.err != nil {
  188. return cmd.err
  189. }
  190. cmd.val = v.([]interface{})
  191. return nil
  192. }
  193. //------------------------------------------------------------------------------
  194. type StatusCmd struct {
  195. baseCmd
  196. val string
  197. }
  198. func NewStatusCmd(args ...interface{}) *StatusCmd {
  199. return &StatusCmd{
  200. baseCmd: baseCmd{_args: args},
  201. }
  202. }
  203. func (cmd *StatusCmd) Val() string {
  204. return cmd.val
  205. }
  206. func (cmd *StatusCmd) Result() (string, error) {
  207. return cmd.val, cmd.err
  208. }
  209. func (cmd *StatusCmd) String() string {
  210. return cmdString(cmd, cmd.val)
  211. }
  212. func (cmd *StatusCmd) readReply(cn *pool.Conn) error {
  213. cmd.val, cmd.err = cn.Rd.ReadStringReply()
  214. return cmd.err
  215. }
  216. //------------------------------------------------------------------------------
  217. type IntCmd struct {
  218. baseCmd
  219. val int64
  220. }
  221. func NewIntCmd(args ...interface{}) *IntCmd {
  222. return &IntCmd{
  223. baseCmd: baseCmd{_args: args},
  224. }
  225. }
  226. func (cmd *IntCmd) Val() int64 {
  227. return cmd.val
  228. }
  229. func (cmd *IntCmd) Result() (int64, error) {
  230. return cmd.val, cmd.err
  231. }
  232. func (cmd *IntCmd) String() string {
  233. return cmdString(cmd, cmd.val)
  234. }
  235. func (cmd *IntCmd) readReply(cn *pool.Conn) error {
  236. cmd.val, cmd.err = cn.Rd.ReadIntReply()
  237. return cmd.err
  238. }
  239. //------------------------------------------------------------------------------
  240. type DurationCmd struct {
  241. baseCmd
  242. val time.Duration
  243. precision time.Duration
  244. }
  245. func NewDurationCmd(precision time.Duration, args ...interface{}) *DurationCmd {
  246. return &DurationCmd{
  247. baseCmd: baseCmd{_args: args},
  248. precision: precision,
  249. }
  250. }
  251. func (cmd *DurationCmd) Val() time.Duration {
  252. return cmd.val
  253. }
  254. func (cmd *DurationCmd) Result() (time.Duration, error) {
  255. return cmd.val, cmd.err
  256. }
  257. func (cmd *DurationCmd) String() string {
  258. return cmdString(cmd, cmd.val)
  259. }
  260. func (cmd *DurationCmd) readReply(cn *pool.Conn) error {
  261. var n int64
  262. n, cmd.err = cn.Rd.ReadIntReply()
  263. if cmd.err != nil {
  264. return cmd.err
  265. }
  266. cmd.val = time.Duration(n) * cmd.precision
  267. return nil
  268. }
  269. //------------------------------------------------------------------------------
  270. type TimeCmd struct {
  271. baseCmd
  272. val time.Time
  273. }
  274. func NewTimeCmd(args ...interface{}) *TimeCmd {
  275. return &TimeCmd{
  276. baseCmd: baseCmd{_args: args},
  277. }
  278. }
  279. func (cmd *TimeCmd) Val() time.Time {
  280. return cmd.val
  281. }
  282. func (cmd *TimeCmd) Result() (time.Time, error) {
  283. return cmd.val, cmd.err
  284. }
  285. func (cmd *TimeCmd) String() string {
  286. return cmdString(cmd, cmd.val)
  287. }
  288. func (cmd *TimeCmd) readReply(cn *pool.Conn) error {
  289. var v interface{}
  290. v, cmd.err = cn.Rd.ReadArrayReply(timeParser)
  291. if cmd.err != nil {
  292. return cmd.err
  293. }
  294. cmd.val = v.(time.Time)
  295. return nil
  296. }
  297. //------------------------------------------------------------------------------
  298. type BoolCmd struct {
  299. baseCmd
  300. val bool
  301. }
  302. func NewBoolCmd(args ...interface{}) *BoolCmd {
  303. return &BoolCmd{
  304. baseCmd: baseCmd{_args: args},
  305. }
  306. }
  307. func (cmd *BoolCmd) Val() bool {
  308. return cmd.val
  309. }
  310. func (cmd *BoolCmd) Result() (bool, error) {
  311. return cmd.val, cmd.err
  312. }
  313. func (cmd *BoolCmd) String() string {
  314. return cmdString(cmd, cmd.val)
  315. }
  316. var ok = []byte("OK")
  317. func (cmd *BoolCmd) readReply(cn *pool.Conn) error {
  318. var v interface{}
  319. v, cmd.err = cn.Rd.ReadReply(nil)
  320. // `SET key value NX` returns nil when key already exists. But
  321. // `SETNX key value` returns bool (0/1). So convert nil to bool.
  322. // TODO: is this okay?
  323. if cmd.err == Nil {
  324. cmd.val = false
  325. cmd.err = nil
  326. return nil
  327. }
  328. if cmd.err != nil {
  329. return cmd.err
  330. }
  331. switch v := v.(type) {
  332. case int64:
  333. cmd.val = v == 1
  334. return nil
  335. case []byte:
  336. cmd.val = bytes.Equal(v, ok)
  337. return nil
  338. default:
  339. cmd.err = fmt.Errorf("got %T, wanted int64 or string", v)
  340. return cmd.err
  341. }
  342. }
  343. //------------------------------------------------------------------------------
  344. type StringCmd struct {
  345. baseCmd
  346. val []byte
  347. }
  348. func NewStringCmd(args ...interface{}) *StringCmd {
  349. return &StringCmd{
  350. baseCmd: baseCmd{_args: args},
  351. }
  352. }
  353. func (cmd *StringCmd) Val() string {
  354. return internal.BytesToString(cmd.val)
  355. }
  356. func (cmd *StringCmd) Result() (string, error) {
  357. return cmd.Val(), cmd.err
  358. }
  359. func (cmd *StringCmd) Bytes() ([]byte, error) {
  360. return cmd.val, cmd.err
  361. }
  362. func (cmd *StringCmd) Int64() (int64, error) {
  363. if cmd.err != nil {
  364. return 0, cmd.err
  365. }
  366. return strconv.ParseInt(cmd.Val(), 10, 64)
  367. }
  368. func (cmd *StringCmd) Uint64() (uint64, error) {
  369. if cmd.err != nil {
  370. return 0, cmd.err
  371. }
  372. return strconv.ParseUint(cmd.Val(), 10, 64)
  373. }
  374. func (cmd *StringCmd) Float64() (float64, error) {
  375. if cmd.err != nil {
  376. return 0, cmd.err
  377. }
  378. return strconv.ParseFloat(cmd.Val(), 64)
  379. }
  380. func (cmd *StringCmd) Scan(val interface{}) error {
  381. if cmd.err != nil {
  382. return cmd.err
  383. }
  384. return proto.Scan(cmd.val, val)
  385. }
  386. func (cmd *StringCmd) String() string {
  387. return cmdString(cmd, cmd.val)
  388. }
  389. func (cmd *StringCmd) readReply(cn *pool.Conn) error {
  390. cmd.val, cmd.err = cn.Rd.ReadBytesReply()
  391. return cmd.err
  392. }
  393. //------------------------------------------------------------------------------
  394. type FloatCmd struct {
  395. baseCmd
  396. val float64
  397. }
  398. func NewFloatCmd(args ...interface{}) *FloatCmd {
  399. return &FloatCmd{
  400. baseCmd: baseCmd{_args: args},
  401. }
  402. }
  403. func (cmd *FloatCmd) Val() float64 {
  404. return cmd.val
  405. }
  406. func (cmd *FloatCmd) Result() (float64, error) {
  407. return cmd.Val(), cmd.Err()
  408. }
  409. func (cmd *FloatCmd) String() string {
  410. return cmdString(cmd, cmd.val)
  411. }
  412. func (cmd *FloatCmd) readReply(cn *pool.Conn) error {
  413. cmd.val, cmd.err = cn.Rd.ReadFloatReply()
  414. return cmd.err
  415. }
  416. //------------------------------------------------------------------------------
  417. type StringSliceCmd struct {
  418. baseCmd
  419. val []string
  420. }
  421. func NewStringSliceCmd(args ...interface{}) *StringSliceCmd {
  422. return &StringSliceCmd{
  423. baseCmd: baseCmd{_args: args},
  424. }
  425. }
  426. func (cmd *StringSliceCmd) Val() []string {
  427. return cmd.val
  428. }
  429. func (cmd *StringSliceCmd) Result() ([]string, error) {
  430. return cmd.Val(), cmd.Err()
  431. }
  432. func (cmd *StringSliceCmd) String() string {
  433. return cmdString(cmd, cmd.val)
  434. }
  435. func (cmd *StringSliceCmd) ScanSlice(container interface{}) error {
  436. return proto.ScanSlice(cmd.Val(), container)
  437. }
  438. func (cmd *StringSliceCmd) readReply(cn *pool.Conn) error {
  439. var v interface{}
  440. v, cmd.err = cn.Rd.ReadArrayReply(stringSliceParser)
  441. if cmd.err != nil {
  442. return cmd.err
  443. }
  444. cmd.val = v.([]string)
  445. return nil
  446. }
  447. //------------------------------------------------------------------------------
  448. type BoolSliceCmd struct {
  449. baseCmd
  450. val []bool
  451. }
  452. func NewBoolSliceCmd(args ...interface{}) *BoolSliceCmd {
  453. return &BoolSliceCmd{
  454. baseCmd: baseCmd{_args: args},
  455. }
  456. }
  457. func (cmd *BoolSliceCmd) Val() []bool {
  458. return cmd.val
  459. }
  460. func (cmd *BoolSliceCmd) Result() ([]bool, error) {
  461. return cmd.val, cmd.err
  462. }
  463. func (cmd *BoolSliceCmd) String() string {
  464. return cmdString(cmd, cmd.val)
  465. }
  466. func (cmd *BoolSliceCmd) readReply(cn *pool.Conn) error {
  467. var v interface{}
  468. v, cmd.err = cn.Rd.ReadArrayReply(boolSliceParser)
  469. if cmd.err != nil {
  470. return cmd.err
  471. }
  472. cmd.val = v.([]bool)
  473. return nil
  474. }
  475. //------------------------------------------------------------------------------
  476. type StringStringMapCmd struct {
  477. baseCmd
  478. val map[string]string
  479. }
  480. func NewStringStringMapCmd(args ...interface{}) *StringStringMapCmd {
  481. return &StringStringMapCmd{
  482. baseCmd: baseCmd{_args: args},
  483. }
  484. }
  485. func (cmd *StringStringMapCmd) Val() map[string]string {
  486. return cmd.val
  487. }
  488. func (cmd *StringStringMapCmd) Result() (map[string]string, error) {
  489. return cmd.val, cmd.err
  490. }
  491. func (cmd *StringStringMapCmd) String() string {
  492. return cmdString(cmd, cmd.val)
  493. }
  494. func (cmd *StringStringMapCmd) readReply(cn *pool.Conn) error {
  495. var v interface{}
  496. v, cmd.err = cn.Rd.ReadArrayReply(stringStringMapParser)
  497. if cmd.err != nil {
  498. return cmd.err
  499. }
  500. cmd.val = v.(map[string]string)
  501. return nil
  502. }
  503. //------------------------------------------------------------------------------
  504. type StringIntMapCmd struct {
  505. baseCmd
  506. val map[string]int64
  507. }
  508. func NewStringIntMapCmd(args ...interface{}) *StringIntMapCmd {
  509. return &StringIntMapCmd{
  510. baseCmd: baseCmd{_args: args},
  511. }
  512. }
  513. func (cmd *StringIntMapCmd) Val() map[string]int64 {
  514. return cmd.val
  515. }
  516. func (cmd *StringIntMapCmd) Result() (map[string]int64, error) {
  517. return cmd.val, cmd.err
  518. }
  519. func (cmd *StringIntMapCmd) String() string {
  520. return cmdString(cmd, cmd.val)
  521. }
  522. func (cmd *StringIntMapCmd) readReply(cn *pool.Conn) error {
  523. var v interface{}
  524. v, cmd.err = cn.Rd.ReadArrayReply(stringIntMapParser)
  525. if cmd.err != nil {
  526. return cmd.err
  527. }
  528. cmd.val = v.(map[string]int64)
  529. return nil
  530. }
  531. //------------------------------------------------------------------------------
  532. type ZSliceCmd struct {
  533. baseCmd
  534. val []Z
  535. }
  536. func NewZSliceCmd(args ...interface{}) *ZSliceCmd {
  537. return &ZSliceCmd{
  538. baseCmd: baseCmd{_args: args},
  539. }
  540. }
  541. func (cmd *ZSliceCmd) Val() []Z {
  542. return cmd.val
  543. }
  544. func (cmd *ZSliceCmd) Result() ([]Z, error) {
  545. return cmd.val, cmd.err
  546. }
  547. func (cmd *ZSliceCmd) String() string {
  548. return cmdString(cmd, cmd.val)
  549. }
  550. func (cmd *ZSliceCmd) readReply(cn *pool.Conn) error {
  551. var v interface{}
  552. v, cmd.err = cn.Rd.ReadArrayReply(zSliceParser)
  553. if cmd.err != nil {
  554. return cmd.err
  555. }
  556. cmd.val = v.([]Z)
  557. return nil
  558. }
  559. //------------------------------------------------------------------------------
  560. type ScanCmd struct {
  561. baseCmd
  562. page []string
  563. cursor uint64
  564. process func(cmd Cmder) error
  565. }
  566. func NewScanCmd(process func(cmd Cmder) error, args ...interface{}) *ScanCmd {
  567. return &ScanCmd{
  568. baseCmd: baseCmd{_args: args},
  569. process: process,
  570. }
  571. }
  572. func (cmd *ScanCmd) Val() (keys []string, cursor uint64) {
  573. return cmd.page, cmd.cursor
  574. }
  575. func (cmd *ScanCmd) Result() (keys []string, cursor uint64, err error) {
  576. return cmd.page, cmd.cursor, cmd.err
  577. }
  578. func (cmd *ScanCmd) String() string {
  579. return cmdString(cmd, cmd.page)
  580. }
  581. func (cmd *ScanCmd) readReply(cn *pool.Conn) error {
  582. cmd.page, cmd.cursor, cmd.err = cn.Rd.ReadScanReply()
  583. return cmd.err
  584. }
  585. // Iterator creates a new ScanIterator.
  586. func (cmd *ScanCmd) Iterator() *ScanIterator {
  587. return &ScanIterator{
  588. cmd: cmd,
  589. }
  590. }
  591. //------------------------------------------------------------------------------
  592. type ClusterNode struct {
  593. Id string
  594. Addr string
  595. }
  596. type ClusterSlot struct {
  597. Start int
  598. End int
  599. Nodes []ClusterNode
  600. }
  601. type ClusterSlotsCmd struct {
  602. baseCmd
  603. val []ClusterSlot
  604. }
  605. func NewClusterSlotsCmd(args ...interface{}) *ClusterSlotsCmd {
  606. return &ClusterSlotsCmd{
  607. baseCmd: baseCmd{_args: args},
  608. }
  609. }
  610. func (cmd *ClusterSlotsCmd) Val() []ClusterSlot {
  611. return cmd.val
  612. }
  613. func (cmd *ClusterSlotsCmd) Result() ([]ClusterSlot, error) {
  614. return cmd.Val(), cmd.Err()
  615. }
  616. func (cmd *ClusterSlotsCmd) String() string {
  617. return cmdString(cmd, cmd.val)
  618. }
  619. func (cmd *ClusterSlotsCmd) readReply(cn *pool.Conn) error {
  620. var v interface{}
  621. v, cmd.err = cn.Rd.ReadArrayReply(clusterSlotsParser)
  622. if cmd.err != nil {
  623. return cmd.err
  624. }
  625. cmd.val = v.([]ClusterSlot)
  626. return nil
  627. }
  628. //------------------------------------------------------------------------------
  629. // GeoLocation is used with GeoAdd to add geospatial location.
  630. type GeoLocation struct {
  631. Name string
  632. Longitude, Latitude, Dist float64
  633. GeoHash int64
  634. }
  635. // GeoRadiusQuery is used with GeoRadius to query geospatial index.
  636. type GeoRadiusQuery struct {
  637. Radius float64
  638. // Can be m, km, ft, or mi. Default is km.
  639. Unit string
  640. WithCoord bool
  641. WithDist bool
  642. WithGeoHash bool
  643. Count int
  644. // Can be ASC or DESC. Default is no sort order.
  645. Sort string
  646. }
  647. type GeoLocationCmd struct {
  648. baseCmd
  649. q *GeoRadiusQuery
  650. locations []GeoLocation
  651. }
  652. func NewGeoLocationCmd(q *GeoRadiusQuery, args ...interface{}) *GeoLocationCmd {
  653. args = append(args, q.Radius)
  654. if q.Unit != "" {
  655. args = append(args, q.Unit)
  656. } else {
  657. args = append(args, "km")
  658. }
  659. if q.WithCoord {
  660. args = append(args, "WITHCOORD")
  661. }
  662. if q.WithDist {
  663. args = append(args, "WITHDIST")
  664. }
  665. if q.WithGeoHash {
  666. args = append(args, "WITHHASH")
  667. }
  668. if q.Count > 0 {
  669. args = append(args, "COUNT", q.Count)
  670. }
  671. if q.Sort != "" {
  672. args = append(args, q.Sort)
  673. }
  674. cmd := newBaseCmd(args)
  675. return &GeoLocationCmd{
  676. baseCmd: cmd,
  677. q: q,
  678. }
  679. }
  680. func (cmd *GeoLocationCmd) Val() []GeoLocation {
  681. return cmd.locations
  682. }
  683. func (cmd *GeoLocationCmd) Result() ([]GeoLocation, error) {
  684. return cmd.locations, cmd.err
  685. }
  686. func (cmd *GeoLocationCmd) String() string {
  687. return cmdString(cmd, cmd.locations)
  688. }
  689. func (cmd *GeoLocationCmd) readReply(cn *pool.Conn) error {
  690. var v interface{}
  691. v, cmd.err = cn.Rd.ReadArrayReply(newGeoLocationSliceParser(cmd.q))
  692. if cmd.err != nil {
  693. return cmd.err
  694. }
  695. cmd.locations = v.([]GeoLocation)
  696. return nil
  697. }
  698. //------------------------------------------------------------------------------
  699. type GeoPos struct {
  700. Longitude, Latitude float64
  701. }
  702. type GeoPosCmd struct {
  703. baseCmd
  704. positions []*GeoPos
  705. }
  706. func NewGeoPosCmd(args ...interface{}) *GeoPosCmd {
  707. return &GeoPosCmd{
  708. baseCmd: baseCmd{_args: args},
  709. }
  710. }
  711. func (cmd *GeoPosCmd) Val() []*GeoPos {
  712. return cmd.positions
  713. }
  714. func (cmd *GeoPosCmd) Result() ([]*GeoPos, error) {
  715. return cmd.Val(), cmd.Err()
  716. }
  717. func (cmd *GeoPosCmd) String() string {
  718. return cmdString(cmd, cmd.positions)
  719. }
  720. func (cmd *GeoPosCmd) readReply(cn *pool.Conn) error {
  721. var v interface{}
  722. v, cmd.err = cn.Rd.ReadArrayReply(geoPosSliceParser)
  723. if cmd.err != nil {
  724. return cmd.err
  725. }
  726. cmd.positions = v.([]*GeoPos)
  727. return nil
  728. }
  729. //------------------------------------------------------------------------------
  730. type CommandInfo struct {
  731. Name string
  732. Arity int8
  733. Flags []string
  734. FirstKeyPos int8
  735. LastKeyPos int8
  736. StepCount int8
  737. ReadOnly bool
  738. }
  739. type CommandsInfoCmd struct {
  740. baseCmd
  741. val map[string]*CommandInfo
  742. }
  743. func NewCommandsInfoCmd(args ...interface{}) *CommandsInfoCmd {
  744. return &CommandsInfoCmd{
  745. baseCmd: baseCmd{_args: args},
  746. }
  747. }
  748. func (cmd *CommandsInfoCmd) Val() map[string]*CommandInfo {
  749. return cmd.val
  750. }
  751. func (cmd *CommandsInfoCmd) Result() (map[string]*CommandInfo, error) {
  752. return cmd.Val(), cmd.Err()
  753. }
  754. func (cmd *CommandsInfoCmd) String() string {
  755. return cmdString(cmd, cmd.val)
  756. }
  757. func (cmd *CommandsInfoCmd) readReply(cn *pool.Conn) error {
  758. var v interface{}
  759. v, cmd.err = cn.Rd.ReadArrayReply(commandInfoSliceParser)
  760. if cmd.err != nil {
  761. return cmd.err
  762. }
  763. cmd.val = v.(map[string]*CommandInfo)
  764. return nil
  765. }