forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_executor.go
93 lines (80 loc) · 2.8 KB
/
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
84
85
86
87
88
89
90
91
92
93
package ravendb
import (
"net/http"
"strings"
)
type OperationExecutor struct {
store *DocumentStore
databaseName string
requestExecutor *RequestExecutor
}
func NewOperationExecutor(store *DocumentStore, databaseName string) *OperationExecutor {
res := &OperationExecutor{
store: store,
databaseName: databaseName,
}
if res.databaseName == "" {
res.databaseName = store.GetDatabase()
}
panicIf(res.databaseName == "", "databaseName is empty")
res.requestExecutor = store.GetRequestExecutor(res.databaseName)
return res
}
func (e *OperationExecutor) ForDatabase(databaseName string) *OperationExecutor {
if strings.EqualFold(e.databaseName, databaseName) {
return e
}
return NewOperationExecutor(e.store, databaseName)
}
// Note: we don't return a result because we could only return interface{}
// The caller has access to operation and can access strongly typed
// command and its result
// sessionInfo can be nil
func (e *OperationExecutor) Send(operation IOperation, sessionInfo *SessionInfo) error {
command, err := operation.GetCommand(e.store, e.requestExecutor.GetConventions(), e.requestExecutor.Cache)
if err != nil {
return err
}
return e.requestExecutor.ExecuteCommand(command, sessionInfo)
}
// sessionInfo can be nil
func (e *OperationExecutor) SendAsync(operation IOperation, sessionInfo *SessionInfo) (*Operation, error) {
command, err := operation.GetCommand(e.store, e.requestExecutor.GetConventions(), e.requestExecutor.Cache)
if err != nil {
return nil, err
}
if err = e.requestExecutor.ExecuteCommand(command, sessionInfo); err != nil {
return nil, err
}
changes := func() *DatabaseChanges {
return e.store.Changes("")
}
result := getCommandOperationIDResult(command)
return NewOperation(e.requestExecutor, changes, e.requestExecutor.GetConventions(), result.OperationID), nil
}
// Note: use SendPatchOperation() instead and check PatchOperationResult.Status
// public PatchStatus Send(PatchOperation operation) {
// public PatchStatus Send(PatchOperation operation, SessionInfo sessionInfo) {
func (e *OperationExecutor) SendPatchOperation(operation *PatchOperation, sessionInfo *SessionInfo) (*PatchOperationResult, error) {
conventions := e.requestExecutor.GetConventions()
cache := e.requestExecutor.Cache
command, err := operation.GetCommand(e.store, conventions, cache)
if err != nil {
return nil, err
}
if err = e.requestExecutor.ExecuteCommand(command, sessionInfo); err != nil {
return nil, err
}
cmdResult := operation.Command.Result
result := &PatchOperationResult{
Status: cmdResult.Status,
Document: cmdResult.ModifiedDocument,
}
switch operation.Command.StatusCode {
case http.StatusNotModified:
result.Status = PatchStatusNotModified
case http.StatusNotFound:
result.Status = PatchStatusDocumentDoesNotExist
}
return result, nil
}