datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
use super::dynamodb_utils::{retry_batch_operation, MAX_RETRIES};
use super::error::{StorageError, StorageResult};
use super::traits::{KvStore, NamespacedStore};
use crate::retry_operation;
use async_trait::async_trait;
use aws_sdk_dynamodb::types::{
    AttributeDefinition, AttributeValue, BillingMode, KeySchemaElement, KeyType,
    ScalarAttributeType, TableStatus,
};
use aws_sdk_dynamodb::Client;
use std::collections::HashMap;
use std::sync::Arc;

/// DynamoDB-backed KvStore implementation
///
/// Uses a separate DynamoDB table per namespace with:
/// - Partition Key (PK): user_id:key (format: user_id:actual_key)
/// - Sort Key (SK): actual_key
/// - Value: binary data
///
/// The user_id:key format ensures all tables use consistent partition key structure.
/// This design enables efficient Query operations instead of expensive Scans.
pub struct DynamoDbKvStore {
    client: Arc<Client>,
    table_name: String,
    /// user_id used as the partition key
    user_id: String,
}

impl DynamoDbKvStore {
    /// Create a new DynamoDB KvStore for a specific table
    ///
    /// - `table_name`: The DynamoDB table name (typically namespace-specific)
    /// - `user_id`: user_id used as the partition key (for multi-tenant isolation)
    pub fn new(client: Arc<Client>, table_name: String, user_id: String) -> Self {
        Self {
            client,
            table_name,
            user_id,
        }
    }

    /// Get the partition key to use for this store
    #[cfg(test)]
    pub fn get_partition_key(&self) -> String {
        self.get_partition_key_impl()
    }

    fn get_partition_key_impl(&self) -> String {
        self.user_id.clone()
    }

    /// Get partition key (user_id)
    /// Note: This is a change from previous implementation where PK was user_id:key
    /// This change enables Query operations with SK prefix
    fn get_partition_key_with_key(&self, _key: &[u8]) -> String {
        self.user_id.clone()
    }

    /// Convert a byte key to a string for the sort key (no user_id prefixing)
    #[cfg(test)]
    pub fn make_sort_key(&self, key: &[u8]) -> String {
        self.make_sort_key_impl(key)
    }

    fn make_sort_key_impl(&self, key: &[u8]) -> String {
        String::from_utf8_lossy(key).to_string()
    }
}

#[async_trait]
impl KvStore for DynamoDbKvStore {
    async fn get(&self, key: &[u8]) -> StorageResult<Option<Vec<u8>>> {
        let pk = self.get_partition_key_with_key(key);
        let sk = self.make_sort_key_impl(key);
        let key_str = String::from_utf8_lossy(key);

        let result = retry_operation!(
            self.client
                .get_item()
                .table_name(&self.table_name)
                .key("PK", AttributeValue::S(pk.clone()))
                .key("SK", AttributeValue::S(sk.clone()))
                .send(),
            "get_item",
            &self.table_name,
            Some(&key_str),
            MAX_RETRIES,
            StorageError::DynamoDbError
        )?;

        if let Some(item) = result.item {
            if let Some(AttributeValue::S(json_str)) = item.get("Value") {
                return Ok(Some(json_str.as_bytes().to_vec()));
            }
        }

        Ok(None)
    }

    async fn put(&self, key: &[u8], value: Vec<u8>) -> StorageResult<()> {
        let pk = self.get_partition_key_with_key(key);
        let sk = self.make_sort_key_impl(key);
        let key_str = String::from_utf8_lossy(key);

        let json_str = String::from_utf8(value.clone()).map_err(|e| {
            StorageError::SerializationError(format!("Invalid UTF-8 in value: {}", e))
        })?;

        retry_operation!(
            self.client
                .put_item()
                .table_name(&self.table_name)
                .item("PK", AttributeValue::S(pk.clone()))
                .item("SK", AttributeValue::S(sk.clone()))
                .item("Value", AttributeValue::S(json_str.clone()))
                .send(),
            "put_item",
            &self.table_name,
            Some(&key_str),
            MAX_RETRIES,
            StorageError::DynamoDbError
        )?;

        Ok(())
    }

