This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_behavior.go
165 lines (139 loc) · 3.03 KB
/
mongodb_behavior.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
package main
import (
"errors"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
_ "log"
"math/rand"
"strconv"
"time"
)
const (
DEFAULT_MONGO_DATABASE string = "knock"
DEFAULT_MONGO_COLLECTION string = "userdata"
DEFAULT_MONGO_WRITE_CONCERN int = 1
)
type MongoBehaviorInfo struct {
session *mgo.Session
writeConcern int
fieldcount int
properties map[string]string
collection func() *mgo.Collection
}
type MongoBehavior interface {
Init(info *MongoBehaviorInfo) (err error)
Close()
Work() (res WorkResult)
}
type mongodb_behavior struct {
s *mgo.Session
properties map[string]string
url string
db string
collectionName string
writeConcern int
fieldcount int
mb MongoBehavior
}
type M bson.M
func (this *mongodb_behavior) Init(props map[string]string) (err error) {
err = this.parseProperties(props)
if err != nil {
return
}
err = this.dial()
if err != nil {
return
}
info := &MongoBehaviorInfo{
this.s,
this.writeConcern,
this.fieldcount,
this.properties,
func() *mgo.Collection {
return this.s.DB(this.db).C(this.collectionName)
},
}
err = this.mb.Init(info)
if err != nil {
return
}
return
}
func (this *mongodb_behavior) Close() {
defer this.s.Close()
this.mb.Close()
}
func (this *mongodb_behavior) Work(t0 time.Time) (res WorkResult) {
return this.mb.Work()
}
func (this *mongodb_behavior) parseProperties(props map[string]string) (err error) {
this.properties = props
if v, ok := props["mongodb.run"]; ok {
switch v {
case "counters":
this.mb = &mongodb_counters{}
case "writes":
this.mb = &mongodb_writes{}
default:
return errors.New("mongodb.run must be one of counters, writes")
}
} else {
return errors.New("mongodb.run is a required property")
}
if v, ok := props["mongodb.url"]; ok {
this.url = v
} else {
return errors.New("mongodb.url is a required property")
}
if v, ok := props["mongodb.database"]; ok {
this.db = v
} else {
this.db = DEFAULT_MONGO_DATABASE
}
if v, ok := props["mongodb.writeConcern"]; ok {
switch v {
case "none":
this.writeConcern = -1
case "w=0":
this.writeConcern = 0
case "w=1":
this.writeConcern = 1
default:
return errors.New("mongodb.writeConcern must be one of none, w=0, w=1")
}
} else {
this.writeConcern = DEFAULT_MONGO_WRITE_CONCERN
}
if v, ok := props["fieldcount"]; ok {
u, err := strconv.Atoi(v)
if err != nil || u <= 0 {
return errors.New("fieldcount must be > 0")
}
this.fieldcount = u
} else {
this.fieldcount = 10
}
this.collectionName = DEFAULT_MONGO_COLLECTION
return
}
func (this *mongodb_behavior) dial() (err error) {
session, err := mgo.Dial(this.url)
if err != nil {
return
}
this.s = session
var safe *mgo.Safe
switch {
case this.writeConcern == -1:
safe = nil
case this.writeConcern >= 0:
safe = &mgo.Safe{W: this.writeConcern}
}
this.s.SetSafe(safe)
return
}
func randomFieldName(fieldcount int) (name string) {
return fmt.Sprintf("field-%d", rand.Intn(fieldcount))
}