Skip to content

Commit

Permalink
RDBC-862 ConfigureExpirationOperation.
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejaszyk committed Jul 23, 2024
1 parent 4474ee7 commit 969bdb7
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 0 deletions.
89 changes: 89 additions & 0 deletions configure_expiration_operation.go
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"`
}
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
42 changes: 42 additions & 0 deletions tests/expiration_operation_test.go
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)
}

0 comments on commit 969bdb7

Please sign in to comment.