github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/store/localstore/mvcc.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package localstore
    15  
    16  import (
    17  	"github.com/insionng/yougam/libraries/juju/errors"
    18  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    19  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/codec"
    20  )
    21  
    22  // ErrInvalidEncodedKey describes parsing an invalid format of EncodedKey.
    23  var ErrInvalidEncodedKey = errors.New("invalid encoded key")
    24  
    25  func isTombstone(v []byte) bool {
    26  	return len(v) == 0
    27  }
    28  
    29  // MvccEncodeVersionKey returns the encoded key.
    30  func MvccEncodeVersionKey(key kv.Key, ver kv.Version) kv.EncodedKey {
    31  	b := codec.EncodeBytes(nil, key)
    32  	ret := codec.EncodeUintDesc(b, ver.Ver)
    33  	return ret
    34  }
    35  
    36  // MvccDecode parses the origin key and version of an encoded key, if the encoded key is a meta key,
    37  // just returns the origin key.
    38  func MvccDecode(encodedKey kv.EncodedKey) (kv.Key, kv.Version, error) {
    39  	// Skip DataPrefix
    40  	remainBytes, key, err := codec.DecodeBytes([]byte(encodedKey))
    41  	if err != nil {
    42  		// should never happen
    43  		return nil, kv.Version{}, errors.Trace(err)
    44  	}
    45  	// if it's meta key
    46  	if len(remainBytes) == 0 {
    47  		return key, kv.Version{}, nil
    48  	}
    49  	var ver uint64
    50  	remainBytes, ver, err = codec.DecodeUintDesc(remainBytes)
    51  	if err != nil {
    52  		// should never happen
    53  		return nil, kv.Version{}, errors.Trace(err)
    54  	}
    55  	if len(remainBytes) != 0 {
    56  		return nil, kv.Version{}, ErrInvalidEncodedKey
    57  	}
    58  	return key, kv.Version{Ver: ver}, nil
    59  }