-
Notifications
You must be signed in to change notification settings - Fork 0
/
cos.go
142 lines (123 loc) · 3.37 KB
/
cos.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
package stores
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"time"
"github.com/mozillazg/go-cos"
"github.com/nzai/qr/exchanges"
"github.com/nzai/qr/quotes"
"go.uber.org/zap"
)
// Cos define tencent cos store
type Cos struct {
client *cos.Client
}
// NewCos create tencent cos store
func NewCos(client *cos.Client) *Cos {
return &Cos{client: client}
}
// storePath return store path
func (s Cos) storePath(exchange exchanges.Exchange, date time.Time) string {
return fmt.Sprintf("%s/%s", date.Format("2006/01/02"), exchange.Code())
}
// Exists check quote exists
func (s Cos) Exists(exchange exchanges.Exchange, date time.Time) (bool, error) {
_, err := s.client.Object.Head(context.Background(), s.storePath(exchange, date), nil)
return err == nil, nil
}
// Save save exchange daily quote
func (s Cos) Save(exchange exchanges.Exchange, date time.Time, edq *quotes.ExchangeDailyQuote) error {
buffer := new(bytes.Buffer)
// init gzip writer
gw, err := gzip.NewWriterLevel(buffer, gzip.BestCompression)
if err != nil {
zap.L().Error("create gzip writer failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return err
}
// encode to gzip writer
err = edq.Encode(gw)
if err != nil {
zap.L().Error("encode quote failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return err
}
gw.Flush()
gw.Close()
_, err = s.client.Object.Put(context.Background(), s.storePath(exchange, date), buffer, nil)
if err != nil {
zap.L().Error("put exchange daily quote failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return err
}
return nil
}
// Load load exchange daily quote
func (s Cos) Load(exchange exchanges.Exchange, date time.Time) (*quotes.ExchangeDailyQuote, error) {
response, err := s.client.Object.Get(context.Background(), s.storePath(exchange, date), nil)
if err != nil {
zap.L().Error("get exchange daily quote failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return nil, err
}
defer response.Body.Close()
// init gzip reader
gr, err := gzip.NewReader(response.Body)
if err != nil {
zap.L().Error("create gzip reader failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return nil, err
}
defer gr.Close()
buffer := new(bytes.Buffer)
_, err = io.Copy(buffer, gr)
if err != nil {
zap.L().Error("get exchange daily quote response failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return nil, err
}
// decode from bytes
edq := new(quotes.ExchangeDailyQuote)
err = edq.Decode(buffer)
if err != nil {
zap.L().Error("decode exchange daily quote failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date))
return nil, err
}
return edq, nil
}
// Delete delete exchange daily quote
func (s Cos) Delete(exchange exchanges.Exchange, date time.Time) error {
key := s.storePath(exchange, date)
_, err := s.client.Object.Delete(context.Background(), key)
if err != nil {
zap.L().Error("delete exchange daily quote failed",
zap.Error(err),
zap.String("exchange", exchange.Code()),
zap.Time("date", date),
zap.String("key", key))
return err
}
return nil
}
// Close close store
func (s Cos) Close() error {
return nil
}