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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
use crate::aql::get_all_data_aql;
use crate::client::auth::handle_auth;
use crate::client::config::ClientConfig;
use crate::client::{build_client, make_url};
use crate::errors::GraphLoaderError;
use crate::request::handle_arangodb_response_with_parsed_body;
use crate::sharding::{compute_faked_shard_map, compute_shard_map};
use crate::types::info::{
    DeploymentInfo, DeploymentType, LoadStrategy, SupportInfo, VersionInformation,
};
use crate::{DataLoadConfiguration, DatabaseConfiguration};
use bytes::Bytes;
use log::{debug, error, info};
use reqwest::StatusCode;
use reqwest_middleware::ClientWithMiddleware;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::cmp::PartialEq;
use std::collections::{HashMap, HashSet};
use std::num::ParseIntError;
use std::thread::JoinHandle;
use std::time::SystemTime;

static MIN_SUPPORTED_MINOR_VERSIONS: &[(u8, u8)] = &[(3, 12)];

// Define the necessary structs
#[derive(Clone)]
pub struct CollectionInfo {
    pub name: String,
    pub fields: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct CursorResult {
    result: Vec<Value>,
}

pub struct GraphLoader {
    db_config: DatabaseConfiguration,
    load_config: DataLoadConfiguration,
    v_collections: HashMap<String, CollectionInfo>,
    e_collections: HashMap<String, CollectionInfo>,
    vertex_map: crate::sharding::ShardMap,
    edge_map: crate::sharding::ShardMap,
    // should be used as private
    load_strategy: Option<LoadStrategy>,
    support_info: Option<SupportInfo>,
    supports_projections: Option<bool>,
}

fn collection_name_from_id(id: &str) -> String {
    match id.find('/') {
        None => "".to_string(),
        Some(p) => id[0..p].to_string(),
    }
}

impl PartialEq<DeploymentType> for &DeploymentType {
    fn eq(&self, other: &DeploymentType) -> bool {
        matches!(
            (self, other),
            (DeploymentType::Cluster, DeploymentType::Cluster)
                | (DeploymentType::Single, DeploymentType::Single)
        )
    }
}

impl GraphLoader {
    pub async fn new(
        db_config: DatabaseConfiguration,
        load_config: DataLoadConfiguration,
        vertex_collections: Vec<CollectionInfo>,
        edge_collections: Vec<CollectionInfo>,
    ) -> Result<GraphLoader, GraphLoaderError> {
        let v_collections = vertex_collections
            .into_iter()
            .map(|c| (c.name.clone(), c))
            .collect();
        let e_collections = edge_collections
            .into_iter()
            .map(|c| (c.name.clone(), c))
            .collect();

        let mut graph_loader = GraphLoader {
            db_config,
            load_config,
            v_collections,
            e_collections,
            vertex_map: HashMap::new(),
            edge_map: HashMap::new(),
            load_strategy: None,
            support_info: None,
            supports_projections: None,
        };
        let init_result = graph_loader.initialize().await;
        init_result?;

        Ok(graph_loader)
    }

    async fn does_arangodb_supports_dump_endpoint(
        &self,
        client: &ClientWithMiddleware,
        support_info: &Option<SupportInfo>,
    ) -> Result<(bool, bool), String> {
        let is_cluster = support_info
            .as_ref()
            .ok_or("Support Info not set".to_string())?
            .deployment
            .deployment_type
            == DeploymentType::Cluster;
        let server_version_url = self.db_config.endpoints[0].clone() + "/_api/version";
        let resp = handle_auth(client.get(server_version_url), &self.db_config)
            .send()
            .await;
        let version_info_result =
            handle_arangodb_response_with_parsed_body::<VersionInformation>(resp, StatusCode::OK)
                .await;
        if let Err(e) = version_info_result {
            return Err(e.to_string());
        }
        let version_info = version_info_result.unwrap();

        let version_parts: Vec<&str> = version_info.version.split('.').collect();
        if version_parts.len() < 3 {
            return Err(format!(
                "Unable to parse ArangoDB Version - got {}",
                version_info.version
            ));
        }

        let (supports_dump_endpoint, support_dump_projections) = {
            let major: u8 = version_parts
                .first()
                .ok_or("Unable to parse Major Version".to_string())?
                .parse()
                .map_err(|err: ParseIntError| err.to_string())?;
            let minor: u8 = version_parts
                .get(1)
                .ok_or("Unable to parse Minor Version".to_string())?
                .parse()
                .map_err(|err: ParseIntError| err.to_string())?;
            let major_supports = MIN_SUPPORTED_MINOR_VERSIONS
                .iter()
                .map(|x| x.0)
                .any(|x| x == major);
            if !major_supports {
                (false, false)
            } else {
                let mut supports_dump = MIN_SUPPORTED_MINOR_VERSIONS
                    .iter()
                    .find(|x| x.0 == major)
                    .ok_or("Unable to find supported version".to_string())?
                    .1
                    <= minor;

                // One special rule, if we are a cluster and 3.11, we support the dump endpoint
                if is_cluster && major == 3 && minor == 11 {
                    supports_dump = true;
                }

                // Rule for projections: Supported from 3.12 onwards
                let mut supports_projections = false;
                if major == 3 && minor >= 12 {
                    supports_projections = true;
                }

                (supports_dump, supports_projections)
            }
        };

        Ok((supports_dump_endpoint, support_dump_projections))
    }

