2
0

scan.tcl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. test "SCAN TYPE" {
  48. r flushdb
  49. # populate only creates strings
  50. r debug populate 1000
  51. # Check non-strings are excluded
  52. set cur 0
  53. set keys {}
  54. while 1 {
  55. set res [r scan $cur type "list"]
  56. set cur [lindex $res 0]
  57. set k [lindex $res 1]
  58. lappend keys {*}$k
  59. if {$cur == 0} break
  60. }
  61. assert_equal 0 [llength $keys]
  62. # Check strings are included
  63. set cur 0
  64. set keys {}
  65. while 1 {
  66. set res [r scan $cur type "string"]
  67. set cur [lindex $res 0]
  68. set k [lindex $res 1]
  69. lappend keys {*}$k
  70. if {$cur == 0} break
  71. }
  72. assert_equal 1000 [llength $keys]
  73. # Check all three args work together
  74. set cur 0
  75. set keys {}
  76. while 1 {
  77. set res [r scan $cur type "string" match "key:*" count 10]
  78. set cur [lindex $res 0]
  79. set k [lindex $res 1]
  80. lappend keys {*}$k
  81. if {$cur == 0} break
  82. }
  83. assert_equal 1000 [llength $keys]
  84. }
  85. foreach enc {intset hashtable} {
  86. test "SSCAN with encoding $enc" {
  87. # Create the Set
  88. r del set
  89. if {$enc eq {intset}} {
  90. set prefix ""
  91. } else {
  92. set prefix "ele:"
  93. }
  94. set elements {}
  95. for {set j 0} {$j < 100} {incr j} {
  96. lappend elements ${prefix}${j}
  97. }
  98. r sadd set {*}$elements
  99. # Verify that the encoding matches.
  100. assert {[r object encoding set] eq $enc}
  101. # Test SSCAN
  102. set cur 0
  103. set keys {}
  104. while 1 {
  105. set res [r sscan set $cur]
  106. set cur [lindex $res 0]
  107. set k [lindex $res 1]
  108. lappend keys {*}$k
  109. if {$cur == 0} break
  110. }
  111. set keys [lsort -unique $keys]
  112. assert_equal 100 [llength $keys]
  113. }
  114. }
  115. foreach enc {ziplist hashtable} {
  116. test "HSCAN with encoding $enc" {
  117. # Create the Hash
  118. r del hash
  119. if {$enc eq {ziplist}} {
  120. set count 30
  121. } else {
  122. set count 1000
  123. }
  124. set elements {}
  125. for {set j 0} {$j < $count} {incr j} {
  126. lappend elements key:$j $j
  127. }
  128. r hmset hash {*}$elements
  129. # Verify that the encoding matches.
  130. assert {[r object encoding hash] eq $enc}
  131. # Test HSCAN
  132. set cur 0
  133. set keys {}
  134. while 1 {
  135. set res [r hscan hash $cur]
  136. set cur [lindex $res 0]
  137. set k [lindex $res 1]
  138. lappend keys {*}$k
  139. if {$cur == 0} break
  140. }
  141. set keys2 {}
  142. foreach {k v} $keys {
  143. assert {$k eq "key:$v"}
  144. lappend keys2 $k
  145. }
  146. set keys2 [lsort -unique $keys2]
  147. assert_equal $count [llength $keys2]
  148. }
  149. }
  150. foreach enc {ziplist skiplist} {
  151. test "ZSCAN with encoding $enc" {
  152. # Create the Sorted Set
  153. r del zset
  154. if {$enc eq {ziplist}} {
  155. set count 30
  156. } else {
  157. set count 1000
  158. }
  159. set elements {}
  160. for {set j 0} {$j < $count} {incr j} {
  161. lappend elements $j key:$j
  162. }
  163. r zadd zset {*}$elements
  164. # Verify that the encoding matches.
  165. assert {[r object encoding zset] eq $enc}
  166. # Test ZSCAN
  167. set cur 0
  168. set keys {}
  169. while 1 {
  170. set res [r zscan zset $cur]
  171. set cur [lindex $res 0]
  172. set k [lindex $res 1]
  173. lappend keys {*}$k
  174. if {$cur == 0} break
  175. }
  176. set keys2 {}
  177. foreach {k v} $keys {
  178. assert {$k eq "key:$v"}
  179. lappend keys2 $k
  180. }
  181. set keys2 [lsort -unique $keys2]
  182. assert_equal $count [llength $keys2]
  183. }
  184. }
  185. test "SCAN guarantees check under write load" {
  186. r flushdb
  187. r debug populate 100
  188. # We start scanning here, so keys from 0 to 99 should all be
  189. # reported at the end of the iteration.
  190. set keys {}
  191. while 1 {
  192. set res [r scan $cur]
  193. set cur [lindex $res 0]
  194. set k [lindex $res 1]
  195. lappend keys {*}$k
  196. if {$cur == 0} break
  197. # Write 10 random keys at every SCAN iteration.
  198. for {set j 0} {$j < 10} {incr j} {
  199. r set addedkey:[randomInt 1000] foo
  200. }
  201. }
  202. set keys2 {}
  203. foreach k $keys {
  204. if {[string length $k] > 6} continue
  205. lappend keys2 $k
  206. }
  207. set keys2 [lsort -unique $keys2]
  208. assert_equal 100 [llength $keys2]
  209. }
  210. test "SSCAN with integer encoded object (issue #1345)" {
  211. set objects {1 a}
  212. r del set
  213. r sadd set {*}$objects
  214. set res [r sscan set 0 MATCH *a* COUNT 100]
  215. assert_equal [lsort -unique [lindex $res 1]] {a}
  216. set res [r sscan set 0 MATCH *1* COUNT 100]
  217. assert_equal [lsort -unique [lindex $res 1]] {1}
  218. }
  219. test "SSCAN with PATTERN" {
  220. r del mykey
  221. r sadd mykey foo fab fiz foobar 1 2 3 4
  222. set res [r sscan mykey 0 MATCH foo* COUNT 10000]
  223. lsort -unique [lindex $res 1]
  224. } {foo foobar}
  225. test "HSCAN with PATTERN" {
  226. r del mykey
  227. r hmset mykey foo 1 fab 2 fiz 3 foobar 10 1 a 2 b 3 c 4 d
  228. set res [r hscan mykey 0 MATCH foo* COUNT 10000]
  229. lsort -unique [lindex $res 1]
  230. } {1 10 foo foobar}
  231. test "ZSCAN with PATTERN" {
  232. r del mykey
  233. r zadd mykey 1 foo 2 fab 3 fiz 10 foobar
  234. set res [r zscan mykey 0 MATCH foo* COUNT 10000]
  235. lsort -unique [lindex $res 1]
  236. }
  237. test "ZSCAN scores: regression test for issue #2175" {
  238. r del mykey
  239. for {set j 0} {$j < 500} {incr j} {
  240. r zadd mykey 9.8813129168249309e-323 $j
  241. }
  242. set res [lindex [r zscan mykey 0] 1]
  243. set first_score [lindex $res 1]
  244. assert {$first_score != 0}
  245. }
  246. test "SCAN regression test for issue #4906" {
  247. for {set k 0} {$k < 100} {incr k} {
  248. r del set
  249. r sadd set x; # Make sure it's not intset encoded
  250. set toremove {}
  251. unset -nocomplain found
  252. array set found {}
  253. # Populate the set
  254. set numele [expr {101+[randomInt 1000]}]
  255. for {set j 0} {$j < $numele} {incr j} {
  256. r sadd set $j
  257. if {$j >= 100} {
  258. lappend toremove $j
  259. }
  260. }
  261. # Start scanning
  262. set cursor 0
  263. set iteration 0
  264. set del_iteration [randomInt 10]
  265. while {!($cursor == 0 && $iteration != 0)} {
  266. lassign [r sscan set $cursor] cursor items
  267. # Mark found items. We expect to find from 0 to 99 at the end
  268. # since those elements will never be removed during the scanning.
  269. foreach i $items {
  270. set found($i) 1
  271. }
  272. incr iteration
  273. # At some point remove most of the items to trigger the
  274. # rehashing to a smaller hash table.
  275. if {$iteration == $del_iteration} {
  276. r srem set {*}$toremove
  277. }
  278. }
  279. # Verify that SSCAN reported everything from 0 to 99
  280. for {set j 0} {$j < 100} {incr j} {
  281. if {![info exists found($j)]} {
  282. fail "SSCAN element missing $j"
  283. }
  284. }
  285. }
  286. }
  287. }