-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbatch.go
205 lines (193 loc) · 5.55 KB
/
batch.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package dsc
import (
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"strings"
)
type batch struct {
processed int
tempDir string
tempFile string
size int
sql string
writer *gzip.Writer
values []interface{}
placeholders string
columns string
dataIndexes []int
firstSeq int64
bulkInsertType string
manager *AbstractManager
sqlProvider func(item interface{}) *ParametrizedSQL
updateId func(index int, seq int64)
connection Connection
table string
}
func (b *batch) flush() (int, error) {
if b.sql == "" {
return 0, nil
}
var dataIndexes = b.dataIndexes
b.dataIndexes = []int{}
switch b.bulkInsertType {
case CopyLocalInsert:
defer os.Remove(b.tempFile)
err := b.writer.Flush()
if err != nil {
return 0, err
}
err = b.writer.Close()
if err != nil {
return 0, err
}
case BulkInsertAllType:
b.sql += " SELECT 1 FROM DUAL"
}
result, err := b.manager.ExecuteOnConnection(b.connection, b.sql, b.values)
b.dataIndexes = []int{}
b.sql = ""
b.values = []interface{}{}
if err != nil {
return 0, err
}
affected, err := result.RowsAffected()
if err != nil {
return 0, err
}
for _, i := range dataIndexes {
b.firstSeq++
b.updateId(i, b.firstSeq)
}
b.firstSeq = 0
return int(affected), nil
}
func (b *batch) expandedValues(parametrizedSQL *ParametrizedSQL) string {
recordLine := b.manager.ExpandSQL(b.placeholders, parametrizedSQL.Values)
if breakCount := strings.Count(recordLine, "\n"); breakCount > 0 {
recordLine = strings.Replace(recordLine, "\n", "", breakCount)
}
return recordLine + "\n"
}
func (b *batch) transformFirst(parametrizedSQL *ParametrizedSQL) error {
b.sql = parametrizedSQL.SQL
b.values = parametrizedSQL.Values
fragment := " VALUES"
valuesIndex := strings.Index(parametrizedSQL.SQL, fragment)
if beginIndex := strings.Index(parametrizedSQL.SQL, "("); beginIndex != -1 {
names := string(parametrizedSQL.SQL[beginIndex+1:])
if endIndex := strings.Index(names, ")"); endIndex != -1 {
b.columns = string(names[:endIndex])
}
}
b.placeholders = strings.Trim(strings.TrimSpace(string(parametrizedSQL.SQL[valuesIndex+7:])), "()")
switch b.bulkInsertType {
case CopyLocalInsert:
b.tempDir = b.manager.config.GetString("tempDir", os.TempDir())
if b.columns == "" {
return fmt.Errorf("columns were empty")
}
file, err := ioutil.TempFile(b.tempDir, "temp")
if err != nil {
return err
}
b.tempFile = file.Name()
b.writer = gzip.NewWriter(file)
if _, err := b.writer.Write([]byte(b.expandedValues(parametrizedSQL))); err != nil {
return err
}
table := b.table
b.sql = fmt.Sprintf(`COPY %v(%v)
FROM LOCAL '%v' GZIP
DELIMITER ','
NULL AS 'null'
ENCLOSED BY ''''
`, table, b.columns, file.Name())
b.values = make([]interface{}, 0)
case UnionSelectInsert:
valuesIndex := strings.Index(parametrizedSQL.SQL, " VALUES")
selectAll := " SELECT " + b.expandedValues(parametrizedSQL)
selectAll = b.manager.ExpandSQL(selectAll, parametrizedSQL.Values)
parametrizedSQL.Values = []interface{}{}
b.sql = b.sql[:valuesIndex] + " " + selectAll
case BulkInsertAllType:
b.sql = strings.Replace(b.sql, "INSERT ", "INSERT ALL ", 1)
default:
}
return nil
}
func (b *batch) transformNext(parametrizedSQL *ParametrizedSQL) error {
switch b.bulkInsertType {
case CopyLocalInsert:
_, err := b.writer.Write([]byte(b.expandedValues(parametrizedSQL)))
return err
case UnionSelectInsert:
b.sql += "\nUNION ALL SELECT " + b.expandedValues(parametrizedSQL)
case BulkInsertAllType:
b.sql += fmt.Sprintf("\nINTO %v(%v) VALUES(%v)", b.table, b.columns, b.placeholders)
b.values = append(b.values, parametrizedSQL.Values...)
default:
b.sql += fmt.Sprintf(",(%v)", b.placeholders)
b.values = append(b.values, parametrizedSQL.Values...)
}
return nil
}
func (b *batch) persist(index int, item interface{}) error {
parametrizedSQL := b.sqlProvider(item)
if len(parametrizedSQL.Values) == 1 && parametrizedSQL.Type == SQLTypeUpdate {
//nothing to udpate, one parameter is ID=? without values to update
return nil
}
if parametrizedSQL.Type == SQLTypeInsert && b.size > 0 {
if len(b.dataIndexes) > b.size {
if _, err := b.flush(); err != nil {
return err
}
}
b.dataIndexes = append(b.dataIndexes, index)
if isFirst := len(b.sql) == 0; isFirst {
return b.transformFirst(parametrizedSQL)
}
return b.transformNext(parametrizedSQL)
}
result, err := b.manager.ExecuteOnConnection(b.connection, parametrizedSQL.SQL, parametrizedSQL.Values)
if err != nil {
return err
}
affected, err := result.RowsAffected()
if err != nil {
return err
}
b.processed += int(affected)
seq, _ := result.LastInsertId()
if b.size > 0 && b.firstSeq == 0 {
b.firstSeq = seq
}
b.updateId(index, seq)
return nil
}
func newBatch(table string, connection Connection, manager *AbstractManager, sqlProvider func(item interface{}) *ParametrizedSQL, updateId func(index int, seq int64)) *batch {
dialect := GetDatastoreDialect(manager.Config().DriverName)
var batchSize = manager.Config().GetInt(BatchSizeKey, defaultBatchSize)
Logf("batch size: %v\n", batchSize)
canUseBatch := dialect != nil && dialect.CanPersistBatch() && batchSize > 1
if !canUseBatch {
batchSize = 0
}
insertType := ""
if dialect != nil {
insertType = dialect.BulkInsertType()
}
return &batch{
connection: connection,
updateId: updateId,
sqlProvider: sqlProvider,
size: batchSize,
values: []interface{}{},
dataIndexes: []int{},
bulkInsertType: insertType,
manager: manager,
table: table,
}
}