/
mongoindex.go
269 lines (235 loc) · 6.46 KB
/
mongoindex.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
/*
Copyright 2011 The Camlistore Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package mongo
import (
"errors"
"os"
"strconv"
"strings"
"sync"
"time"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/index"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/third_party/labix.org/v2/mgo"
"camlistore.org/third_party/labix.org/v2/mgo/bson"
)
// We explicitely separate the key and the value in a document,
// instead of simply storing as key:value, to avoid problems
// such as "." being an illegal char in a key name. Also because
// there is no way to do partial matching for key names (one can
// only check for their existence with bson.M{$exists: true}).
const (
collectionName = "keys"
mgoKey = "k"
mgoValue = "v"
)
type MongoWrapper struct {
Servers string
User string
Password string
Database string
Collection string
}
func (mgw *MongoWrapper) url() string {
if mgw.User == "" || mgw.Password == "" {
return mgw.Servers
}
return mgw.User + ":" + mgw.Password + "@" + mgw.Servers + "/" + mgw.Database
}
// Note that Ping won't work with old (1.2) mongo servers.
func (mgw *MongoWrapper) TestConnection(timeout time.Duration) bool {
session, err := mgo.DialWithTimeout(mgw.url(), timeout)
if err != nil {
return false
}
defer session.Close()
session.SetSyncTimeout(timeout)
if err = session.Ping(); err != nil {
return false
}
return true
}
func (mgw *MongoWrapper) getConnection() (*mgo.Session, error) {
// TODO(mpl): do some "client caching" as in mysql, to avoid systematically dialing?
session, err := mgo.Dial(mgw.url())
if err != nil {
return nil, err
}
session.SetMode(mgo.Monotonic, true)
session.SetSafe(&mgo.Safe{})
return session, nil
}
// TODO(mpl): I'm only calling getCollection at the beginning, and
// keeping the collection around and reusing it everywhere, instead
// of calling getCollection everytime, because that's the easiest.
// But I can easily change that. Gustavo says it does not make
// much difference either way.
// Brad, what do you think?
func (mgw *MongoWrapper) getCollection() (*mgo.Collection, error) {
session, err := mgw.getConnection()
if err != nil {
return nil, err
}
session.SetSafe(&mgo.Safe{})
session.SetMode(mgo.Strong, true)
c := session.DB(mgw.Database).C(mgw.Collection)
return c, nil
}
func init() {
blobserver.RegisterStorageConstructor("mongodbindexer",
blobserver.StorageConstructor(newMongoIndexFromConfig))
}
func newMongoIndex(mgw *MongoWrapper) (*index.Index, error) {
db, err := mgw.getCollection()
if err != nil {
return nil, err
}
mongoStorage := &mongoKeys{db: db}
return index.New(mongoStorage), nil
}
func newMongoIndexFromConfig(ld blobserver.Loader, config jsonconfig.Obj) (blobserver.Storage, error) {
blobPrefix := config.RequiredString("blobSource")
mgw := &MongoWrapper{
Servers: config.OptionalString("host", "localhost"),
Database: config.RequiredString("database"),
User: config.OptionalString("user", ""),
Password: config.OptionalString("password", ""),
Collection: collectionName,
}
if err := config.Validate(); err != nil {
return nil, err
}
sto, err := ld.GetStorage(blobPrefix)
if err != nil {
return nil, err
}
ix, err := newMongoIndex(mgw)
if err != nil {
return nil, err
}
ix.BlobSource = sto
// Good enough, for now:
ix.KeyFetcher = ix.BlobSource
if wipe := os.Getenv("CAMLI_MONGO_WIPE"); wipe != "" {
dowipe, err := strconv.ParseBool(wipe)
if err != nil {
return nil, err
}
if dowipe {
err = ix.Storage().Delete("")
if err != nil {
return nil, err
}
}
}
return ix, err
}
// Implementation of index Iterator
type mongoStrIterator struct {
res bson.M
*mgo.Iter
}
func (s mongoStrIterator) Next() bool {
return s.Iter.Next(&s.res)
}
func (s mongoStrIterator) Key() (key string) {
key, ok := (s.res[mgoKey]).(string)
if !ok {
return ""
}
return key
}
func (s mongoStrIterator) Value() (value string) {
value, ok := (s.res[mgoValue]).(string)
if !ok {
return ""
}
return value
}
func (s mongoStrIterator) Close() error {
// TODO(mpl): think about anything more to be done here.
return nil
}
// Implementation of index.Storage
type mongoKeys struct {
mu sync.Mutex // guards db
db *mgo.Collection
}
func (mk *mongoKeys) Get(key string) (string, error) {
mk.mu.Lock()
defer mk.mu.Unlock()
res := bson.M{}
q := mk.db.Find(&bson.M{mgoKey: key})
err := q.One(&res)
if err != nil {
if err == mgo.ErrNotFound {
return "", index.ErrNotFound
} else {
return "", err
}
}
return res[mgoValue].(string), err
}
func (mk *mongoKeys) Find(key string) index.Iterator {
mk.mu.Lock()
defer mk.mu.Unlock()
// TODO(mpl): escape other special chars, or maybe replace $regex with something
// more suited if possible.
cleanedKey := strings.Replace(key, "|", `\|`, -1)
iter := mk.db.Find(&bson.M{mgoKey: &bson.M{"$regex": "^" + cleanedKey}}).Sort(mgoKey).Iter()
return mongoStrIterator{res: bson.M{}, Iter: iter}
}
func (mk *mongoKeys) Set(key, value string) error {
mk.mu.Lock()
defer mk.mu.Unlock()
_, err := mk.db.Upsert(&bson.M{mgoKey: key}, &bson.M{mgoKey: key, mgoValue: value})
return err
}
// Delete removes the document with the matching key.
// If key is "", it removes all documents.
func (mk *mongoKeys) Delete(key string) error {
mk.mu.Lock()
defer mk.mu.Unlock()
if key == "" {
_, err := mk.db.RemoveAll(nil)
return err
}
return mk.db.Remove(&bson.M{mgoKey: key})
}
func (mk *mongoKeys) BeginBatch() index.BatchMutation {
return index.NewBatchMutation()
}
type batch interface {
Mutations() []index.Mutation
}
func (mk *mongoKeys) CommitBatch(bm index.BatchMutation) error {
b, ok := bm.(batch)
if !ok {
return errors.New("invalid batch type")
}
mk.mu.Lock()
defer mk.mu.Unlock()
for _, m := range b.Mutations() {
if m.IsDelete() {
if err := mk.db.Remove(bson.M{mgoKey: m.Key()}); err != nil {
return err
}
} else {
if _, err := mk.db.Upsert(&bson.M{mgoKey: m.Key()}, &bson.M{mgoKey: m.Key(), mgoValue: m.Value()}); err != nil {
return err
}
}
}
return nil
}