    async fn delete(&self, key: &[u8]) -> StorageResult<bool> {
        let pk = self.get_partition_key_with_key(key);
        let sk = self.make_sort_key_impl(key);
        let key_str = String::from_utf8_lossy(key);

        let result = retry_operation!(
            self.client
                .delete_item()
                .table_name(&self.table_name)
                .key("PK", AttributeValue::S(pk.clone()))
                .key("SK", AttributeValue::S(sk.clone()))
                .return_values(aws_sdk_dynamodb::types::ReturnValue::AllOld)
                .send(),
            "delete_item",
            &self.table_name,
            Some(&key_str),
            MAX_RETRIES,
            StorageError::DynamoDbError
        )?;

        Ok(result.attributes.is_some())
    }

    async fn exists(&self, key: &[u8]) -> StorageResult<bool> {
        let pk = self.get_partition_key_with_key(key);
        let sk = self.make_sort_key_impl(key);
        let key_str = String::from_utf8_lossy(key);

        let result = retry_operation!(
            self.client
                .get_item()
                .table_name(&self.table_name)
                .key("PK", AttributeValue::S(pk.clone()))
                .key("SK", AttributeValue::S(sk.clone()))
                .projection_expression("PK") // Only fetch key, not value
                .send(),
            "get_item",
            &self.table_name,
            Some(&key_str),
            MAX_RETRIES,
            StorageError::DynamoDbError
        )?;

        Ok(result.item.is_some())
    }

    async fn scan_prefix(&self, prefix: &[u8]) -> StorageResult<Vec<(Vec<u8>, Vec<u8>)>> {
        let prefix_str = String::from_utf8_lossy(prefix).to_string();
        let pk = self.get_partition_key_impl();

        // Use Query instead of Scan for efficiency
        // PK = user_id, SK begins_with prefix
        let mut results = Vec::new();
        let mut last_evaluated_key: Option<HashMap<String, AttributeValue>> = None;

        loop {
            let mut query = self
                .client
                .query()
                .table_name(&self.table_name)
                .key_condition_expression("PK = :pk AND begins_with(SK, :prefix)")
                .expression_attribute_values(":pk", AttributeValue::S(pk.clone()))
                .expression_attribute_values(":prefix", AttributeValue::S(prefix_str.clone()));

            if let Some(key) = last_evaluated_key.take() {
                query = query.set_exclusive_start_key(Some(key));
            }

            let response = match query.send().await {
                Ok(r) => r,
                Err(e) => {
                    let error_str = e.to_string();
                    // If table doesn't exist or is still being created, return empty results
                    if error_str.contains("ResourceNotFoundException")
                        || error_str.contains("ResourceInUseException")
                        || error_str.contains("cannot do operations on a non-existent table")
                    {
                        return Ok(Vec::new());
                    }
                    return Err(StorageError::DynamoDbError(error_str));
                }
            };

            if let Some(items) = response.items {
                for item in items {
                    if let (Some(AttributeValue::S(sk)), Some(AttributeValue::S(json_str))) =
                        (item.get("SK"), item.get("Value"))
                    {
                        // The sort key is the actual key (no user_id prefix to remove)
                        results.push((sk.as_bytes().to_vec(), json_str.as_bytes().to_vec()));
                    }
                }
            }

            last_evaluated_key = response.last_evaluated_key;
            if last_evaluated_key.is_none() {
                break;
            }
        }

        Ok(results)
    }

