From 44c564761c6fa351bf87f933ecbe2e5389af1a9e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 16 Feb 2024 21:55:16 -0500 Subject: [PATCH] refactor: store length in result --- bao/bao.go | 14 ++++++++------ bao/plugin.go | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/bao/bao.go b/bao/bao.go index 0763934..c1d831f 100644 --- a/bao/bao.go +++ b/bao/bao.go @@ -5,12 +5,13 @@ import ( "bytes" _ "embed" "errors" - "github.com/docker/go-units" - "github.com/hashicorp/go-plugin" "io" "math" "os" "os/exec" + + "github.com/docker/go-units" + "github.com/hashicorp/go-plugin" ) //go:generate buf generate @@ -70,7 +71,7 @@ func Shutdown() { client.Kill() } -func Hash(r io.Reader) (*Result, int, error) { +func Hash(r io.Reader) (*Result, error) { hasherId := bao.NewHasher() initialSize := 4 * units.KiB maxSize := 3.5 * units.MiB @@ -86,12 +87,12 @@ func Hash(r io.Reader) (*Result, int, error) { if err == io.EOF { break } - return nil, 0, err + return nil, err } totalReadSize += n if !bao.Hash(hasherId, buf[:n]) { - return nil, 0, errors.New("hashing failed") + return nil, errors.New("hashing failed") } // Adaptively adjust buffer size based on read patterns @@ -103,6 +104,7 @@ func Hash(r io.Reader) (*Result, int, error) { } result := bao.Finish(hasherId) + result.Length = uint(totalReadSize) - return &result, totalReadSize, nil + return &result, nil } diff --git a/bao/plugin.go b/bao/plugin.go index f8db212..1f89c6f 100644 --- a/bao/plugin.go +++ b/bao/plugin.go @@ -2,6 +2,7 @@ package bao import ( "context" + "git.lumeweb.com/LumeWeb/portal/bao/proto" "github.com/google/uuid" "github.com/hashicorp/go-plugin" @@ -29,8 +30,9 @@ func (p *BaoPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c } type Result struct { - Hash []byte - Proof []byte + Hash []byte + Proof []byte + Length uint } type BaoGRPC struct { client proto.BaoClient