2020-09-28 13:22:13 +00:00
|
|
|
package rangehashsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"hash"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"github.com/nspcc-dev/tzhash/tz"
|
|
|
|
)
|
|
|
|
|
|
|
|
type onceHashWriter struct {
|
|
|
|
once *sync.Once
|
|
|
|
|
|
|
|
traverser *placement.Traverser
|
|
|
|
|
|
|
|
resp *Response
|
|
|
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
type hasher interface {
|
2020-12-07 17:49:47 +00:00
|
|
|
WriteChunk([]byte) error
|
2020-09-28 13:22:13 +00:00
|
|
|
sum() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type tzHasher struct {
|
|
|
|
hashes [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type commonHasher struct {
|
|
|
|
h hash.Hash
|
|
|
|
}
|
|
|
|
|
|
|
|
type singleHasher struct {
|
|
|
|
hash []byte
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (h *singleHasher) WriteChunk(p []byte) error {
|
2020-09-28 13:22:13 +00:00
|
|
|
h.hash = p
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-28 13:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *singleHasher) sum() ([]byte, error) {
|
|
|
|
return h.hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *onceHashWriter) write(hs [][]byte) {
|
|
|
|
w.once.Do(func() {
|
|
|
|
w.resp.hashes = hs
|
|
|
|
w.traverser.SubmitSuccess()
|
|
|
|
w.cancel()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (h *tzHasher) WriteChunk(p []byte) error {
|
2020-09-28 13:22:13 +00:00
|
|
|
h.hashes = append(h.hashes, p)
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
return nil
|
2020-09-28 13:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *tzHasher) sum() ([]byte, error) {
|
|
|
|
return tz.Concat(h.hashes)
|
|
|
|
}
|
|
|
|
|
2020-12-07 17:49:47 +00:00
|
|
|
func (h *commonHasher) WriteChunk(p []byte) error {
|
2020-09-28 13:22:13 +00:00
|
|
|
h.h.Write(p)
|
2020-12-07 17:49:47 +00:00
|
|
|
|
|
|
|
return nil
|
2020-09-28 13:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *commonHasher) sum() ([]byte, error) {
|
|
|
|
return h.h.Sum(nil), nil
|
|
|
|
}
|