This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo_storage.go
187 lines (157 loc) · 3.7 KB
/
mongo_storage.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
package dor
import (
"log"
"sync"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
const docsLimit = 100
// MongoStorage implements the Storage interface for MongoDB
type MongoStorage struct {
sess *mgo.Session // mongodb session
db string // database name
c string // collection name
wNum int // number of workers
size int // size of batch request
ret bool // data retention
}
// NewMongoStorage bootstraps MongoStorage, creates indexes
// u is the Mongo URL
// db is the database name
// col is the collection name
// size is the bulk message size
// w is number of workers
// ret is the data retention option
func NewMongoStorage(u string, db string, col string, size int, w int, ret bool) (*MongoStorage, error) {
sess, err := mgo.Dial(u)
if err != nil {
return nil, err
}
index := mgo.Index{
Key: []string{"domain", "rank", "source"},
Background: true,
Sparse: true,
// Capped: true,
// MaxBytes: 10737418240, // 10Gb
// MaxDocs: 200000000, // 200M
}
if ret {
index.DropDups = true
}
err = sess.DB(db).C(col).EnsureIndex(index)
if err != nil {
return nil, err
}
if ret {
expindex := mgo.Index{
Key: []string{"last_update"},
Background: true,
ExpireAfter: time.Hour * 24 * 7,
}
err = sess.DB(db).C(col).EnsureIndex(expindex)
if err != nil {
return nil, err
}
}
return &MongoStorage{
sess: sess,
wNum: w,
db: db,
c: col,
size: size,
}, nil
}
// Put implements Storage interface method Put
// s - is the data source
// t - is the data datetime
func (m *MongoStorage) Put(c <-chan *Entry, s string, t time.Time) error {
var wg sync.WaitGroup
wg.Add(m.wNum)
for i := 1; i <= m.wNum; i++ {
go func() {
defer wg.Done()
m.send(c, s, t)
}()
}
wg.Wait()
return nil
}
// Get implements Storage interface method Get
func (m *MongoStorage) Get(d string, sources ...string) ([]*Entry, error) {
s := m.sess.Copy()
c := s.DB(m.db).C(m.c)
var query *mgo.Query
var ranks []*Entry
var e Entry
if len(sources) > 0 {
for i := range sources {
err := c.Find(bson.M{"domain": d, "source": sources[i]}).Sort("-last_update").One(&e)
if err != nil {
log.Println(err)
continue
}
ranks = append(ranks, &e)
}
} else {
query = c.Find(bson.M{"domain": d}).Sort("-last_update").Limit(docsLimit)
items := query.Iter()
for items.Next(&e) {
ranks = append(ranks, &e)
}
}
return ranks, nil
}
// GetMore implements Storage GetMore function
func (m *MongoStorage) GetMore(d string, lps int, sources ...string) ([]*Entry, error) {
s := m.sess.Copy()
c := s.DB(m.db).C(m.c)
var query *mgo.Query
var ranks []*Entry
var e Entry
// check if lps is not bigger than allowed
if lps > docsLimit {
lps = docsLimit
}
if len(sources) > 0 {
for i := range sources {
query = c.Find(bson.M{"domain": d, "source": sources[i]}).Sort("-last_update").Limit(lps)
items := query.Iter()
for items.Next(&e) {
ranks = append(ranks, &e)
}
}
} else {
query = c.Find(bson.M{"domain": d}).Sort("-last_update").Limit(lps)
items := query.Iter()
for items.Next(&e) {
ranks = append(ranks, &e)
}
}
return ranks, nil
}
func (m *MongoStorage) send(c <-chan *Entry, s string, t time.Time) error {
mc := m.sess.Copy()
col := mc.DB(m.db).C(m.c)
bulk := col.Bulk()
bulk.Unordered()
i := 0
for r := range c {
bulk.Insert(r)
i++
if i == m.size {
if _, err := bulk.Run(); err != nil {
log.Println("mongo storage: failed to run bulk run")
}
bulk = col.Bulk()
bulk.Unordered()
i = 0
}
}
if i != 0 {
if _, err := bulk.Run(); err != nil {
log.Println("mongo storage: failed to run bulk run")
}
}
return nil
}