-
Notifications
You must be signed in to change notification settings - Fork 61
/
maintenance_manager.go
289 lines (251 loc) · 9.33 KB
/
maintenance_manager.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
The MIT License (MIT)
Copyright (c) 2023-2024 Artyom Smirnov <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
package firebirdsql
type MaintenanceManager struct {
connBuilder func() (*ServiceManager, error)
}
func NewMaintenanceManager(addr string, user string, password string, options ServiceManagerOptions) (*MaintenanceManager, error) {
connBuilder := func() (*ServiceManager, error) {
return NewServiceManager(addr, user, password, options)
}
return &MaintenanceManager{
connBuilder: connBuilder,
}, nil
}
func (mm *MaintenanceManager) setAccessMode(database string, mode byte) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutByte(isc_spb_prp_access_mode, mode)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) SetAccessModeReadWrite(database string) error {
return mm.setAccessMode(database, isc_spb_prp_am_readwrite)
}
func (mm *MaintenanceManager) SetAccessModeReadOnly(database string) error {
return mm.setAccessMode(database, isc_spb_prp_am_readonly)
}
func (mm *MaintenanceManager) SetDialect(database string, dialect int) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_prp_set_sql_dialect, int32(dialect))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) SetPageBuffers(database string, pageCount int) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_prp_page_buffers, int32(pageCount))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) setWriteMode(database string, mode byte) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutByte(isc_spb_prp_write_mode, mode)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) SetWriteModeAsync(database string) error {
return mm.setWriteMode(database, isc_spb_prp_wm_async)
}
func (mm *MaintenanceManager) SetWriteModeSync(database string) error {
return mm.setWriteMode(database, isc_spb_prp_wm_sync)
}
func (mm *MaintenanceManager) setPageFill(database string, mode byte) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutByte(isc_spb_prp_reserve_space, mode)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) SetPageFillNoReserve(database string) error {
return mm.setPageFill(database, isc_spb_prp_res_use_full)
}
func (mm *MaintenanceManager) SetPageFillReserve(database string) error {
return mm.setPageFill(database, isc_spb_prp_res)
}
func (mm *MaintenanceManager) Shutdown(database string, shutdownMode ShutdownMode, timeout uint) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(byte(shutdownMode), int32(timeout))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) Online(database string) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_prp_db_online)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) NoLinger(database string) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_prp_nolinger)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) ShutdownEx(database string, operationMode OperationMode, shutdownModeEx ShutdownModeEx, timeout uint) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutByte(isc_spb_prp_shutdown_mode, byte(operationMode))
spb.PutInt32(byte(shutdownModeEx), int32(timeout))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) OnlineEx(database string, operationMode OperationMode) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutByte(isc_spb_prp_online_mode, byte(operationMode))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) SetSweepInterval(database string, transactions uint) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_prp_sweep_interval, int32(transactions))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) Sweep(database string) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_rpr_sweep_db)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) ActivateShadow(shadow string) error {
spb := NewXPBWriterFromTag(isc_action_svc_properties)
spb.PutString(isc_spb_dbname, shadow)
spb.PutInt32(isc_spb_options, isc_spb_prp_activate)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) KillShadow(database string) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_rpr_kill_shadows)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) Mend(database string) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_rpr_mend_db)
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) Validate(database string, options int) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_rpr_validate_db|int32(options))
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) GetLimboTransactions(database string) ([]int64, error) {
var (
err error
resChan = make(chan []byte)
done = make(chan bool)
cont = true
buf []byte
tids []int64
)
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
spb.PutInt32(isc_spb_options, isc_spb_rpr_list_limbo_trans)
go func() {
err = mm.attachBuffer(spb.Bytes(), resChan)
done <- true
}()
for cont {
select {
case buf = <-resChan:
srb := NewXPBReader(buf)
var (
have bool
val byte
)
for {
if have, val = srb.Next(); !have {
break
}
switch val {
case isc_spb_single_tra_id:
fallthrough
case isc_spb_multi_tra_id:
tids = append(tids, int64(srb.GetInt32()))
case isc_spb_single_tra_id_64:
fallthrough
case isc_spb_multi_tra_id_64:
tids = append(tids, srb.GetInt64())
case isc_spb_tra_id:
srb.Skip(4)
case isc_spb_tra_id_64:
srb.Skip(8)
case isc_spb_tra_state:
fallthrough
case isc_spb_tra_advise:
srb.Skip(1)
case isc_spb_tra_host_site:
fallthrough
case isc_spb_tra_remote_site:
fallthrough
case isc_spb_tra_db_path:
srb.GetString()
}
}
case <-done:
cont = false
break
}
}
return tids, err
}
func (mm *MaintenanceManager) CommitTransaction(database string, transaction int64) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
if fitsUint32(transaction) {
spb.PutInt32(isc_spb_rpr_commit_trans, int32(transaction))
} else {
spb.PutInt64(isc_spb_rpr_commit_trans_64, transaction)
}
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) RollbackTransaction(database string, transaction int64) error {
spb := NewXPBWriterFromTag(isc_action_svc_repair)
spb.PutString(isc_spb_dbname, database)
if fitsUint32(transaction) {
spb.PutInt32(isc_spb_rpr_rollback_trans, int32(transaction))
} else {
spb.PutInt64(isc_spb_rpr_rollback_trans_64, transaction)
}
return mm.attach(spb.Bytes(), nil)
}
func (mm *MaintenanceManager) attach(spb []byte, verbose chan string) error {
var (
err error
conn *ServiceManager
)
if conn, err = mm.connBuilder(); err != nil {
return err
}
defer func(conn *ServiceManager) {
_ = conn.Close()
}(conn)
return conn.ServiceAttach(spb, verbose)
}
func (mm *MaintenanceManager) attachBuffer(spb []byte, verbose chan []byte) error {
var (
err error
conn *ServiceManager
)
if conn, err = mm.connBuilder(); err != nil {
return err
}
defer func(conn *ServiceManager) {
_ = conn.Close()
}(conn)
return conn.ServiceAttachBuffer(spb, verbose)
}