forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7cb89d5
commit 42cea84
Showing
31 changed files
with
1,456 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ | |
coverage.out | ||
.idea/ | ||
*.iml | ||
temp_parser_file | ||
y.output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2015 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kv | ||
|
||
import "github.com/juju/errors" | ||
|
||
var _ Snapshot = (*cacheSnapshot)(nil) | ||
|
||
// cacheSnapshot wraps a snapshot and supports cache for read. | ||
type cacheSnapshot struct { | ||
cache MemBuffer | ||
snapshot Snapshot | ||
} | ||
|
||
// NewCacheSnapshot creates a new snapshot with cache embedded. | ||
func NewCacheSnapshot(snapshot Snapshot) Snapshot { | ||
return &cacheSnapshot{ | ||
cache: p.Get().(MemBuffer), | ||
snapshot: snapshot, | ||
} | ||
} | ||
|
||
// Get gets value from snapshot and saves it in cache. | ||
func (c *cacheSnapshot) Get(k Key) ([]byte, error) { | ||
v, err := c.cache.Get(k) | ||
if IsErrNotFound(err) { | ||
v, err = c.snapshot.Get(k) | ||
if err == nil { | ||
err = c.cache.Set([]byte(k), v) | ||
} | ||
return v, errors.Trace(err) | ||
} | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return v, nil | ||
} | ||
|
||
// BatchGet gets a batch of values from snapshot and saves them in cache. | ||
func (c *cacheSnapshot) BatchGet(keys []Key) (map[string][]byte, error) { | ||
m := make(map[string][]byte) | ||
var missedKeys []Key | ||
for _, k := range keys { | ||
v, err := c.cache.Get(k) | ||
if IsErrNotFound(err) { | ||
missedKeys = append(missedKeys, k) | ||
continue | ||
} | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
if len(v) > 0 { | ||
m[string(k)] = v | ||
} | ||
// If len(v) == 0, it means that the key does not exist. | ||
} | ||
|
||
values, err := c.snapshot.BatchGet(missedKeys) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
for _, k := range missedKeys { | ||
ks := string(k) | ||
if v, ok := values[ks]; ok && len(v) > 0 { | ||
err = c.cache.Set(k, v) | ||
m[ks] = v | ||
} else { | ||
err = c.cache.Set(k, nil) | ||
} | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
} | ||
return m, nil | ||
} | ||
|
||
// RangeGet gets values from snapshot and saves them in cache. | ||
// The range should be [start, end] as Snapshot.RangeGet() indicated. | ||
func (c *cacheSnapshot) RangeGet(start, end Key, limit int) (map[string][]byte, error) { | ||
values, err := c.snapshot.RangeGet(start, end, limit) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
for k, v := range values { | ||
err = c.cache.Set([]byte(k), v) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
} | ||
return values, nil | ||
} | ||
|
||
// NewIterator creates an iterator of snapshot. | ||
func (c *cacheSnapshot) NewIterator(param interface{}) Iterator { | ||
return newUnionIter(c.cache.NewIterator(param), c.snapshot.NewIterator(param)) | ||
} | ||
|
||
// Release reset membuffer and release snapshot. | ||
func (c *cacheSnapshot) Release() { | ||
if c.cache != nil { | ||
c.cache.Release() | ||
c.cache = nil | ||
} | ||
if c.snapshot != nil { | ||
c.snapshot.Release() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2015 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package kv | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
. "github.com/pingcap/check" | ||
) | ||
|
||
var _ = Suite(&testCacheSnapshotSuite{}) | ||
|
||
type testCacheSnapshotSuite struct { | ||
store MemBuffer | ||
cache Snapshot | ||
} | ||
|
||
func (s *testCacheSnapshotSuite) SetUpTest(c *C) { | ||
s.store = NewMemDbBuffer() | ||
s.cache = NewCacheSnapshot(&mockSnapshot{s.store}) | ||
} | ||
|
||
func (s *testCacheSnapshotSuite) TearDownTest(c *C) { | ||
s.cache.Release() | ||
} | ||
|
||
func (s *testCacheSnapshotSuite) TestGet(c *C) { | ||
s.store.Set([]byte("1"), []byte("1")) | ||
s.store.Set([]byte("2"), []byte("2")) | ||
|
||
v, err := s.cache.Get([]byte("1")) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, BytesEquals, []byte("1")) | ||
|
||
s.store.Set([]byte("1"), []byte("3")) | ||
v, err = s.cache.Get([]byte("1")) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, BytesEquals, []byte("1")) | ||
|
||
s.store.Set([]byte("2"), []byte("4")) | ||
v, err = s.cache.Get([]byte("2")) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, BytesEquals, []byte("4")) | ||
} | ||
|
||
func (s *testCacheSnapshotSuite) TestBatchGet(c *C) { | ||
s.store.Set([]byte("1"), []byte("1")) | ||
s.store.Set([]byte("2"), []byte("2")) | ||
|
||
m, err := s.cache.BatchGet([]Key{[]byte("1"), []byte("2"), []byte("3")}) | ||
c.Assert(err, IsNil) | ||
c.Assert(m["1"], BytesEquals, []byte("1")) | ||
c.Assert(m["2"], BytesEquals, []byte("2")) | ||
_, exist := m["3"] | ||
c.Assert(exist, IsFalse) | ||
|
||
// result should be saved in cache | ||
s.store.Set([]byte("1"), []byte("4")) | ||
v, err := s.cache.Get([]byte("1")) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, BytesEquals, []byte("1")) | ||
|
||
// nil result should also be saved in cache | ||
s.store.Set([]byte("3"), []byte("3")) | ||
m, err = s.cache.BatchGet([]Key{[]byte("3")}) | ||
c.Assert(err, IsNil) | ||
_, exist = m["3"] | ||
c.Assert(exist, IsFalse) | ||
} | ||
|
||
func (s *testCacheSnapshotSuite) TestRangeGet(c *C) { | ||
s.store.Set([]byte("1"), []byte("1")) | ||
s.store.Set([]byte("2"), []byte("2")) | ||
s.store.Set([]byte("3"), []byte("3")) | ||
|
||
m, err := s.cache.RangeGet([]byte("1"), []byte("2"), 100) | ||
c.Assert(err, IsNil) | ||
c.Assert(m, HasLen, 2) | ||
c.Assert(m["1"], BytesEquals, []byte("1")) | ||
c.Assert(m["2"], BytesEquals, []byte("2")) | ||
|
||
// result should be saved in cache | ||
s.store.Set([]byte("1"), []byte("4")) | ||
v, err := s.cache.Get([]byte("1")) | ||
c.Assert(err, IsNil) | ||
c.Assert(v, BytesEquals, []byte("1")) | ||
} | ||
|
||
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) RangeGet(start, end Key, limit int) (map[string][]byte, error) { | ||
m := make(map[string][]byte) | ||
it := s.NewIterator([]byte(start)) | ||
defer it.Close() | ||
endKey := string(end) | ||
for i := 0; i < limit; i++ { | ||
if !it.Valid() { | ||
break | ||
} | ||
if it.Key() > endKey { | ||
break | ||
} | ||
m[string(it.Key())] = it.Value() | ||
err := it.Next() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
return m, nil | ||
} | ||
|
||
func (s *mockSnapshot) NewIterator(param interface{}) Iterator { | ||
return s.store.NewIterator(param) | ||
} | ||
|
||
func (s *mockSnapshot) Release() { | ||
s.store.Release() | ||
} |
Oops, something went wrong.