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.

181 lines
2.6 KiB

  1. package leveldb
  2. import (
  3. "encoding/binary"
  4. "reflect"
  5. "testing"
  6. "github.com/onsi/gomega"
  7. "github.com/syndtr/goleveldb/leveldb/testutil"
  8. )
  9. type testFileRec struct {
  10. level int
  11. num int64
  12. }
  13. func TestVersionStaging(t *testing.T) {
  14. gomega.RegisterTestingT(t)
  15. stor := testutil.NewStorage()
  16. defer stor.Close()
  17. s, err := newSession(stor, nil)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. v := newVersion(s)
  22. v.newStaging()
  23. tmp := make([]byte, 4)
  24. mik := func(i uint64) []byte {
  25. binary.BigEndian.PutUint32(tmp, uint32(i))
  26. return []byte(makeInternalKey(nil, tmp, 0, keyTypeVal))
  27. }
  28. for i, x := range []struct {
  29. add, del []testFileRec
  30. levels [][]int64
  31. }{
  32. {
  33. add: []testFileRec{
  34. {1, 1},
  35. },
  36. levels: [][]int64{
  37. {},
  38. {1},
  39. },
  40. },
  41. {
  42. add: []testFileRec{
  43. {1, 1},
  44. },
  45. levels: [][]int64{
  46. {},
  47. {1},
  48. },
  49. },
  50. {
  51. del: []testFileRec{
  52. {1, 1},
  53. },
  54. levels: [][]int64{},
  55. },
  56. {
  57. add: []testFileRec{
  58. {0, 1},
  59. {0, 3},
  60. {0, 2},
  61. {2, 5},
  62. {1, 4},
  63. },
  64. levels: [][]int64{
  65. {3, 2, 1},
  66. {4},
  67. {5},
  68. },
  69. },
  70. {
  71. add: []testFileRec{
  72. {1, 6},
  73. {2, 5},
  74. },
  75. del: []testFileRec{
  76. {0, 1},
  77. {0, 4},
  78. },
  79. levels: [][]int64{
  80. {3, 2},
  81. {4, 6},
  82. {5},
  83. },
  84. },
  85. {
  86. del: []testFileRec{
  87. {0, 3},
  88. {0, 2},
  89. {1, 4},
  90. {1, 6},
  91. {2, 5},
  92. },
  93. levels: [][]int64{},
  94. },
  95. {
  96. add: []testFileRec{
  97. {0, 1},
  98. },
  99. levels: [][]int64{
  100. {1},
  101. },
  102. },
  103. {
  104. add: []testFileRec{
  105. {1, 2},
  106. },
  107. levels: [][]int64{
  108. {1},
  109. {2},
  110. },
  111. },
  112. {
  113. add: []testFileRec{
  114. {0, 3},
  115. },
  116. levels: [][]int64{
  117. {3, 1},
  118. {2},
  119. },
  120. },
  121. {
  122. add: []testFileRec{
  123. {6, 9},
  124. },
  125. levels: [][]int64{
  126. {3, 1},
  127. {2},
  128. {},
  129. {},
  130. {},
  131. {},
  132. {9},
  133. },
  134. },
  135. {
  136. del: []testFileRec{
  137. {6, 9},
  138. },
  139. levels: [][]int64{
  140. {3, 1},
  141. {2},
  142. },
  143. },
  144. } {
  145. rec := &sessionRecord{}
  146. for _, f := range x.add {
  147. ik := mik(uint64(f.num))
  148. rec.addTable(f.level, f.num, 1, ik, ik)
  149. }
  150. for _, f := range x.del {
  151. rec.delTable(f.level, f.num)
  152. }
  153. vs := v.newStaging()
  154. vs.commit(rec)
  155. v = vs.finish()
  156. if len(v.levels) != len(x.levels) {
  157. t.Fatalf("#%d: invalid level count: want=%d got=%d", i, len(x.levels), len(v.levels))
  158. }
  159. for j, want := range x.levels {
  160. tables := v.levels[j]
  161. if len(want) != len(tables) {
  162. t.Fatalf("#%d.%d: invalid tables count: want=%d got=%d", i, j, len(want), len(tables))
  163. }
  164. got := make([]int64, len(tables))
  165. for k, t := range tables {
  166. got[k] = t.fd.Num
  167. }
  168. if !reflect.DeepEqual(want, got) {
  169. t.Fatalf("#%d.%d: invalid tables: want=%v got=%v", i, j, want, got)
  170. }
  171. }
  172. }
  173. }