-
Notifications
You must be signed in to change notification settings - Fork 390
/
countobserver.go
50 lines (40 loc) · 1.34 KB
/
countobserver.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
// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package rangedlooptest
import (
"context"
"time"
"storj.io/storj/satellite/metabase/rangedloop"
)
var _ rangedloop.Observer = (*CountObserver)(nil)
var _ rangedloop.Partial = (*CountObserver)(nil)
// CountObserver is a subscriber to the ranged segment loop which counts the number of segments.
type CountObserver struct {
NumSegments int
}
// Start is the callback for segment loop start.
func (c *CountObserver) Start(ctx context.Context, time time.Time) error {
c.NumSegments = 0
return nil
}
// Fork splits the observer to count ranges of segments.
func (c *CountObserver) Fork(ctx context.Context) (rangedloop.Partial, error) {
// return new instance for threadsafety
return &CountObserver{}, nil
}
// Join adds the count of all the ranges together.
func (c *CountObserver) Join(ctx context.Context, partial rangedloop.Partial) error {
countPartial := partial.(*CountObserver)
c.NumSegments += countPartial.NumSegments
// Range done
return nil
}
// Finish is the callback for ranged segment loop end.
func (c *CountObserver) Finish(ctx context.Context) error {
return nil
}
// Process counts the size of a batch of segments.
func (c *CountObserver) Process(ctx context.Context, segments []rangedloop.Segment) error {
c.NumSegments += len(segments)
return nil
}