-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwriter.go
173 lines (149 loc) · 3.04 KB
/
writer.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package feedx
import (
"bufio"
"context"
"io"
"time"
"github.com/bsm/bfs"
)
// WriterOptions configure the producer instance.
type WriterOptions struct {
// Format specifies the format
// Default: auto-detected from URL path.
Format Format
// Compression specifies the compression type.
// Default: auto-detected from URL path.
Compression Compression
// Provides an optional last modified timestamp which is stored with the remote metadata.
// Default: time.Time{}.
LastMod time.Time
}
func (o *WriterOptions) norm(name string) {
if o.Format == nil {
o.Format = DetectFormat(name)
}
if o.Compression == nil {
o.Compression = DetectCompression(name)
}
}
// Writer encodes feeds to remote locations.
type Writer struct {
ctx context.Context
remote *bfs.Object
opt WriterOptions
num int
bw bfs.Writer
cw io.WriteCloser // compression writer
ww *bufio.Writer
fe FormatEncoder
}
// NewWriter inits a new feed writer.
func NewWriter(ctx context.Context, remote *bfs.Object, opt *WriterOptions) *Writer {
var o WriterOptions
if opt != nil {
o = *opt
}
o.norm(remote.Name())
return &Writer{
ctx: ctx,
remote: remote,
opt: o,
}
}
// Write write raw bytes to the feed.
func (w *Writer) Write(p []byte) (int, error) {
if err := w.ensureCreated(); err != nil {
return 0, err
}
return w.ww.Write(p)
}
// WriteString write a raw string to the feed.
func (w *Writer) WriteString(s string) (int, error) {
if err := w.ensureCreated(); err != nil {
return 0, err
}
return w.ww.WriteString(s)
}
// Encode appends a value to the feed.
func (w *Writer) Encode(v interface{}) error {
if err := w.ensureCreated(); err != nil {
return err
}
if w.fe == nil {
fe, err := w.opt.Format.NewEncoder(w.ww)
if err != nil {
return err
}
w.fe = fe
}
if err := w.fe.Encode(v); err != nil {
return err
}
w.num++
return nil
}
// NumWritten returns the number of written values.
func (w *Writer) NumWritten() int {
return w.num
}
// Discard closes the writer and discards the contents.
func (w *Writer) Discard() error {
err := w.close()
if w.bw != nil {
if e := w.bw.Discard(); e != nil {
err = e
}
}
return err
}
// Commit closes the writer and persists the contents.
func (w *Writer) Commit() error {
err := w.close()
if w.bw != nil {
if e := w.bw.Commit(); e != nil {
err = e
}
}
return err
}
func (w *Writer) close() (err error) {
if w.fe != nil {
if e := w.fe.Close(); e != nil {
err = e
}
}
if w.ww != nil {
if e := w.ww.Flush(); e != nil {
err = e
}
}
if w.cw != nil {
if e := w.cw.Close(); e != nil {
err = e
}
}
return err
}
func (w *Writer) ensureCreated() error {
if w.bw == nil {
ts := timestampFromTime(w.opt.LastMod)
bw, err := w.remote.Create(w.ctx, &bfs.WriteOptions{
Metadata: bfs.Metadata{metaLastModified: ts.String()},
})
if err != nil {
return err
}
w.bw = bw
}
if w.cw == nil {
cw, err := w.opt.Compression.NewWriter(w.bw)
if err != nil {
return err
}
w.cw = cw
}
if w.ww == nil {
w.ww = bufio.NewWriter(w.cw)
}
return nil
}