-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update cancel all orders for user to return cancelled order IDs, add …
…find orders by IDs batch Redis method (#52)
- Loading branch information
1 parent
3c56554
commit 8925dc2
Showing
18 changed files
with
508 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package redisrepo | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/google/uuid" | ||
"github.com/orbs-network/order-book/models" | ||
"github.com/orbs-network/order-book/utils/logger" | ||
"github.com/orbs-network/order-book/utils/logger/logctx" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
const MAX_ORDER_IDS = 100 | ||
|
||
// FindOrdersByIds finds orders by their IDs. If an order is not found for any of the provided IDs, an error is returned. | ||
// | ||
// Only finding orders by their IDs is supported, not by their clientOIds. | ||
func (r *redisRepository) FindOrdersByIds(ctx context.Context, ids []uuid.UUID) ([]models.Order, error) { | ||
|
||
if len(ids) > MAX_ORDER_IDS { | ||
return nil, fmt.Errorf("exceeded maximum number of IDs: %d", MAX_ORDER_IDS) | ||
} | ||
|
||
pipeline := r.client.Pipeline() | ||
|
||
cmds := make([]*redis.MapStringStringCmd, len(ids)) | ||
for i, id := range ids { | ||
cmds[i] = pipeline.HGetAll(ctx, CreateOrderIDKey(id)) | ||
} | ||
|
||
_, err := pipeline.Exec(ctx) | ||
if err != nil { | ||
logctx.Error(ctx, "failed to execute pipeline getting orders by IDs", logger.Error(err), logger.Int("numIds", len(ids))) | ||
return nil, fmt.Errorf("failed to execute pipeline: %v", err) | ||
} | ||
|
||
orders := make([]models.Order, 0, len(ids)) | ||
for _, cmd := range cmds { | ||
orderMap, err := cmd.Result() | ||
if err != nil { | ||
logctx.Error(ctx, "could not get order", logger.Error(err)) | ||
return nil, fmt.Errorf("could not get order: %v", err) | ||
} | ||
|
||
if len(orderMap) == 0 { | ||
logctx.Warn(ctx, "order not found but was expected to exist", logger.String("orderId", cmd.Args()[1].(string))) | ||
return nil, errors.New("order not found but was expected to exist") | ||
} | ||
|
||
order := models.Order{} | ||
err = order.MapToOrder(orderMap) | ||
if err != nil { | ||
logctx.Error(ctx, "could not map order", logger.Error(err)) | ||
return nil, fmt.Errorf("could not map order: %v", err) | ||
} | ||
|
||
orders = append(orders, order) | ||
} | ||
|
||
logctx.Info(ctx, "found orders by IDs", logger.Int("numIdsProvided", len(ids)), logger.Int("numOrders", len(orders))) | ||
return orders, nil | ||
} |
Oops, something went wrong.