-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_connection.go
69 lines (51 loc) · 1.39 KB
/
shard_connection.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
package goshard
import "fmt"
// define an interface to return another interface
type ShardConfig interface {
GetShardLookup() ShardLookup
NewShardConnection(uint64) (error, *ShardConnection)
NewShardConnectionByShardId(uint) (error, *ShardConnection)
}
// this holds the Shard connection
type ShardConnection struct {
Connection
ShardId uint
}
// our test type imp the ShardConfig interface
type ShotsShardConfig struct{}
func (s ShotsShardConfig) GetShardLookup() ShardLookup {
var cp []*ConnectionParams
var i uint = 1
for ; i <= 55; i++ {
cp = append(cp, &ConnectionParams{
Host: "localhost",
Dbname: fmt.Sprintf("usershard%d", i),
User: "hanksapi",
Password: "H@nk$@p11p@$kn@H",
ShardId: i,
})
}
rsl := NewShardLookup(cp)
return rsl
}
//set up all the hosts
func (sconf ShotsShardConfig) NewShardConnection(entity_id uint64) (error, *ShardConnection) {
rsl := sconf.GetShardLookup()
err, c := rsl.Lookup(entity_id)
if err != nil {
return err, nil
}
err, rc := NewConnection(c)
sc := &ShardConnection{*rc, c.ShardId}
return err, sc
}
//set up all the hosts
func (sconf ShotsShardConfig) NewShardConnectionByShardId(shard_id uint) (error, *ShardConnection) {
rsl := sconf.GetShardLookup()
cs := rsl.GetAll()
shard_id = shard_id - 1 // 0 based
c := cs[shard_id]
err, rc := NewConnection(c)
sc := &ShardConnection{*rc, c.ShardId}
return err, sc
}