forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintenance_operation_executor.go
83 lines (73 loc) · 2.44 KB
/
maintenance_operation_executor.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
package ravendb
import "strings"
type MaintenanceOperationExecutor struct {
store *DocumentStore
databaseName string
requestExecutor *RequestExecutor
serverOperationExecutor *ServerOperationExecutor
}
func NewMaintenanceOperationExecutor(store *DocumentStore, databaseName string) *MaintenanceOperationExecutor {
res := &MaintenanceOperationExecutor{
store: store,
databaseName: firstNonEmptyString(databaseName, store.GetDatabase()),
}
if res.databaseName != "" {
res.requestExecutor = store.GetRequestExecutor(res.databaseName)
}
return res
}
func (e *MaintenanceOperationExecutor) GetRequestExecutor() *RequestExecutor {
if e.requestExecutor != nil {
return e.requestExecutor
}
if e.databaseName != "" {
e.requestExecutor = e.store.GetRequestExecutor(e.databaseName)
}
return e.requestExecutor
}
func (e *MaintenanceOperationExecutor) Server() *ServerOperationExecutor {
if e.serverOperationExecutor == nil {
e.serverOperationExecutor = NewServerOperationExecutor(e.store)
}
return e.serverOperationExecutor
}
func (e *MaintenanceOperationExecutor) ForDatabase(databaseName string) *MaintenanceOperationExecutor {
if strings.EqualFold(e.databaseName, databaseName) {
return e
}
return NewMaintenanceOperationExecutor(e.store, databaseName)
}
func (e *MaintenanceOperationExecutor) Send(operation IMaintenanceOperation) error {
if err := e.assertDatabaseNameSet(); err != nil {
return err
}
command, err := operation.GetCommand(e.GetRequestExecutor().GetConventions())
if err != nil {
return err
}
return e.GetRequestExecutor().ExecuteCommand(command, nil)
}
func (e *MaintenanceOperationExecutor) SendAsync(operation IMaintenanceOperation) (*Operation, error) {
if err := e.assertDatabaseNameSet(); err != nil {
return nil, err
}
command, err := operation.GetCommand(e.GetRequestExecutor().GetConventions())
if err != nil {
return nil, err
}
if err = e.GetRequestExecutor().ExecuteCommand(command, nil); err != nil {
return nil, err
}
fn := func() *DatabaseChanges {
return e.store.Changes("")
}
re := e.GetRequestExecutor()
id := getCommandOperationIDResult(command)
return NewOperation(re, fn, re.GetConventions(), id.OperationID), nil
}
func (e *MaintenanceOperationExecutor) assertDatabaseNameSet() error {
if e.databaseName == "" {
return newIllegalStateError("Cannot use maintenance without a database defined, did you forget to call forDatabase?")
}
return nil
}