Skip to content

Commit

Permalink
return nil when their are no results
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth More authored and Siddharth More committed Jan 9, 2024
1 parent ba13aa2 commit ee26668
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 4 additions & 0 deletions common/aws/dynamodb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (c *Client) QueryIndexWithPagination(ctx context.Context, tableName string,
return QueryResult{}, err
}

if len(response.Items) == 0 {
return QueryResult{Items: nil, LastEvaluatedKey: nil}, nil
}

// Return the items and the pagination token
return QueryResult{
Items: response.Items,
Expand Down
17 changes: 16 additions & 1 deletion common/aws/dynamodb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,22 @@ func TestQueryIndexPaginationSingleItem(t *testing.T) {
Value: "0",
}}, 1, lastEvaluatedKey)
assert.NoError(t, err)
assert.Len(t, queryResult.Items, 0)
assert.Nil(t, queryResult.Items)
assert.Nil(t, queryResult.LastEvaluatedKey)
}

func TestQueryIndexPaginationNoStoredItems(t *testing.T) {
tableName := "ProcessingWithPaginationNoItem"
createTable(t, tableName)
indexName := "StatusIndex"

ctx := context.Background()
queryResult, err := dynamoClient.QueryIndexWithPagination(ctx, tableName, indexName, "BlobStatus = :status", commondynamodb.ExpresseionValues{
":status": &types.AttributeValueMemberN{
Value: "0",
}}, 1, nil)
assert.NoError(t, err)
assert.Nil(t, queryResult.Items)
assert.Nil(t, queryResult.LastEvaluatedKey)
}

Expand Down
5 changes: 5 additions & 0 deletions disperser/common/blobstore/blob_metadata_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (s *BlobMetadataStore) GetBlobMetadataByStatusWithPagination(ctx context.Co
return nil, nil, err
}

// When their are no more results to fetch, the LastEvaluatedKey is nil
if queryResult.Items == nil && queryResult.LastEvaluatedKey == nil {
return nil, nil, nil
}

metadata := make([]*disperser.BlobMetadata, len(queryResult.Items))
for i, item := range queryResult.Items {
metadata[i], err = UnmarshalBlobMetadata(item)
Expand Down
10 changes: 10 additions & 0 deletions disperser/common/blobstore/blob_metadata_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ func TestBlobMetadataStoreOperationsWithPagination(t *testing.T) {
})
}

func TestBlobMetadataStoreOperationsWithPaginationNoStoredBlob(t *testing.T) {
ctx := context.Background()
// Query BlobMetadataStore for a blob that does not exist
// This should return nil for both the blob and lastEvaluatedKey
processing, lastEvaluatedKey, err := blobMetadataStore.GetBlobMetadataByStatusWithPagination(ctx, disperser.Processing, 1, nil)
assert.NoError(t, err)
assert.Nil(t, processing)
assert.Nil(t, lastEvaluatedKey)
}

func deleteItems(t *testing.T, keys []commondynamodb.Key) {
_, err := dynamoClient.DeleteItems(context.Background(), metadataTableName, keys)
assert.NoError(t, err)
Expand Down

0 comments on commit ee26668

Please sign in to comment.