github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/core/ledger/kvledger/history/historydb/histmgr_helper.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package historydb
    18  
    19  import (
    20  	"bytes"
    21  
    22  	"github.com/hyperledger/fabric/common/ledger/util"
    23  )
    24  
    25  var compositeKeySep = []byte{0x00}
    26  
    27  //ConstructCompositeHistoryKey builds the History Key of namespace~key~blocknum~trannum
    28  // using an order preserving encoding so that history query results are ordered by height
    29  func ConstructCompositeHistoryKey(ns string, key string, blocknum uint64, trannum uint64) []byte {
    30  
    31  	var compositeKey []byte
    32  	compositeKey = append(compositeKey, []byte(ns)...)
    33  	compositeKey = append(compositeKey, compositeKeySep...)
    34  	compositeKey = append(compositeKey, []byte(key)...)
    35  	compositeKey = append(compositeKey, compositeKeySep...)
    36  	compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(blocknum)...)
    37  	compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(trannum)...)
    38  
    39  	return compositeKey
    40  }
    41  
    42  //ConstructPartialCompositeHistoryKey builds a partial History Key namespace~key~
    43  // for use in history key range queries
    44  func ConstructPartialCompositeHistoryKey(ns string, key string, endkey bool) []byte {
    45  	var compositeKey []byte
    46  	compositeKey = append(compositeKey, []byte(ns)...)
    47  	compositeKey = append(compositeKey, compositeKeySep...)
    48  	compositeKey = append(compositeKey, []byte(key)...)
    49  	compositeKey = append(compositeKey, compositeKeySep...)
    50  	if endkey {
    51  		compositeKey = append(compositeKey, []byte{0xff}...)
    52  	}
    53  	return compositeKey
    54  }
    55  
    56  //SplitCompositeHistoryKey splits the key bytes using a separator
    57  func SplitCompositeHistoryKey(bytesToSplit []byte, separator []byte) ([]byte, []byte) {
    58  	split := bytes.SplitN(bytesToSplit, separator, 2)
    59  	return split[0], split[1]
    60  }