From f28f4bff95c284110621d913a9c6e7bacbb50143 Mon Sep 17 00:00:00 2001 From: Al Cutter Date: Fri, 19 Jul 2024 14:20:51 +0100 Subject: [PATCH] Use storage options --- cmd/example-posix/main.go | 15 ++------------- storage/posix/files.go | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/cmd/example-posix/main.go b/cmd/example-posix/main.go index 2c2782097..35d2f24c3 100644 --- a/cmd/example-posix/main.go +++ b/cmd/example-posix/main.go @@ -22,6 +22,7 @@ import ( "fmt" "os" "path/filepath" + "time" "golang.org/x/mod/sumdb/note" @@ -125,19 +126,7 @@ func main() { } return cp.Size, cp.Hash, nil } - writeCP := func(size uint64, root []byte) error { - cp := &fmtlog.Checkpoint{ - Origin: s.Name(), - Size: size, - Hash: root, - } - n, err := note.Sign(¬e.Note{Text: string(cp.Marshal())}, s) - if err != nil { - return err - } - return posix.WriteCheckpoint(*storageDir, n) - } - st := posix.New(ctx, *storageDir, readCP, writeCP) + st := posix.New(ctx, *storageDir, readCP, tessera.WithCheckpointSigner(s), tessera.WithBatching(256, time.Second)) // sequence entries diff --git a/storage/posix/files.go b/storage/posix/files.go index e6d67d977..20f9f5489 100644 --- a/storage/posix/files.go +++ b/storage/posix/files.go @@ -23,7 +23,6 @@ import ( "path/filepath" "sync" "syscall" - "time" tessera "github.com/transparency-dev/trillian-tessera" "github.com/transparency-dev/trillian-tessera/api" @@ -47,9 +46,9 @@ type Storage struct { cpFile *os.File curTree CurrentTreeFunc - newTree NewTreeFunc curSize uint64 + newCP tessera.NewCPFunc } // NewTreeFunc is the signature of a function which receives information about newly integrated trees. @@ -59,18 +58,20 @@ type NewTreeFunc func(size uint64, root []byte) error type CurrentTreeFunc func() (uint64, []byte, error) // New creates a new POSIX storage. -func New(ctx context.Context, path string, curTree CurrentTreeFunc, newTree NewTreeFunc) *Storage { +func New(ctx context.Context, path string, curTree func() (uint64, []byte, error), opts ...func(*tessera.StorageOptions)) *Storage { curSize, _, err := curTree() if err != nil { panic(err) } + opt := tessera.ResolveStorageOptions(nil, opts...) + r := &Storage{ path: path, curSize: curSize, curTree: curTree, - newTree: newTree, + newCP: opt.NewCP, } - r.queue = storage.NewQueue(ctx, time.Second, 256, r.sequenceBatch) + r.queue = storage.NewQueue(ctx, opt.BatchMaxAge, opt.BatchMaxSize, r.sequenceBatch) return r } @@ -239,9 +240,17 @@ func (s *Storage) doIntegrate(ctx context.Context, fromSeq uint64, entries []sto } } - if err := s.newTree(newSize, newRoot); err != nil { - return fmt.Errorf("newTree: %v", err) + klog.Infof("New CP: %d, %x", newSize, newRoot) + if s.newCP != nil { + cpRaw, err := s.newCP(newSize, newRoot) + if err != nil { + return fmt.Errorf("newCP: %v", err) + } + if err := WriteCheckpoint(layout.CheckpointPath, cpRaw); err != nil { + return fmt.Errorf("failed to write new checkpoint: %v", err) + } } + return nil }