Skip to content

Commit

Permalink
chore: remove unused db query fns
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Oct 11, 2024
1 parent 29a1401 commit 41cf38c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
28 changes: 2 additions & 26 deletions common/aws/dynamodb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,34 +223,10 @@ func (c *Client) QueryIndex(ctx context.Context, tableName string, indexName str
return response.Items, nil
}

// QueryIndexOrderWithLimit returns all items in the index that match the given key
// If forward is true, the items are returned in ascending order
func (c *Client) QueryIndexOrderWithLimit(ctx context.Context, tableName string, indexName string, keyCondition string, expAttributeValues ExpressionValues, forward bool, limit int32) ([]Item, error) {
response, err := c.dynamoClient.Query(ctx, &dynamodb.QueryInput{
TableName: aws.String(tableName),
IndexName: aws.String(indexName),
KeyConditionExpression: aws.String(keyCondition),
ExpressionAttributeValues: expAttributeValues,
ScanIndexForward: &forward,
Limit: aws.Int32(limit),
})
if err != nil {
return nil, err
}

return response.Items, nil
}

// QueryWithOrderLimit returns all items in the index that match the given key
// If forward is true, the items are returned in ascending order
func (c *Client) QueryWithOrderLimit(ctx context.Context, tableName string, keyCondition string, expAttributeValues ExpressionValues, forward bool, limit int32) ([]Item, error) {
response, err := c.dynamoClient.Query(ctx, &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String(keyCondition),
ExpressionAttributeValues: expAttributeValues,
ScanIndexForward: &forward,
Limit: aws.Int32(limit),
})
func (c *Client) QueryWithInput(ctx context.Context, input *dynamodb.QueryInput) ([]Item, error) {
response, err := c.dynamoClient.Query(ctx, input)
if err != nil {
return nil, err
}
Expand Down
55 changes: 40 additions & 15 deletions common/aws/dynamodb/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,9 @@ func TestQueryIndexWithPaginationForBatch(t *testing.T) {
assert.Nil(t, queryResult.LastEvaluatedKey)
}

func TestQueryIndexOrderWithLimit(t *testing.T) {
tableName := "ProcessingQueryIndexOrderWithLimit"
func TestQueryWithInput(t *testing.T) {
tableName := "ProcessingQueryWithInput"
createTable(t, tableName)
indexName := "StatusIndex"

ctx := context.Background()
numItems := 30
Expand All @@ -626,9 +625,16 @@ func TestQueryIndexOrderWithLimit(t *testing.T) {
assert.Len(t, unprocessed, 0)

// Test forward order with limit
queryResult, err := dynamoClient.QueryIndexOrderWithLimit(ctx, tableName, indexName, "BlobStatus = :status", commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
}, true, 10)
queryInput := &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("BlobStatus = :status"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
},
ScanIndexForward: aws.Bool(true),
Limit: aws.Int32(10),
}
queryResult, err := dynamoClient.QueryWithInput(ctx, queryInput)
assert.NoError(t, err)
assert.Len(t, queryResult, 10)
// Check if the items are in ascending order
Expand All @@ -637,9 +643,16 @@ func TestQueryIndexOrderWithLimit(t *testing.T) {
}

// Test reverse order with limit
queryResult, err = dynamoClient.QueryIndexOrderWithLimit(ctx, tableName, indexName, "BlobStatus = :status", commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
}, false, 10)
queryInput = &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("BlobStatus = :status"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
},
ScanIndexForward: aws.Bool(true),
Limit: aws.Int32(10),
}
queryResult, err = dynamoClient.QueryWithInput(ctx, queryInput)
assert.NoError(t, err)
assert.Len(t, queryResult, 10)
// Check if the items are in descending order
Expand All @@ -648,16 +661,28 @@ func TestQueryIndexOrderWithLimit(t *testing.T) {
}