    async fn batch_put(&self, items: Vec<(Vec<u8>, Vec<u8>)>) -> StorageResult<()> {
        const BATCH_SIZE: usize = 25;

        // DynamoDB batch write supports up to 25 items per request
        for chunk in items.chunks(BATCH_SIZE) {
            let mut write_requests = Vec::new();

            for (key, value) in chunk {
                let pk = self.get_partition_key_with_key(key);
                let sk = self.make_sort_key_impl(key);
                let mut item = HashMap::new();
                let json_str = String::from_utf8(value.clone()).map_err(|e| {
                    StorageError::SerializationError(format!("Invalid UTF-8 in batch value: {}", e))
                })?;

                item.insert("PK".to_string(), AttributeValue::S(pk));
                item.insert("SK".to_string(), AttributeValue::S(sk));
                item.insert("Value".to_string(), AttributeValue::S(json_str));

                write_requests.push(
                    aws_sdk_dynamodb::types::WriteRequest::builder()
                        .put_request(
                            aws_sdk_dynamodb::types::PutRequest::builder()
                                .set_item(Some(item))
                                .build()
                                .map_err(|e| {
                                    StorageError::DynamoDbError(format!(
                                        "Failed to build put request for table '{}': {}",
                                        self.table_name, e
                                    ))
                                })?,
                        )
                        .build(),
                );
            }

            retry_batch_operation(
                |requests| {
                    let mut req_map = HashMap::new();
                    req_map.insert(self.table_name.clone(), requests.to_vec());
                    Box::pin(
                        self.client
                            .batch_write_item()
                            .set_request_items(Some(req_map))
                            .send(),
                    )
                },
                &self.table_name,
                write_requests,
            )
            .await
            .map_err(StorageError::DynamoDbError)?;
        }

        Ok(())
    }

    async fn batch_delete(&self, keys: Vec<Vec<u8>>) -> StorageResult<()> {
        const BATCH_SIZE: usize = 25;

        for chunk in keys.chunks(BATCH_SIZE) {
            let mut write_requests = Vec::new();

            for key in chunk {
                let pk = self.get_partition_key_with_key(key);
                let sk = self.make_sort_key_impl(&key);
                let mut key_map = HashMap::new();
                key_map.insert("PK".to_string(), AttributeValue::S(pk));
                key_map.insert("SK".to_string(), AttributeValue::S(sk));

                write_requests.push(
                    aws_sdk_dynamodb::types::WriteRequest::builder()
                        .delete_request(
                            aws_sdk_dynamodb::types::DeleteRequest::builder()
                                .set_key(Some(key_map))
                                .build()
                                .map_err(|e| {
                                    StorageError::DynamoDbError(format!(
                                        "Failed to build delete request for table '{}': {}",
                                        self.table_name, e
                                    ))
                                })?,
                        )
                        .build(),
                );
            }

            retry_batch_operation(
                |requests| {
                    let mut req_map = HashMap::new();
                    req_map.insert(self.table_name.clone(), requests.to_vec());
                    Box::pin(
                        self.client
                            .batch_write_item()
                            .set_request_items(Some(req_map))
                            .send(),
                    )
                },
                &self.table_name,
                write_requests,
            )
            .await
            .map_err(StorageError::DynamoDbError)?;
        }

        Ok(())
    }

    async fn flush(&self) -> StorageResult<()> {
        // DynamoDB is always consistent, no flush needed
        Ok(())
    }

    fn backend_name(&self) -> &'static str {
        "dynamodb"
    }

    fn execution_model(&self) -> super::traits::ExecutionModel {
        // DynamoDB is truly async (network I/O)
        super::traits::ExecutionModel::Async
    }

    fn flush_behavior(&self) -> super::traits::FlushBehavior {
        // DynamoDB is eventually consistent, flush is a no-op
        super::traits::FlushBehavior::NoOp
    }
}

/// Strategy for resolving table names from namespaces
#[derive(Clone, Debug)]
pub enum TableNameResolver {
    /// Append namespace to prefix: "{prefix}-{namespace}"
    Prefix(String),
    /// Map namespace to exact table name. keys are namespaces ("main", "metadata", etc)
    Explicit(HashMap<String, String>),
}

/// DynamoDB-backed NamespacedStore
///
/// Each namespace maps to a separate DynamoDB table for optimal performance.
/// Table names are resolved using the `TableNameResolver`.
/// The user_id is used as the partition key for multi-tenant isolation.

