command.go 20 KB

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