-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb.go
133 lines (110 loc) · 3.09 KB
/
mongodb.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
/*
Copyright (C) 2003-2011 Institute for Systems Biology
Seattle, Washington, USA.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import (
"os"
"launchpad.net/mgo"
"launchpad.net/gobson/bson"
)
type JsonStore struct {
Domain string
Host string
Database string
}
func (this *JsonStore) GetCollection() (c mgo.Collection, err os.Error) {
logger.Debug("GetCollection()")
session, err := mgo.Mongo(this.Host)
if err != nil {
return
}
session.SetMode(mgo.Strong, true) // [Safe, Monotonic, Strong] Strong syncs on inserts/updates
db := session.DB(this.Database)
c = db.C(this.Domain)
return
}
func (this *JsonStore) Create(itemId string, item interface{}) (err os.Error) {
logger.Debug("Create(%v,%v)", itemId, item)
// item["Id"] = itemId
collection, err := this.GetCollection()
if err != nil {
logger.Warn(err)
return
}
if err = collection.Insert(item); err != nil {
logger.Warn(err)
}
return
}
func (this *JsonStore) Get(itemId string) (item interface{}, err os.Error) {
logger.Debug("Get(%v)", itemId)
collection, err := this.GetCollection()
if err != nil {
logger.Warn(err)
return
}
if err = collection.Find(bson.M{"Id": itemId}).One(&item); err != nil {
logger.Warn(err)
}
return
}
func (this *JsonStore) Update(itemId string, item interface{}) (err os.Error) {
logger.Debug("Update(%v,%v)", itemId, item)
if itemId == "" {
return os.NewError("Existing Item Not Found")
}
collection, err := this.GetCollection()
if err != nil {
logger.Warn(err)
return
}
return collection.Update(bson.M{"Id": itemId}, item)
}
func (this *JsonStore) Find(m map[string][]string) (items []interface{}, err os.Error) {
// queryByValues := bson.M{}
// for key, val := range values {
// queryByValues[key] = val
// }
// return this.Find(values)
logger.Debug("Find(%v)", m)
collection, err := this.GetCollection()
if err != nil {
logger.Warn(err)
return
}
iter, err := collection.Find(m).Iter()
if err != nil {
logger.Warn(err)
return
}
for {
item := new(interface{})
if nexterr := iter.Next(&item); nexterr != nil {
logger.Warn(nexterr)
break
}
items = append(items, item)
}
return
}
func (this *JsonStore) Count(m map[string]interface{}) (int, os.Error) {
logger.Debug("Count(%v)", m)
collection, err := this.GetCollection()
if err != nil {
logger.Warn(err)
return 0, err
}
return collection.Find(m).Count()
}