-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RDBC-862 ConfigureExpirationOperation.
- Loading branch information
1 parent
4474ee7
commit 969bdb7
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package ravendb | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
var _ IVoidMaintenanceOperation = &ConfigureExpirationOperation{} | ||
|
||
type ConfigureExpirationOperation struct { | ||
parameters *ExpirationConfiguration | ||
Command *ConfigureExpirationCommand | ||
} | ||
|
||
func NewConfigureExpirationOperationWithConfiguration(expirationConfiguration *ExpirationConfiguration) (*ConfigureExpirationOperation, error) { | ||
return &ConfigureExpirationOperation{ | ||
parameters: expirationConfiguration, | ||
}, nil | ||
} | ||
|
||
func NewConfigureExpirationOperation(disabled bool, deleteFrequencyInSec *int64, maxItemsToProcess *int64) (*ConfigureExpirationOperation, error) { | ||
|
||
p := &ExpirationConfiguration{ | ||
Disabled: disabled, | ||
DeleteFrequencyInSec: deleteFrequencyInSec, | ||
MaxItemsToProcess: maxItemsToProcess, | ||
} | ||
return &ConfigureExpirationOperation{ | ||
parameters: p, | ||
}, nil | ||
} | ||
|
||
type ConfigureExpirationCommand struct { | ||
RavenCommandBase | ||
_parameters []byte | ||
Result *ExpirationConfigurationResult | ||
} | ||
|
||
// GetCommand returns a command | ||
func (o *ConfigureExpirationOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) { | ||
var err error | ||
o.Command, err = newConfigureExpirationCommand(conventions, o.parameters) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return o.Command, nil | ||
} | ||
|
||
func newConfigureExpirationCommand(conventions *DocumentConventions, parameters *ExpirationConfiguration) (*ConfigureExpirationCommand, 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 map[string]interface{} and then to JSON | ||
d, err := jsonMarshal(parameters) | ||
panicIf(err != nil, "jsonMarshal failed with %s", err) | ||
cmd := &ConfigureExpirationCommand{ | ||
RavenCommandBase: NewRavenCommandBase(), | ||
_parameters: d, | ||
} | ||
cmd.ResponseType = RavenCommandResponseTypeObject | ||
return cmd, nil | ||
} | ||
|
||
func (c *ConfigureExpirationCommand) CreateRequest(node *ServerNode) (*http.Request, error) { | ||
url := node.URL + "/databases/" + node.Database + "/admin/expiration/config" | ||
|
||
return NewHttpPost(url, c._parameters) | ||
} | ||
|
||
func (c *ConfigureExpirationCommand) SetResponse(response []byte, fromCache bool) error { | ||
return json.Unmarshal(response, &c.Result) | ||
} | ||
|
||
// ExpirationConfiguration | ||
type ExpirationConfiguration struct { | ||
Disabled bool `json:"Disabled"` | ||
DeleteFrequencyInSec *int64 `json:"DeleteFrequencyInSec"` | ||
MaxItemsToProcess *int64 `json:"MaxItemsToProcess"` | ||
} | ||
|
||
type ExpirationConfigurationResult struct { | ||
RaftCommandIndex *int64 `json:"RaftCommandIndex"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tests | ||
|
||
import ( | ||
"github.com/ravendb/ravendb-go-client" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func expirationConfigurationOperation(t *testing.T, driver *RavenTestDriver) { | ||
store := driver.getDocumentStoreMust(t) | ||
defer store.Close() | ||
|
||
configureExpiration := ravendb.ExpirationConfiguration{ | ||
Disabled: false, | ||
} | ||
|
||
opExpiration, err := ravendb.NewConfigureExpirationOperationWithConfiguration(&configureExpiration) | ||
assert.NoError(t, err) | ||
|
||
err = store.Maintenance().Send(opExpiration) | ||
assert.NoError(t, err) | ||
|
||
lastRaftEtag := *opExpiration.Command.Result.RaftCommandIndex | ||
assert.NotNil(t, lastRaftEtag) | ||
|
||
var deleteFrequency int64 = 60 | ||
opExpiration, err = ravendb.NewConfigureExpirationOperation(false, &deleteFrequency, nil) | ||
assert.NoError(t, err) | ||
|
||
err = store.Maintenance().Send(opExpiration) | ||
assert.NoError(t, err) | ||
|
||
assert.NotNil(t, *opExpiration.Command.Result.RaftCommandIndex) | ||
assert.NotEqual(t, lastRaftEtag, *opExpiration.Command.Result.RaftCommandIndex) | ||
} | ||
|
||
func TestExpirationConfiguration(t *testing.T) { | ||
driver := createTestDriver(t) | ||
destroy := func() { destroyDriver(t, driver) } | ||
defer recoverTest(t, destroy) | ||
expirationConfigurationOperation(t, driver) | ||
} |