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.

135 lines
3.7 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 memdb
  7. import (
  8. . "github.com/onsi/ginkgo"
  9. . "github.com/onsi/gomega"
  10. "github.com/syndtr/goleveldb/leveldb/comparer"
  11. "github.com/syndtr/goleveldb/leveldb/iterator"
  12. "github.com/syndtr/goleveldb/leveldb/testutil"
  13. "github.com/syndtr/goleveldb/leveldb/util"
  14. )
  15. func (p *DB) TestFindLT(key []byte) (rkey, value []byte, err error) {
  16. p.mu.RLock()
  17. if node := p.findLT(key); node != 0 {
  18. n := p.nodeData[node]
  19. m := n + p.nodeData[node+nKey]
  20. rkey = p.kvData[n:m]
  21. value = p.kvData[m : m+p.nodeData[node+nVal]]
  22. } else {
  23. err = ErrNotFound
  24. }
  25. p.mu.RUnlock()
  26. return
  27. }
  28. func (p *DB) TestFindLast() (rkey, value []byte, err error) {
  29. p.mu.RLock()
  30. if node := p.findLast(); node != 0 {
  31. n := p.nodeData[node]
  32. m := n + p.nodeData[node+nKey]
  33. rkey = p.kvData[n:m]
  34. value = p.kvData[m : m+p.nodeData[node+nVal]]
  35. } else {
  36. err = ErrNotFound
  37. }
  38. p.mu.RUnlock()
  39. return
  40. }
  41. func (p *DB) TestPut(key []byte, value []byte) error {
  42. p.Put(key, value)
  43. return nil
  44. }
  45. func (p *DB) TestDelete(key []byte) error {
  46. p.Delete(key)
  47. return nil
  48. }
  49. func (p *DB) TestFind(key []byte) (rkey, rvalue []byte, err error) {
  50. return p.Find(key)
  51. }
  52. func (p *DB) TestGet(key []byte) (value []byte, err error) {
  53. return p.Get(key)
  54. }
  55. func (p *DB) TestNewIterator(slice *util.Range) iterator.Iterator {
  56. return p.NewIterator(slice)
  57. }
  58. var _ = testutil.Defer(func() {
  59. Describe("Memdb", func() {
  60. Describe("write test", func() {
  61. It("should do write correctly", func() {
  62. db := New(comparer.DefaultComparer, 0)
  63. t := testutil.DBTesting{
  64. DB: db,
  65. Deleted: testutil.KeyValue_Generate(nil, 1000, 1, 1, 30, 5, 5).Clone(),
  66. PostFn: func(t *testutil.DBTesting) {
  67. Expect(db.Len()).Should(Equal(t.Present.Len()))
  68. Expect(db.Size()).Should(Equal(t.Present.Size()))
  69. switch t.Act {
  70. case testutil.DBPut, testutil.DBOverwrite:
  71. Expect(db.Contains(t.ActKey)).Should(BeTrue())
  72. default:
  73. Expect(db.Contains(t.ActKey)).Should(BeFalse())
  74. }
  75. },
  76. }
  77. testutil.DoDBTesting(&t)
  78. })
  79. })
  80. Describe("read test", func() {
  81. testutil.AllKeyValueTesting(nil, func(kv testutil.KeyValue) testutil.DB {
  82. // Building the DB.
  83. db := New(comparer.DefaultComparer, 0)
  84. kv.IterateShuffled(nil, func(i int, key, value []byte) {
  85. db.Put(key, value)
  86. })
  87. if kv.Len() > 1 {
  88. It("Should find correct keys with findLT", func() {
  89. testutil.ShuffledIndex(nil, kv.Len()-1, 1, func(i int) {
  90. key_, key, _ := kv.IndexInexact(i + 1)
  91. expectedKey, expectedValue := kv.Index(i)
  92. // Using key that exist.
  93. rkey, rvalue, err := db.TestFindLT(key)
  94. Expect(err).ShouldNot(HaveOccurred(), "Error for key %q -> %q", key, expectedKey)
  95. Expect(rkey).Should(Equal(expectedKey), "Key")
  96. Expect(rvalue).Should(Equal(expectedValue), "Value for key %q -> %q", key, expectedKey)
  97. // Using key that doesn't exist.
  98. rkey, rvalue, err = db.TestFindLT(key_)
  99. Expect(err).ShouldNot(HaveOccurred(), "Error for key %q (%q) -> %q", key_, key, expectedKey)
  100. Expect(rkey).Should(Equal(expectedKey))
  101. Expect(rvalue).Should(Equal(expectedValue), "Value for key %q (%q) -> %q", key_, key, expectedKey)
  102. })
  103. })
  104. }
  105. if kv.Len() > 0 {
  106. It("Should find last key with findLast", func() {
  107. key, value := kv.Index(kv.Len() - 1)
  108. rkey, rvalue, err := db.TestFindLast()
  109. Expect(err).ShouldNot(HaveOccurred())
  110. Expect(rkey).Should(Equal(key))
  111. Expect(rvalue).Should(Equal(value))
  112. })
  113. }
  114. return db
  115. }, nil, nil)
  116. })
  117. })
  118. })