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.

133 lines
3.6 KiB

  1. // Copyright (c) 2012, 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 leveldb
  7. import (
  8. "bytes"
  9. "testing"
  10. "github.com/syndtr/goleveldb/leveldb/comparer"
  11. )
  12. var defaultIComparer = &iComparer{comparer.DefaultComparer}
  13. func ikey(key string, seq uint64, kt keyType) internalKey {
  14. return makeInternalKey(nil, []byte(key), uint64(seq), kt)
  15. }
  16. func shortSep(a, b []byte) []byte {
  17. dst := make([]byte, len(a))
  18. dst = defaultIComparer.Separator(dst[:0], a, b)
  19. if dst == nil {
  20. return a
  21. }
  22. return dst
  23. }
  24. func shortSuccessor(b []byte) []byte {
  25. dst := make([]byte, len(b))
  26. dst = defaultIComparer.Successor(dst[:0], b)
  27. if dst == nil {
  28. return b
  29. }
  30. return dst
  31. }
  32. func testSingleKey(t *testing.T, key string, seq uint64, kt keyType) {
  33. ik := ikey(key, seq, kt)
  34. if !bytes.Equal(ik.ukey(), []byte(key)) {
  35. t.Errorf("user key does not equal, got %v, want %v", string(ik.ukey()), key)
  36. }
  37. rseq, rt := ik.parseNum()
  38. if rseq != seq {
  39. t.Errorf("seq number does not equal, got %v, want %v", rseq, seq)
  40. }
  41. if rt != kt {
  42. t.Errorf("type does not equal, got %v, want %v", rt, kt)
  43. }
  44. if rukey, rseq, rt, kerr := parseInternalKey(ik); kerr == nil {
  45. if !bytes.Equal(rukey, []byte(key)) {
  46. t.Errorf("user key does not equal, got %v, want %v", string(ik.ukey()), key)
  47. }
  48. if rseq != seq {
  49. t.Errorf("seq number does not equal, got %v, want %v", rseq, seq)
  50. }
  51. if rt != kt {
  52. t.Errorf("type does not equal, got %v, want %v", rt, kt)
  53. }
  54. } else {
  55. t.Errorf("key error: %v", kerr)
  56. }
  57. }
  58. func TestInternalKey_EncodeDecode(t *testing.T) {
  59. keys := []string{"", "k", "hello", "longggggggggggggggggggggg"}
  60. seqs := []uint64{
  61. 1, 2, 3,
  62. (1 << 8) - 1, 1 << 8, (1 << 8) + 1,
  63. (1 << 16) - 1, 1 << 16, (1 << 16) + 1,
  64. (1 << 32) - 1, 1 << 32, (1 << 32) + 1,
  65. }
  66. for _, key := range keys {
  67. for _, seq := range seqs {
  68. testSingleKey(t, key, seq, keyTypeVal)
  69. testSingleKey(t, "hello", 1, keyTypeDel)
  70. }
  71. }
  72. }
  73. func assertBytes(t *testing.T, want, got []byte) {
  74. if !bytes.Equal(got, want) {
  75. t.Errorf("assert failed, got %v, want %v", got, want)
  76. }
  77. }
  78. func TestInternalKeyShortSeparator(t *testing.T) {
  79. // When user keys are same
  80. assertBytes(t, ikey("foo", 100, keyTypeVal),
  81. shortSep(ikey("foo", 100, keyTypeVal),
  82. ikey("foo", 99, keyTypeVal)))
  83. assertBytes(t, ikey("foo", 100, keyTypeVal),
  84. shortSep(ikey("foo", 100, keyTypeVal),
  85. ikey("foo", 101, keyTypeVal)))
  86. assertBytes(t, ikey("foo", 100, keyTypeVal),
  87. shortSep(ikey("foo", 100, keyTypeVal),
  88. ikey("foo", 100, keyTypeVal)))
  89. assertBytes(t, ikey("foo", 100, keyTypeVal),
  90. shortSep(ikey("foo", 100, keyTypeVal),
  91. ikey("foo", 100, keyTypeDel)))
  92. // When user keys are misordered
  93. assertBytes(t, ikey("foo", 100, keyTypeVal),
  94. shortSep(ikey("foo", 100, keyTypeVal),
  95. ikey("bar", 99, keyTypeVal)))
  96. // When user keys are different, but correctly ordered
  97. assertBytes(t, ikey("g", uint64(keyMaxSeq), keyTypeSeek),
  98. shortSep(ikey("foo", 100, keyTypeVal),
  99. ikey("hello", 200, keyTypeVal)))
  100. // When start user key is prefix of limit user key
  101. assertBytes(t, ikey("foo", 100, keyTypeVal),
  102. shortSep(ikey("foo", 100, keyTypeVal),
  103. ikey("foobar", 200, keyTypeVal)))
  104. // When limit user key is prefix of start user key
  105. assertBytes(t, ikey("foobar", 100, keyTypeVal),
  106. shortSep(ikey("foobar", 100, keyTypeVal),
  107. ikey("foo", 200, keyTypeVal)))
  108. }
  109. func TestInternalKeyShortestSuccessor(t *testing.T) {
  110. assertBytes(t, ikey("g", uint64(keyMaxSeq), keyTypeSeek),
  111. shortSuccessor(ikey("foo", 100, keyTypeVal)))
  112. assertBytes(t, ikey("\xff\xff", 100, keyTypeVal),
  113. shortSuccessor(ikey("\xff\xff", 100, keyTypeVal)))
  114. }