-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao.go
52 lines (40 loc) · 880 Bytes
/
dao.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
package main
import "gopkg.in/mgo.v2"
type Dao struct {
Instance *mgo.Session
Settings Settings
}
func NewDao(settings Settings) *Dao {
dao := new(Dao)
session, err := mgo.Dial(settings.HostName)
if err != nil {
panic(err) // db not responding is a good reason to panic
}
session.SetMode(mgo.Monotonic, true)
dao.Instance = session
dao.Settings = settings
return dao
}
func (dao *Dao) Close() {
dao.Instance.Close()
}
func (dao *Dao) GetInstance() *Dao {
return &Dao{dao.Instance.Copy(), dao.Settings}
}
func (dao *Dao) Database() *mgo.Database {
return dao.Instance.DB(dao.Settings.DbName)
}
func (dao *Dao) EnsureIndex() {
c := dao.Database().C("palindromes")
index := mgo.Index{
Key: []string{"phrase"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}