github.com/altipla-consulting/ravendb-go-client@v0.1.3/set_indexes_lock_operation.go (about) 1 package ravendb 2 3 import ( 4 "net/http" 5 "strings" 6 ) 7 8 var _ IVoidMaintenanceOperation = &SetIndexesLockOperation{} 9 10 type SetIndexesLockOperation struct { 11 parameters *SetIndexesLockParameters 12 Command *SetIndexesLockCommand 13 } 14 15 func NewSetIndexesLockOperation(indexName string, mode IndexLockMode) (*SetIndexesLockOperation, error) { 16 if indexName == "" { 17 return nil, newIllegalArgumentError("indexName cannot be empty") 18 } 19 20 p := &SetIndexesLockParameters{ 21 IndexNames: []string{indexName}, 22 Mode: mode, 23 } 24 return NewSetIndexesLockOperationWithParameters(p) 25 } 26 27 func NewSetIndexesLockOperationWithParameters(parameters *SetIndexesLockParameters) (*SetIndexesLockOperation, error) { 28 if parameters == nil { 29 return nil, newIllegalArgumentError("parameters cannot be nil") 30 } 31 32 res := &SetIndexesLockOperation{ 33 parameters: parameters, 34 } 35 if err := res.filterAutoIndexes(); err != nil { 36 return nil, err 37 } 38 return res, nil 39 } 40 41 func (o *SetIndexesLockOperation) filterAutoIndexes() error { 42 // Check for auto-indexes - we do not set lock for auto-indexes 43 for _, indexName := range o.parameters.IndexNames { 44 s := strings.ToLower(indexName) 45 if strings.HasPrefix(s, "auto/") { 46 return newIllegalArgumentError("Indexes list contains Auto-Indexes. Lock Mode is not set for Auto-Indexes.") 47 } 48 } 49 return nil 50 } 51 52 func (o *SetIndexesLockOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) { 53 var err error 54 o.Command, err = NewSetIndexesLockCommand(conventions, o.parameters) 55 if err != nil { 56 return nil, err 57 } 58 return o.Command, nil 59 } 60 61 var ( 62 _ RavenCommand = &SetIndexesLockCommand{} 63 ) 64 65 type SetIndexesLockCommand struct { 66 RavenCommandBase 67 68 parameters []byte 69 } 70 71 func NewSetIndexesLockCommand(conventions *DocumentConventions, parameters *SetIndexesLockParameters) (*SetIndexesLockCommand, error) { 72 if conventions == nil { 73 return nil, newIllegalArgumentError("conventions cannot be null") 74 } 75 if parameters == nil { 76 return nil, newIllegalArgumentError("parameters cannot be null") 77 } 78 79 // Note: compared to Java, we shortcut things by serializing to JSON 80 // here as it's simpler and faster than two-step serialization, 81 // first to ObjectNode and then to JSON 82 d, err := jsonMarshal(parameters) 83 if err != nil { 84 return nil, err 85 } 86 cmd := &SetIndexesLockCommand{ 87 RavenCommandBase: NewRavenCommandBase(), 88 89 parameters: d, 90 } 91 cmd.ResponseType = RavenCommandResponseTypeEmpty 92 return cmd, nil 93 } 94 95 func (c *SetIndexesLockCommand) createRequest(node *ServerNode) (*http.Request, error) { 96 url := node.URL + "/databases/" + node.Database + "/indexes/set-lock" 97 98 return newHttpPost(url, c.parameters) 99 } 100 101 // Note: in Java it's Parameters class nested in SetIndexesLockOperation 102 // Parameters is already taken 103 type SetIndexesLockParameters struct { 104 IndexNames []string `json:"IndexNames"` 105 Mode IndexLockMode `json:"Mode"` 106 }