@@ -43,23 +43,6 @@ func (d *DynamoAllocationStore) Get(ctx context.Context, mh multihash.Multihash,
4343 if err != nil {
4444 return allocation.Allocation {}, fmt .Errorf ("getting item: %w" , err )
4545 }
46-
47- // HACK: (ash) Temporary hack to allow allocation to be found if it was
48- // stored with the old style key ("<digest>/<cause>") not the new key
49- // ("<digest>/<space>"). This works because listing works on digest
50- // prefix i.e. "<digest>/*".
51- if res .Item == nil {
52- allocs , listErr := d .List (ctx , mh )
53- if listErr != nil {
54- return allocation.Allocation {}, fmt .Errorf ("listing items: %w" , listErr )
55- }
56- for _ , a := range allocs {
57- if a .Space == space {
58- return a , nil
59- }
60- }
61- }
62-
6346 if res .Item == nil {
6447 return allocation.Allocation {}, store .ErrNotFound
6548 }
@@ -75,53 +58,63 @@ func (d *DynamoAllocationStore) Get(ctx context.Context, mh multihash.Multihash,
7558 return alloc , nil
7659}
7760
78- // List implements allocationstore.AllocationStore.
79- func (d * DynamoAllocationStore ) List (ctx context.Context , mh multihash.Multihash , options ... allocationstore.ListOption ) ([]allocation.Allocation , error ) {
80- cfg := allocationstore.ListConfig {}
81- for _ , opt := range options {
82- opt (& cfg )
83- }
84-
61+ // GetAny retrieves any allocation for a blob (digest), regardless of space.
62+ func (d * DynamoAllocationStore ) GetAny (ctx context.Context , mh multihash.Multihash ) (allocation.Allocation , error ) {
8563 keyEx := expression .Key ("hash" ).Equal (expression .Value (digestutil .Format (mh )))
8664 expr , err := expression .NewBuilder ().WithKeyCondition (keyEx ).Build ()
8765 if err != nil {
88- return nil , fmt .Errorf ("building query: %w" , err )
66+ return allocation.Allocation {}, fmt .Errorf ("building query: %w" , err )
67+ }
68+
69+ res , err := d .dynamoDbClient .Query (ctx , & dynamodb.QueryInput {
70+ TableName : aws .String (d .tableName ),
71+ ExpressionAttributeNames : expr .Names (),
72+ ExpressionAttributeValues : expr .Values (),
73+ KeyConditionExpression : expr .KeyCondition (),
74+ ConsistentRead : aws .Bool (true ),
75+ Limit : aws .Int32 (1 ),
76+ })
77+ if err != nil {
78+ return allocation.Allocation {}, fmt .Errorf ("querying allocation: %w" , err )
79+ }
80+ if len (res .Items ) == 0 {
81+ return allocation.Allocation {}, store .ErrNotFound
82+ }
83+
84+ var item allocationItem
85+ err = attributevalue .UnmarshalMap (res .Items [0 ], & item )
86+ if err != nil {
87+ return allocation.Allocation {}, fmt .Errorf ("unmarshalling allocation item: %w" , err )
88+ }
89+ alloc , err := allocation .Decode (item .Allocation , dagcbor .Decode )
90+ if err != nil {
91+ return allocation.Allocation {}, fmt .Errorf ("decoding allocation: %w" , err )
8992 }
93+ return alloc , nil
94+ }
9095
91- var limit * int32
92- if cfg .Limit > 0 {
93- limit = aws .Int32 (int32 (cfg .Limit ))
96+ // Exists checks if any allocation exists for a blob (digest).
97+ func (d * DynamoAllocationStore ) Exists (ctx context.Context , mh multihash.Multihash ) (bool , error ) {
98+ keyEx := expression .Key ("hash" ).Equal (expression .Value (digestutil .Format (mh )))
99+ proj := expression .NamesList (expression .Name ("hash" ))
100+ expr , err := expression .NewBuilder ().WithKeyCondition (keyEx ).WithProjection (proj ).Build ()
101+ if err != nil {
102+ return false , fmt .Errorf ("building query: %w" , err )
94103 }
95104
96- var allocations []allocation.Allocation
97- queryPaginator := dynamodb .NewQueryPaginator (d .dynamoDbClient , & dynamodb.QueryInput {
105+ res , err := d .dynamoDbClient .Query (ctx , & dynamodb.QueryInput {
98106 TableName : aws .String (d .tableName ),
99107 ExpressionAttributeNames : expr .Names (),
100108 ExpressionAttributeValues : expr .Values (),
101109 KeyConditionExpression : expr .KeyCondition (),
110+ ProjectionExpression : expr .Projection (),
102111 ConsistentRead : aws .Bool (true ),
103- Limit : limit ,
112+ Limit : aws . Int32 ( 1 ) ,
104113 })
105- for queryPaginator .HasMorePages () {
106- response , err := queryPaginator .NextPage (ctx )
107- if err != nil {
108- return nil , fmt .Errorf ("querying allocations: %w" , err )
109- }
110- var allocationPage []allocationItem
111- err = attributevalue .UnmarshalListOfMaps (response .Items , & allocationPage )
112- if err != nil {
113- return nil , fmt .Errorf ("parsing query responses: %w" , err )
114- }
115-
116- for _ , item := range allocationPage {
117- a , err := allocation .Decode (item .Allocation , dagcbor .Decode )
118- if err != nil {
119- return nil , fmt .Errorf ("decoding data: %w" , err )
120- }
121- allocations = append (allocations , a )
122- }
114+ if err != nil {
115+ return false , fmt .Errorf ("querying allocation: %w" , err )
123116 }
124- return allocations , nil
117+ return len ( res . Items ) > 0 , nil
125118}
126119
127120// Put implements allocationstore.AllocationStore.
0 commit comments