    async fn get_arangodb_support_information(
        &self,
        client: &ClientWithMiddleware,
    ) -> Result<SupportInfo, String> {
        let server_information_url = self.db_config.endpoints[0].clone() + "/_admin/support-info";
        let support_info_res = handle_auth(client.get(server_information_url), &self.db_config)
            .send()
            .await;
        let support_info_result = handle_arangodb_response_with_parsed_body::<SupportInfo>(
            support_info_res,
            StatusCode::OK,
        )
        .await;
        if let Err(e) = support_info_result {
            return Err(e.to_string());
        }
        let support_info = support_info_result.unwrap();
        Ok(support_info)
    }

    fn identify_arangodb_load_strategy(&self, dump_support_enabled: bool) -> LoadStrategy {
        let support_info = &self
            .support_info
            .as_ref()
            .unwrap()
            .deployment
            .deployment_type;
        if !dump_support_enabled && support_info == DeploymentType::Single {
            LoadStrategy::Aql
        } else {
            LoadStrategy::Dump
        }
    }

    async fn identify_load_strategy(
        &mut self,
        client: &ClientWithMiddleware,
    ) -> Result<(), String> {
        self.support_info = match self.get_arangodb_support_information(client).await {
            Ok(info) => Some(info),
            Err(e) => {
                print!(
                    "Failed to read ArangoDB environment information: {}. \
                    This can happen if the current user is not allowed to access the '/_admin/support-info' endpoint. \
                    Assuming ArangoDB instance version is at least '3.12'. \
                    We will use the aql load strategy as the loading strategy. \
                    While this works, this will be slower then using the dump endpoint instead. ", e
                );
                None
            }
        };

        if self.support_info.is_none() {
            // assume the environment is a cluster
            self.support_info = Some(SupportInfo {
                deployment: DeploymentInfo {
                    deployment_type: DeploymentType::Single,
                },
            });

            // assume that dump endpoint is supported
            self.load_strategy = Some(LoadStrategy::Aql);
            // assume projections are supported
            self.supports_projections = Some(true);
        } else {
            let (dump_support_enabled, dump_projections_support) = self
                .does_arangodb_supports_dump_endpoint(client, &self.support_info)
                .await?;
            self.supports_projections = Some(dump_projections_support);
            self.load_strategy = Some(self.identify_arangodb_load_strategy(dump_support_enabled));
        }

        debug_assert!(self.load_strategy.is_some());
        debug_assert!(self.supports_projections.is_some());

        Ok(())
    }

    fn verify_parameters(&self) -> Result<(), GraphLoaderError> {
        if !self.get_all_vertex_fields_as_list_to_return().is_empty()
            && self.load_config.load_all_vertex_attributes
        {
            return Err(GraphLoaderError::from(
                "load_all_vertex_attributes is set to true, but vertex collections are not empty."
                    .to_string(),
            ));
        }
        if !self.get_all_edges_fields_as_list_to_return().is_empty()
            && self.load_config.load_all_edge_attributes
        {
            return Err(GraphLoaderError::from(
                "load_all_edge_attributes is set to true, but edge collections are not empty."
                    .to_string(),
            ));
        }
        Ok(())
    }

