github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/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 (
    18  	"github.com/dolthub/dolt/go/store/hash"
    19  )
    20  
    21  // HashesToSlices takes a list of hashes and converts each hash to a byte slice returning a slice of byte slices
    22  func HashesToSlices(hashes []hash.Hash) [][]byte {
    23  	slices := make([][]byte, len(hashes))
    24  
    25  	for i, h := range hashes {
    26  		tmp := h
    27  		slices[i] = tmp[:]
    28  	}
    29  
    30  	return slices
    31  }
    32  
    33  // HashSetToSlices takes a HashSet and converts it to a slice of hashes, and a slice of byte slices
    34  func HashSetToSlices(hashes hash.HashSet) ([]hash.Hash, [][]byte) {
    35  	hashSl := make([]hash.Hash, len(hashes))
    36  	bytesSl := make([][]byte, len(hashes))
    37  
    38  	i := 0
    39  	for h := range hashes {
    40  		tmp := h
    41  		hashSl[i] = tmp
    42  		bytesSl[i] = tmp[:]
    43  		i++
    44  	}
    45  
    46  	return hashSl, bytesSl
    47  }
    48  
    49  // ParseByteSlices takes a slice of byte slices and converts it to a HashSet, and a map from hash to it's index in the
    50  // original slice
    51  func ParseByteSlices(byteSlices [][]byte) (hash.HashSet, map[hash.Hash]int) {
    52  	hs := make(hash.HashSet)
    53  	hashToIndex := make(map[hash.Hash]int)
    54  
    55  	for i, byteSl := range byteSlices {
    56  		h := hash.New(byteSl)
    57  		hs[h] = struct{}{}
    58  		hashToIndex[h] = i
    59  	}
    60  
    61  	return hs, hashToIndex
    62  }