-
Notifications
You must be signed in to change notification settings - Fork 0
/
in-memory.go
90 lines (79 loc) · 2.12 KB
/
in-memory.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
package quota
import (
"fmt"
"sync"
"time"
)
// InMemoryStorage is an in-memory implementation of the Storage interface.
type InMemoryStorage struct {
mutex *sync.RWMutex
symbol string
interval time.Duration
Q *Quota
}
// NewInMemoryStorage creates a new InMemoryStorage instance.
func NewInMemoryStorage(symbol string, interval time.Duration) *InMemoryStorage {
return &InMemoryStorage{
mutex: &sync.RWMutex{},
symbol: symbol,
interval: interval,
Q: &Quota{},
}
}
// All returns all the data in the storage.
func (s *InMemoryStorage) All() (*Quota, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
return s.Q, nil
}
// Get returns the value for the given key.
func (s *InMemoryStorage) Get(openTime time.Time) (*Candle, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
candle, _ := s.Q.Find(openTime.Unix())
return candle, nil
}
// GetByIndex retrieves candle from the storage by index.
func (s *InMemoryStorage) GetByIndex(index int) (*Candle, error) {
return (*s.Q)[index], nil
}
// Put stores the given value for the given key.
func (s *InMemoryStorage) Put(c ...*Candle) error {
s.mutex.Lock()
defer s.mutex.Unlock()
for _, candle := range c {
*s.Q = append(*s.Q, candle)
}
return nil
}
// Update updates the value for the given key.
func (s *InMemoryStorage) Update(c ...*Candle) error {
return s.Put(c...)
}
// Delete deletes the value for the given key.
func (s *InMemoryStorage) Delete(c *Candle) error {
s.mutex.Lock()
defer s.mutex.Unlock()
_, index := s.Q.Find(c.OpenTime.Unix())
if index == -1 {
return fmt.Errorf("candle not found")
}
*s.Q = append((*s.Q)[:index], (*s.Q)[index+1:]...)
return nil
}
// Close closes the storage.
func (s *InMemoryStorage) Close() error {
*s.Q = (*s.Q)[:0]
return nil
}
// PersistOlds will store the old candles into a persistance storage and remove them from the quota.
func (s *InMemoryStorage) PersistOlds(persist Storage, size int) error {
s.mutex.Lock()
defer s.mutex.Unlock()
if len(*s.Q) <= size {
return fmt.Errorf("not enough candles to persist")
}
candles := (*s.Q)[:size]
*s.Q = (*s.Q)[size:]
return persist.Put(candles...)
}