Skip to content

Commit

Permalink
*: remove MemBuffer cache and Release method. (pingcap#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Jul 8, 2016
1 parent 34380ce commit 8edbf55
Show file tree
Hide file tree
Showing 18 changed files with 37 additions and 169 deletions.
2 changes: 0 additions & 2 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,6 @@ func (d *ddl) getSnapshotRows(t table.Table, version uint64, seekHandle int64) (
return nil, errors.Trace(err)
}

defer snap.Release()

firstKey := t.RecordKey(seekHandle)

it, err := snap.Seek(firstKey)
Expand Down
1 change: 0 additions & 1 deletion inspectkv/inspectkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ func ScanSnapshotTableRecord(store kv.Storage, ver kv.Version, t table.Table, st
if err != nil {
return nil, 0, errors.Trace(err)
}
defer snap.Release()

records, nextHandle, err := ScanTableRecord(snap, t, startHandle, limit)

Expand Down
1 change: 0 additions & 1 deletion kv/buffer_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
// BufferStore wraps a Retriever for read and a MemBuffer for buffered write.
// Common usage pattern:
// bs := NewBufferStore(r) // use BufferStore to wrap a Retriever
// defer bs.Release() // make sure it will be released
// // ...
// // read/write on bs
// // ...
Expand Down
58 changes: 0 additions & 58 deletions kv/bufpool.go

This file was deleted.

10 changes: 2 additions & 8 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,8 @@ type RetrieverMutator interface {
Mutator
}

// MemBuffer is an in-memory kv collection. It should be released after use.
type MemBuffer interface {
RetrieverMutator
// Release releases the buffer.
Release()
}
// MemBuffer is an in-memory kv collection, can be used to buffer write operations.
type MemBuffer RetrieverMutator

// Transaction defines the interface for operations inside a Transaction.
// This is not thread safe.
Expand Down Expand Up @@ -156,8 +152,6 @@ type Snapshot interface {
Retriever
// BatchGet gets a batch of values from snapshot.
BatchGet(keys []Key) (map[string][]byte, error)
// Release releases the snapshot to store.
Release()
}

// Driver is the interface that must be implemented by a KV storage.
Expand Down
27 changes: 8 additions & 19 deletions kv/mem_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ func (s *testKVSuite) SetUpSuite(c *C) {
s.bs[1] = NewMemDbBuffer()
}

func (s *testKVSuite) TearDownSuite(c *C) {
for _, buffer := range s.bs {
buffer.Release()
}
func (s *testKVSuite) ResetMembuffers() {
s.bs[0] = NewRBTreeBuffer()
s.bs[1] = NewMemDbBuffer()
}

func insertData(c *C, buffer MemBuffer) {
Expand Down Expand Up @@ -142,8 +141,8 @@ func (s *testKVSuite) TestGetSet(c *C) {
for _, buffer := range s.bs {
insertData(c, buffer)
mustGet(c, buffer)
buffer.Release()
}
s.ResetMembuffers()
}

func (s *testKVSuite) TestNewIterator(c *C) {
Expand All @@ -156,8 +155,8 @@ func (s *testKVSuite) TestNewIterator(c *C) {

insertData(c, buffer)
checkNewIterator(c, buffer)
buffer.Release()
}
s.ResetMembuffers()
}

func (s *testKVSuite) TestBasicNewIterator(c *C) {
Expand All @@ -166,7 +165,6 @@ func (s *testKVSuite) TestBasicNewIterator(c *C) {
it, err := buffer.Seek([]byte("2"))
c.Assert(err, IsNil)
c.Assert(it.Valid(), IsFalse)
buffer.Release()
}
}

Expand Down Expand Up @@ -200,9 +198,8 @@ func (s *testKVSuite) TestNewIteratorMin(c *C) {
it, err = buffer.Seek([]byte("DATA_test_main_db_tbl_tbl_test_record__00000000000000000000"))
c.Assert(err, IsNil)
c.Assert(string(it.Key()), Equals, "DATA_test_main_db_tbl_tbl_test_record__00000000000000000001")

buffer.Release()
}
s.ResetMembuffers()
}

var opCnt = 100000
Expand All @@ -214,7 +211,6 @@ func BenchmarkRBTreeBufferSequential(b *testing.B) {
}
buffer := NewRBTreeBuffer()
benchmarkSetGet(b, buffer, data)
buffer.Release()
b.ReportAllocs()
}

Expand All @@ -226,7 +222,6 @@ func BenchmarkRBTreeBufferRandom(b *testing.B) {
shuffle(data)
buffer := NewRBTreeBuffer()
benchmarkSetGet(b, buffer, data)
buffer.Release()
b.ReportAllocs()
}

Expand All @@ -237,7 +232,6 @@ func BenchmarkMemDbBufferSequential(b *testing.B) {
}
buffer := NewMemDbBuffer()
benchmarkSetGet(b, buffer, data)
buffer.Release()
b.ReportAllocs()
}

Expand All @@ -249,36 +243,31 @@ func BenchmarkMemDbBufferRandom(b *testing.B) {
shuffle(data)
buffer := NewMemDbBuffer()
benchmarkSetGet(b, buffer, data)
buffer.Release()
b.ReportAllocs()
}

func BenchmarkRBTreeIter(b *testing.B) {
buffer := NewRBTreeBuffer()
benchIterator(b, buffer)
buffer.Release()
b.ReportAllocs()
}

func BenchmarkMemDbIter(b *testing.B) {
buffer := NewMemDbBuffer()
benchIterator(b, buffer)
buffer.Release()
b.ReportAllocs()
}

func BenchmarkRBTreeCreation(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewRBTreeBuffer()
buffer.Release()
NewRBTreeBuffer()
}
b.ReportAllocs()
}

