...

Source file src/github.com/syndtr/goleveldb/leveldb/memdb/memdb_test.go

Documentation: github.com/syndtr/goleveldb/leveldb/memdb

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

View as plain text