github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/structure/type.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 structure
    15  
    16  import (
    17  	"bytes"
    18  
    19  	"github.com/insionng/yougam/libraries/juju/errors"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/kv"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/codec"
    22  )
    23  
    24  // TypeFlag is for data structure meta/data flag.
    25  type TypeFlag byte
    26  
    27  const (
    28  	// StringMeta is the flag for string meta.
    29  	StringMeta TypeFlag = 'S'
    30  	// StringData is the flag for string data.
    31  	StringData TypeFlag = 's'
    32  	// HashMeta is the flag for hash meta.
    33  	HashMeta TypeFlag = 'H'
    34  	// HashData is the flag for hash data.
    35  	HashData TypeFlag = 'h'
    36  	// ListMeta is the flag for list meta.
    37  	ListMeta TypeFlag = 'L'
    38  	// ListData is the flag for list data.
    39  	ListData TypeFlag = 'l'
    40  )
    41  
    42  func (t *TxStructure) encodeStringDataKey(key []byte) kv.Key {
    43  	// for codec Encode, we may add extra bytes data, so here and following encode
    44  	// we will use extra length like 4 for a little optimization.
    45  	ek := make([]byte, 0, len(t.prefix)+len(key)+24)
    46  	ek = append(ek, t.prefix...)
    47  	ek = codec.EncodeBytes(ek, key)
    48  	return codec.EncodeUint(ek, uint64(StringData))
    49  }
    50  
    51  func (t *TxStructure) encodeHashMetaKey(key []byte) kv.Key {
    52  	ek := make([]byte, 0, len(t.prefix)+len(key)+24)
    53  	ek = append(ek, t.prefix...)
    54  	ek = codec.EncodeBytes(ek, key)
    55  	return codec.EncodeUint(ek, uint64(HashMeta))
    56  }
    57  
    58  func (t *TxStructure) encodeHashDataKey(key []byte, field []byte) kv.Key {
    59  	ek := make([]byte, 0, len(t.prefix)+len(key)+len(field)+30)
    60  	ek = append(ek, t.prefix...)
    61  	ek = codec.EncodeBytes(ek, key)
    62  	ek = codec.EncodeUint(ek, uint64(HashData))
    63  	return codec.EncodeBytes(ek, field)
    64  }
    65  
    66  func (t *TxStructure) decodeHashDataKey(ek kv.Key) ([]byte, []byte, error) {
    67  	var (
    68  		key   []byte
    69  		field []byte
    70  		err   error
    71  		tp    uint64
    72  	)
    73  
    74  	if !bytes.HasPrefix(ek, t.prefix) {
    75  		return nil, nil, errors.New("invalid encoded hash data key prefix")
    76  	}
    77  
    78  	ek = ek[len(t.prefix):]
    79  
    80  	ek, key, err = codec.DecodeBytes(ek)
    81  	if err != nil {
    82  		return nil, nil, errors.Trace(err)
    83  	}
    84  
    85  	ek, tp, err = codec.DecodeUint(ek)
    86  	if err != nil {
    87  		return nil, nil, errors.Trace(err)
    88  	} else if TypeFlag(tp) != HashData {
    89  		return nil, nil, errInvalidHashKeyFlag.Gen("invalid encoded hash data key flag %c", byte(tp))
    90  	}
    91  
    92  	_, field, err = codec.DecodeBytes(ek)
    93  	return key, field, errors.Trace(err)
    94  }
    95  
    96  func (t *TxStructure) hashDataKeyPrefix(key []byte) kv.Key {
    97  	ek := make([]byte, 0, len(t.prefix)+len(key)+24)
    98  	ek = append(ek, t.prefix...)
    99  	ek = codec.EncodeBytes(ek, key)
   100  	return codec.EncodeUint(ek, uint64(HashData))
   101  }
   102  
   103  func (t *TxStructure) encodeListMetaKey(key []byte) kv.Key {
   104  	ek := make([]byte, 0, len(t.prefix)+len(key)+24)
   105  	ek = append(ek, t.prefix...)
   106  	ek = codec.EncodeBytes(ek, key)
   107  	return codec.EncodeUint(ek, uint64(ListMeta))
   108  }
   109  
   110  func (t *TxStructure) encodeListDataKey(key []byte, index int64) kv.Key {
   111  	ek := make([]byte, 0, len(t.prefix)+len(key)+36)
   112  	ek = append(ek, t.prefix...)
   113  	ek = codec.EncodeBytes(ek, key)
   114  	ek = codec.EncodeUint(ek, uint64(ListData))
   115  	return codec.EncodeInt(ek, index)
   116  }