    async fn initialize(&mut self) -> Result<(), GraphLoaderError> {
        self.verify_parameters()?;

        let use_tls = self.db_config.endpoints[0].starts_with("https://");
        let client_config = ClientConfig::builder()
            .n_retries(5)
            .use_tls(use_tls)
            .tls_cert_opt(self.db_config.tls_cert.clone())
            .build();
        let client = build_client(&client_config)?;
        self.identify_load_strategy(&client).await?;

        let load_strategy = self.load_strategy.as_ref().unwrap();
        let deployment_type = &self
            .support_info
            .as_ref()
            .unwrap()
            .deployment
            .deployment_type;

        if load_strategy == &LoadStrategy::Dump {
            if *deployment_type == DeploymentType::Cluster {
                // Compute which shard we must get from which dbserver, we do vertices
                // and edges right away to be able to error out early:

                // First ask for the shard distribution:
                let url = make_url(&self.db_config, "/_admin/cluster/shardDistribution");
                let resp = handle_auth(client.get(url), &self.db_config).send().await;

                let shard_dist = handle_arangodb_response_with_parsed_body::<
                    crate::sharding::ShardDistribution,
                >(resp, StatusCode::OK)
                .await?;
                info!("Using dump strategy for loading data.");
                self.vertex_map =
                    compute_shard_map(&shard_dist, &self.get_vertex_collections_as_list())?;
                self.edge_map =
                    compute_shard_map(&shard_dist, &self.get_edge_collections_as_list())?;
            } else {
                self.vertex_map = compute_faked_shard_map(&self.get_vertex_collections_as_list());
                self.edge_map = compute_faked_shard_map(&self.get_edge_collections_as_list());
            }
            info!(
                "{:?} Need to fetch data from {} vertex shards and {} edge shards...",
                std::time::SystemTime::now(),
                self.vertex_map.values().map(|v| v.len()).sum::<usize>(),
                self.edge_map.values().map(|v| v.len()).sum::<usize>()
            );
        } else {
            info!("Using AQL strategy for loading data.");
        }

        Ok(())
    }

    pub async fn new_named(
        db_config: DatabaseConfiguration,
        load_config: DataLoadConfiguration,
        graph_name: String,
        vertex_global_fields: Option<Vec<String>>,
        edge_global_fields: Option<Vec<String>>,
    ) -> Result<GraphLoader, GraphLoaderError> {
        let vertex_coll_list;
        let edge_coll_list;

        match get_graph_collections(&db_config, graph_name).await {
            Ok((vertex_collections, edge_collections)) => {
                vertex_coll_list = vertex_collections
                    .iter()
                    .map(|c| CollectionInfo {
                        name: c.clone(),
                        fields: vertex_global_fields.clone().unwrap_or_default(),
                    })
                    .collect();

                edge_coll_list = edge_collections
                    .iter()
                    .map(|c| CollectionInfo {
                        name: c.clone(),
                        fields: edge_global_fields.clone().unwrap_or_default(),
                    })
                    .collect();
            }
            Err(err) => {
                return Err(err);
            }
        }

        let graph_loader =
            GraphLoader::new(db_config, load_config, vertex_coll_list, edge_coll_list).await?;
        Ok(graph_loader)
    }

    pub async fn new_custom(
        db_config: DatabaseConfiguration,
        load_config: DataLoadConfiguration,
        vertex_collections: Vec<CollectionInfo>,
        edge_collections: Vec<CollectionInfo>,
    ) -> Result<Self, GraphLoaderError> {
        let graph_loader =
            GraphLoader::new(db_config, load_config, vertex_collections, edge_collections).await?;
        Ok(graph_loader)
    }

