-
Notifications
You must be signed in to change notification settings - Fork 16
/
create_database_operation.go
90 lines (74 loc) · 2.69 KB
/
create_database_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
package ravendb
import (
"net/http"
"net/url"
"strconv"
)
var _ IServerOperation = &CreateDatabaseOperation{}
// CreateDatabaseOperation represents "create database" operation
type CreateDatabaseOperation struct {
databaseRecord *DatabaseRecord
replicationFactor int
}
// NewCreateDatabaseOperation returns CreateDatabaseOperation
func NewCreateDatabaseOperation(databaseRecord *DatabaseRecord, replicationFactor int) *CreateDatabaseOperation {
if databaseRecord.DatabaseTopology != nil && databaseRecord.DatabaseTopology.ReplicationFactor > 0 {
replicationFactor = databaseRecord.DatabaseTopology.ReplicationFactor
} else if replicationFactor <= 0 {
replicationFactor = 1
}
return &CreateDatabaseOperation{
databaseRecord: databaseRecord,
replicationFactor: replicationFactor,
}
}
// GetCommand returns command for this operation
func (o *CreateDatabaseOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
return NewCreateDatabaseCommand(conventions, o.databaseRecord, o.replicationFactor)
}
var (
_ RavenCommand = &CreateDatabaseCommand{}
)
// CreateDatabaseCommand represents "create database" command
type CreateDatabaseCommand struct {
RavenCommandBase
conventions *DocumentConventions
databaseRecord *DatabaseRecord
replicationFactor int
databaseName string
Result *DatabasePutResult
}
// NewCreateDatabaseCommand returns new CreateDatabaseCommand
func NewCreateDatabaseCommand(conventions *DocumentConventions, databaseRecord *DatabaseRecord, replicationFactor int) (*CreateDatabaseCommand, error) {
if databaseRecord.DatabaseName == "" {
return nil, newIllegalArgumentError("databaseRecord.DatabaseName cannot be empty")
}
if databaseRecord.DatabaseTopology != nil && databaseRecord.DatabaseTopology.ReplicationFactor > 0 {
replicationFactor = databaseRecord.DatabaseTopology.ReplicationFactor
} else if replicationFactor <= 0 {
replicationFactor = 1
}
cmd := &CreateDatabaseCommand{
RavenCommandBase: NewRavenCommandBase(),
conventions: conventions,
databaseRecord: databaseRecord,
replicationFactor: replicationFactor,
databaseName: databaseRecord.DatabaseName,
}
return cmd, nil
}
func (c *CreateDatabaseCommand) CreateRequest(node *ServerNode) (*http.Request, error) {
url := node.URL + "/admin/databases?name=" + url.QueryEscape(c.databaseName)
url += "&replicationFactor=" + strconv.Itoa(c.replicationFactor)
js, err := jsonMarshal(c.databaseRecord)
if err != nil {
return nil, err
}
return newHttpPut(url, js)
}
func (c *CreateDatabaseCommand) SetResponse(response []byte, fromCache bool) error {
if len(response) == 0 {
return throwInvalidResponse()
}
return jsonUnmarshal(response, &c.Result)
}