-
Notifications
You must be signed in to change notification settings - Fork 9
/
memstore.go
159 lines (129 loc) · 2.89 KB
/
memstore.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package backfill
import (
"context"
"fmt"
"sync"
"time"
"github.com/ipfs/go-cid"
typegen "github.com/whyrusleeping/cbor-gen"
)
type bufferedOp struct {
kind string
rec *typegen.CBORMarshaler
cid *cid.Cid
}
type Memjob struct {
repo string
state string
lk sync.Mutex
bufferedOps map[string][]*bufferedOp
createdAt time.Time
updatedAt time.Time
}
// Memstore is a simple in-memory implementation of the Backfill Store interface
type Memstore struct {
lk sync.RWMutex
jobs map[string]*Memjob
}
func NewMemstore() *Memstore {
return &Memstore{
jobs: make(map[string]*Memjob),
}
}
func (s *Memstore) EnqueueJob(repo string) error {
s.lk.Lock()
defer s.lk.Unlock()
if _, ok := s.jobs[repo]; ok {
return fmt.Errorf("job already exists for repo %s", repo)
}
j := &Memjob{
repo: repo,
createdAt: time.Now(),
updatedAt: time.Now(),
state: StateEnqueued,
bufferedOps: map[string][]*bufferedOp{},
}
s.jobs[repo] = j
return nil
}
func (s *Memstore) BufferOp(ctx context.Context, repo, kind, path string, rec *typegen.CBORMarshaler, cid *cid.Cid) (bool, error) {
s.lk.Lock()
// If the job doesn't exist, we can't buffer an op for it
j, ok := s.jobs[repo]
s.lk.Unlock()
if !ok {
return false, ErrJobNotFound
}
j.lk.Lock()
defer j.lk.Unlock()
switch j.state {
case StateComplete:
return false, ErrJobComplete
case StateInProgress:
// keep going and buffer the op
default:
return false, nil
}
j.bufferedOps[path] = append(j.bufferedOps[path], &bufferedOp{
kind: kind,
rec: rec,
cid: cid,
})
j.updatedAt = time.Now()
return true, nil
}
func (s *Memstore) GetJob(ctx context.Context, repo string) (Job, error) {
s.lk.RLock()
defer s.lk.RUnlock()
j, ok := s.jobs[repo]
if !ok || j == nil {
return nil, nil
}
return j, nil
}
func (s *Memstore) GetNextEnqueuedJob(ctx context.Context) (Job, error) {
s.lk.RLock()
defer s.lk.RUnlock()
for _, j := range s.jobs {
if j.State() == StateEnqueued {
return j, nil
}
}
return nil, nil
}
func (j *Memjob) Repo() string {
return j.repo
}
func (j *Memjob) State() string {
j.lk.Lock()
defer j.lk.Unlock()
return j.state
}
func (j *Memjob) SetState(ctx context.Context, state string) error {
j.lk.Lock()
defer j.lk.Unlock()
j.state = state
j.updatedAt = time.Now()
return nil
}
func (j *Memjob) FlushBufferedOps(ctx context.Context, fn func(kind, path string, rec *typegen.CBORMarshaler, cid *cid.Cid) error) error {
j.lk.Lock()
defer j.lk.Unlock()
for path, ops := range j.bufferedOps {
for _, op := range ops {
if err := fn(op.kind, path, op.rec, op.cid); err != nil {
return err
}
}
}
j.bufferedOps = map[string][]*bufferedOp{}
j.state = StateComplete
return nil
}
func (j *Memjob) ClearBufferedOps(ctx context.Context) error {
j.lk.Lock()
defer j.lk.Unlock()
j.bufferedOps = map[string][]*bufferedOp{}
j.updatedAt = time.Now()
return nil
}