/// Specialized DynamoDB store for native index with simplified key structure
/// Uses user_id:feature (classification) as partition key and term as sort key
/// Format: PK = user_id:feature, SK = term
/// This enables efficient queries like "all words starting with 'hel'"
pub struct DynamoDbNativeIndexStore {
    client: Arc<Client>,
    table_name: String,
    user_id: String,
}

impl DynamoDbNativeIndexStore {
    fn new(client: Arc<Client>, table_name: String, user_id: String) -> Self {
        Self {
            client,
            table_name,
            user_id,
        }
    }

    /// Parse key to extract feature and term
    /// Keys are in format: "feature:term" (e.g., "word:hello", "email:test@example.com")
    fn parse_key(&self, key: &[u8]) -> StorageResult<(String, String)> {
        let key_str = String::from_utf8_lossy(key);
        if let Some(colon_pos) = key_str.find(':') {
            let feature = key_str[..colon_pos].to_string();
            let term = key_str[colon_pos + 1..].to_string();
            Ok((feature, term))
        } else {
            Err(StorageError::SerializationError(format!(
                "Invalid key format: missing colon in '{}'",
                key_str
            )))
        }
    }

    /// Get partition key (feature) for native index
    /// Format: user_id:feature
    fn get_partition_key(&self, feature: &str) -> String {
        format!("{}:{}", self.user_id, feature)
    }
}

pub struct DynamoDbNamespacedStore {
    client: Arc<Client>,
    /// Strategy to resolve namespace to table name
    resolver: TableNameResolver,
    /// Whether to automatically create tables if they don't exist
    auto_create: bool,
    /// user_id that will be used as the partition key (for multi-tenant isolation)
    user_id: String,
}

impl DynamoDbNamespacedStore {
    /// Create a new DynamoDB NamespacedStore with flexible configuration
    pub fn new(
        client: Client,
        resolver: TableNameResolver,
        auto_create: bool,
        user_id: String,
    ) -> Self {
        Self {
            client: Arc::new(client),
            resolver,
            auto_create,
            user_id,
        }
    }

    /// Create a new DynamoDB NamespacedStore with legacy prefix behavior (auto-create enabled)
    pub fn new_with_prefix(client: Client, prefix: String, user_id: String) -> Self {
        Self::new(client, TableNameResolver::Prefix(prefix), true, user_id)
    }

    /// Set user_id for multi-tenant isolation
    /// The user_id will be used as the partition key
    pub fn with_user_id(mut self, user_id: String) -> Self {
        self.user_id = user_id;
        self
    }

    /// Generate table name for a namespace
    fn table_name_for_namespace(&self, namespace: &str) -> StorageResult<String> {
        match &self.resolver {
            TableNameResolver::Prefix(prefix) => Ok(format!("{}-{}", prefix, namespace)),
            TableNameResolver::Explicit(map) => map.get(namespace).cloned().ok_or_else(|| {
                StorageError::ConfigurationError(format!(
                    "No explicit table name configured for namespace '{}'",
                    namespace
                ))
            }),
        }
    }

    /// Test helper to get table name for a namespace
    #[cfg(test)]
    pub fn get_table_name_for_namespace(&self, namespace: &str) -> String {
        self.table_name_for_namespace(namespace)
            .unwrap_or_else(|_| "unknown".to_string())
    }

