-
Notifications
You must be signed in to change notification settings - Fork 175
/
consumer_progress.go
50 lines (40 loc) · 1.39 KB
/
consumer_progress.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
package badger
import (
"fmt"
"github.com/dgraph-io/badger/v2"
"github.com/onflow/flow-go/storage/badger/operation"
)
type ConsumerProgress struct {
db *badger.DB
consumer string // to distinguish the consume progress between different consumers
}
func NewConsumerProgress(db *badger.DB, consumer string) *ConsumerProgress {
return &ConsumerProgress{
db: db,
consumer: consumer,
}
}
func (cp *ConsumerProgress) ProcessedIndex() (uint64, error) {
var processed uint64
err := cp.db.View(operation.RetrieveProcessedIndex(cp.consumer, &processed))
if err != nil {
return 0, fmt.Errorf("failed to retrieve processed index: %w", err)
}
return processed, nil
}
// InitProcessedIndex insert the default processed index to the storage layer, can only be done once.
// initialize for the second time will return storage.ErrAlreadyExists
func (cp *ConsumerProgress) InitProcessedIndex(defaultIndex uint64) error {
err := operation.RetryOnConflict(cp.db.Update, operation.InsertProcessedIndex(cp.consumer, defaultIndex))
if err != nil {
return fmt.Errorf("could not update processed index: %w", err)
}
return nil
}
func (cp *ConsumerProgress) SetProcessedIndex(processed uint64) error {
err := operation.RetryOnConflict(cp.db.Update, operation.SetProcessedIndex(cp.consumer, processed))
if err != nil {
return fmt.Errorf("could not update processed index: %w", err)
}
return nil
}