-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from pingcap/c4pt0r/MVCC-support
first MVCC support
- Loading branch information
Showing
11 changed files
with
439 additions
and
68 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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: go | ||
|
||
go: | ||
- 1.4.2 | ||
- 1.5 | ||
|
||
script: make |
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
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,43 @@ | ||
package localstore | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/pingcap/tidb/kv" | ||
) | ||
|
||
// ErrOverflow is the error returned by CurrentVersion, it describes if | ||
// there're too many versions allocations in a very short period of time, ID | ||
// may conflict. | ||
var ErrOverflow = errors.New("overflow when allocating new version") | ||
|
||
// LocalVersioProvider uses local timestamp for version. | ||
type LocalVersioProvider struct { | ||
mu sync.Mutex | ||
lastTimeStampTs uint64 | ||
n uint64 | ||
} | ||
|
||
const ( | ||
timePrecisionOffset = 18 | ||
) | ||
|
||
// CurrentVersion implements the VersionProvider's GetCurrentVer interface. | ||
func (l *LocalVersioProvider) CurrentVersion() (kv.Version, error) { | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
var ts uint64 | ||
ts = uint64((time.Now().UnixNano() / int64(time.Millisecond)) << timePrecisionOffset) | ||
if l.lastTimeStampTs == uint64(ts) { | ||
l.n++ | ||
if l.n >= 1<<timePrecisionOffset { | ||
return kv.Version{}, ErrOverflow | ||
} | ||
return kv.Version{ts + l.n}, nil | ||
} | ||
l.lastTimeStampTs = ts | ||
l.n = 0 | ||
return kv.Version{ts}, nil | ||
} |
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,46 @@ | ||
package localstore | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/util/codec" | ||
) | ||
|
||
// ErrInvalidEncodedKey describes parsing an invalid format of EncodedKey | ||
var ErrInvalidEncodedKey = errors.New("invalid encoded key") | ||
|
||
func isTombstone(v []byte) bool { | ||
return len(v) == 0 | ||
} | ||
|
||
// MvccEncodeVersionKey returns the encoded key | ||
func MvccEncodeVersionKey(key kv.Key, ver kv.Version) kv.EncodedKey { | ||
b := codec.EncodeBytes(nil, key) | ||
ret := codec.EncodeUintDesc(b, ver.Ver) | ||
return ret | ||
} | ||
|
||
// MvccDecode parses the origin key and version of an encoded key, if the encoded key is a meta key, | ||
// just returns the origin key | ||
func MvccDecode(encodedKey kv.EncodedKey) (kv.Key, kv.Version, error) { | ||
// Skip DataPrefix | ||
remainBytes, key, err := codec.DecodeBytes([]byte(encodedKey)) | ||
if err != nil { | ||
// should never happen | ||
return nil, kv.Version{}, errors.Trace(err) | ||
} | ||
// if it's meta key | ||
if len(remainBytes) == 0 { | ||
return key, kv.Version{}, nil | ||
} | ||
var ver uint64 | ||
remainBytes, ver, err = codec.DecodeUintDesc(remainBytes) | ||
if err != nil { | ||
// should never happen | ||
return nil, kv.Version{}, errors.Trace(err) | ||
} | ||
if len(remainBytes) != 0 { | ||
return nil, kv.Version{}, ErrInvalidEncodedKey | ||
} | ||
return key, kv.Version{ver}, nil | ||
} |
Oops, something went wrong.