You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
5.2 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestPrefixIteratorNoMatchNil(t *testing.T) {
  7. for backend, _ := range backends {
  8. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  9. db := newTempDB(t, backend)
  10. itr := IteratePrefix(db, []byte("2"))
  11. checkInvalid(t, itr)
  12. })
  13. }
  14. }
  15. func TestPrefixIteratorNoMatch1(t *testing.T) {
  16. for backend, _ := range backends {
  17. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  18. db := newTempDB(t, backend)
  19. itr := IteratePrefix(db, []byte("2"))
  20. db.SetSync(bz("1"), bz("value_1"))
  21. checkInvalid(t, itr)
  22. })
  23. }
  24. }
  25. func TestPrefixIteratorMatch2(t *testing.T) {
  26. for backend, _ := range backends {
  27. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  28. db := newTempDB(t, backend)
  29. db.SetSync(bz("2"), bz("value_2"))
  30. itr := IteratePrefix(db, []byte("2"))
  31. checkValid(t, itr, true)
  32. checkItem(t, itr, bz("2"), bz("value_2"))
  33. checkNext(t, itr, false)
  34. // Once invalid...
  35. checkInvalid(t, itr)
  36. })
  37. }
  38. }
  39. func TestPrefixIteratorMatch3(t *testing.T) {
  40. for backend, _ := range backends {
  41. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  42. db := newTempDB(t, backend)
  43. db.SetSync(bz("3"), bz("value_3"))
  44. itr := IteratePrefix(db, []byte("2"))
  45. // Once invalid...
  46. checkInvalid(t, itr)
  47. })
  48. }
  49. }
  50. // Search for a/1, fail by too much Next()
  51. func TestPrefixIteratorMatches1N(t *testing.T) {
  52. for backend, _ := range backends {
  53. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  54. db := newTempDB(t, backend)
  55. db.SetSync(bz("a/1"), bz("value_1"))
  56. db.SetSync(bz("a/3"), bz("value_3"))
  57. itr := IteratePrefix(db, []byte("a/"))
  58. itr.Seek(bz("a/1"))
  59. checkValid(t, itr, true)
  60. checkItem(t, itr, bz("a/1"), bz("value_1"))
  61. checkNext(t, itr, true)
  62. checkItem(t, itr, bz("a/3"), bz("value_3"))
  63. // Bad!
  64. checkNext(t, itr, false)
  65. // Once invalid...
  66. checkInvalid(t, itr)
  67. })
  68. }
  69. }
  70. // Search for a/1, fail by too much Prev()
  71. func TestPrefixIteratorMatches1P(t *testing.T) {
  72. for backend, _ := range backends {
  73. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  74. db := newTempDB(t, backend)
  75. db.SetSync(bz("a/1"), bz("value_1"))
  76. db.SetSync(bz("a/3"), bz("value_3"))
  77. itr := IteratePrefix(db, []byte("a/"))
  78. itr.Seek(bz("a/1"))
  79. checkValid(t, itr, true)
  80. checkItem(t, itr, bz("a/1"), bz("value_1"))
  81. checkNext(t, itr, true)
  82. checkItem(t, itr, bz("a/3"), bz("value_3"))
  83. checkPrev(t, itr, true)
  84. checkItem(t, itr, bz("a/1"), bz("value_1"))
  85. // Bad!
  86. checkPrev(t, itr, false)
  87. // Once invalid...
  88. checkInvalid(t, itr)
  89. })
  90. }
  91. }
  92. // Search for a/2, fail by too much Next()
  93. func TestPrefixIteratorMatches2N(t *testing.T) {
  94. for backend, _ := range backends {
  95. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  96. db := newTempDB(t, backend)
  97. db.SetSync(bz("a/1"), bz("value_1"))
  98. db.SetSync(bz("a/3"), bz("value_3"))
  99. itr := IteratePrefix(db, []byte("a/"))
  100. itr.Seek(bz("a/2"))
  101. checkValid(t, itr, true)
  102. checkItem(t, itr, bz("a/3"), bz("value_3"))
  103. checkPrev(t, itr, true)
  104. checkItem(t, itr, bz("a/1"), bz("value_1"))
  105. checkNext(t, itr, true)
  106. checkItem(t, itr, bz("a/3"), bz("value_3"))
  107. // Bad!
  108. checkNext(t, itr, false)
  109. // Once invalid...
  110. checkInvalid(t, itr)
  111. })
  112. }
  113. }
  114. // Search for a/2, fail by too much Prev()
  115. func TestPrefixIteratorMatches2P(t *testing.T) {
  116. for backend, _ := range backends {
  117. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  118. db := newTempDB(t, backend)
  119. db.SetSync(bz("a/1"), bz("value_1"))
  120. db.SetSync(bz("a/3"), bz("value_3"))
  121. itr := IteratePrefix(db, []byte("a/"))
  122. itr.Seek(bz("a/2"))
  123. checkValid(t, itr, true)
  124. checkItem(t, itr, bz("a/3"), bz("value_3"))
  125. checkPrev(t, itr, true)
  126. checkItem(t, itr, bz("a/1"), bz("value_1"))
  127. // Bad!
  128. checkPrev(t, itr, false)
  129. // Once invalid...
  130. checkInvalid(t, itr)
  131. })
  132. }
  133. }
  134. // Search for a/3, fail by too much Next()
  135. func TestPrefixIteratorMatches3N(t *testing.T) {
  136. for backend, _ := range backends {
  137. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  138. db := newTempDB(t, backend)
  139. db.SetSync(bz("a/1"), bz("value_1"))
  140. db.SetSync(bz("a/3"), bz("value_3"))
  141. itr := IteratePrefix(db, []byte("a/"))
  142. itr.Seek(bz("a/3"))
  143. checkValid(t, itr, true)
  144. checkItem(t, itr, bz("a/3"), bz("value_3"))
  145. checkPrev(t, itr, true)
  146. checkItem(t, itr, bz("a/1"), bz("value_1"))
  147. checkNext(t, itr, true)
  148. checkItem(t, itr, bz("a/3"), bz("value_3"))
  149. // Bad!
  150. checkNext(t, itr, false)
  151. // Once invalid...
  152. checkInvalid(t, itr)
  153. })
  154. }
  155. }
  156. // Search for a/3, fail by too much Prev()
  157. func TestPrefixIteratorMatches3P(t *testing.T) {
  158. for backend, _ := range backends {
  159. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  160. db := newTempDB(t, backend)
  161. db.SetSync(bz("a/1"), bz("value_1"))
  162. db.SetSync(bz("a/3"), bz("value_3"))
  163. itr := IteratePrefix(db, []byte("a/"))
  164. itr.Seek(bz("a/3"))
  165. checkValid(t, itr, true)
  166. checkItem(t, itr, bz("a/3"), bz("value_3"))
  167. checkPrev(t, itr, true)
  168. checkItem(t, itr, bz("a/1"), bz("value_1"))
  169. // Bad!
  170. checkPrev(t, itr, false)
  171. // Once invalid...
  172. checkInvalid(t, itr)
  173. })
  174. }
  175. }