forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb.go
93 lines (80 loc) · 2.14 KB
/
mongodb.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
package mongodb
import (
"encoding/json"
"errors"
"sync"
"github.com/compose/transporter/adaptor"
"github.com/compose/transporter/client"
)
const (
description = "a mongodb adaptor that functions as both a source and a sink"
sampleConfig = `{
"uri": "${MONGODB_URI}"
// "timeout": "30s",
// "tail": false,
// "ssl": false,
// "cacerts": ["/path/to/cert.pem"],
// "wc": 1,
// "fsync": false,
// "bulk": false,
// "collection_filters": "{}",
// "read_preference": "Primary"
}`
)
var (
_ adaptor.Adaptor = &mongoDB{}
// ErrCollectionFilter is returned when an error occurs attempting to Unmarshal the string.
ErrCollectionFilter = errors.New("malformed collection_filters")
)
// mongoDB is an adaptor to read / write to mongodb.
// it works as a source by copying files, and then optionally tailing the oplog
type mongoDB struct {
adaptor.BaseConfig
SSL bool `json:"ssl"`
CACerts []string `json:"cacerts"`
Tail bool `json:"tail"`
Wc int `json:"wc"`
FSync bool `json:"fsync"`
Bulk bool `json:"bulk"`
CollectionFilters string `json:"collection_filters"`
ReadPreference string `json:"read_preference"`
}
func init() {
adaptor.Add(
"mongodb",
func() adaptor.Adaptor {
return &mongoDB{}
},
)
}
func (m *mongoDB) Client() (client.Client, error) {
return NewClient(WithURI(m.URI),
WithTimeout(m.Timeout),
WithSSL(m.SSL),
WithCACerts(m.CACerts),
WithFsync(m.FSync),
WithTail(m.Tail),
WithWriteConcern(m.Wc),
WithReadPreference(m.ReadPreference))
}
func (m *mongoDB) Reader() (client.Reader, error) {
var f map[string]CollectionFilter
if m.CollectionFilters != "" {
if jerr := json.Unmarshal([]byte(m.CollectionFilters), &f); jerr != nil {
return nil, ErrCollectionFilter
}
}
return newReader(m.Tail, f), nil
}
func (m *mongoDB) Writer(done chan struct{}, wg *sync.WaitGroup) (client.Writer, error) {
if m.Bulk {
return newBulker(done, wg), nil
}
return newWriter(), nil
}
func (m *mongoDB) Description() string {
return description
}
func (m *mongoDB) SampleConfig() string {
return sampleConfig
}