    /// Ensure a DynamoDB table exists, creating it if necessary
    async fn ensure_table_exists(&self, table_name: &str) -> StorageResult<()> {
        // First, check if the table exists
        match self
            .client
            .describe_table()
            .table_name(table_name)
            .send()
            .await
        {
            Ok(response) => {
                // Table exists, check if it's active
                if let Some(table) = response.table() {
                    if let Some(status) = table.table_status() {
                        if status == &aws_sdk_dynamodb::types::TableStatus::Active {
                            // Table exists and is active, we're good
                            return Ok(());
                        } else {
                            // Table exists but not active yet - wait a bit
                            log::debug!(
                                "Table {} exists but status is {:?}, waiting...",
                                table_name,
                                status
                            );
                            // For now, we'll proceed anyway as the table will become active soon
                            return Ok(());
                        }
                    }
                }
                // Table exists (even if we couldn't check status), we're good
                return Ok(());
            }
            Err(e) => {
                let error_str = e.to_string();
                // Check for ResourceNotFoundException specifically
                if error_str.contains("ResourceNotFoundException") {
                    // Table doesn't exist, we'll create it below
                } else if error_str.contains("service error") {
                    // "service error" is often a transient error or permissions issue
                    // Try to proceed - if the table doesn't exist, creation will fail
                    // If it does exist, operations will work
                    log::warn!("Got 'service error' when checking table {} - proceeding to attempt creation", table_name);
                    // Do NOT return Ok(()) here; let it fall through to create_table
                } else {
                    // For other errors, still try to proceed but log a warning
                    log::warn!(
                        "Unexpected error checking table {}: {} - proceeding anyway",
                        table_name,
                        error_str
                    );
                    // Don't fail immediately - let the create attempt below handle it
                }
            }
        }

        // Table doesn't exist, create it
        let create_result = self
            .client
            .create_table()
            .table_name(table_name)
            .attribute_definitions(
                AttributeDefinition::builder()
                    .attribute_name("PK")
                    .attribute_type(ScalarAttributeType::S)
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!(
                            "Failed to build attribute definition: {}",
                            e
                        ))
                    })?,
            )
            .attribute_definitions(
                AttributeDefinition::builder()
                    .attribute_name("SK")
                    .attribute_type(ScalarAttributeType::S)
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!(
                            "Failed to build attribute definition: {}",
                            e
                        ))
                    })?,
            )
            .key_schema(
                KeySchemaElement::builder()
                    .attribute_name("PK")
                    .key_type(KeyType::Hash)
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!("Failed to build key schema: {}", e))
                    })?,
            )
            .key_schema(
                KeySchemaElement::builder()
                    .attribute_name("SK")
                    .key_type(KeyType::Range)
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!("Failed to build key schema: {}", e))
                    })?,
            )
            .billing_mode(BillingMode::PayPerRequest)
            .send()
            .await;

        match create_result {
            Ok(_) => {
                // Wait for table to be ACTIVE before returning
                // Poll with exponential backoff (max 30 seconds total)
                let mut attempts = 0;
                const MAX_ATTEMPTS: u32 = 30;

                loop {
                    tokio::time::sleep(tokio::time::Duration::from_millis(500)).await;

                    match self
                        .client
                        .describe_table()
                        .table_name(table_name)
                        .send()
                        .await
                    {
                        Ok(response) => {
                            if let Some(table) = response.table {
                                if let Some(status) = table.table_status {
                                    if matches!(status, TableStatus::Active) {
                                        return Ok(());
                                    }
                                }
                            }
                        }
                        Err(_) => {
                            // Continue polling
                        }
                    }

                    attempts += 1;
                    if attempts >= MAX_ATTEMPTS {
                        return Err(StorageError::DynamoDbError(format!(
                            "Table '{}' did not become ACTIVE within timeout",
                            table_name
                        )));
                    }
                }
            }
            Err(e) => {
                // If table was created by another process between our check and create, that's ok
                if e.to_string().contains("ResourceInUseException") {
                    Ok(())
                } else {
                    Err(StorageError::DynamoDbError(format!(
                        "Failed to create table {}: {}",
                        table_name, e
                    )))
                }
            }
        }
    }
}

#[async_trait]
impl KvStore for DynamoDbNativeIndexStore {
    async fn get(&self, key: &[u8]) -> StorageResult<Option<Vec<u8>>> {
        let (feature, term) = self.parse_key(key)?;
        let pk = self.get_partition_key(&feature);

        let result = self
            .client
            .get_item()
            .table_name(&self.table_name)
            .key("PK", AttributeValue::S(pk))
            .key("SK", AttributeValue::S(term))
            .send()
            .await
            .map_err(|e| StorageError::DynamoDbError(e.to_string()))?;

        if let Some(item) = result.item {
            if let Some(AttributeValue::S(json_str)) = item.get("Value") {
                return Ok(Some(json_str.as_bytes().to_vec()));
            }
        }

        Ok(None)
    }

