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.

352 lines
7.9 KiB

  1. // Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. package testutil
  7. import (
  8. "fmt"
  9. "math/rand"
  10. "sort"
  11. "strings"
  12. "github.com/syndtr/goleveldb/leveldb/util"
  13. )
  14. type KeyValueEntry struct {
  15. key, value []byte
  16. }
  17. type KeyValue struct {
  18. entries []KeyValueEntry
  19. nbytes int
  20. }
  21. func (kv *KeyValue) Put(key, value []byte) {
  22. if n := len(kv.entries); n > 0 && cmp.Compare(kv.entries[n-1].key, key) >= 0 {
  23. panic(fmt.Sprintf("Put: keys are not in increasing order: %q, %q", kv.entries[n-1].key, key))
  24. }
  25. kv.entries = append(kv.entries, KeyValueEntry{key, value})
  26. kv.nbytes += len(key) + len(value)
  27. }
  28. func (kv *KeyValue) PutString(key, value string) {
  29. kv.Put([]byte(key), []byte(value))
  30. }
  31. func (kv *KeyValue) PutU(key, value []byte) bool {
  32. if i, exist := kv.Get(key); !exist {
  33. if i < kv.Len() {
  34. kv.entries = append(kv.entries[:i+1], kv.entries[i:]...)
  35. kv.entries[i] = KeyValueEntry{key, value}
  36. } else {
  37. kv.entries = append(kv.entries, KeyValueEntry{key, value})
  38. }
  39. kv.nbytes += len(key) + len(value)
  40. return true
  41. } else {
  42. kv.nbytes += len(value) - len(kv.ValueAt(i))
  43. kv.entries[i].value = value
  44. }
  45. return false
  46. }
  47. func (kv *KeyValue) PutUString(key, value string) bool {
  48. return kv.PutU([]byte(key), []byte(value))
  49. }
  50. func (kv *KeyValue) Delete(key []byte) (exist bool, value []byte) {
  51. i, exist := kv.Get(key)
  52. if exist {
  53. value = kv.entries[i].value
  54. kv.DeleteIndex(i)
  55. }
  56. return
  57. }
  58. func (kv *KeyValue) DeleteIndex(i int) bool {
  59. if i < kv.Len() {
  60. kv.nbytes -= len(kv.KeyAt(i)) + len(kv.ValueAt(i))
  61. kv.entries = append(kv.entries[:i], kv.entries[i+1:]...)
  62. return true
  63. }
  64. return false
  65. }
  66. func (kv KeyValue) Len() int {
  67. return len(kv.entries)
  68. }
  69. func (kv *KeyValue) Size() int {
  70. return kv.nbytes
  71. }
  72. func (kv KeyValue) KeyAt(i int) []byte {
  73. return kv.entries[i].key
  74. }
  75. func (kv KeyValue) ValueAt(i int) []byte {
  76. return kv.entries[i].value
  77. }
  78. func (kv KeyValue) Index(i int) (key, value []byte) {
  79. if i < 0 || i >= len(kv.entries) {
  80. panic(fmt.Sprintf("Index #%d: out of range", i))
  81. }
  82. return kv.entries[i].key, kv.entries[i].value
  83. }
  84. func (kv KeyValue) IndexInexact(i int) (key_, key, value []byte) {
  85. key, value = kv.Index(i)
  86. var key0 []byte
  87. var key1 = kv.KeyAt(i)
  88. if i > 0 {
  89. key0 = kv.KeyAt(i - 1)
  90. }
  91. key_ = BytesSeparator(key0, key1)
  92. return
  93. }
  94. func (kv KeyValue) IndexOrNil(i int) (key, value []byte) {
  95. if i >= 0 && i < len(kv.entries) {
  96. return kv.entries[i].key, kv.entries[i].value
  97. }
  98. return nil, nil
  99. }
  100. func (kv KeyValue) IndexString(i int) (key, value string) {
  101. key_, _value := kv.Index(i)
  102. return string(key_), string(_value)
  103. }
  104. func (kv KeyValue) Search(key []byte) int {
  105. return sort.Search(kv.Len(), func(i int) bool {
  106. return cmp.Compare(kv.KeyAt(i), key) >= 0
  107. })
  108. }
  109. func (kv KeyValue) SearchString(key string) int {
  110. return kv.Search([]byte(key))
  111. }
  112. func (kv KeyValue) Get(key []byte) (i int, exist bool) {
  113. i = kv.Search(key)
  114. if i < kv.Len() && cmp.Compare(kv.KeyAt(i), key) == 0 {
  115. exist = true
  116. }
  117. return
  118. }
  119. func (kv KeyValue) GetString(key string) (i int, exist bool) {
  120. return kv.Get([]byte(key))
  121. }
  122. func (kv KeyValue) Iterate(fn func(i int, key, value []byte)) {
  123. for i, x := range kv.entries {
  124. fn(i, x.key, x.value)
  125. }
  126. }
  127. func (kv KeyValue) IterateString(fn func(i int, key, value string)) {
  128. kv.Iterate(func(i int, key, value []byte) {
  129. fn(i, string(key), string(value))
  130. })
  131. }
  132. func (kv KeyValue) IterateShuffled(rnd *rand.Rand, fn func(i int, key, value []byte)) {
  133. ShuffledIndex(rnd, kv.Len(), 1, func(i int) {
  134. fn(i, kv.entries[i].key, kv.entries[i].value)
  135. })
  136. }
  137. func (kv KeyValue) IterateShuffledString(rnd *rand.Rand, fn func(i int, key, value string)) {
  138. kv.IterateShuffled(rnd, func(i int, key, value []byte) {
  139. fn(i, string(key), string(value))
  140. })
  141. }
  142. func (kv KeyValue) IterateInexact(fn func(i int, key_, key, value []byte)) {
  143. for i := range kv.entries {
  144. key_, key, value := kv.IndexInexact(i)
  145. fn(i, key_, key, value)
  146. }
  147. }
  148. func (kv KeyValue) IterateInexactString(fn func(i int, key_, key, value string)) {
  149. kv.IterateInexact(func(i int, key_, key, value []byte) {
  150. fn(i, string(key_), string(key), string(value))
  151. })
  152. }
  153. func (kv KeyValue) Clone() KeyValue {
  154. return KeyValue{append([]KeyValueEntry{}, kv.entries...), kv.nbytes}
  155. }
  156. func (kv KeyValue) Slice(start, limit int) KeyValue {
  157. if start < 0 || limit > kv.Len() {
  158. panic(fmt.Sprintf("Slice %d .. %d: out of range", start, limit))
  159. } else if limit < start {
  160. panic(fmt.Sprintf("Slice %d .. %d: invalid range", start, limit))
  161. }
  162. return KeyValue{append([]KeyValueEntry{}, kv.entries[start:limit]...), kv.nbytes}
  163. }
  164. func (kv KeyValue) SliceKey(start, limit []byte) KeyValue {
  165. start_ := 0
  166. limit_ := kv.Len()
  167. if start != nil {
  168. start_ = kv.Search(start)
  169. }
  170. if limit != nil {
  171. limit_ = kv.Search(limit)
  172. }
  173. return kv.Slice(start_, limit_)
  174. }
  175. func (kv KeyValue) SliceKeyString(start, limit string) KeyValue {
  176. return kv.SliceKey([]byte(start), []byte(limit))
  177. }
  178. func (kv KeyValue) SliceRange(r *util.Range) KeyValue {
  179. if r != nil {
  180. return kv.SliceKey(r.Start, r.Limit)
  181. }
  182. return kv.Clone()
  183. }
  184. func (kv KeyValue) Range(start, limit int) (r util.Range) {
  185. if kv.Len() > 0 {
  186. if start == kv.Len() {
  187. r.Start = BytesAfter(kv.KeyAt(start - 1))
  188. } else {
  189. r.Start = kv.KeyAt(start)
  190. }
  191. }
  192. if limit < kv.Len() {
  193. r.Limit = kv.KeyAt(limit)
  194. }
  195. return
  196. }
  197. func KeyValue_EmptyKey() *KeyValue {
  198. kv := &KeyValue{}
  199. kv.PutString("", "v")
  200. return kv
  201. }
  202. func KeyValue_EmptyValue() *KeyValue {
  203. kv := &KeyValue{}
  204. kv.PutString("abc", "")
  205. kv.PutString("abcd", "")
  206. return kv
  207. }
  208. func KeyValue_OneKeyValue() *KeyValue {
  209. kv := &KeyValue{}
  210. kv.PutString("abc", "v")
  211. return kv
  212. }
  213. func KeyValue_BigValue() *KeyValue {
  214. kv := &KeyValue{}
  215. kv.PutString("big1", strings.Repeat("1", 200000))
  216. return kv
  217. }
  218. func KeyValue_SpecialKey() *KeyValue {
  219. kv := &KeyValue{}
  220. kv.PutString("\xff\xff", "v3")
  221. return kv
  222. }
  223. func KeyValue_MultipleKeyValue() *KeyValue {
  224. kv := &KeyValue{}
  225. kv.PutString("a", "v")
  226. kv.PutString("aa", "v1")
  227. kv.PutString("aaa", "v2")
  228. kv.PutString("aaacccccccccc", "v2")
  229. kv.PutString("aaaccccccccccd", "v3")
  230. kv.PutString("aaaccccccccccf", "v4")
  231. kv.PutString("aaaccccccccccfg", "v5")
  232. kv.PutString("ab", "v6")
  233. kv.PutString("abc", "v7")
  234. kv.PutString("abcd", "v8")
  235. kv.PutString("accccccccccccccc", "v9")
  236. kv.PutString("b", "v10")
  237. kv.PutString("bb", "v11")
  238. kv.PutString("bc", "v12")
  239. kv.PutString("c", "v13")
  240. kv.PutString("c1", "v13")
  241. kv.PutString("czzzzzzzzzzzzzz", "v14")
  242. kv.PutString("fffffffffffffff", "v15")
  243. kv.PutString("g11", "v15")
  244. kv.PutString("g111", "v15")
  245. kv.PutString("g111\xff", "v15")
  246. kv.PutString("zz", "v16")
  247. kv.PutString("zzzzzzz", "v16")
  248. kv.PutString("zzzzzzzzzzzzzzzz", "v16")
  249. return kv
  250. }
  251. var keymap = []byte("012345678ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxy")
  252. func KeyValue_Generate(rnd *rand.Rand, n, incr, minlen, maxlen, vminlen, vmaxlen int) *KeyValue {
  253. if rnd == nil {
  254. rnd = NewRand()
  255. }
  256. if maxlen < minlen {
  257. panic("max len should >= min len")
  258. }
  259. rrand := func(min, max int) int {
  260. if min == max {
  261. return max
  262. }
  263. return rnd.Intn(max-min) + min
  264. }
  265. kv := &KeyValue{}
  266. endC := byte(len(keymap) - incr)
  267. gen := make([]byte, 0, maxlen)
  268. for i := 0; i < n; i++ {
  269. m := rrand(minlen, maxlen)
  270. last := gen
  271. retry:
  272. gen = last[:m]
  273. if k := len(last); m > k {
  274. for j := k; j < m; j++ {
  275. gen[j] = 0
  276. }
  277. } else {
  278. for j := m - 1; j >= 0; j-- {
  279. c := last[j]
  280. if c == endC {
  281. continue
  282. }
  283. gen[j] = c + byte(incr)
  284. for j++; j < m; j++ {
  285. gen[j] = 0
  286. }
  287. goto ok
  288. }
  289. if m < maxlen {
  290. m++
  291. goto retry
  292. }
  293. panic(fmt.Sprintf("only able to generate %d keys out of %d keys, try increasing max len", kv.Len(), n))
  294. ok:
  295. }
  296. key := make([]byte, m)
  297. for j := 0; j < m; j++ {
  298. key[j] = keymap[gen[j]]
  299. }
  300. value := make([]byte, rrand(vminlen, vmaxlen))
  301. for n := copy(value, []byte(fmt.Sprintf("v%d", i))); n < len(value); n++ {
  302. value[n] = 'x'
  303. }
  304. kv.Put(key, value)
  305. }
  306. return kv
  307. }