refactor: store length in result

This commit is contained in:
Derrick Hammer 2024-02-16 21:55:16 -05:00
parent ffbb7e371a
commit 44c564761c
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 12 additions and 8 deletions

View File

@ -5,12 +5,13 @@ import (
"bytes" "bytes"
_ "embed" _ "embed"
"errors" "errors"
"github.com/docker/go-units"
"github.com/hashicorp/go-plugin"
"io" "io"
"math" "math"
"os" "os"
"os/exec" "os/exec"
"github.com/docker/go-units"
"github.com/hashicorp/go-plugin"
) )
//go:generate buf generate //go:generate buf generate
@ -70,7 +71,7 @@ func Shutdown() {
client.Kill() client.Kill()
} }
func Hash(r io.Reader) (*Result, int, error) { func Hash(r io.Reader) (*Result, error) {
hasherId := bao.NewHasher() hasherId := bao.NewHasher()
initialSize := 4 * units.KiB initialSize := 4 * units.KiB
maxSize := 3.5 * units.MiB maxSize := 3.5 * units.MiB
@ -86,12 +87,12 @@ func Hash(r io.Reader) (*Result, int, error) {
if err == io.EOF { if err == io.EOF {
break break
} }
return nil, 0, err return nil, err
} }
totalReadSize += n totalReadSize += n
if !bao.Hash(hasherId, buf[: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 // 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 := bao.Finish(hasherId)
result.Length = uint(totalReadSize)
return &result, totalReadSize, nil return &result, nil
} }

View File

@ -2,6 +2,7 @@ package bao
import ( import (
"context" "context"
"git.lumeweb.com/LumeWeb/portal/bao/proto" "git.lumeweb.com/LumeWeb/portal/bao/proto"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/hashicorp/go-plugin" "github.com/hashicorp/go-plugin"
@ -29,8 +30,9 @@ func (p *BaoPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c
} }
type Result struct { type Result struct {
Hash []byte Hash []byte
Proof []byte Proof []byte
Length uint
} }
type BaoGRPC struct { type BaoGRPC struct {
client proto.BaoClient client proto.BaoClient