Repository navigation
Expand file tree
/
Copy pathinmemory.go
More file actions
79 lines (70 loc) · 2.04 KB
/
Copy pathinmemory.go
File metadata and controls
79 lines (70 loc) · 2.04 KB
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
package scheduler
import (
"context"
"fmt"
"sync"
"github.com/ieshan/idx"
)
// InMemoryJobStore is a thread-safe in-memory [JobStore] implementation for testing.
// It stores jobs in a map and provides full [JobStore] semantics.
//
// This is useful for unit tests and demos where persistence is not required.
// For production, implement [JobStore] with a database backend.
type InMemoryJobStore struct {
mu sync.RWMutex
jobs map[idx.ID]*Job
}
// NewInMemoryJobStore creates a new empty in-memory job store.
func NewInMemoryJobStore() *InMemoryJobStore {
return &InMemoryJobStore{jobs: make(map[idx.ID]*Job)}
}
// List returns all jobs in the store.
func (s *InMemoryJobStore) List(_ context.Context) ([]Job, error) {
s.mu.RLock()
defer s.mu.RUnlock()
result := make([]Job, 0, len(s.jobs))
for _, j := range s.jobs {
result = append(result, *j)
}
return result, nil
}
// Get returns a single job by ID, or [ErrJobNotFound] if not found.
func (s *InMemoryJobStore) Get(_ context.Context, id idx.ID) (*Job, error) {
s.mu.RLock()
defer s.mu.RUnlock()
j, ok := s.jobs[id]
if !ok {
return nil, fmt.Errorf("%w: %s", ErrJobNotFound, id.String())
}
return new(*j), nil
}
// Save creates or overwrites a job in the store.
func (s *InMemoryJobStore) Save(_ context.Context, job *Job) error {
s.mu.Lock()
defer s.mu.Unlock()
s.jobs[job.ID] = new(*job)
return nil
}
// Delete removes a job from the store.
// Returns [ErrJobNotFound] if the job does not exist.
func (s *InMemoryJobStore) Delete(_ context.Context, id idx.ID) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.jobs[id]; !ok {
return fmt.Errorf("%w: %s", ErrJobNotFound, id.String())
}
delete(s.jobs, id)
return nil
}
// UpdateState updates only the [JobState] fields of a job.
// Returns [ErrJobNotFound] if the job does not exist.
func (s *InMemoryJobStore) UpdateState(_ context.Context, id idx.ID, state JobState) error {
s.mu.Lock()
defer s.mu.Unlock()
j, ok := s.jobs[id]
if !ok {
return fmt.Errorf("%w: %s", ErrJobNotFound, id.String())
}
j.State = state
return nil
}