    pub async fn do_vertices<F>(&self, vertices_function: F) -> Result<(), GraphLoaderError>
    where
        F: Fn(&Vec<Vec<u8>>, &mut Vec<Vec<Value>>, &Vec<String>) -> Result<(), GraphLoaderError>
            + Send
            + Sync
            + Clone
            + 'static,
    {
        {
            // We use multiple threads to receive the data in batches:
            let mut senders: Vec<tokio::sync::mpsc::Sender<Bytes>> = vec![];
            let mut consumers: Vec<JoinHandle<Result<(), GraphLoaderError>>> = vec![];

            for _i in 0..self.load_config.parallelism {
                let (sender, mut receiver) = tokio::sync::mpsc::channel::<Bytes>(10);
                senders.push(sender);

                let vertex_global_fields = self.get_all_vertex_fields_as_list_to_return();
                let insert_vertex_clone = vertices_function.clone();
                let strategy_clone = self.load_strategy;
                let load_config_clone = self.load_config.clone();

                let consumer = std::thread::spawn(move || -> Result<(), GraphLoaderError> {
                    let begin = SystemTime::now();
                    while let Some(resp) = receiver.blocking_recv() {
                        let body_result = std::str::from_utf8(resp.as_ref());
                        let body = match body_result {
                            Ok(body) => body,
                            Err(e) => {
                                return Err(GraphLoaderError::Utf8Error(format!(
                                    "UTF8 error when parsing body: {:?}",
                                    e
                                )))
                            }
                        };
                        debug!(
                            "{:?} Received post response, body size: {}",
                            SystemTime::now().duration_since(begin),
                            body.len()
                        );
                        let mut vertex_ids: Vec<Vec<u8>> = Vec::with_capacity(400000);
                        let mut vertex_json: Vec<Vec<Value>> = Vec::with_capacity(400000);

                        if strategy_clone == Option::from(LoadStrategy::Dump) {
                            for line in body.lines() {
                                let mut vertex: Value = match serde_json::from_str(line) {
                                    Err(err) => {
                                        return Err(GraphLoaderError::JsonParseError(format!(
                                            "Error parsing document for line:\n{}\n{:?}",
                                            line, err
                                        )));
                                    }
                                    Ok(val) => val,
                                };

                                let id = &vertex["_id"];
                                let idstr: &String = match id {
                                    Value::String(i) => {
                                        let mut buf = vec![];
                                        buf.extend_from_slice(i[..].as_bytes());
                                        vertex_ids.push(buf);
                                        i
                                    }
                                    _ => {
                                        return Err(GraphLoaderError::JsonParseError(format!(
                                            "JSON is no object with a string _id attribute:\n{}",
                                            vertex
                                        )));
                                    }
                                };

                                if load_config_clone.load_all_vertex_attributes {
                                    vertex.as_object_mut().unwrap().remove("_id");
                                    vertex_json.push(vec![vertex]);
                                } else {
                                    // If we get here, we have to extract the field
                                    // values in `fields` from the json and store it
                                    // to vertex_json:
                                    let get_value = |v: &Value, field: &str| -> Value {
                                        if field == "@collection_name" {
                                            Value::String(collection_name_from_id(idstr))
                                        } else {
                                            v[field].clone()
                                        }
                                    };

                                    let mut cols: Vec<Value> =
                                        Vec::with_capacity(vertex_global_fields.len());
                                    for f in vertex_global_fields.iter() {
                                        let j = get_value(&vertex, f);
                                        cols.push(j);
                                    }

                                    vertex_json.push(cols);
                                }
                            }
                        } else {
                            // This it the AQL Loading variant
                            let values = match serde_json::from_str::<CursorResult>(body) {
                                Err(err) => {
                                    return Err(GraphLoaderError::JsonParseError(format!(
                                        "AQL Error parsing document for body:\n{}\n{:?}",
                                        body, err
                                    )));
                                }
                                Ok(val) => val,
                            };

                            for mut vertex in values.result.into_iter() {
                                let id = &vertex["_id"];
                                let idstr: &String = match id {
                                    Value::String(i) => {
                                        let mut buf = vec![];
                                        buf.extend_from_slice(i[..].as_bytes());
                                        vertex_ids.push(buf);
                                        i
                                    }
                                    _ => {
                                        return Err(GraphLoaderError::JsonParseError(format!(
                                            "JSON is no object with a string _id attribute:\n{}",
                                            vertex
                                        )));
                                    }
                                };

                                if load_config_clone.load_all_vertex_attributes {
                                    vertex.as_object_mut().unwrap().remove("_id");
                                    vertex_json.push(vec![vertex]);
                                } else {
                                    // If we get here, we have to extract the field
                                    // values in `fields` from the json and store it
                                    // to vertex_json:
                                    let get_value = |v: &Value, field: &str| -> Value {
                                        if field == "@collection_name" {
                                            Value::String(collection_name_from_id(idstr))
                                        } else {
                                            v[field].clone()
                                        }
                                    };

                                    let mut cols: Vec<Value> =
                                        Vec::with_capacity(vertex_global_fields.len());
                                    for f in vertex_global_fields.iter() {
                                        let j = get_value(&vertex, f);
                                        cols.push(j);
                                    }
                                    vertex_json.push(cols);
                                }
                            }
                        }
                        insert_vertex_clone(&vertex_ids, &mut vertex_json, &vertex_global_fields)?;
                    }
                    Ok(())
                });
                consumers.push(consumer);
            }

            match &self.load_strategy {
                Some(LoadStrategy::Dump) => {
                    if self.v_collections.is_empty() {
                        error!("No vertex collections given!");
                        return Err(GraphLoaderError::from(
                            "No vertex collections given!".to_string(),
                        ));
                    }
                    if self.vertex_map.is_empty() {
                        error!("No vertex shards found!");
                        return Err(GraphLoaderError::from(
                            "No vertex shards found!".to_string(),
                        ));
                    }

                    let potential_vertex_projections = if self.supports_projections.unwrap_or(false)
                    {
                        self.produce_vertex_projections()
                    } else {
                        None
                    };

                    let dump_result = crate::sharding::get_all_shard_data(
                        &self.db_config,
                        &self.load_config,
                        &self.vertex_map,
                        senders,
                        &self
                            .support_info
                            .as_ref()
                            .unwrap()
                            .deployment
                            .deployment_type,
                        potential_vertex_projections,
                    )
                    .await;
                    if let Err(e) = dump_result {
                        error!("Error fetching vertex data: {:?}", e);
                        return Err(GraphLoaderError::from(format!(
                            "Error fetching vertex data: {:?}",
                            e
                        )));
                    }
                }
                Some(LoadStrategy::Aql) => {
                    let mut v_collection_infos: Vec<CollectionInfo> = vec![];
                    for (_name, info) in self.v_collections.iter() {
                        v_collection_infos.push(info.clone());
                    }

                    let aql_result = get_all_data_aql(
                        &self.db_config,
                        &self.load_config,
                        v_collection_infos.as_slice(),
                        senders,
                        false,
                    )
                    .await;
                    if let Err(e) = aql_result {
                        error!("Error fetching edge data: {:?}", e);
                        return Err(GraphLoaderError::from(format!(
                            "Failed to get aql cursor data: {}",
                            e
                        )));
                    }
                }
                None => {
                    return Err(GraphLoaderError::from("Load strategy not set".to_string()));
                }
            }

            info!("{:?} Got all data, processing...", SystemTime::now());
            for c in consumers {
                match c.join() {
                    Ok(Ok(())) => {
                        // The thread completed successfully and returned Ok
                    }
                    Ok(Err(e)) => {
                        // The thread completed but returned an error
                        eprintln!("Thread returned error: {:?}", e);
                        return Err(e); // Propagate the error
                    }
                    Err(e) => {
                        // The thread panicked
                        eprintln!("Thread panicked in do_vertices: {:?}", e);
                        return Err(GraphLoaderError::from(
                            "Thread panicked in do_vertices".to_string(),
                        ));
                    }
                }
            }
        }
        Ok(())
    }

