-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.go
170 lines (140 loc) · 4.09 KB
/
mongo.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
package azdrivers
import (
"context"
"errors"
"log"
"sync"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type (
AZMongoApp struct {
client *mongo.Client
// streamChannels map[string]chan *
}
)
var (
AZMongoErrUnInitialized = errors.New("mongo driver - not initialized")
)
func NewMongoApp(uri string) (*AZMongoApp, error) {
// var err error
mongoClient, err := mongo.NewClient(options.Client().ApplyURI(uri))
if err != nil {
// log.Println(err)
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = mongoClient.Connect(ctx)
if err != nil {
// log.Println(err)
return nil, err
}
log.Println("mongo driver init")
return &AZMongoApp{client: mongoClient}, nil
}
func (app *AZMongoApp) IsInit() bool {
return app.client == nil
}
func (app *AZMongoApp) GetAllDatabases() ([]string, error) {
if !app.IsInit() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
databases, err := app.client.ListDatabaseNames(ctx, bson.M{})
if err != nil {
log.Fatal(err)
}
return databases, nil
}
return nil, AZMongoErrUnInitialized
}
func (app *AZMongoApp) GetCollection(dbName, collectionName string) (*mongo.Collection, error) {
var collection *mongo.Collection
if !app.IsInit() {
db := app.client.Database(dbName)
collection = db.Collection(collectionName)
return collection, nil
}
return collection, AZMongoErrUnInitialized
}
func (app *AZMongoApp) InsertIntoCollection(dbName, collectionName string, data map[string]interface{}, postOpDisconnect bool, opts *options.InsertOneOptions) (*mongo.InsertOneResult, error) {
if !app.IsInit() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if postOpDisconnect {
defer app.client.Disconnect(ctx)
}
collection, err := app.GetCollection(dbName, collectionName)
if err != nil {
return nil, err
}
return collection.InsertOne(ctx, InitGenericMongoDoc(data), opts)
}
return nil, AZMongoErrUnInitialized
}
func (app *AZMongoApp) UpdateCollection(opKey, dbName, collectionName string, filter interface{}, data map[string]interface{}, postOpDisconnect bool, opts *options.UpdateOptions) (*mongo.UpdateResult, error) {
if !app.IsInit() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if postOpDisconnect {
defer app.client.Disconnect(ctx)
}
collection, err := app.GetCollection(dbName, collectionName)
if err != nil {
return nil, err
}
return collection.UpdateOne(ctx, filter, InitGenericSetMongoDoc(opKey, data), opts)
}
return nil, AZMongoErrUnInitialized
}
func (app *AZMongoApp) ConcurrentMongoStreamParse(dbName, collectionName string, closeOnFinish bool, streamCallback func(data bson.M)) {
if !app.IsInit() {
if closeOnFinish {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
defer app.client.Disconnect(ctx)
}
db := app.client.Database(dbName)
collection := db.Collection(collectionName)
var wg sync.WaitGroup
currStream, err := collection.Watch(context.TODO(), mongo.Pipeline{})
if err != nil {
panic(err)
}
wg.Add(1)
routineCtx, cancel := context.WithCancel(context.Background())
defer cancel()
go app.parseStreamChanges(routineCtx, wg, currStream, streamCallback)
wg.Wait()
}
}
func (app *AZMongoApp) parseStreamChanges(ctx context.Context, wg sync.WaitGroup, stream *mongo.ChangeStream, f func(data bson.M)) {
if !app.IsInit() {
defer stream.Close(ctx)
defer wg.Done()
for stream.Next(ctx) {
var data bson.M
if err := stream.Decode(&data); err != nil {
log.Fatal(err)
}
// fmt.Printf("%v\n", data)
f(data)
}
}
}
func InitGenericSetMongoDoc(key string, m map[string]interface{}) bson.M {
genMongoDoc := bson.M{}
for k, v := range m {
genMongoDoc[k] = v
}
return bson.M{key: genMongoDoc}
}
func InitGenericMongoDoc(m map[string]interface{}) bson.M {
genMongoDoc := bson.M{}
for k, v := range m {
genMongoDoc[k] = v
}
return genMongoDoc
}