    async fn put(&self, key: &[u8], value: Vec<u8>) -> StorageResult<()> {
        let (feature, term) = self.parse_key(key)?;
        let pk = self.get_partition_key(&feature);

        let json_str = String::from_utf8(value).map_err(|e| {
            StorageError::SerializationError(format!("Invalid UTF-8 in value: {}", e))
        })?;

        self.client
            .put_item()
            .table_name(&self.table_name)
            .item("PK", AttributeValue::S(pk))
            .item("SK", AttributeValue::S(term))
            .item("Value", AttributeValue::S(json_str))
            .send()
            .await
            .map_err(|e| StorageError::DynamoDbError(e.to_string()))?;

        Ok(())
    }

    async fn delete(&self, key: &[u8]) -> StorageResult<bool> {
        let (feature, term) = self.parse_key(key)?;
        let pk = self.get_partition_key(&feature);

        let result = self
            .client
            .delete_item()
            .table_name(&self.table_name)
            .key("PK", AttributeValue::S(pk))
            .key("SK", AttributeValue::S(term))
            .return_values(aws_sdk_dynamodb::types::ReturnValue::AllOld)
            .send()
            .await
            .map_err(|e| StorageError::DynamoDbError(e.to_string()))?;

        Ok(result.attributes.is_some())
    }

    async fn exists(&self, key: &[u8]) -> StorageResult<bool> {
        let (feature, term) = self.parse_key(key)?;
        let pk = self.get_partition_key(&feature);

        let result = self
            .client
            .get_item()
            .table_name(&self.table_name)
            .key("PK", AttributeValue::S(pk))
            .key("SK", AttributeValue::S(term))
            .projection_expression("PK")
            .send()
            .await
            .map_err(|e| StorageError::DynamoDbError(e.to_string()))?;

        Ok(result.item.is_some())
    }

    async fn scan_prefix(&self, prefix: &[u8]) -> StorageResult<Vec<(Vec<u8>, Vec<u8>)>> {
        let prefix_str = String::from_utf8_lossy(prefix);

        // Parse prefix to extract feature
        // Prefixes are in format: "feature:" or "feature:term_prefix"
        let (feature, term_prefix) = if let Some(colon_pos) = prefix_str.find(':') {
            let feature = prefix_str[..colon_pos].to_string();
            let term_prefix = prefix_str[colon_pos + 1..].to_string();
            (feature, term_prefix)
        } else {
            return Err(StorageError::SerializationError(format!(
                "Invalid prefix format: missing colon in '{}'",
                prefix_str
            )));
        };

        let pk = self.get_partition_key(&feature);

        // Query with feature as PK and term prefix on SK
        let mut results = Vec::new();
        let mut last_evaluated_key: Option<HashMap<String, AttributeValue>> = None;

        loop {
            let mut query = self
                .client
                .query()
                .table_name(&self.table_name)
                .key_condition_expression("PK = :pk AND begins_with(SK, :prefix)")
                .expression_attribute_values(":pk", AttributeValue::S(pk.clone()))
                .expression_attribute_values(":prefix", AttributeValue::S(term_prefix.clone()));

            if let Some(key) = last_evaluated_key.take() {
                query = query.set_exclusive_start_key(Some(key));
            }

            let response = match query.send().await {
                Ok(r) => r,
                Err(e) => {
                    let error_str = e.to_string();
                    if error_str.contains("ResourceNotFoundException")
                        || error_str.contains("ResourceInUseException")
                        || error_str.contains("cannot do operations on a non-existent table")
                    {
                        return Ok(Vec::new());
                    }
                    return Err(StorageError::DynamoDbError(error_str));
                }
            };

            if let Some(items) = response.items {
                for item in items {
                    if let (Some(AttributeValue::S(sk)), Some(AttributeValue::S(json_str))) =
                        (item.get("SK"), item.get("Value"))
                    {
                        // Reconstruct full key: "feature:term"
                        let full_key = format!("{}:{}", feature, sk);
                        results.push((full_key.as_bytes().to_vec(), json_str.as_bytes().to_vec()));
                    }
                }
            }

            last_evaluated_key = response.last_evaluated_key;
            if last_evaluated_key.is_none() {
                break;
            }
        }

        Ok(results)
    }