// Test with a smaller limit
queryResult, err = dynamoClient.QueryIndexOrderWithLimit(ctx, tableName, indexName, "BlobStatus = :status", commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
}, true, 5)
queryInput = &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("BlobStatus = :status"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
},
Limit: aws.Int32(5),
}
queryResult, err = dynamoClient.QueryWithInput(ctx, queryInput)
assert.NoError(t, err)
assert.Len(t, queryResult, 5)

// Test with a limit larger than the number of items
queryResult, err = dynamoClient.QueryIndexOrderWithLimit(ctx, tableName, indexName, "BlobStatus = :status", commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
}, true, 50)
queryInput = &dynamodb.QueryInput{
TableName: aws.String(tableName),
KeyConditionExpression: aws.String("BlobStatus = :status"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":status": &types.AttributeValueMemberN{Value: "0"},
},
Limit: aws.Int32(50),
}
queryResult, err = dynamoClient.QueryWithInput(ctx, queryInput)
assert.NoError(t, err)
assert.Len(t, queryResult, 30) // Should return all items
}
31 changes: 18 additions & 13 deletions core/meterer/offchain_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
commondynamodb "github.com/Layr-Labs/eigenda/common/aws/dynamodb"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)

Expand Down Expand Up @@ -181,19 +183,20 @@ func (s *OffchainStore) RemoveOnDemandPayment(ctx context.Context, accountID str
// The queries are done sequentially instead of one-go for efficient querying and would not cause race condition errors for honest requests
func (s *OffchainStore) GetRelevantOnDemandRecords(ctx context.Context, accountID string, cumulativePayment uint64) (uint64, uint64, uint32, error) {
// Fetch the largest entry smaller than the given cumulativePayment
smallerResult, err := s.dynamoClient.QueryWithOrderLimit(ctx, s.onDemandTableName,
"AccountID = :account AND CumulativePayments < :cumulativePayment",
commondynamodb.ExpressionValues{
queryInput := &dynamodb.QueryInput{
TableName: aws.String(s.onDemandTableName),
KeyConditionExpression: aws.String("AccountID = :account AND CumulativePayments < :cumulativePayment"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{Value: accountID},
":cumulativePayment": &types.AttributeValueMemberN{Value: strconv.FormatUint(cumulativePayment, 10)},
},
false, // Retrieve results in descending order for the largest smaller amount
1,
)
ScanIndexForward: aws.Bool(false),
Limit: aws.Int32(1),
}
smallerResult, err := s.dynamoClient.QueryWithInput(ctx, queryInput)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to query smaller payments for account: %w", err)
}

var prevPayment uint64
if len(smallerResult) > 0 {
prevPayment, err = strconv.ParseUint(smallerResult[0]["CumulativePayments"].(*types.AttributeValueMemberN).Value, 10, 64)
Expand All @@ -203,15 +206,17 @@ func (s *OffchainStore) GetRelevantOnDemandRecords(ctx context.Context, accountI
}

// Fetch the smallest entry larger than the given cumulativePayment
largerResult, err := s.dynamoClient.QueryWithOrderLimit(ctx, s.onDemandTableName,
"AccountID = :account AND CumulativePayments > :cumulativePayment",
commondynamodb.ExpressionValues{
queryInput = &dynamodb.QueryInput{
TableName: aws.String(s.onDemandTableName),
KeyConditionExpression: aws.String("AccountID = :account AND CumulativePayments > :cumulativePayment"),
ExpressionAttributeValues: commondynamodb.ExpressionValues{
":account": &types.AttributeValueMemberS{Value: accountID},
":cumulativePayment": &types.AttributeValueMemberN{Value: strconv.FormatUint(cumulativePayment, 10)},
},
true, // Retrieve results in ascending order for the smallest greater amount
1,
)
ScanIndexForward: aws.Bool(true),
Limit: aws.Int32(1),
}
largerResult, err := s.dynamoClient.QueryWithInput(ctx, queryInput)
if err != nil {
return 0, 0, 0, fmt.Errorf("failed to query the next payment for account: %w", err)
}
Expand Down

0 comments on commit 41cf38c

Please sign in to comment.