/
mongo.go
179 lines (154 loc) · 4.34 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
package common
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"sync"
)
// This struct handles all communication with the Mongo database that provides
// metadata storage and query capabilities. The "schema" of the metadata
// collection is simple. Each document is flat (just k-v pairs) and corresponds
// to a producer. The producer UUIDv4 is stored in the primary key _id field,
// and the rest of the document is just the key/value pairs of metadata
type MetadataStore struct {
session *mgo.Session
db *mgo.Database
metadata *mgo.Collection
}
var selectID = bson.M{"uuid": 1}
func NewMetadataStore(c *Config) *MetadataStore {
var (
m = new(MetadataStore)
err error
)
address := fmt.Sprintf("%s:%d", c.Mongo.Host, c.Mongo.Port)
m.session, err = mgo.Dial(address)
if err != nil {
log.WithFields(log.Fields{
"address": address, "error": err.Error(),
}).Fatal("Could not connect to MongoDB")
// exits
}
log.WithFields(log.Fields{
"address": address,
}).Info("Connected to MongoDB")
m.db = m.session.DB(c.Mongo.Database)
m.metadata = m.db.C("metadata")
index := mgo.Index{
Key: []string{"uuid"},
Unique: true,
DropDups: false,
Background: false,
Sparse: false,
}
err = m.metadata.EnsureIndex(index)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Could not create index on metadata.uuid")
}
return m
}
func (ms *MetadataStore) Save(publisherID *UUID, metadata map[string]interface{}) error {
if publisherID == nil {
return errors.New("PublisherID is null")
}
if len(metadata) == 0 { // nothing to save
log.WithField("UUID", *publisherID).Debug("No message metadata to save")
return nil
}
_, err := ms.metadata.Upsert(bson.M{"uuid": *publisherID}, bson.M{"$set": bson.M(metadata)})
return err
}
func (ms *MetadataStore) RemovePublisher(uuid UUID) error {
return ms.metadata.Remove(bson.M{"uuid": uuid})
}
func (ms *MetadataStore) Query(node rootNode) (*Query, error) {
query := node.Tree.MongoQuery()
log.WithFields(log.Fields{
"query": query,
}).Debug("Running mongo query")
var q = NewQuery(node.String, node.Keys, node.Tree)
iter := ms.metadata.Find(query).Select(selectID).Iter()
var r map[string]string
for iter.Next(&r) {
uuid := UUID(r["uuid"])
q.MatchingProducers[uuid] = ProdStateNew
}
err := iter.Close()
return q, err
}
func (ms *MetadataStore) Reevaluate(query *Query) (added, removed []UUID) {
iter := ms.metadata.Find(query.Mongo).Select(selectID).Iter()
var r map[string]string
// mark new UUIDs
query.Lock()
for iter.Next(&r) {
uuid := UUID(r["uuid"])
if _, found := query.MatchingProducers[uuid]; found {
query.MatchingProducers[uuid] = ProdStateSame
} else {
query.MatchingProducers[uuid] = ProdStateNew
added = append(added, uuid)
}
}
// remove old ones
for uuid, status := range query.MatchingProducers {
if status == ProdStateOld {
removed = append(removed, uuid)
delete(query.MatchingProducers, uuid)
}
}
// prepare statuses
for uuid, _ := range query.MatchingProducers {
query.MatchingProducers[uuid] = ProdStateOld
}
query.Unlock()
return
}
// Obviously use carefully - in place only for testing!
func (ms *MetadataStore) DropDatabase() {
ms.db.DropDatabase()
}
type Query struct {
QueryString string
Keys []string
MatchingProducers map[UUID]ProducerState
Mongo bson.M
sync.RWMutex
}
func NewQuery(query string, keys []string, root Node) *Query {
return &Query{
QueryString: query,
Keys: keys,
MatchingProducers: make(map[UUID]ProducerState),
Mongo: root.MongoQuery(),
}
}
// updates internal list of qualified streams. Returns the lists of added and removed UUIDs
func (q *Query) changeUUIDs(uuids []UUID) (added, removed []UUID) {
// mark the UUIDs that are new
q.Lock()
for _, uuid := range uuids {
if _, found := q.MatchingProducers[uuid]; found {
q.MatchingProducers[uuid] = ProdStateSame
} else {
q.MatchingProducers[uuid] = ProdStateNew
added = append(added, uuid)
}
}
// remove the old ones
for uuid, status := range q.MatchingProducers {
if status == ProdStateOld {
removed = append(removed, uuid)
delete(q.MatchingProducers, uuid)
}
}
for uuid, _ := range q.MatchingProducers {
q.MatchingProducers[uuid] = ProdStateOld
}
q.Unlock()
return
}