    pub async fn do_edges<F>(&self, edges_function: F) -> Result<(), GraphLoaderError>
    where
        F: Fn(
                &Vec<Vec<u8>>,
                &Vec<Vec<u8>>,
                &mut Vec<Vec<Value>>,
                &Vec<String>,
            ) -> Result<(), GraphLoaderError>
            + Send
            + Sync
            + Clone
            + 'static,
    {
        let mut senders: Vec<tokio::sync::mpsc::Sender<Bytes>> = vec![];
        let mut consumers: Vec<JoinHandle<Result<(), GraphLoaderError>>> = vec![];

        for _i in 0..self.load_config.parallelism {
            let (sender, mut receiver) = tokio::sync::mpsc::channel::<Bytes>(10);
            senders.push(sender);

            let edge_global_fields = self.get_all_edges_fields_as_list_to_return();
            let insert_edge_clone = edges_function.clone();
            let strategy_clone = self.load_strategy;
            let load_config_clone = self.load_config.clone();

            let consumer = std::thread::spawn(move || -> Result<(), GraphLoaderError> {
                while let Some(resp) = receiver.blocking_recv() {
                    let body = std::str::from_utf8(resp.as_ref())
                        .map_err(|e| format!("UTF8 error when parsing body: {:?}", e))?;

                    let mut froms: Vec<Vec<u8>> = Vec::with_capacity(1000000);
                    let mut tos: Vec<Vec<u8>> = Vec::with_capacity(1000000);
                    let mut edge_json: Vec<Vec<Value>> = Vec::with_capacity(400000);

                    if strategy_clone == Option::from(LoadStrategy::Dump) {
                        for line in body.lines() {
                            let mut edge: Value = match serde_json::from_str(line) {
                                Err(err) => {
                                    return Err(GraphLoaderError::from(format!(
                                        "Error parsing document for line:\n{}\n{:?}",
                                        line, err
                                    )));
                                }
                                Ok(val) => val,
                            };

                            let from = &edge["_from"];
                            match from {
                                Value::String(i) => {
                                    let mut buf = vec![];
                                    buf.extend_from_slice(i[..].as_bytes());
                                    froms.push(buf);
                                }
                                _ => {
                                    return Err(GraphLoaderError::from(format!(
                                        "JSON is no object with a string _from attribute:\n{}",
                                        line
                                    )));
                                }
                            }

                            let to = &edge["_to"];
                            match to {
                                Value::String(i) => {
                                    let mut buf = vec![];
                                    buf.extend_from_slice(i[..].as_bytes());
                                    tos.push(buf);
                                }
                                _ => {
                                    return Err(GraphLoaderError::from(format!(
                                        "JSON is no object with a string _from attribute:\n{}",
                                        line
                                    )));
                                }
                            }

                            if load_config_clone.load_all_edge_attributes {
                                edge.as_object_mut().unwrap().remove("_from");
                                edge.as_object_mut().unwrap().remove("_to");
                                edge_json.push(vec![edge]);
                            } else {
                                // it is not guaranteed that the _id field is present
                                let id = &edge["_id"];
                                let idstr: Option<&String> = match id {
                                    Value::String(i) => Some(i),
                                    _ => None,
                                };

                                // If we get here, we have to extract the field
                                // values in `fields` from the json and store it
                                // to edge_json:
                                let get_value = |v: &Value, field: &str| -> Value {
                                    if field == "@collection_name" {
                                        if let Some(id) = idstr {
                                            Value::String(collection_name_from_id(id))
                                        } else {
                                            Value::String("n/A - _id is missing".to_string())
                                        }
                                    } else {
                                        v[field].clone()
                                    }
                                };

                                let mut cols: Vec<Value> =
                                    Vec::with_capacity(edge_global_fields.len());
                                for f in edge_global_fields.iter() {
                                    let j = get_value(&edge, f);
                                    cols.push(j);
                                }

                                edge_json.push(cols);
                            }
                        }
                    } else {
                        // AQL Variant
                        let values = match serde_json::from_str::<CursorResult>(body) {
                            Err(err) => {
                                return Err(GraphLoaderError::from(format!(
                                    "Error parsing document for body:\n{}\n{:?}",
                                    body, err
                                )));
                            }
                            Ok(val) => val,
                        };

                        for mut edge in values.result.into_iter() {
                            let from = &edge["_from"];
                            match from {
                                Value::String(i) => {
                                    let mut buf = vec![];
                                    buf.extend_from_slice(i[..].as_bytes());
                                    froms.push(buf);
                                }
                                _ => {
                                    return Err(GraphLoaderError::from(format!(
                                        "JSON is no object with a string _from attribute:\n{}",
                                        edge
                                    )));
                                }
                            }
                            let to = &edge["_to"];

                            match to {
                                Value::String(i) => {
                                    let mut buf = vec![];
                                    buf.extend_from_slice(i[..].as_bytes());
                                    tos.push(buf);
                                }
                                _ => {
                                    return Err(GraphLoaderError::from(format!(
                                        "JSON is no object with a string _from attribute:\n{}",
                                        edge
                                    )));
                                }
                            }

                            if load_config_clone.load_all_edge_attributes {
                                edge.as_object_mut().unwrap().remove("_from");
                                edge.as_object_mut().unwrap().remove("_to");
                                edge_json.push(vec![edge]);
                            } else {
                                // it is not guaranteed that the _id field is present
                                let id = &edge["_id"];
                                let idstr: Option<&String> = match id {
                                    Value::String(i) => Some(i),
                                    _ => None,
                                };

                                // If we get here, we have to extract the field
                                // values in `fields` from the json and store it
                                // to edge_json:
                                let get_value = |v: &Value, field: &str| -> Value {
                                    if field == "@collection_name" {
                                        if let Some(id) = idstr {
                                            Value::String(collection_name_from_id(id))
                                        } else {
                                            Value::String("n/A - _id is missing".to_string())
                                        }
                                    } else {
                                        v[field].clone()
                                    }
                                };

                                let mut cols: Vec<Value> =
                                    Vec::with_capacity(edge_global_fields.len());
                                for f in edge_global_fields.iter() {
                                    let j = get_value(&edge, f);
                                    cols.push(j);
                                }
                                edge_json.push(cols);
                            }
                        }
                    }
                    insert_edge_clone(&froms, &tos, &mut edge_json, &edge_global_fields)?;
                }
                Ok(())
            });
            consumers.push(consumer);
        }

        match self.load_strategy {
            Some(LoadStrategy::Dump) => {
                if self.e_collections.is_empty() {
                    error!("No edge collections given!");
                    return Err(GraphLoaderError::from(
                        "No edge collections given!".to_string(),
                    ));
                }
                if self.edge_map.is_empty() {
                    error!("No edge shards found!");
                    return Err(GraphLoaderError::from("No edge shards found!".to_string()));
                }
                let potential_edge_projections = if self.supports_projections.unwrap_or(false) {
                    self.produce_edge_projections()
                } else {
                    None
                };

                let shard_result = crate::sharding::get_all_shard_data(
                    &self.db_config,
                    &self.load_config,
                    &self.edge_map,
                    senders,
                    &self
                        .support_info
                        .as_ref()
                        .unwrap()
                        .deployment
                        .deployment_type,
                    potential_edge_projections,
                )
                .await;
                if let Err(e) = shard_result {
                    error!("Error fetching edge data: {:?}", e);
                    return Err(e);
                }
            }
            Some(LoadStrategy::Aql) => {
                let mut e_collection_infos: Vec<CollectionInfo> = vec![];
                for (_name, info) in self.e_collections.iter() {
                    e_collection_infos.push(info.clone());
                }

                let aql_result = get_all_data_aql(
                    &self.db_config,
                    &self.load_config,
                    e_collection_infos.as_slice(),
                    senders,
                    true,
                )
                .await;
                if let Err(e) = aql_result {
                    error!("Error fetching edge data: {:?}", e);
                    return Err(GraphLoaderError::from(format!(
                        "Failed to get aql cursor data: {}",
                        e
                    )));
                }
            }
            None => {
                return Err(GraphLoaderError::from("Load strategy not set".to_string()));
            }
        }

        info!(
            "{:?} Got all edge data, processing...",
            std::time::SystemTime::now()
        );
        for c in consumers {
            match c.join() {
                Ok(Ok(())) => {
                    // The thread completed successfully and returned Ok
                }
                Ok(Err(e)) => {
                    // The thread completed but returned an error
                    eprintln!("Thread returned error: {:?}", e);
                    return Err(e); // Propagate the error
                }
                Err(e) => {
                    // The thread panicked
                    eprintln!("Thread panicked in do_edges: {:?}", e);
                    return Err(GraphLoaderError::from(
                        "Thread panicked in do_edges".to_string(),
                    ));
                }
            }
        }
        Ok(())
    }

