-
Notifications
You must be signed in to change notification settings - Fork 23
/
db.go
178 lines (165 loc) · 4.8 KB
/
db.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
package node
import (
"errors"
"io/ioutil"
"path/filepath"
"strings"
"github.com/MG-RAST/Shock/shock-server/conf"
"github.com/MG-RAST/Shock/shock-server/db"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// mongodb has hard limit of 16 MB docuemnt size
var DocumentMaxByte = 16777216
// Initialize creates a copy of the mongodb connection and then uses that connection to
// create the Nodes collection in mongodb. Then, it ensures that there is a unique index
// on the id key in this collection, creating the index if necessary.
func Initialize() {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
c.EnsureIndex(mgo.Index{Key: []string{"acl.owner"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"acl.read"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"acl.write"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"acl.delete"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"created_on"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"expiration"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"type"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"priority"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"file.path"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"file.virtual_parts"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"file.checksum.md5"}, Background: true})
c.EnsureIndex(mgo.Index{Key: []string{"id"}, Unique: true})
if conf.MONGODB_ATTRIBUTE_INDEXES != "" {
for _, v := range strings.Split(conf.MONGODB_ATTRIBUTE_INDEXES, ",") {
v = "attributes." + strings.TrimSpace(v)
c.EnsureIndex(mgo.Index{Key: []string{v}, Background: true})
}
}
}
func HasAttributeField(a string) bool {
for _, b := range strings.Split(conf.MONGODB_ATTRIBUTE_INDEXES, ",") {
if a == strings.TrimSpace(b) {
return true
}
}
return false
}
func dbDelete(q bson.M) (err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
_, err = c.RemoveAll(q)
return
}
func dbUpsert(n *Node) (err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
_, err = c.Upsert(bson.M{"id": n.Id}, &n)
return
}
func dbFind(q bson.M, results *Nodes, order string, options map[string]int) (count int, err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
if order == "" {
order = "created_on"
}
if limit, has := options["limit"]; has {
if offset, has := options["offset"]; has {
query := c.Find(q).Sort(order)
count, err = query.Count()
if err != nil {
count = 0
return
}
err = query.Limit(limit).Skip(offset).All(results)
if err != nil {
return
}
} else {
err = errors.New("store.db.Find options limit and offset must be used together")
return
}
} else {
err = c.Find(q).Sort(order).All(results)
if err != nil {
return
}
}
results.DBInit()
return
}
func dbFindD(q bson.D, results *Nodes, order string, options map[string]int) (count int, err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
if order == "" {
order = "created_on"
}
if limit, has := options["limit"]; has {
if offset, has := options["offset"]; has {
query := c.Find(q).Sort(order)
count, err = query.Count()
if err != nil {
count = 0
return
}
err = query.Limit(limit).Skip(offset).All(results)
if err != nil {
return
}
} else {
err = errors.New("store.db.Find options limit and offset must be used together")
return
}
} else {
err = c.Find(q).Sort(order).All(results)
if err != nil {
return
}
}
results.DBInit()
return
}
func DbFindDistinct(q bson.M, d string) (results interface{}, err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
err = c.Find(q).Distinct("attributes."+d, &results)
return
}
func Load(id string) (n *Node, err error) {
session := db.Connection.Session.Copy()
defer session.Close()
c := session.DB(conf.MONGODB_DATABASE).C("Nodes")
n = new(Node)
err = c.Find(bson.M{"id": id}).One(&n)
if err != nil {
n = nil
return
}
n.DBInit()
return
}
func LoadNodes(ids []string) (n Nodes, err error) {
if _, err = dbFind(bson.M{"id": bson.M{"$in": ids}}, &n, "", nil); err == nil {
return n, err
}
return nil, err
}
func ReloadFromDisk(path string) (err error) {
id := filepath.Base(path)
nbson, err := ioutil.ReadFile(path + "/" + id + ".bson")
if err != nil {
return
}
node := new(Node)
if err = bson.Unmarshal(nbson, &node); err == nil {
if err = dbUpsert(node); err != nil {
return err
}
}
return
}