This repository has been archived by the owner on Jan 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
74 lines (66 loc) · 1.97 KB
/
collector.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
package replicator
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Collector is a used to fetch change stream from a client, database or collection.
type Collector struct {
uri string
db string
coll string
pipeline mongo.Pipeline
}
// NewCollector creates a new Collector instance.
func NewCollector(uri string, databases []string, collections []string) *Collector {
var (
db string
coll string
)
if len(databases) == 1 {
db = databases[0]
if len(collections) == 1 {
coll = collections[0]
}
}
return &Collector{
uri: uri,
db: db,
coll: coll,
pipeline: newPipeline(databases, collections),
}
}
func newPipeline(databases []string, collections []string) mongo.Pipeline {
var match []bson.E
if len(databases) > 0 {
match = append(match, bson.E{Key: "ns.db", Value: bson.M{"$in": databases}})
}
if len(collections) > 0 {
match = append(match, bson.E{Key: "ns.coll", Value: bson.M{"$in": collections}})
}
if len(match) > 0 {
return mongo.Pipeline{bson.D{{Key: "$match", Value: match}}}
}
return mongo.Pipeline{}
}
// Collect returns a change stream for all changes on the corresponding container.
//
// The opts parameter can be used to specify options for change stream creation. See
// https://godoc.org/go.mongodb.org/mongo-driver/mongo/options#ChangeStreamOptions for details.
func (c *Collector) Collect(opts ...*options.ChangeStreamOptions) (*mongo.ChangeStream, error) {
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(c.uri))
if err != nil {
return nil, err
}
// watch a client
if c.db == "" {
return client.Watch(context.Background(), c.pipeline, opts...)
}
// watch a database
if c.coll == "" {
return client.Database(c.db).Watch(context.Background(), c.pipeline, opts...)
}
// watch a collection
return client.Database(c.db).Collection(c.coll).Watch(context.Background(), c.pipeline, opts...)
}