    pub fn get_vertex_collections_as_list(&self) -> Vec<String> {
        self.v_collections.keys().cloned().collect()
    }

    pub fn get_edge_collections_as_list(&self) -> Vec<String> {
        self.e_collections.keys().cloned().collect()
    }

    pub fn get_all_vertex_fields_as_list_to_return(&self) -> Vec<String> {
        // Guaranteed to be unique
        let mut unique_fields = HashSet::new();
        for fields in self.v_collections.values().flat_map(|c| c.fields.clone()) {
            unique_fields.insert(fields);
        }
        unique_fields.into_iter().collect()
    }

    pub fn get_all_vertices_fields_to_fetch_as_list(&self) -> Vec<String> {
        // Guaranteed to be unique
        // This method adds required fields if they are not present,
        // which need to be available to deliver the required resources.
        let mut unique_fields = self.get_all_vertex_fields_as_list_to_return();
        if !unique_fields.contains(&"_id".to_string()) {
            // _id is always required.
            unique_fields.insert(0, "_id".to_string());
        }
        unique_fields
    }

    pub fn get_all_edges_fields_as_list_to_return(&self) -> Vec<String> {
        // Guaranteed to be unique
        let mut unique_fields = HashSet::new();
        for fields in self.e_collections.values().flat_map(|c| c.fields.clone()) {
            unique_fields.insert(fields);
        }
        unique_fields.into_iter().collect()
    }

