From 969bdb758412b8da66452cce7077bdf66efb77c4 Mon Sep 17 00:00:00 2001 From: Maciej Aszyk Date: Tue, 23 Jul 2024 11:52:03 +0200 Subject: [PATCH] RDBC-862 ConfigureExpirationOperation. --- configure_expiration_operation.go | 89 ++++++++++++++++++++++++++++++ readme.md | 45 +++++++++++++++ tests/expiration_operation_test.go | 42 ++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 configure_expiration_operation.go create mode 100644 tests/expiration_operation_test.go diff --git a/configure_expiration_operation.go b/configure_expiration_operation.go new file mode 100644 index 00000000..e807357e --- /dev/null +++ b/configure_expiration_operation.go @@ -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"` +} diff --git a/readme.md b/readme.md index 3b23c42f..bcf2ce08 100644 --- a/readme.md +++ b/readme.md @@ -1034,4 +1034,49 @@ var compareExchangeValue *ravendb.CompareExchangeValue compareExchangeValue , error := session.Advanced().ClusterTransaction().GetCompareExchangeValue[...] err := session.Advanced().ClusterTransaction().DeleteCompareExchangeValue(compareExchangeValue) +``` + +## Operations + +#### Configure expiration operation +Options: +```go +type ExpirationConfiguration struct { + Disabled bool `json:"Disabled"` + DeleteFrequencyInSec *int64 `json:"DeleteFrequencyInSec"` + MaxItemsToProcess *int64 `json:"MaxItemsToProcess"` +} +``` + +Operation creation is available by passing the `ExpirationConfiguration` object: +```go +configureExpiration := ravendb.ExpirationConfiguration{ + Disabled: false, +} +//Method: NewConfigureExpirationOperationWithConfiguration(expirationConfiguration *ExpirationConfiguration) (*ConfigureExpirationOperation, error) +operation, err := ravendb.NewConfigureExpirationOperationWithConfiguration(&configureExpiration) +``` + +Or directly by passing parameters: +```go +var deleteFrequency int64 = 60 +//Method: func NewConfigureExpirationOperation(disabled bool, deleteFrequencyInSec *int64, maxItemsToProcess *int64) (*ConfigureExpirationOperation, error) +opExpiration, err = ravendb.NewConfigureExpirationOperation(false, &deleteFrequency, nil) +``` + +Operation returns object: +```go +type ExpirationConfigurationResult struct { + RaftCommandIndex *int64 `json:"RaftCommandIndex"` +} +``` + +Example of usage: +```go +var deleteFrequency int64 = 60 +opExpiration, err = ravendb.NewConfigureExpirationOperation(false, &deleteFrequency, nil) +assert.NoError(t, err) + +err = store.Maintenance().Send(opExpiration) +assert.NoError(t, err) ``` \ No newline at end of file diff --git a/tests/expiration_operation_test.go b/tests/expiration_operation_test.go new file mode 100644 index 00000000..f99f394b --- /dev/null +++ b/tests/expiration_operation_test.go @@ -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) +}