-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
177 lines (148 loc) · 3.89 KB
/
database.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
166
167
168
169
170
171
172
173
174
175
176
177
package redis
import (
"context"
"fmt"
"github.com/go-redis/redis"
"libs.altipla.consulting/errors"
)
// Database keeps a connection to a Redis server.
type Database struct {
app string
directSess *redis.Client
}
// Open a new database connection to the remote Redis server.
func Open(hostname, applicationName string) *Database {
return &Database{
app: applicationName,
directSess: redis.NewClient(&redis.Options{Addr: hostname}),
}
}
// Close the connection to the remote database.
func (db *Database) Close() error {
return db.directSess.Close()
}
func (db *Database) StringsSet(key string) *StringsSet {
return &StringsSet{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int64Set(key string) *Int64Set {
return &Int64Set{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) StringKV(key string) *StringKV {
return &StringKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int32KV(key string) *Int32KV {
return &Int32KV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Int64KV(key string) *Int64KV {
return &Int64KV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoKV(key string) *ProtoKV {
return &ProtoKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoHash(key string) *ProtoHash {
return &ProtoHash{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) Counters(key string) *Counters {
return &Counters{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) BooleanKV(key string) *BooleanKV {
return &BooleanKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) TimeKV(key string) *TimeKV {
return &TimeKV{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) ProtoList(key string) *ProtoList {
return &ProtoList{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
func (db *Database) StringsList(key string) *StringsList {
return &StringsList{
db: db,
key: fmt.Sprintf("%s:%s", db.app, key),
}
}
// FlushAllKeysFromDatabase is exposed as a simple way for tests to reset the local
// database. It is not intended to be run in production. It will clean up all the keys
// of the whole database and leav an empty canvas to fill again.
func (db *Database) FlushAllKeysFromDatabase() error {
return db.directSess.FlushAll().Err()
}
// PubSub returns an entrypoint to a PubSub queue in redisIt can be used to publish
// and receive protobuf messages.
func (db *Database) PubSub(name string) *PubSub {
return &PubSub{
db: db,
name: fmt.Sprintf("%s:%s", db.app, name),
}
}
// Hash returns a hash instance that stores full models as individual hash keys.
func (db *Database) Hash(name string, model Model) *Hash {
props, err := extractModelProps(model)
if err != nil {
panic(err)
}
return &Hash{
db: db,
name: fmt.Sprintf("%s:%s", db.app, name),
props: props,
}
}
// Cmdable returns a reference to the direct Redis database connection. If context
// is inside a transaction it will return the Tx object instead. Both objects has
// the same Cmdable interface of the third party library.
func (db *Database) Cmdable(ctx context.Context) redis.Cmdable {
tx, ok := ctx.Value(keyTx).(redis.Pipeliner)
if ok {
return tx
}
return db.directSess
}
type key int
const (
keyTx = key(1)
)
// TransactionFn is a callback for transactions.
type TransactionFn func(ctx context.Context) error
// Transaction runs fn inside a transaction. You have to use the new context
// for every operation, otherwise they won't be transactional.
func (db *Database) Transaction(ctx context.Context, fn TransactionFn) error {
tx := db.directSess.TxPipeline()
ctx = context.WithValue(ctx, keyTx, tx)
if err := fn(ctx); err != nil {
return errors.Trace(err)
}
_, err := tx.Exec()
return errors.Trace(err)
}