Skip to main content

dynoxide/actions/
batch_get_item.rs

1use crate::actions::helpers;
2use crate::errors::{DynoxideError, Result};
3use crate::expressions;
4use crate::storage::Storage;
5use crate::types::{AttributeValue, Item};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Default, Deserialize)]
10pub struct BatchGetItemRequest {
11    #[serde(rename = "RequestItems")]
12    pub request_items: HashMap<String, KeysAndAttributes>,
13    #[serde(rename = "ReturnConsumedCapacity", default)]
14    pub return_consumed_capacity: Option<String>,
15}
16
17#[derive(Debug, Default, Deserialize)]
18pub struct KeysAndAttributes {
19    #[serde(rename = "Keys")]
20    pub keys: Vec<HashMap<String, AttributeValue>>,
21    #[serde(rename = "ProjectionExpression", default)]
22    pub projection_expression: Option<String>,
23    #[serde(rename = "ExpressionAttributeNames", default)]
24    pub expression_attribute_names: Option<HashMap<String, String>>,
25    #[serde(rename = "ConsistentRead", default)]
26    pub consistent_read: Option<bool>,
27    #[serde(rename = "AttributesToGet", default)]
28    pub attributes_to_get: Option<Vec<String>>,
29}
30
31#[derive(Debug, Default, Serialize)]
32pub struct BatchGetItemResponse {
33    #[serde(rename = "Responses")]
34    pub responses: HashMap<String, Vec<Item>>,
35    #[serde(rename = "UnprocessedKeys")]
36    pub unprocessed_keys: HashMap<String, serde_json::Value>,
37    #[serde(rename = "ConsumedCapacity", skip_serializing_if = "Option::is_none")]
38    pub consumed_capacity: Option<Vec<crate::types::ConsumedCapacity>>,
39}
40
41pub fn execute(storage: &Storage, request: BatchGetItemRequest) -> Result<BatchGetItemResponse> {
42    // Validate RequestItems is not empty.
43    // AWS routes the empty-map case through a separate parameter-required path
44    // rather than the standard "N validation errors detected" envelope.
45    if request.request_items.is_empty() {
46        return Err(DynoxideError::ValidationException(
47            "The requestItems parameter is required for BatchGetItem".to_string(),
48        ));
49    }
50
51    // Validate each table entry has at least one key
52    for (table_name, ka) in &request.request_items {
53        if ka.keys.is_empty() {
54            return Err(DynoxideError::ValidationException(format!(
55                "1 validation error detected: Value at 'requestItems.{table_name}.member.keys' failed to satisfy constraint: Member must have length greater than or equal to 1"
56            )));
57        }
58    }
59
60    // Validate table name format for all tables before checking existence
61    for table_name in request.request_items.keys() {
62        crate::validation::validate_table_name(table_name)?;
63    }
64
65    // Validate total key count.
66    // AWS surfaces this as the standard "1 validation error detected" envelope
67    // with the field path pinned to one of the request's tables. The conformance
68    // suite exercises a single-table case; for multi-table requests we pick the
69    // table with the largest Keys array (and, on ties, fall through to whichever
70    // HashMap iteration yields first) so the field path lines up with the table
71    // that pushed the total over.
72    let total_keys: usize = request.request_items.values().map(|ka| ka.keys.len()).sum();
73    if total_keys > 100 {
74        let table_name = request
75            .request_items
76            .iter()
77            .max_by_key(|(_, ka)| ka.keys.len())
78            .map(|(name, _)| name.as_str())
79            .unwrap_or("");
80        return Err(DynoxideError::ValidationException(format!(
81            "1 validation error detected: Value at 'RequestItems.{table_name}.member.Keys' failed to satisfy constraint: Member must have length less than or equal to 100"
82        )));
83    }
84
85    // --- Pre-table validations ---
86    // DynamoDB validates expression attributes, key values, projections, and duplicates
87    // BEFORE checking table existence. Perform these checks first.
88    for keys_and_attrs in request.request_items.values() {
89        // Check AttributesToGet + expression conflict
90        let has_attributes_to_get = keys_and_attrs.attributes_to_get.is_some();
91        let has_projection_expr = keys_and_attrs.projection_expression.is_some();
92        let has_expr_attr_names = keys_and_attrs.expression_attribute_names.is_some();
93
94        if has_attributes_to_get && has_projection_expr {
95            return Err(DynoxideError::ValidationException(
96                "Can not use both expression and non-expression parameters in the same request: Non-expression parameters: {AttributesToGet} Expression parameters: {ProjectionExpression}".to_string(),
97            ));
98        }
99
100        // ExpressionAttributeNames without expression
101        if has_expr_attr_names && !has_projection_expr {
102            return Err(DynoxideError::ValidationException(
103                "ExpressionAttributeNames can only be specified when using expressions".to_string(),
104            ));
105        }
106
107        // Empty ExpressionAttributeNames
108        if let Some(ref ean) = keys_and_attrs.expression_attribute_names {
109            if ean.is_empty() {
110                return Err(DynoxideError::ValidationException(
111                    "ExpressionAttributeNames must not be empty".to_string(),
112                ));
113            }
114            // Invalid EAN keys (must start with #)
115            for key in ean.keys() {
116                if !key.starts_with('#') {
117                    return Err(DynoxideError::ValidationException(format!(
118                        "ExpressionAttributeNames contains invalid key: Syntax error; key: \"{key}\""
119                    )));
120                }
121            }
122        }
123
124        // Empty ProjectionExpression
125        if let Some(ref pe) = keys_and_attrs.projection_expression {
126            if pe.is_empty() {
127                return Err(DynoxideError::ValidationException(
128                    "Invalid ProjectionExpression: The expression can not be empty;".to_string(),
129                ));
130            }
131        }
132
133        // Duplicate AttributesToGet check (must come before duplicate keys check)
134        if let Some(ref atg) = keys_and_attrs.attributes_to_get {
135            let mut seen = std::collections::HashSet::new();
136            for attr in atg {
137                if !seen.insert(attr.as_str()) {
138                    return Err(DynoxideError::ValidationException(format!(
139                        "One or more parameter values were invalid: Duplicate value in attribute name: {attr}"
140                    )));
141                }
142            }
143        }
144
145        // Validate key attribute values (empty attrs, invalid numbers, etc.)
146        for key in &keys_and_attrs.keys {
147            crate::validation::validate_item_attribute_values(key)?;
148        }
149
150        // Duplicate keys check
151        if keys_and_attrs.keys.len() > 1 {
152            let serialised: Vec<String> = keys_and_attrs
153                .keys
154                .iter()
155                .map(|k| {
156                    let mut pairs: Vec<_> = k.iter().map(|(k, v)| format!("{k}={v:?}")).collect();
157                    pairs.sort();
158                    pairs.join(",")
159                })
160                .collect();
161            let mut seen = std::collections::HashSet::new();
162            for s in &serialised {
163                if !seen.insert(s) {
164                    return Err(DynoxideError::ValidationException(
165                        "Provided list of item keys contains duplicates".to_string(),
166                    ));
167                }
168            }
169        }
170    }
171
172    const MAX_RESPONSE_SIZE: usize = 16 * 1024 * 1024; // 16MB
173
174    let mut responses: HashMap<String, Vec<Item>> = HashMap::new();
175    let mut unprocessed_keys: HashMap<String, serde_json::Value> = HashMap::new();
176    let mut cumulative_size: usize = 0;
177    let mut size_limit_reached = false;
178    // Track per-key RCU for ConsumedCapacity (uses full item size, not projected)
179    let mut table_rcu: HashMap<String, f64> = HashMap::new();
180
181    for (table_name, keys_and_attrs) in &request.request_items {
182        let meta = helpers::require_table_for_item_op(storage, table_name)?;
183        let key_schema = helpers::parse_key_schema(&meta)?;
184
185        // Parse projection if present; also handle legacy AttributesToGet
186        let projection = if let Some(ref expr) = keys_and_attrs.projection_expression {
187            Some(expressions::projection::parse(expr).map_err(DynoxideError::ValidationException)?)
188        } else {
189            keys_and_attrs
190                .attributes_to_get
191                .as_ref()
192                .map(|attrs| crate::actions::helpers::attributes_to_get_to_projection(attrs))
193        };
194
195        let tracker = crate::expressions::TrackedExpressionAttributes::new(
196            &keys_and_attrs.expression_attribute_names,
197            &None, // BatchGetItem has no ExpressionAttributeValues
198        );
199
200        // Pre-register projection expression references
201        if let Some(ref proj) = projection {
202            tracker.track_projection_expr(proj);
203        }
204
205        // BatchGetItem does NOT automatically include key attributes in projections.
206        let key_attrs = Vec::new();
207
208        let consistent = keys_and_attrs.consistent_read.unwrap_or(false);
209        let mut table_items = Vec::new();
210        let mut remaining_keys: Vec<HashMap<String, AttributeValue>> = Vec::new();
211        let mut per_table_rcu: f64 = 0.0;
212
213        for key in &keys_and_attrs.keys {
214            if size_limit_reached {
215                remaining_keys.push(key.clone());
216                continue;
217            }
218
219            helpers::validate_key_only(key, &key_schema)?;
220            // TODO: validation must precede this call -- if reaching this line, caller has already validated keys.
221            let (pk, sk) = helpers::extract_key_strings(key, &key_schema)?;
222
223            if let Some(item_json) = storage.get_item(table_name, &pk, &sk)? {
224                let item: Item = serde_json::from_str(&item_json).map_err(|e| {
225                    DynoxideError::InternalServerError(format!("Bad item JSON: {e}"))
226                })?;
227
228                // Use full item size for both capacity and response limit
229                let item_size = crate::types::item_size(&item);
230
231                if cumulative_size + item_size > MAX_RESPONSE_SIZE {
232                    size_limit_reached = true;
233                    remaining_keys.push(key.clone());
234                    continue;
235                }
236
237                cumulative_size += item_size;
238
239                // RCU is based on full item size, not projected size
240                per_table_rcu +=
241                    crate::types::read_capacity_units_with_consistency(item_size, consistent);
242
243                let result_item = if let Some(ref proj) = projection {
244                    expressions::projection::apply(&item, proj, &tracker, &key_attrs)
245                        .map_err(DynoxideError::ValidationException)?
246                } else {
247                    item
248                };
249
250                table_items.push(result_item);
251            } else {
252                // DynamoDB charges for the read attempt even if the item is not found
253                per_table_rcu += crate::types::read_capacity_units_with_consistency(0, consistent);
254            }
255        }
256
257        // Check for unused expression attribute names
258        tracker.check_unused()?;
259
260        table_rcu.insert(table_name.clone(), per_table_rcu);
261        responses.insert(table_name.clone(), table_items);
262
263        if !remaining_keys.is_empty() {
264            let mut unprocessed = serde_json::json!({
265                "Keys": remaining_keys,
266            });
267            // Preserve original request settings so the caller can retry
268            // without losing projection or consistency configuration.
269            if let Some(ref pe) = keys_and_attrs.projection_expression {
270                unprocessed["ProjectionExpression"] = serde_json::json!(pe);
271            }
272            if let Some(ref ean) = keys_and_attrs.expression_attribute_names {
273                unprocessed["ExpressionAttributeNames"] = serde_json::json!(ean);
274            }
275            if let Some(cr) = keys_and_attrs.consistent_read {
276                unprocessed["ConsistentRead"] = serde_json::json!(cr);
277            }
278            unprocessed_keys.insert(table_name.clone(), unprocessed);
279        }
280    }
281
282    // Build consumed capacity per table
283    let consumed_capacity = if matches!(
284        request.return_consumed_capacity.as_deref(),
285        Some("TOTAL") | Some("INDEXES")
286    ) {
287        let mut caps = Vec::new();
288        for table_name in request.request_items.keys() {
289            let total_rcu = table_rcu.get(table_name).copied().unwrap_or(0.0);
290            if let Some(cc) = crate::types::consumed_capacity(
291                table_name,
292                total_rcu,
293                &request.return_consumed_capacity,
294            ) {
295                caps.push(cc);
296            }
297        }
298        Some(caps)
299    } else {
300        None
301    };
302
303    Ok(BatchGetItemResponse {
304        responses,
305        unprocessed_keys,
306        consumed_capacity,
307    })
308}