Skip to content
This repository has been archived by the owner on Oct 30, 2020. It is now read-only.

[Spike] Check for children more efficiently during clean-up #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions physical/dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ func (d *DynamoDBBackend) Delete(key string) error {
prefixes := prefixes(key)
sort.Sort(sort.Reverse(sort.StringSlice(prefixes)))
for _, prefix := range prefixes {
items, err := d.List(prefix)
hasChildren, err := d.hasChildren(prefix)
if err != nil {
return err
}
if len(items) == 1 {
if !hasChildren {
requests = append(requests, &dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Key: map[string]*dynamodb.AttributeValue{
Expand Down Expand Up @@ -378,6 +378,36 @@ func (d *DynamoDBBackend) List(prefix string) ([]string, error) {
return keys, nil
}

func (d *DynamoDBBackend) hasChildren(prefix string) (bool, error) {
defer metrics.MeasureSince([]string{"dynamodb", "list"}, time.Now())

prefix = strings.TrimSuffix(prefix, "/")

prefix = escapeEmptyPath(prefix)
queryInput := &dynamodb.QueryInput{
TableName: aws.String(d.table),
ConsistentRead: aws.Bool(true),
KeyConditions: map[string]*dynamodb.Condition{
"Path": {
ComparisonOperator: aws.String("EQ"),
AttributeValueList: []*dynamodb.AttributeValue{{
S: aws.String(prefix),
}},
},
},
Limit: aws.Int64(2),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was @TobiasBales's idea! ❤️

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should document why the limit is 2 and not 1 (in a comment I'd guess)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes of course! This is just a spike to validate the idea 😉

}

d.permitPool.Acquire()
defer d.permitPool.Release()

out, err := d.client.Query(queryInput)
if err != nil {
return false, err
}
return len(out.Items) > 1, nil
}

// LockWith is used for mutual exclusion based on the given key.
func (d *DynamoDBBackend) LockWith(key, value string) (Lock, error) {
identity, err := uuid.GenerateUUID()
Expand Down