Skip to content

Commit

Permalink
Use storage options
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Jul 19, 2024
1 parent 143eaba commit f28f4bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
15 changes: 2 additions & 13 deletions cmd/example-posix/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"golang.org/x/mod/sumdb/note"

Expand Down Expand Up @@ -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(&note.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

Expand Down
23 changes: 16 additions & 7 deletions storage/posix/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"path/filepath"
"sync"
"syscall"
"time"

tessera "github.com/transparency-dev/trillian-tessera"
"github.com/transparency-dev/trillian-tessera/api"
Expand All @@ -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.
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit f28f4bf

Please sign in to comment.