-
Notifications
You must be signed in to change notification settings - Fork 390
/
observer.go
55 lines (43 loc) · 1.7 KB
/
observer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package rangedloop
import (
"context"
"time"
"storj.io/storj/satellite/metabase"
)
// Segment contains information about segment metadata which will be received by observers.
type Segment metabase.LoopSegmentEntry
// Inline returns true if segment is inline.
func (s Segment) Inline() bool {
return s.Redundancy.IsZero() && len(s.Pieces) == 0
}
// Expired checks if segment expired relative to now.
func (s *Segment) Expired(now time.Time) bool {
return s.ExpiresAt != nil && s.ExpiresAt.Before(now)
}
// PieceSize returns calculated piece size for segment.
func (s Segment) PieceSize() int64 {
return s.Redundancy.PieceSize(int64(s.EncryptedSize))
}
// Observer subscribes to the parallel segment loop.
// It is intended that a naïve implementation is threadsafe.
type Observer interface {
// Start is called at the beginning of each segment loop.
Start(context.Context, time.Time) error
// Fork creates a Partial to process a chunk of all the segments. It is
// called after Start. It is not called concurrently.
Fork(context.Context) (Partial, error)
// Join is called for each partial returned by Fork. This gives the
// opportunity to merge the output like in a reduce step. It will be called
// before Finish. It is not called concurrently.
Join(context.Context, Partial) error
// Finish is called after all segments are processed by all observers.
Finish(context.Context) error
}
// Partial processes a part of the total range of segments.
type Partial interface {
// Process is called repeatedly with batches of segments.
// It is not called concurrently on the same instance.
Process(context.Context, []Segment) error
}