forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_indexes_lock_operation.go
106 lines (88 loc) · 2.81 KB
/
set_indexes_lock_operation.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
package ravendb
import (
"net/http"
"strings"
)
var _ IVoidMaintenanceOperation = &SetIndexesLockOperation{}
type SetIndexesLockOperation struct {
parameters *SetIndexesLockParameters
Command *SetIndexesLockCommand
}
func NewSetIndexesLockOperation(indexName string, mode IndexLockMode) (*SetIndexesLockOperation, error) {
if indexName == "" {
return nil, newIllegalArgumentError("indexName cannot be empty")
}
p := &SetIndexesLockParameters{
IndexNames: []string{indexName},
Mode: mode,
}
return NewSetIndexesLockOperationWithParameters(p)
}
func NewSetIndexesLockOperationWithParameters(parameters *SetIndexesLockParameters) (*SetIndexesLockOperation, error) {
if parameters == nil {
return nil, newIllegalArgumentError("parameters cannot be nil")
}
res := &SetIndexesLockOperation{
parameters: parameters,
}
if err := res.filterAutoIndexes(); err != nil {
return nil, err
}
return res, nil
}
func (o *SetIndexesLockOperation) filterAutoIndexes() error {
// Check for auto-indexes - we do not set lock for auto-indexes
for _, indexName := range o.parameters.IndexNames {
s := strings.ToLower(indexName)
if strings.HasPrefix(s, "auto/") {
return newIllegalArgumentError("Indexes list contains Auto-Indexes. Lock Mode is not set for Auto-Indexes.")
}
}
return nil
}
func (o *SetIndexesLockOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
var err error
o.Command, err = NewSetIndexesLockCommand(conventions, o.parameters)
if err != nil {
return nil, err
}
return o.Command, nil
}
var (
_ RavenCommand = &SetIndexesLockCommand{}
)
type SetIndexesLockCommand struct {
RavenCommandBase
parameters []byte
}
func NewSetIndexesLockCommand(conventions *DocumentConventions, parameters *SetIndexesLockParameters) (*SetIndexesLockCommand, error) {
if conventions == nil {
return nil, newIllegalArgumentError("conventions cannot be null")
}
if parameters == nil {
return nil, newIllegalArgumentError("parameters cannot be null")
}
// Note: compared to Java, we shortcut things by serializing to JSON
// here as it's simpler and faster than two-step serialization,
// first to ObjectNode and then to JSON
d, err := jsonMarshal(parameters)
if err != nil {
return nil, err
}
cmd := &SetIndexesLockCommand{
RavenCommandBase: NewRavenCommandBase(),
parameters: d,
}
cmd.ResponseType = RavenCommandResponseTypeEmpty
return cmd, nil
}
func (c *SetIndexesLockCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/indexes/set-lock"
return NewHttpPost(url, c.parameters)
}
// Note: in Java it's Parameters class nested in SetIndexesLockOperation
// Parameters is already taken
type SetIndexesLockParameters struct {
IndexNames []string `json:"IndexNames"`
Mode IndexLockMode `json:"Mode"`
}