forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathput_indexes_operation.go
94 lines (76 loc) · 2.34 KB
/
put_indexes_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
package ravendb
import (
"net/http"
)
var (
_ IMaintenanceOperation = &PutIndexesOperation{}
)
// PutIndexesOperation represents put indexes operation
type PutIndexesOperation struct {
indexToAdd []*IndexDefinition
Command *PutIndexesCommand
}
// NewPutIndexesOperation returns new PutIndexesOperation
func NewPutIndexesOperation(indexToAdd ...*IndexDefinition) *PutIndexesOperation {
return &PutIndexesOperation{
indexToAdd: indexToAdd,
}
}
// GetCommand returns a command for this operation
func (o *PutIndexesOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
var err error
o.Command, err = NewPutIndexesCommand(conventions, o.indexToAdd)
if err != nil {
return nil, err
}
return o.Command, nil
}
var _ RavenCommand = &PutIndexesCommand{}
// PutIndexesCommand represents put indexes command
type PutIndexesCommand struct {
RavenCommandBase
indexToAdd []map[string]interface{}
Result []*PutIndexResult
}
// NewPutIndexesCommand returns new PutIndexesCommand
func NewPutIndexesCommand(conventions *DocumentConventions, indexesToAdd []*IndexDefinition) (*PutIndexesCommand, error) {
if conventions == nil {
return nil, newIllegalArgumentError("conventions cannot be nil")
}
if indexesToAdd == nil {
return nil, newIllegalArgumentError("indexesToAdd cannot be nil")
}
cmd := &PutIndexesCommand{
RavenCommandBase: NewRavenCommandBase(),
}
for _, indexToAdd := range indexesToAdd {
// Note: unlike java, Type is not calculated on demand. This is a decent
// place to ensure it. Assumes that indexToAdd will not be modified
// between now an CreateRequest()
indexToAdd.updateIndexTypeAndMaps()
panicIf(indexToAdd.Name == "", "Index name cannot be empty")
objectNode := convertEntityToJSON(indexToAdd, nil)
cmd.indexToAdd = append(cmd.indexToAdd, objectNode)
}
return cmd, nil
}
func (c *PutIndexesCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/databases/" + node.Database + "/admin/indexes"
m := map[string]interface{}{
"Indexes": c.indexToAdd,
}
d, err := jsonMarshal(m)
if err != nil {
return nil, err
}
return newHttpPut(url, d)
}
func (c *PutIndexesCommand) SetResponse(response []byte, fromCache bool) error {
var res PutIndexesResponse
err := jsonUnmarshal(response, &res)
if err != nil {
return err
}
c.Result = res.Results
return nil
}