/
mongo.go
286 lines (228 loc) · 7.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package mongo
import (
"context"
"sync"
"time"
logger "github.com/farovictor/MongoDbDriver/logging"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func Ping(connUri string) (bool, error) {
// Create a Client to a MongoDB server and use Ping to verify that the
// server is running.
clientOpts := options.Client().ApplyURI(connUri).SetServerSelectionTimeout(5 * time.Second)
client, err := mongo.Connect(context.TODO(), clientOpts)
if err != nil {
// log.Fatal(err)
return false, err
}
defer func() {
if err = client.Disconnect(context.TODO()); err != nil {
logger.WarningLogger.Println(err)
}
}()
// Call Ping to verify that the deployment is up and the Client was
// configured successfully. As mentioned in the Ping documentation, this
// reduces application resiliency as the server may be temporarily
// unavailable when Ping is called.
if err = client.Ping(context.TODO(), readpref.Nearest()); err != nil {
return false, err
}
return true, nil
}
func NewConnectionHandler(connUri string, dbName string, appName string) (*ConnectionHandler, func()) {
handler := ConnectionHandler{}
disconnect := handler.Connect(connUri, appName)
handler.setDatabase(dbName)
return &handler, disconnect
}
// Connection Handler holds a client and database instances
type ConnectionHandler struct {
// some fields in here
client *mongo.Client
database *mongo.Database
}
func (m *ConnectionHandler) Connect(connUri string, appName string) func() {
clientOpts := options.Client().ApplyURI(connUri).SetConnectTimeout(5 * time.Second).SetAppName(appName)
client, err := mongo.Connect(context.TODO(), clientOpts)
if err != nil {
logger.ErrorLogger.Fatal(err)
}
logger.InfoLogger.Println("Client connected!")
m.client = client
return func() {
if err = client.Disconnect(context.TODO()); err != nil {
logger.ErrorLogger.Fatal(err)
}
logger.InfoLogger.Println("Client disconnected!")
}
}
func (m *ConnectionHandler) setDatabase(dbname string) {
logger.InfoLogger.Println("Setting database", dbname)
m.database = m.client.Database(dbname)
}
func (m *ConnectionHandler) ExtractResults(mapping string, filePrefix string, fileLocation string, process func([]*bson.M, string, string, string) error, coll *mongo.Collection, filter interface{}, opts ...*options.FindOptions) error {
// Find all documents in which the "name" field is "Bob".
// Specify the Sort option to sort the returned documents by age in
// ascending order.
// e.g.: opts := options.Find().SetSort(bson.D{{"age", 1}})
// e.g.: filter := bson.D{{"name", "Bob"}}
ctx := context.TODO()
cursor, err := coll.Find(ctx, filter, opts...)
defer cursor.Close(ctx)
if err != nil {
logger.ErrorLogger.Fatal(err)
}
// Get a list of all returned documents and print them out.
// See the mongo.Cursor documentation for more examples of using cursors.
var results []*bson.M
if err = cursor.All(ctx, &results); err != nil {
logger.ErrorLogger.Fatal(err)
}
if err := process(results, mapping, filePrefix, fileLocation); err != nil {
return err
}
return nil
}
func (m *ConnectionHandler) CollectionExists(collection string) bool {
logger.InfoLogger.Println("Collection Exists called")
cursor, err := m.database.ListCollections(context.TODO(), bson.D{})
if err != nil {
logger.InfoLogger.Printf("Could not list collections. Error: %s", err)
return false
}
var results []bson.M
for cursor.Next(context.TODO()) {
var element bson.M
if err := cursor.Decode(&element); err != nil {
logger.ErrorLogger.Printf("Could not decode results. Error: %s", err)
return false
}
results = append(results, element)
}
for _, item := range results {
val, ok := item["name"]
if ok && val == collection {
return true
}
}
return false
}
// Retrieve collection
func (m *ConnectionHandler) GetCollection(collectionName string) *mongo.Collection {
logger.InfoLogger.Println("GetCollectionName:", collectionName)
return m.database.Collection(collectionName)
}
// Streaming results into a pool of workers
// Process function should loop through channel. Once the channel is closed, worker should send a Done signal when finished iterating the channel.
func (m *ConnectionHandler) StreamingResults(mapping, filePrefix, fileLocation string,
batchSize, numWorkers int32,
process func(ctx context.Context, batchData <-chan []*bson.M, mapping string, wg *sync.WaitGroup, filePrefix string, folder string),
coll *mongo.Collection, filter interface{}, opts ...*options.FindOptions) error {
ctx := context.Background()
// Creating channel that will handler the results list
pipe := make(chan []*bson.M, numWorkers)
// Wait Group to Handler file dumps
var wg sync.WaitGroup
// Creating workers and attaching channel
for i := int32(0); i < numWorkers; i++ {
wg.Add(1)
// Dispatching function to workers
go process(ctx, pipe, mapping, &wg, filePrefix, fileLocation)
}
cursor, err := coll.Find(ctx, filter, opts...)
defer cursor.Close(ctx)
if err != nil {
// logger.ErrorLogger.Fatal(err)
close(pipe)
return err
}
// Get a list of all returned documents and print them out.
// See the mongo.Cursor documentation for more examples of using cursors.
var counter int32 = 0
var results []*bson.M
for cursor.Next(ctx) {
var result bson.M
if err := cursor.Decode(&result); err != nil {
// logger.WarningLogger.Println("Error decoding document:", err)
close(pipe)
return err
} else {
// Appending to slice
results = append(results, &result)
counter += 1
}
if counter > batchSize {
// Reset counter
counter = 0
// Send data to channel
pipe <- results
// Resetting results
results = []*bson.M{}
}
}
// Send residual results to channel
pipe <- results
close(pipe)
// Wait for all workers
wg.Wait()
if cursor.Err() != nil {
// logger.ErrorLogger.Fatalln("Error while iterating cursor:", cursor.Err())
return err
}
return nil
}
// This method reads files, populates a slice and inserts into a collection (this may require a huge amount of memory)
func (m *ConnectionHandler) InsertFromFiles(filePrefix, folder string, walk func(filePrefix string, folder string) ([]any, error), coll *mongo.Collection, opts ...*options.InsertManyOptions) error {
ctx := context.Background()
// Walking function
documents, err := walk(filePrefix, folder)
if err != nil {
logger.ErrorLogger.Println(err)
return err
}
if len(documents) == 0 {
logger.WarningLogger.Println("No documents to insert.")
return nil
}
// Inserting resutls
results, err := coll.InsertMany(ctx, documents, opts...)
if err != nil {
logger.ErrorLogger.Println(err)
return err
}
logger.InfoLogger.Printf("Inserted %d documents", len(results.InsertedIDs))
return nil
}
// This method reads files concurrently, send them to workers that will inserts the values concurrently
func (m *ConnectionHandler) ConcurrentBatchInsert(
filePrefix, folder string,
numWorkers int32,
insert func(ctx context.Context, files <-chan string, wg *sync.WaitGroup, coll *mongo.Collection),
coll *mongo.Collection) error {
ctx := context.Background()
// Creating channel that will handler the results list
pipe := make(chan string, numWorkers)
// Wait Group to Handler file dumps
var wg sync.WaitGroup
// Creating workers and attaching channel
for i := int32(0); i < numWorkers; i++ {
wg.Add(1)
// dispatching workers
go insert(ctx, pipe, &wg, coll)
}
// Send files across channel
counter, err := findFiles(filePrefix, folder, pipe)
if err != nil {
logger.ErrorLogger.Println(err)
return err
}
// Close files channel
close(pipe)
// Wait for all workers
wg.Wait()
logger.InfoLogger.Printf("%d files were collected", counter)
return nil
}