    pub fn get_all_edges_fields_to_fetch_as_list(&self) -> Vec<String> {
        // Guaranteed to be unique
        // This method adds required fields if they are not present,
        // which need to be available to deliver the required resources.

        let mut unique_fields = self.get_all_edges_fields_as_list_to_return();
        if unique_fields.is_empty() && !self.load_config.load_all_edge_attributes {
            unique_fields.insert(0, "_to".to_string());
            unique_fields.insert(0, "_from".to_string());
        }

        if unique_fields.contains(&"@collection_name".to_string())
            && !unique_fields.contains(&"_id".to_string())
        {
            // Compared to vertices, this is not a mandatory field.
            unique_fields.insert(0, "_id".to_string());
        }

        unique_fields
    }

    pub fn produce_vertex_projections(&self) -> Option<HashMap<String, Vec<String>>> {
        assert!(self.supports_projections.unwrap());
        let mut potential_vertex_projections: Option<HashMap<String, Vec<String>>> = None;
        let vertex_global_fields = self.get_all_vertices_fields_to_fetch_as_list();

        // We can only make use of projections in case:
        // 1.) The user has not requested all vertex attributes
        // 2.) ArangoDB supports the dump endpoint, which is Version 3.12 or higher
        let client_wants_all_vertex_attributes = self.load_config.load_all_vertex_attributes;
        if !client_wants_all_vertex_attributes {
            let mut vertex_projections: HashMap<String, Vec<String>> = HashMap::new();

            // now add all user specific fields
            for field in vertex_global_fields {
                vertex_projections.insert(field.to_string(), vec![field.to_string()]);
            }
            potential_vertex_projections = Some(vertex_projections);
        }
        potential_vertex_projections
    }

