Skip to content

Commit

Permalink
Merge pull request pingcap#799 from pingcap/disksing/remove-cache-sna…
Browse files Browse the repository at this point in the history
…pshot

kv: remove cacheSnapshot
  • Loading branch information
ngaut committed Dec 25, 2015
2 parents 3b410a0 + a15502e commit a19ca03
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 320 deletions.
130 changes: 0 additions & 130 deletions kv/cache_snapshot.go

This file was deleted.

120 changes: 0 additions & 120 deletions kv/cache_snapshot_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions kv/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ type MemBuffer interface {
// This is not thread safe.
type Transaction interface {
RetrieverMutator
// BatchPrefetch fetches values from KV storage to cache for later use.
BatchPrefetch(keys []Key) error
// Commit commits the transaction operations to KV store.
Commit() error
// Rollback undoes the transaction operations to KV store.
Expand Down
48 changes: 34 additions & 14 deletions kv/union_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ type UnionStore interface {
// CheckLazyConditionPairs loads all lazy values from store then checks if all values are matched.
// Lazy condition pairs should be checked before transaction commit.
CheckLazyConditionPairs() error
// BatchPrefetch fetches values from KV storage to cache for later use.
BatchPrefetch(keys []Key) error
// WalkBuffer iterates all buffered kv pairs.
WalkBuffer(f func(k Key, v []byte) error) error
// SetOption sets an option with a value, when val is nil, uses the default
Expand Down Expand Up @@ -65,15 +63,11 @@ type unionStore struct {

// NewUnionStore builds a new UnionStore.
func NewUnionStore(snapshot Snapshot) UnionStore {
lazy := &lazyMemBuffer{}
opts := make(map[Option]interface{})
cacheSnapshot := NewCacheSnapshot(snapshot, lazy, options(opts))
bufferStore := NewBufferStore(cacheSnapshot)
return &unionStore{
BufferStore: bufferStore,
snapshot: cacheSnapshot,
lazyConditionPairs: lazy,
opts: opts,
BufferStore: NewBufferStore(snapshot),
snapshot: snapshot,
lazyConditionPairs: &lazyMemBuffer{},
opts: make(map[Option]interface{}),
}
}

Expand Down Expand Up @@ -124,10 +118,36 @@ func (lmb *lazyMemBuffer) Release() {
lmb.mb = nil
}

// BatchPrefetch implements the UnionStore interface.
func (us *unionStore) BatchPrefetch(keys []Key) error {
_, err := us.snapshot.BatchGet(keys)
return errors.Trace(err)
// Get implements the Retriever interface.
func (us *unionStore) Get(k Key) ([]byte, error) {
v, err := us.MemBuffer.Get(k)
if IsErrNotFound(err) {
if _, ok := us.opts.Get(PresumeKeyNotExists); ok {
err = us.markLazyConditionPair(k, nil)
if err != nil {
return nil, errors.Trace(err)
}
return nil, errors.Trace(ErrNotExist)
}
}
if IsErrNotFound(err) {
v, err = us.BufferStore.r.Get(k)
}
if err != nil {
return v, errors.Trace(err)
}
if len(v) == 0 {
return nil, errors.Trace(ErrNotExist)
}
return v, nil
}

// markLazyConditionPair marks a kv pair for later check.
func (us *unionStore) markLazyConditionPair(k Key, v []byte) error {
if len(v) == 0 {
return errors.Trace(us.lazyConditionPairs.Delete(k))
}
return errors.Trace(us.lazyConditionPairs.Set(k, v))
}

// CheckLazyConditionPairs implements the UnionStore interface.
Expand Down
32 changes: 32 additions & 0 deletions kv/union_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package kv

import (
"github.com/juju/errors"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/terror"
)
Expand Down Expand Up @@ -110,3 +111,34 @@ func checkIterator(c *C, iter Iterator, keys [][]byte, values [][]byte) {
}
c.Assert(iter.Valid(), IsFalse)
}

type mockSnapshot struct {
store MemBuffer
}

func (s *mockSnapshot) Get(k Key) ([]byte, error) {
return s.store.Get(k)
}

func (s *mockSnapshot) BatchGet(keys []Key) (map[string][]byte, error) {
m := make(map[string][]byte)
for _, k := range keys {
v, err := s.store.Get(k)
if IsErrNotFound(err) {
continue
}
if err != nil {
return nil, errors.Trace(err)
}
m[string(k)] = v
}
return m, nil
}

func (s *mockSnapshot) Seek(k Key) (Iterator, error) {
return s.store.Seek(k)
}

func (s *mockSnapshot) Release() {
s.store.Release()
}
Loading

0 comments on commit a19ca03

Please sign in to comment.