scan.tcl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. start_server {tags {"scan"}} {
  2. test "SCAN basic" {
  3. r flushdb
  4. r debug populate 1000
  5. set cur 0
  6. set keys {}
  7. while 1 {
  8. set res [r scan $cur]
  9. set cur [lindex $res 0]
  10. set k [lindex $res 1]
  11. lappend keys {*}$k
  12. if {$cur == 0} break
  13. }
  14. set keys [lsort -unique $keys]
  15. assert_equal 1000 [llength $keys]
  16. }
  17. test "SCAN COUNT" {
  18. r flushdb
  19. r debug populate 1000
  20. set cur 0
  21. set keys {}
  22. while 1 {
  23. set res [r scan $cur count 5]
  24. set cur [lindex $res 0]
  25. set k [lindex $res 1]
  26. lappend keys {*}$k
  27. if {$cur == 0} break
  28. }
  29. set keys [lsort -unique $keys]
  30. assert_equal 1000 [llength $keys]
  31. }
  32. test "SCAN MATCH" {
  33. r flushdb
  34. r debug populate 1000
  35. set cur 0
  36. set keys {}
  37. while 1 {
  38. set res [r scan $cur match "key:1??"]
  39. set cur [lindex $res 0]
  40. set k [lindex $res 1]
  41. lappend keys {*}$k
  42. if {$cur == 0} break
  43. }
  44. set keys [lsort -unique $keys]
  45. assert_equal 100 [llength $keys]
  46. }
  47. foreach enc {intset hashtable} {
  48. test "SSCAN with encoding $enc" {
  49. # Create the Set
  50. r del set
  51. if {$enc eq {intset}} {
  52. set prefix ""
  53. } else {
  54. set prefix "ele:"
  55. }
  56. set elements {}
  57. for {set j 0} {$j < 100} {incr j} {
  58. lappend elements ${prefix}${j}
  59. }
  60. r sadd set {*}$elements
  61. # Verify that the encoding matches.
  62. assert {[r object encoding set] eq $enc}
  63. # Test SSCAN
  64. set cur 0
  65. set keys {}
  66. while 1 {
  67. set res [r sscan set $cur]
  68. set cur [lindex $res 0]
  69. set k [lindex $res 1]
  70. lappend keys {*}$k
  71. if {$cur == 0} break
  72. }
  73. set keys [lsort -unique $keys]
  74. assert_equal 100 [llength $keys]
  75. }
  76. }
  77. foreach enc {ziplist hashtable} {
  78. test "HSCAN with encoding $enc" {
  79. # Create the Hash
  80. r del hash
  81. if {$enc eq {ziplist}} {
  82. set count 30
  83. } else {
  84. set count 1000
  85. }
  86. set elements {}
  87. for {set j 0} {$j < $count} {incr j} {
  88. lappend elements key:$j $j
  89. }
  90. r hmset hash {*}$elements
  91. # Verify that the encoding matches.
  92. assert {[r object encoding hash] eq $enc}
  93. # Test HSCAN
  94. set cur 0
  95. set keys {}
  96. while 1 {
  97. set res [r hscan hash $cur]
  98. set cur [lindex $res 0]
  99. set k [lindex $res 1]
  100. lappend keys {*}$k
  101. if {$cur == 0} break
  102. }
  103. set keys2 {}
  104. foreach {k v} $keys {
  105. assert {$k eq "key:$v"}
  106. lappend keys2 $k
  107. }
  108. set keys2 [lsort -unique $keys2]
  109. assert_equal $count [llength $keys2]
  110. }
  111. }
  112. foreach enc {ziplist skiplist} {
  113. test "ZSCAN with encoding $enc" {
  114. # Create the Sorted Set
  115. r del zset
  116. if {$enc eq {ziplist}} {
  117. set count 30
  118. } else {
  119. set count 1000
  120. }
  121. set elements {}
  122. for {set j 0} {$j < $count} {incr j} {
  123. lappend elements $j key:$j
  124. }
  125. r zadd zset {*}$elements
  126. # Verify that the encoding matches.
  127. assert {[r object encoding zset] eq $enc}
  128. # Test ZSCAN
  129. set cur 0
  130. set keys {}
  131. while 1 {
  132. set res [r zscan zset $cur]
  133. set cur [lindex $res 0]
  134. set k [lindex $res 1]
  135. lappend keys {*}$k
  136. if {$cur == 0} break
  137. }
  138. set keys2 {}
  139. foreach {k v} $keys {
  140. assert {$k eq "key:$v"}
  141. lappend keys2 $k
  142. }
  143. set keys2 [lsort -unique $keys2]
  144. assert_equal $count [llength $keys2]
  145. }
  146. }
  147. test "SCAN guarantees check under write load" {
  148. r flushdb
  149. r debug populate 100
  150. # We start scanning here, so keys from 0 to 99 should all be
  151. # reported at the end of the iteration.
  152. set keys {}
  153. while 1 {
  154. set res [r scan $cur]
  155. set cur [lindex $res 0]
  156. set k [lindex $res 1]
  157. lappend keys {*}$k
  158. if {$cur == 0} break
  159. # Write 10 random keys at every SCAN iteration.
  160. for {set j 0} {$j < 10} {incr j} {
  161. r set addedkey:[randomInt 1000] foo
  162. }
  163. }
  164. set keys2 {}
  165. foreach k $keys {
  166. if {[string length $k] > 6} continue
  167. lappend keys2 $k
  168. }
  169. set keys2 [lsort -unique $keys2]
  170. assert_equal 100 [llength $keys2]
  171. }
  172. test "SSCAN with integer encoded object (issue #1345)" {
  173. set objects {1 a}
  174. r del set
  175. r sadd set {*}$objects
  176. set res [r sscan set 0 MATCH *a* COUNT 100]
  177. assert_equal [lsort -unique [lindex $res 1]] {a}
  178. set res [r sscan set 0 MATCH *1* COUNT 100]
  179. assert_equal [lsort -unique [lindex $res 1]] {1}
  180. }
  181. test "SSCAN with PATTERN" {
  182. r del mykey
  183. r sadd mykey foo fab fiz foobar 1 2 3 4
  184. set res [r sscan mykey 0 MATCH foo* COUNT 10000]
  185. lsort -unique [lindex $res 1]
  186. } {foo foobar}
  187. test "HSCAN with PATTERN" {
  188. r del mykey
  189. r hmset mykey foo 1 fab 2 fiz 3 foobar 10 1 a 2 b 3 c 4 d
  190. set res [r hscan mykey 0 MATCH foo* COUNT 10000]
  191. lsort -unique [lindex $res 1]
  192. } {1 10 foo foobar}
  193. test "ZSCAN with PATTERN" {
  194. r del mykey
  195. r zadd mykey 1 foo 2 fab 3 fiz 10 foobar
  196. set res [r zscan mykey 0 MATCH foo* COUNT 10000]
  197. lsort -unique [lindex $res 1]
  198. }
  199. test "ZSCAN scores: regression test for issue #2175" {
  200. r del mykey
  201. for {set j 0} {$j < 500} {incr j} {
  202. r zadd mykey 9.8813129168249309e-323 $j
  203. }
  204. set res [lindex [r zscan mykey 0] 1]
  205. set first_score [lindex $res 1]
  206. assert {$first_score != 0}
  207. }
  208. }