func BenchmarkMemDbCreation(b *testing.B) {
for i := 0; i < b.N; i++ {
buffer := NewMemDbBuffer()
buffer.Release()
NewMemDbBuffer()
}
b.ReportAllocs()
}
Expand Down
5 changes: 0 additions & 5 deletions kv/memdb_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ func (m *memDbBuffer) Delete(k Key) error {
return errors.Trace(err)
}

// Release reset the buffer.
func (m *memDbBuffer) Release() {
m.db.Reset()
}

// Next implements the Iterator Next.
func (i *memDbIter) Next() error {
if i.reverse {
Expand Down
5 changes: 0 additions & 5 deletions kv/rbtree_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ func (m *rbTreeBuffer) Delete(k Key) error {
return nil
}

// Release reset the buffer.
func (m *rbTreeBuffer) Release() {
m.tree = llrb.New()
}

// Next implements the Iterator Next.
func (i *rbTreeIter) Next() error {
i.pair = nil
Expand Down
54 changes: 26 additions & 28 deletions kv/union_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ type Options interface {
Get(opt Option) (v interface{}, ok bool)
}

var (
p = newCache("memdb pool", 100, func() MemBuffer {
return NewMemDbBuffer()
})
)

// conditionPair is used to store lazy check condition.
// If condition not match (value is not equal as expected one), returns err.
type conditionPair struct {
Expand Down Expand Up @@ -77,6 +71,28 @@ func NewUnionStore(snapshot Snapshot) UnionStore {
}
}

// invalidIterator implements Iterator interface.
// It is used for read-only transaction which has no data written, the iterator is always invalid.
type invalidIterator struct{}

func (it invalidIterator) Valid() bool {
return false
}

func (it invalidIterator) Next() error {
return nil
}

func (it invalidIterator) Key() Key {
return nil
}

func (it invalidIterator) Value() []byte {
return nil
}

func (it invalidIterator) Close() {}

type lazyMemBuffer struct {
mb MemBuffer
}
Expand All @@ -91,46 +107,34 @@ func (lmb *lazyMemBuffer) Get(k Key) ([]byte, error) {

func (lmb *lazyMemBuffer) Set(key Key, value []byte) error {
if lmb.mb == nil {
lmb.mb = p.get()
lmb.mb = NewMemDbBuffer()
}

return lmb.mb.Set(key, value)
}

func (lmb *lazyMemBuffer) Delete(k Key) error {
if lmb.mb == nil {
lmb.mb = p.get()
lmb.mb = NewMemDbBuffer()
}

return lmb.mb.Delete(k)
}

func (lmb *lazyMemBuffer) Seek(k Key) (Iterator, error) {
if lmb.mb == nil {
lmb.mb = p.get()
return invalidIterator{}, nil
}

return lmb.mb.Seek(k)
}

func (lmb *lazyMemBuffer) SeekReverse(k Key) (Iterator, error) {
if lmb.mb == nil {
lmb.mb = p.get()
return invalidIterator{}, nil
}
return lmb.mb.SeekReverse(k)
}

func (lmb *lazyMemBuffer) Release() {
if lmb.mb == nil {
return
}

lmb.mb.Release()

p.put(lmb.mb)
lmb.mb = nil
}

// Get implements the Retriever interface.
func (us *unionStore) Get(k Key) ([]byte, error) {
v, err := us.MemBuffer.Get(k)
Expand Down Expand Up @@ -205,12 +209,6 @@ func (us *unionStore) DelOption(opt Option) {
delete(us.opts, opt)
}

// Release implements the UnionStore Release interface.
func (us *unionStore) Release() {
us.snapshot.Release()
us.BufferStore.Release()
}

type options map[Option]interface{}

func (opts options) Get(opt Option) (interface{}, bool) {
Expand Down
8 changes: 0 additions & 8 deletions kv/union_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ func (s *testUnionStoreSuite) SetUpTest(c *C) {
s.us = NewUnionStore(&mockSnapshot{s.store})
}

func (s *testUnionStoreSuite) TearDownTest(c *C) {
s.us.Release()
}

func (s *testUnionStoreSuite) TestGetSet(c *C) {
defer testleak.AfterTest(c)()
s.store.Set([]byte("1"), []byte("1"))
Expand Down Expand Up @@ -173,7 +169,3 @@ func (s *mockSnapshot) Seek(k Key) (Iterator, error) {
func (s *mockSnapshot) SeekReverse(k Key) (Iterator, error) {
return s.store.SeekReverse(k)
}

func (s *mockSnapshot) Release() {
s.store.Release()
}
1 change: 0 additions & 1 deletion store/localstore/mvcc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func (t *testMvccSuite) TestSnapshotGet(c *C) {
testKey := encodeTestDataKey(1)

snapshot, err := t.s.GetSnapshot(kv.MaxVersion)
defer snapshot.Release()
b, err = snapshot.Get(testKey)
c.Assert(err, IsNil)
c.Assert(string(b), Equals, "new")
Expand Down
3 changes: 0 additions & 3 deletions store/localstore/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ func (s *dbSnapshot) SeekReverse(k kv.Key) (kv.Iterator, error) {
return it, errors.Trace(err)
}

func (s *dbSnapshot) Release() {
}

type dbIter struct {
s *dbSnapshot
valid bool
Expand Down
1 change: 0 additions & 1 deletion store/localstore/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func (txn *dbTxn) Commit() error {
}

func (txn *dbTxn) close() error {
txn.us.Release()
txn.lockedKeys = nil
txn.valid = false
return nil
Expand Down
Loading

0 comments on commit 8edbf55

Please sign in to comment.