Skip to content

Commit

Permalink
Adding deleteWithPrefix for in-memory statestore (dapr#3314)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
  • Loading branch information
RyanLettieri committed Jan 12, 2024
1 parent 7aa4013 commit 3da3d78
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions state/in-memory/in_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -92,6 +93,7 @@ func (store *inMemoryStore) Features() []state.Feature {
state.FeatureETag,
state.FeatureTransactional,
state.FeatureTTL,
state.FeatureDeleteWithPrefix,
}
}

Expand All @@ -116,6 +118,34 @@ func (store *inMemoryStore) Delete(ctx context.Context, req *state.DeleteRequest
return nil
}

func (store *inMemoryStore) DeleteWithPrefix(ctx context.Context, req state.DeleteWithPrefixRequest) (state.DeleteWithPrefixResponse, error) {
// step1: validate parameters
err := req.Validate()
if err != nil {
return state.DeleteWithPrefixResponse{}, err
}

// step2 should be protected by write-lock
store.lock.Lock()
defer store.lock.Unlock()

// step2: do really delete
// this operation won't fail
var count int64

for key := range store.items {
if strings.HasPrefix(key, req.Prefix) {
// The string contains the prefix, now we check to make sure there aren't more || after
longerPrefix := strings.Contains(key[len(req.Prefix):], "||")
if !longerPrefix {
delete(store.items, key)
count++
}
}
}
return state.DeleteWithPrefixResponse{Count: count}, nil
}

func (store *inMemoryStore) doValidateEtag(key string, etag *string, concurrency string) error {
hasEtag := etag != nil && *etag != ""

Expand Down
2 changes: 1 addition & 1 deletion tests/config/state/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ components:
- component: rethinkdb
operations: []
- component: in-memory
operations: [ "transaction", "etag", "first-write", "ttl" ]
operations: [ "transaction", "etag", "first-write", "ttl", "delete-with-prefix" ]
- component: aws.dynamodb.docker
# In the Docker variant, we do not set ttlAttributeName in the metadata, so TTLs are not enabled
operations: [ "transaction", "etag", "first-write" ]
Expand Down

0 comments on commit 3da3d78

Please sign in to comment.