github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/remotestorage/utils.go (about)

     1  // Copyright 2019 Dolthub, 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package remotestorage
    16  
    17  import "github.com/dolthub/dolt/go/store/hash"
    18  
    19  // HashesToSlices takes a list of hashes and converts each hash to a byte slice returning a slice of byte slices
    20  func HashesToSlices(hashes []hash.Hash) [][]byte {
    21  	slices := make([][]byte, len(hashes))
    22  
    23  	for i, h := range hashes {
    24  		tmp := h
    25  		slices[i] = tmp[:]
    26  	}
    27  
    28  	return slices
    29  }
    30  
    31  // HashSetToSlices takes a HashSet and converts it to a slice of hashes, and a slice of byte slices
    32  func HashSetToSlices(hashes hash.HashSet) ([]hash.Hash, [][]byte) {
    33  	hashSl := make([]hash.Hash, len(hashes))
    34  	bytesSl := make([][]byte, len(hashes))
    35  
    36  	i := 0
    37  	for h := range hashes {
    38  		tmp := h
    39  		hashSl[i] = tmp
    40  		bytesSl[i] = tmp[:]
    41  		i++
    42  	}
    43  
    44  	return hashSl, bytesSl
    45  }
    46  
    47  // ParseByteSlices takes a slice of byte slices and converts it to a HashSet, and a map from hash to it's index in the
    48  // original slice
    49  func ParseByteSlices(byteSlices [][]byte) (hash.HashSet, map[hash.Hash]int) {
    50  	hs := make(hash.HashSet)
    51  	hashToIndex := make(map[hash.Hash]int)
    52  
    53  	for i, byteSl := range byteSlices {
    54  		h := hash.New(byteSl)
    55  		hs[h] = struct{}{}
    56  		hashToIndex[h] = i
    57  	}
    58  
    59  	return hs, hashToIndex
    60  }