-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstore.go
140 lines (111 loc) · 3.29 KB
/
store.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
package memory
import (
"context"
"sort"
"sync"
"time"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/code/data/currency"
)
const (
dateFormat = "2006-01-02"
)
type store struct {
currencyStoreMu sync.Mutex
currencyStore []*currency.ExchangeRateRecord
lastIndex uint64
}
type ByTime []*currency.ExchangeRateRecord
func (a ByTime) Len() int { return len(a) }
func (a ByTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByTime) Less(i, j int) bool {
// DESC order (most recent first)
return a[i].Time.Unix() > a[j].Time.Unix()
}
func New() currency.Store {
return &store{
currencyStore: make([]*currency.ExchangeRateRecord, 0),
lastIndex: 1,
}
}
func (s *store) reset() {
s.currencyStoreMu.Lock()
s.currencyStore = make([]*currency.ExchangeRateRecord, 0)
s.currencyStoreMu.Unlock()
}
func (s *store) Put(ctx context.Context, data *currency.MultiRateRecord) error {
s.currencyStoreMu.Lock()
defer s.currencyStoreMu.Unlock()
// Not ideal but fine for testing the currency store
for _, item := range s.currencyStore {
if item.Time.Unix() == data.Time.Unix() {
return currency.ErrExists
}
}
for symbol, item := range data.Rates {
s.currencyStore = append(s.currencyStore, ¤cy.ExchangeRateRecord{
Id: s.lastIndex,
Rate: item,
Time: data.Time,
Symbol: symbol,
})
s.lastIndex = s.lastIndex + 1
}
return nil
}
func (s *store) Get(ctx context.Context, symbol string, t time.Time) (*currency.ExchangeRateRecord, error) {
s.currencyStoreMu.Lock()
defer s.currencyStoreMu.Unlock()
// Not ideal but fine for testing the currency store
var results []*currency.ExchangeRateRecord
for _, item := range s.currencyStore {
if item.Symbol == symbol && item.Time.Unix() <= t.Unix() {
results = append(results, item)
}
}
if len(results) == 0 {
return nil, currency.ErrNotFound
}
sort.Sort(ByTime(results))
return results[0], nil
}
func (s *store) GetAll(ctx context.Context, t time.Time) (*currency.MultiRateRecord, error) {
s.currencyStoreMu.Lock()
defer s.currencyStoreMu.Unlock()
// Not ideal but fine for testing the currency store
sort.Sort(ByTime(s.currencyStore))
result := currency.MultiRateRecord{
Rates: make(map[string]float64),
}
for _, item := range s.currencyStore {
if item.Time.Unix() <= t.Unix() && item.Time.Format(dateFormat) == t.Format(dateFormat) {
result.Rates[item.Symbol] = item.Rate
}
}
if len(result.Rates) == 0 {
return nil, currency.ErrNotFound
}
return &result, nil
}
func (s *store) GetRange(ctx context.Context, symbol string, interval query.Interval, start time.Time, end time.Time, ordering query.Ordering) ([]*currency.ExchangeRateRecord, error) {
s.currencyStoreMu.Lock()
defer s.currencyStoreMu.Unlock()
sort.Sort(ByTime(s.currencyStore))
// Not ideal but fine for testing the currency store
var all []*currency.ExchangeRateRecord
for _, item := range s.currencyStore {
if item.Symbol == symbol && item.Time.Unix() >= start.Unix() && item.Time.Unix() <= end.Unix() {
all = append(all, item)
}
}
// TODO: handle the interval
if len(all) == 0 {
return nil, currency.ErrNotFound
}
if ordering == query.Ascending {
for i, j := 0, len(all)-1; i < j; i, j = i+1, j-1 {
all[i], all[j] = all[j], all[i]
}
}
return all, nil
}