-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgob.go
188 lines (155 loc) · 3.85 KB
/
gob.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
178
179
180
181
182
183
184
185
186
187
188
package gob
import (
"context"
"fmt"
"log"
"sync"
"time"
"github.com/csmadhu/gob/utils"
)
var (
defaultBatchSize = 10000
defaultDBProvider = DBProviderPg
defaultIdleConns = 2
defaultOpenConns = 10
defaultConnIdleTime = 3 * time.Second
defaultconnLifeTime = 3 * time.Second
defaultConnStr = "postgres://postgres:postgres@localhost:5432/gob?pool_max_conns=1"
)
// Gob provides APIs to upsert data in bulk
type Gob struct {
batchSize int // upsert rows in batches
dbProvider DBProvider // provider of database
connStr string // database conn string
idleConns int // max number of conns idle in pool
openConns int // max number of conns open to database
connIdleTime time.Duration // max amount of time conn may be idle
connLifeTime time.Duration // max amount of time conn may be reused
db // connection handler to database
dbMu sync.Mutex // mutex to synchornize connection handler
}
func defaultGob() *Gob {
return &Gob{
batchSize: defaultBatchSize,
dbProvider: defaultDBProvider,
connStr: defaultConnStr,
idleConns: defaultIdleConns,
openConns: defaultOpenConns,
connIdleTime: defaultConnIdleTime,
connLifeTime: defaultconnLifeTime,
}
}
// New returns Gob instance customized with options
func New(options ...Option) (*Gob, error) {
gob := defaultGob()
for _, option := range options {
if err := option(gob); err != nil {
return nil, err
}
}
var (
err error
args connArgs
)
args = connArgs{
connStr: gob.connStr,
idleConns: gob.idleConns,
openConns: gob.openConns,
connIdleTime: gob.connIdleTime,
connLifeTime: gob.connLifeTime,
}
switch gob.dbProvider {
case DBProviderPg:
gob.db, err = newPg(args)
case DBProviderMySQL:
gob.db, err = newMySQL(args)
case DBProviderCassandra:
gob.db, err = newCassandra(args)
default:
return nil, fmt.Errorf("gob: invalid dbProvider: %s", gob.dbProvider)
}
if err != nil {
return nil, err
}
return gob, nil
}
func (gob *Gob) setBatchSize(size int) {
gob.batchSize = size
}
func (gob *Gob) setDBProvider(dbProvider DBProvider) {
gob.dbProvider = dbProvider
}
func (gob *Gob) setConnStr(connStr string) {
gob.connStr = connStr
}
func (gob *Gob) setConnIdleTime(d time.Duration) {
gob.connIdleTime = d
}
func (gob *Gob) setConnLifeTime(d time.Duration) {
gob.connLifeTime = d
}
func (gob *Gob) setIdleConns(n int) {
gob.idleConns = n
}
func (gob *Gob) setOpenConns(n int) {
gob.openConns = n
}
func (gob *Gob) getDB() db {
gob.dbMu.Lock()
defer gob.dbMu.Unlock()
return gob.db
}
// Upsert rows to model
func (gob *Gob) Upsert(ctx context.Context, args UpsertArgs) error {
var gobDB db
gobDB = gob.getDB()
// conn closed
if gobDB == nil {
return ErrConnClosed
}
// model not specified
if args.Model == "" {
return ErrEmptyModel
}
// zero rows
if len(args.Rows) == 0 {
return nil
}
// empty conflict action
if args.ConflictAction == "" {
return ErrEmptyConflictAction
}
var (
start = 0
end = gob.batchSize
t0 = time.Now()
upsertArgs UpsertArgs // required to avoid copy of rows
)
upsertArgs.ConflictAction = args.ConflictAction
upsertArgs.Model = args.Model
upsertArgs.keySet = utils.NewStringSet(args.Keys...)
upsertArgs.Keys = upsertArgs.keySet.ToSlice()
if len(args.Rows) <= gob.batchSize {
end = len(args.Rows)
}
for start < len(args.Rows) {
upsertArgs.Rows = args.Rows[start:end]
if err := gob.upsert(ctx, upsertArgs); err != nil {
return err
}
start = start + gob.batchSize
end = end + gob.batchSize
if end > len(args.Rows) {
end = len(args.Rows)
}
}
log.Printf("gob: upsert %d rows to model '%s' in %v", len(args.Rows), args.Model, time.Since(t0))
return nil
}
// Close the resources
func (gob *Gob) Close() {
gob.dbMu.Lock()
defer gob.dbMu.Unlock()
gob.db.close()
gob.db = nil
}