    async fn batch_put(&self, items: Vec<(Vec<u8>, Vec<u8>)>) -> StorageResult<()> {
        // DynamoDB has a 25-item batch limit
        const BATCH_SIZE: usize = 25;

        for chunk in items.chunks(BATCH_SIZE) {
            let mut write_requests = Vec::new();

            for (key, value) in chunk {
                let (feature, term) = self.parse_key(key)?;
                let pk = self.get_partition_key(&feature);

                let json_str = String::from_utf8(value.clone()).map_err(|e| {
                    StorageError::SerializationError(format!("Invalid UTF-8 in batch value: {}", e))
                })?;

                let put_request = aws_sdk_dynamodb::types::PutRequest::builder()
                    .item("PK", AttributeValue::S(pk))
                    .item("SK", AttributeValue::S(term))
                    .item("Value", AttributeValue::S(json_str))
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!(
                            "Failed to build put request for table '{}': {}",
                            self.table_name, e
                        ))
                    })?;

                let write_request = aws_sdk_dynamodb::types::WriteRequest::builder()
                    .put_request(put_request)
                    .build();

                write_requests.push(write_request);
            }

            retry_batch_operation(
                |requests| {
                    Box::pin(
                        self.client
                            .batch_write_item()
                            .request_items(&self.table_name, requests.to_vec())
                            .send(),
                    )
                },
                &self.table_name,
                write_requests,
            )
            .await
            .map_err(StorageError::DynamoDbError)?;
        }

        Ok(())
    }

    async fn batch_delete(&self, keys: Vec<Vec<u8>>) -> StorageResult<()> {
        // DynamoDB has a 25-item batch limit
        const BATCH_SIZE: usize = 25;

        for chunk in keys.chunks(BATCH_SIZE) {
            let mut write_requests = Vec::new();

            for key in chunk {
                let (feature, term) = self.parse_key(key)?;
                let pk = self.get_partition_key(&feature);

                let delete_request = aws_sdk_dynamodb::types::DeleteRequest::builder()
                    .key("PK", AttributeValue::S(pk))
                    .key("SK", AttributeValue::S(term))
                    .build()
                    .map_err(|e| {
                        StorageError::DynamoDbError(format!(
                            "Failed to build delete request for table '{}': {}",
                            self.table_name, e
                        ))
                    })?;

                let write_request = aws_sdk_dynamodb::types::WriteRequest::builder()
                    .delete_request(delete_request)
                    .build();

                write_requests.push(write_request);
            }

            retry_batch_operation(
                |requests| {
                    Box::pin(
                        self.client
                            .batch_write_item()
                            .request_items(&self.table_name, requests.to_vec())
                            .send(),
                    )
                },
                &self.table_name,
                write_requests,
            )
            .await
            .map_err(StorageError::DynamoDbError)?;
        }

        Ok(())
    }

    async fn flush(&self) -> StorageResult<()> {
        // DynamoDB is eventually consistent, no explicit flush needed
        Ok(())
    }

    fn backend_name(&self) -> &'static str {
        "dynamodb-native-index"
    }

    fn execution_model(&self) -> super::traits::ExecutionModel {
        // DynamoDB is truly async (network I/O)
        super::traits::ExecutionModel::Async
    }

    fn flush_behavior(&self) -> super::traits::FlushBehavior {
        // DynamoDB is eventually consistent, flush is a no-op
        super::traits::FlushBehavior::NoOp
    }
}