    pub fn produce_edge_projections(&self) -> Option<HashMap<String, Vec<String>>> {
        assert!(self.supports_projections.unwrap());
        let mut potential_edge_projections: Option<HashMap<String, Vec<String>>> = None;
        let edge_global_fields = self.get_all_edges_fields_to_fetch_as_list();

        // We can only make use of projections in case:
        // 1.) The user has not requested all vertex attributes
        // 2.) ArangoDB supports the dump endpoint, which is Version 3.12 or higher
        let client_wants_all_edge_attributes = self.load_config.load_all_edge_attributes;
        if !client_wants_all_edge_attributes {
            let mut edge_projections: HashMap<String, Vec<String>> = HashMap::new();

            // if edge_global_fields does not contain "_from" and "_to" we have to add it as it is required.
            // if this is not done, those fields will not be returned from the server's dump endpoint
            if !edge_global_fields.contains(&"_from".to_string()) {
                edge_projections.insert("_from".to_string(), vec!["_from".to_string()]);
            }
            if !edge_global_fields.contains(&"_to".to_string()) {
                edge_projections.insert("_to".to_string(), vec!["_to".to_string()]);
            }

            for field in edge_global_fields {
                edge_projections.insert(field.to_string(), vec![field.to_string()]);
            }
            potential_edge_projections = Some(edge_projections);
        }
        potential_edge_projections
    }
}

async fn get_graph_collections(
    db_config: &DatabaseConfiguration,
    graph_name: String,
) -> Result<(Vec<String>, Vec<String>), GraphLoaderError> {
    let param_url = format!("/_api/gharial/{}", graph_name);
    let url = make_url(db_config, &param_url);
    let graph_name = graph_name.clone();
    let (vertex_collections, edge_collections) =
        fetch_edge_and_vertex_collections_by_graph(db_config, url).await?;
    info!(
            "{:?} Got vertex collections: {:?}, edge collections: {:?} from graph definition for: {:?}.",
            SystemTime::now(),
            vertex_collections, edge_collections, graph_name
        );

    Ok((vertex_collections, edge_collections))
}

async fn fetch_edge_and_vertex_collections_by_graph(
    db_config: &DatabaseConfiguration,
    url: String,
) -> Result<(Vec<String>, Vec<String>), GraphLoaderError> {
    let mut edge_collection_names = vec![];
    let mut vertex_collection_names = vec![];

    let use_tls = db_config.endpoints[0].starts_with("https://");
    let client_config = ClientConfig::builder()
        .n_retries(5)
        .use_tls(use_tls)
        .tls_cert_opt(db_config.tls_cert.clone())
        .build();
    let client = build_client(&client_config)?;

    let resp = handle_auth(client.get(url), db_config).send().await;

    let parsed_response =
        handle_arangodb_response_with_parsed_body::<serde_json::Value>(resp, StatusCode::OK)
            .await?;
    let graph = parsed_response["graph"]
        .as_object()
        .ok_or(GraphLoaderError::GraphNotObject)?;
    let edge_definitions = graph
        .get("edgeDefinitions")
        .ok_or(GraphLoaderError::NoEdgeDefinitions)?
        .as_array()
        .ok_or(GraphLoaderError::EdgeDefinitionsNotArray)?;

    let mut non_unique_vertex_collection_names = vec![];
    for edge_definition in edge_definitions {
        let edge_collection_name = edge_definition["collection"]
            .as_str()
            .ok_or(GraphLoaderError::CollectionNotString)?;
        edge_collection_names.push(edge_collection_name.to_string());

        let from = edge_definition["from"]
            .as_array()
            .ok_or(GraphLoaderError::FromNotArray)?;
        for vertex in from {
            let vertex_collection_name = vertex
                .as_str()
                .ok_or(GraphLoaderError::FromCollectionNotString)?;
            non_unique_vertex_collection_names.push(vertex_collection_name.to_string());
        }

        let to = edge_definition["to"]
            .as_array()
            .ok_or(GraphLoaderError::ToNotArray)?;
        for vertex in to {
            let vertex_collection_name = vertex
                .as_str()
                .ok_or(GraphLoaderError::ToCollectionNotString)?;
            non_unique_vertex_collection_names.push(vertex_collection_name.to_string());
        }
    }

    non_unique_vertex_collection_names.sort();
    non_unique_vertex_collection_names.dedup();
    vertex_collection_names.append(&mut non_unique_vertex_collection_names);

    Ok((vertex_collection_names, edge_collection_names))
}