34
hash/hash.go
Normal file
34
hash/hash.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package hash
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hash"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var hashTypes map[string]hash.Hash
|
||||
|
||||
func init() {
|
||||
hashTypes = make(map[string]hash.Hash)
|
||||
}
|
||||
|
||||
func RegisterHash(engine string, hash hash.Hash) {
|
||||
hashTypes[engine] = hash
|
||||
}
|
||||
|
||||
func New(htype string) (hash.Hash, error) {
|
||||
hash, ok := hashTypes[htype]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown hash type %s. only %s supported", htype, strings.Join(HashTypes(), ","))
|
||||
}
|
||||
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
func HashTypes() []string {
|
||||
var htypes []string
|
||||
for htype, _ := range hashTypes {
|
||||
htypes = append(htypes, htype)
|
||||
}
|
||||
return htypes
|
||||
}
|
11
hash/xxhash/xxhash.go
Normal file
11
hash/xxhash/xxhash.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package xxhash
|
||||
|
||||
import (
|
||||
"github.com/OneOfOne/xxhash"
|
||||
|
||||
"github.com/sdstack/storage/hash"
|
||||
)
|
||||
|
||||
func init() {
|
||||
hash.RegisterHash("xxhash", &xxhash.XXHash64{})
|
||||
}
|
Reference in New Issue
Block a user