-
Notifications
You must be signed in to change notification settings - Fork 32
/
history.go
59 lines (48 loc) · 1.23 KB
/
history.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
// Package history implements functionality for fetching/adding to play history.
package history
import (
"fmt"
"sync"
"time"
"tchaik.com/index"
)
// Store is an interface which defines methods necessary for fetching/adding to play history for
// index paths. All times are stored in UTC.
type Store interface {
// Add a play event to the store.
Add(index.Path) error
// Get the play events associated to a path.
Get(index.Path) []time.Time
}
// NewStore creates a basic implementation of a play history store, using the given path as the
// source of data. If the file does not exist it will be created.
func NewStore(path string) (Store, error) {
m := make(map[string][]time.Time)
s, err := index.NewPersistStore(path, &m)
if err != nil {
return nil, err
}
return &store{
m: m,
store: s,
}, nil
}
type store struct {
sync.RWMutex
m map[string][]time.Time
store index.PersistStore
}
// Add implements Store.
func (s *store) Add(p index.Path) error {
s.Lock()
defer s.Unlock()
k := fmt.Sprintf("%v", p)
s.m[k] = append(s.m[k], time.Now().UTC())
return s.store.Persist(&s.m)
}
// Get implements Store.
func (s *store) Get(p index.Path) []time.Time {
s.RLock()
defer s.RUnlock()
return s.m[fmt.Sprintf("%v", p)]
}