1
0
mirror of https://github.com/nxshock/zkv.git synced 2024-11-27 11:21:02 +05:00
zkv/utils.go
nxshock 4ec53665af New version
* Simplify internal structures
* Update minimal Go version
* Add Delete() function
2022-12-02 20:32:09 +05:00

46 lines
779 B
Go

package zkv
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"io"
)
func encode(value interface{}) ([]byte, error) {
buf := new(bytes.Buffer)
err := gob.NewEncoder(buf).Encode(value)
return buf.Bytes(), err
}
func decode(b []byte, value interface{}) error {
return gob.NewDecoder(bytes.NewReader(b)).Decode(value)
}
func hashInterface(value interface{}) ([]byte, error) {
valueBytes, err := encode(value)
if err != nil {
return nil, err
}
return hashBytes(valueBytes), nil
}
func hashBytes(b []byte) []byte {
bytes := sha256.Sum224(b)
return bytes[:]
}
func skip(r io.Reader, count int64) (err error) {
switch r := r.(type) {
case io.Seeker:
_, err = r.Seek(count, io.SeekCurrent)
default:
_, err = io.CopyN(io.Discard, r, count)
}
return err
}