#[async_trait]
impl NamespacedStore for DynamoDbNamespacedStore {
    async fn open_namespace(&self, name: &str) -> StorageResult<Arc<dyn KvStore>> {
        let table_name = self.table_name_for_namespace(name)?;

        // Ensure the table exists if auto_create is enabled
        if self.auto_create {
            self.ensure_table_exists(&table_name).await?;
        }

        // For native_index namespace, use simplified key structure: feature as PK, term as SK
        // This enables efficient queries by feature type (word, email, etc.)
        if name == "native_index" {
            let store = DynamoDbNativeIndexStore::new(
                self.client.clone(),
                table_name,
                self.user_id.clone(),
            );
            Ok(Arc::new(store))
        } else {
            let store = DynamoDbKvStore::new(self.client.clone(), table_name, self.user_id.clone());
            Ok(Arc::new(store))
        }
    }

    async fn list_namespaces(&self) -> StorageResult<Vec<String>> {
        // This would require scanning all keys and extracting unique namespaces
        // For now, we'll return an error indicating it's not implemented
        Err(StorageError::InvalidOperation(
            "list_namespaces not implemented for DynamoDB - requires custom implementation"
                .to_string(),
        ))
    }

    async fn delete_namespace(&self, _name: &str) -> StorageResult<bool> {
        // Would need to scan and delete all items with the namespace prefix
        Err(StorageError::InvalidOperation(
            "delete_namespace not implemented for DynamoDB - requires custom implementation"
                .to_string(),
        ))
    }
}

#[cfg(test)]
mod unit_tests {
    use super::*;
    use aws_sdk_dynamodb::config::Region;

    // Helper to create a dummy client (won't actually be used for network calls in these tests)
    async fn create_dummy_client() -> Arc<Client> {
        let config = aws_config::defaults(aws_config::BehaviorVersion::latest())
            .region(Region::new("us-east-1"))
            .load()
            .await;
        Arc::new(Client::new(&config))
    }

    #[tokio::test]
    async fn test_kv_store_key_generation() {
        let client = create_dummy_client().await;

        // Case 1: With user_id
        let store = DynamoDbKvStore::new(
            client.clone(),
            "TestTable".to_string(),
            "user123".to_string(),
        );

        let key = b"my_key";
        let pk = store.get_partition_key_with_key(key);
        let sk = store.make_sort_key_impl(key);

        // PK should now be just user_id (or default)
        // SK should be the key itself
        assert_eq!(pk, "user123");
        assert_eq!(sk, "my_key");

        // Case 2: Without user_id (default)
        let store_default = DynamoDbKvStore::new(
            client.clone(),
            "TestTable".to_string(),
            "default".to_string(),
        );

        let pk_default = store_default.get_partition_key_with_key(key);
        assert_eq!(pk_default, "default");
    }

    #[tokio::test]
    async fn test_native_index_key_parsing() {
        let client = create_dummy_client().await;
        let store =
            DynamoDbNativeIndexStore::new(client, "IndexTable".to_string(), "user123".to_string());

        // Case 1: Standard feature:term key
        let (feature, term) = store.parse_key(b"word:hello").unwrap();
        assert_eq!(feature, "word");
        assert_eq!(term, "hello");

        // Case 2: Email feature
        let (feature, term) = store.parse_key(b"email:test@example.com").unwrap();
        assert_eq!(feature, "email");
        assert_eq!(term, "test@example.com");

        // Case 3: No colon (error)
        let result = store.parse_key(b"just_a_word");
        assert!(result.is_err());

        // Case 4: Empty term
        let (feature, term) = store.parse_key(b"word:").unwrap();
        assert_eq!(feature, "word");
        assert_eq!(term, "");
    }

    #[tokio::test]
    async fn test_native_index_partition_key() {
        let client = create_dummy_client().await;

        // Case 1: With user_id
        let store = DynamoDbNativeIndexStore::new(
            client.clone(),
            "IndexTable".to_string(),
            "user123".to_string(),
        );

        let pk = store.get_partition_key("word");
        assert_eq!(pk, "user123:word");

        // Case 2: Without user_id
        let store_default =
            DynamoDbNativeIndexStore::new(client, "IndexTable".to_string(), "default".to_string());

        let pk_default = store_default.get_partition_key("email");
        assert_eq!(pk_default, "default:email");
    }
}