Adding deleteWithPrefix for in-memory statestore (#3314)

Signed-off-by: Ryan Lettieri <ryanLettieri@microsoft.com>
This commit is contained in:
Ryan Lettieri 2024-01-12 12:40:07 -07:00 committed by GitHub
parent 7aa4013ca8
commit 3da3d783d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -19,6 +19,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
@ -92,6 +93,7 @@ func (store *inMemoryStore) Features() []state.Feature {
state.FeatureETag,
state.FeatureTransactional,
state.FeatureTTL,
state.FeatureDeleteWithPrefix,
}
}
@ -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 != ""

View File

@ -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" ]