-
Notifications
You must be signed in to change notification settings - Fork 11
/
helper.go
89 lines (83 loc) · 1.84 KB
/
helper.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
package wizard
// UseMaster returns db master
func (w *Wizard) UseMaster(obj interface{}) interface{} {
cluster := w.Select(obj)
if cluster == nil {
return nil
}
db := cluster.Master()
if db == nil {
return nil
}
return db.DB()
}
// UseMasters returns all db master instances for sharding
func (w *Wizard) UseMasters(obj interface{}) []interface{} {
var results []interface{}
c := w.getCluster(obj)
if c == nil {
return results
}
for _, node := range c.Masters() {
db := node.DB()
if db == nil {
continue
}
results = append(results, db)
}
return results
}
// UseSlave randomly returns db slave from the slaves
// if any slave is not set, master is returned
func (w *Wizard) UseSlave(obj interface{}) interface{} {
cluster := w.Select(obj)
if cluster == nil {
return nil
}
db := cluster.Slave()
if db == nil {
return nil
}
return db.DB()
}
// UseSlaves randomly returns all db slave instances for sharding
func (w *Wizard) UseSlaves(obj interface{}) []interface{} {
var results []interface{}
c := w.getCluster(obj)
if c == nil {
return results
}
for _, node := range c.Slaves() {
db := node.DB()
if db == nil {
continue
}
results = append(results, db)
}
return results
}
// UseMasterByKey returns db master for sharding by shard key
func (w *Wizard) UseMasterByKey(obj interface{}, key interface{}) interface{} {
cluster := w.SelectByKey(obj, key)
if cluster == nil {
return nil
}
db := cluster.Master()
if db == nil {
return nil
}
return db.DB()
}
// UseSlaveByKey randomly returns db slave for sharding by shard key
// if any slave is not set in the cluster, master is returned
func (w *Wizard) UseSlaveByKey(obj interface{}, key interface{}) interface{} {
cluster := w.SelectByKey(obj, key)
if cluster == nil {
return nil
}
db := cluster.Slave()
if db == nil {
return nil
}
return db.DB()
}