-
Notifications
You must be signed in to change notification settings - Fork 111
/
toMongoDB.go
136 lines (131 loc) · 3.7 KB
/
toMongoDB.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
package library
import (
"errors"
"github.com/nytlabs/streamtools/st/blocks" // blocks
"github.com/nytlabs/streamtools/st/util"
"labix.org/v2/mgo"
)
// specify those channels we're going to use to communicate with streamtools
type ToMongoDB struct {
blocks.Block
queryrule chan chan interface{}
inrule chan interface{}
in chan interface{}
quit chan interface{}
}
// we need to build a simple factory so that streamtools can make new blocks of this kind
func NewToMongoDB() blocks.BlockInterface {
return &ToMongoDB{}
}
// Setup is called once before running the block. We build up the channels and specify what kind of block this is.
func (b *ToMongoDB) Setup() {
b.Kind = "ToMongoDB"
b.Desc = "sends messages to MongoDB, optionally in batches"
b.in = b.InRoute("in")
b.inrule = b.InRoute("rule")
b.queryrule = b.QueryRoute("rule")
b.quit = b.Quit()
}
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ToMongoDB) Run() {
var collectionname string
var dbname string
var collection *mgo.Collection
var session *mgo.Session
var host = ""
var err error
var batch = 0
var count = 0
var maxindex = 0
var list []interface{}
for {
select {
case msgI := <-b.inrule:
// set host string for MongoDB server
host, err = util.ParseRequiredString(msgI, "Host")
if err != nil {
b.Error(err.Error())
continue
}
// set database name
dbname, err = util.ParseRequiredString(msgI, "Database")
if err != nil {
b.Error(err.Error())
continue
}
// set collection name
collectionname, err = util.ParseRequiredString(msgI, "Collection")
if err != nil {
b.Error(err.Error())
continue
}
// set number of records to insert at a time
batch, err = util.ParseInt(msgI, "BatchSize")
if err != nil || batch < 0 {
b.Error(errors.New("Error parsing batch size....setting to 0"))
batch = 0
} else {
if batch > 1 {
list = make([]interface{}, batch, batch)
// set maxindex to 1 minus batch size
// use maxindex for looping everywhere for consistency
maxindex = batch - 1
}
}
// create MongoDB connection
session, err = mgo.Dial(host)
if err != nil {
// swallowing a panic from mgo here - streamtools must not die
b.Error(errors.New("Could not initiate connection with MongoDB service"))
continue
}
// use the specified DB and collection
collection = session.DB(dbname).C(collectionname)
case <-b.quit:
// close connection to MongoDB and quit
if session != nil {
session.Close()
}
return
case msg := <-b.in:
// deal with inbound data
if session != nil {
// mgo is so cool - it will check if the message can be serialized to valid bson.
// So, no need to do a json.Marshal on the inbound. just append to the list and
// batch insert
if maxindex >= 1 {
if count <= maxindex {
list[count] = msg
count = count + 1
}
if count == maxindex {
// insert batch if count reaches batch size
err = collection.Insert(list...)
if err != nil {
b.Error(err.Error())
}
// reset list and count
list = make([]interface{}, batch, batch)
count = 0
}
} else {
// mgo coolness again. No need to do a json.Marshal on the inbound.
err = collection.Insert(msg)
if err != nil {
b.Error(err.Error())
}
}
} else {
b.Error(errors.New("MongoDB connection not initated or lost. Please check your MongoDB server or block settings."))
}
case respChan := <-b.queryrule:
// deal with a query request
respChan <- map[string]interface{}{
"Collection": collectionname,
"Database": dbname,
"Host": host,
"BatchSize": batch,
}
}
}
}