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
//! struct and enum pertain to arangoDB database
//!
//! AQL query are all executed in database level, so Database offers AQL query.
use std::{collections::HashMap, fmt::Debug, sync::Arc};
use uclient::ClientExt;

use log::trace;
use maybe_async::maybe_async;
use serde::{de::DeserializeOwned, Deserialize};
use serde_json::value::Value;
use url::Url;

use crate::graph::{GraphCollection, GraphResponse, GHARIAL_API_PATH};
use crate::index::INDEX_API_PATH;
use crate::transaction::TRANSACTION_HEADER;
use crate::{
    analyzer::{AnalyzerDescription, AnalyzerInfo},
    aql::{AqlQuery, Cursor},
    collection::{
        options::{CreateOptions, CreateParameters},
        response::{Info, Properties},
        Collection, CollectionType,
    },
    connection::Version,
    graph::Graph,
    index::{DeleteIndexResponse, Index, IndexCollection},
    response::{deserialize_response, ArangoResult},
    transaction::ArangoTransaction,
    transaction::Transaction,
    transaction::TransactionList,
    transaction::TransactionSettings,
    transaction::TransactionState,
    view::ArangoSearchViewProperties,
    view::ArangoSearchViewPropertiesOptions,
    view::ViewDescription,
    view::{View, ViewOptions},
    ClientError,
};

#[derive(Debug, Clone)]
pub struct Database<C: ClientExt> {
    name: String,
    base_url: Url,
    session: Arc<C>,
}

impl<'a, C: ClientExt> Database<C> {
    pub(crate) fn new<T: Into<String>>(name: T, arango_url: &Url, session: Arc<C>) -> Database<C> {
        let name = name.into();
        let path = format!("/_db/{}/", name.as_str());
        let url = arango_url.join(path.as_str()).unwrap();
        Database {
            name,
            session,
            base_url: url,
        }
    }

    /// Retrieve all collections of this database.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn accessible_collections(&self) -> Result<Vec<Info>, ClientError> {
        // an invalid arango_url should never running through initialization
        // so we assume arango_url is a valid url
        // When we pass an invalid path, it should panic to eliminate the bug
        // in development.
        let url = self.base_url.join("_api/collection").unwrap();
        trace!(
            "Retrieving collections from {:?}: {}",
            self.name,
            url.as_str()
        );
        let resp = self.session.get(url, "").await?;
        let result: ArangoResult<Vec<Info>> = deserialize_response(resp.body())?;
        trace!("Collections retrieved");
        Ok(result.unwrap())
    }

    pub fn url(&self) -> &Url {
        &self.base_url
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn session(&self) -> Arc<C> {
        Arc::clone(&self.session)
    }

    /// Get collection object with name.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn collection(&self, name: &str) -> Result<Collection<C>, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/collection/{}", name))
            .unwrap();
        let resp: Info = deserialize_response(self.session.get(url, "").await?.body())?;
        Ok(Collection::from_response(self, &resp))
    }

    /// Create a collection via HTTP request with options.
    ///
    /// Return a collection object if success.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_collection_with_options<'f>(
        &self,
        options: CreateOptions<'f>,
        parameters: CreateParameters,
    ) -> Result<Collection<C>, ClientError> {
        let mut url = self.base_url.join("_api/collection").unwrap();
        let query = serde_qs::to_string(&parameters).unwrap();
        url.set_query(Some(query.as_str()));

        let resp = self
            .session
            .post(url, &serde_json::to_string(&options)?)
            .await?;
        let result: Properties = deserialize_response(resp.body())?;
        self.collection(&result.info.name).await
    }

    /// Create a collection via HTTP request.
    ///
    /// Return a collection object if success.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_collection(&self, name: &str) -> Result<Collection<C>, ClientError> {
        self.create_collection_with_options(
            CreateOptions::builder().name(name).build(),
            Default::default(),
        )
        .await
    }

    #[maybe_async]
    pub async fn create_edge_collection(&self, name: &str) -> Result<Collection<C>, ClientError> {
        self.create_collection_with_options(
            CreateOptions::builder()
                .name(name)
                .collection_type(CollectionType::Edge)
                .build(),
            Default::default(),
        )
        .await
    }

    /// Drops a collection
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn drop_collection(&self, name: &str) -> Result<String, ClientError> {
        let url_path = format!("_api/collection/{}", name);
        let url = self.base_url.join(&url_path).unwrap();

        #[derive(Debug, Deserialize)]
        struct DropCollectionResponse {
            id: String,
        }

        let resp: DropCollectionResponse =
            deserialize_response(self.session.delete(url, "").await?.body())?;
        Ok(resp.id)
    }

    /// Get the version remote arango database server
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn arango_version(&self) -> Result<Version, ClientError> {
        let url = self.base_url.join("_api/version").unwrap();
        let resp = self.session.get(url, "").await?;
        let version: Version = serde_json::from_str(resp.body())?;
        Ok(version)
    }

    /// Get information of current database.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn info(&self) -> Result<DatabaseDetails, ClientError> {
        let url = self.base_url.join("_api/database/current").unwrap();
        let resp = self.session.get(url, "").await?;
        let res: ArangoResult<DatabaseDetails> = deserialize_response(resp.body())?;
        Ok(res.unwrap())
    }

    /// Execute aql query, return a cursor if succeed. The major advantage of
    /// batch query is that cursors contain more information and stats
    /// about the AQL query, and users can fetch results in batch to save memory
    /// resources on clients.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn aql_query_batch<R>(&self, aql: AqlQuery<'_>) -> Result<Cursor<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let url = self.base_url.join("_api/cursor").unwrap();
        let resp = self
            .session
            .post(url, &serde_json::to_string(&aql)?)
            .await?;
        deserialize_response(resp.body())
    }

    /// Get next batch given the cursor id.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn aql_next_batch<R>(&self, cursor_id: &str) -> Result<Cursor<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let url = self
            .base_url
            .join(&format!("_api/cursor/{}", cursor_id))
            .unwrap();
        let resp = self.session.put(url, "").await?;
        deserialize_response(resp.body())
    }

    #[maybe_async]
    async fn aql_fetch_all<R>(&self, response: Cursor<R>) -> Result<Vec<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let mut response_cursor = response;
        let mut results: Vec<R> = Vec::new();
        loop {
            results.extend(response_cursor.result.into_iter());
            if response_cursor.more {
                let id = response_cursor.id.unwrap().clone();
                response_cursor = self.aql_next_batch(id.as_str()).await?;
            } else {
                break;
            }
        }
        Ok(results)
    }

    /// Execute AQL query fetch all results.
    ///
    /// DO NOT do this when the count of results is too large that network or
    /// memory resources cannot afford.
    ///
    /// DO NOT set a small batch size, otherwise clients will have to make many
    /// HTTP requests.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn aql_query<R>(&self, aql: AqlQuery<'_>) -> Result<Vec<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let response = self.aql_query_batch(aql).await?;
        if response.more {
            self.aql_fetch_all(response).await
        } else {
            Ok(response.result)
        }
    }

    /// Similar to `aql_query`, except that this method only accept a string of
    /// AQL query.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn aql_str<R>(&self, query: &str) -> Result<Vec<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let aql = AqlQuery::builder().query(query).build();
        self.aql_query(aql).await
    }

    /// Similar to `aql_query`, except that this method only accept a string of
    /// AQL query, with additional bind vars.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn aql_bind_vars<R>(
        &self,
        query: &str,
        bind_vars: HashMap<&str, Value>,
    ) -> Result<Vec<R>, ClientError>
    where
        R: DeserializeOwned,
    {
        let aql = AqlQuery::builder()
            .query(query)
            .bind_vars(bind_vars)
            .build();
        self.aql_query(aql).await
    }

    /// Create a new index on a collection.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_index(
        &self,
        collection: &str,
        index: &Index,
    ) -> Result<Index, ClientError> {
        let mut url = self.base_url.join(INDEX_API_PATH).unwrap();
        url.set_query(Some(&format!("collection={}", collection)));

        let resp = self
            .session
            .post(url, &serde_json::to_string(&index)?)
            .await?;

        let result: Index = deserialize_response::<Index>(resp.body())?;

        Ok(result)
    }

    /// Retrieve an index by id
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn index(&self, id: &str) -> Result<Index, ClientError> {
        let url = self
            .base_url
            .join(&format!("{}/{}", INDEX_API_PATH, id))
            .unwrap();

        let resp = self.session.get(url, "").await?;

        let result: Index = deserialize_response::<Index>(resp.body())?;

        Ok(result)
    }

    /// Retrieve a list of indexes for a collection.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn indexes(&self, collection: &str) -> Result<IndexCollection, ClientError> {
        let mut url = self.base_url.join(INDEX_API_PATH).unwrap();
        url.set_query(Some(&format!("collection={}", collection)));

        let resp = self.session.get(url, "").await?;

        let result: IndexCollection = deserialize_response::<IndexCollection>(resp.body())?;

        Ok(result)
    }

    /// Delete an index by id.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn delete_index(&self, id: &str) -> Result<DeleteIndexResponse, ClientError> {
        let url = self
            .base_url
            .join(&format!("{}/{}", INDEX_API_PATH, id))
            .unwrap();
        let resp = self.session.delete(url, "").await?;

        let result: DeleteIndexResponse = deserialize_response::<DeleteIndexResponse>(resp.body())?;

        Ok(result)
    }

    /// Create a new graph in the graph module.
    ///
    /// # Arguments
    /// * `graph` - The graph object to create, its name must be unique.
    /// * `wait_for_sync` - define if the request should wait until everything is synced to disc.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_graph(
        &self,
        graph: Graph,
        wait_for_sync: bool,
    ) -> Result<Graph, ClientError> {
        let mut url = self.base_url.join(GHARIAL_API_PATH).unwrap();
        url.set_query(Some(&format!("waitForSync={}", wait_for_sync)));

        let resp = self
            .session
            .post(url, &serde_json::to_string(&graph)?)
            .await?;

        let result: GraphResponse = deserialize_response::<GraphResponse>(resp.body())?;

        Ok(result.graph)
    }

    /// Retrieve an graph by name
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn graph(&self, name: &str) -> Result<Graph, ClientError> {
        let url = self
            .base_url
            .join(&format!("{}/{}", GHARIAL_API_PATH, name))
            .unwrap();

        let resp = self.session.get(url, "").await?;

        let result: GraphResponse = deserialize_response::<GraphResponse>(resp.body())?;

        Ok(result.graph)
    }

    /// Retrieve the list of created graphs.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn graphs(&self) -> Result<GraphCollection, ClientError> {
        let url = self.base_url.join(GHARIAL_API_PATH).unwrap();

        let resp = self.session.get(url, "").await?;

        let result: GraphCollection = deserialize_response::<GraphCollection>(resp.body())?;

        Ok(result)
    }

    /// Drops an existing graph object by name. Optionally all collections not used by other graphs can be dropped as well.
    ///
    /// # Arguments
    /// * `name` - The name of the graph to drop
    /// * `drop_collections`- if set to `true`, drops collections of this graph as well.
    /// Collections will only be dropped if they are not used in other graphs.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn drop_graph(&self, name: &str, drop_collections: bool) -> Result<(), ClientError> {
        let mut url = self
            .base_url
            .join(&format!("{}/{}", GHARIAL_API_PATH, name))
            .unwrap();
        url.set_query(Some(&format!("dropCollections={}", drop_collections)));

        self.session.delete(url, "").await?;

        Ok(())
    }

    /// Return the currently running server-side transactions
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn list_transactions(&self) -> Result<Vec<TransactionState>, ClientError> {
        let url = self.base_url.join("_api/transaction").unwrap();

        let resp = self.session.get(url, "").await?;

        let result: TransactionList = deserialize_response(resp.body())?;
        Ok(result.transactions)
    }

    /// Begin a server-side transaction, the transaction settings should specify
    /// at least collections to be updated through the write list
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn begin_transaction(
        &self,
        transaction_settings: TransactionSettings,
    ) -> Result<Transaction<C>, ClientError> {
        let url = self.base_url.join("_api/transaction/begin").unwrap();

        let resp = self
            .session
            .post(url, &serde_json::to_string(&transaction_settings)?)
            .await?;

        let result: ArangoResult<ArangoTransaction> = deserialize_response(resp.body())?;
        let transaction = result.unwrap();
        let tx_id = transaction.id.clone();

        let mut session = (*self.session).clone();
        session
            .headers()
            .insert(TRANSACTION_HEADER, tx_id.parse().unwrap());

        Ok(Transaction::<C>::new(
            transaction,
            Arc::new(session),
            self.base_url.clone(),
        ))
    }

    /// Returns an object containing a listing of all Views in a database, regardless of their typ
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn list_views(&self) -> Result<Vec<ViewDescription>, ClientError> {
        let url = self.base_url.join("_api/view").unwrap();

        let resp = self.session.get(url, "").await?;

        let result: ArangoResult<Vec<ViewDescription>> = deserialize_response(resp.body())?;
        Ok(result.unwrap())
    }

    /// Creates an ArangoSearch View
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_view(&self, view_options: ViewOptions) -> Result<View, ClientError> {
        let url = self.base_url.join("_api/view").unwrap();

        let resp = self
            .session
            .post(url, &serde_json::to_string(&view_options)?)
            .await?;

        let result: View = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Return information about a View
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn view(&self, view_name: &str) -> Result<ViewDescription, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/view/{}", view_name))
            .unwrap();

        let resp = self.session.get(url, "").await?;

        let result: ViewDescription = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Read properties of a View
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn view_properties(
        &self,
        view_name: &str,
    ) -> Result<ArangoSearchViewProperties, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/view/{}/properties", view_name))
            .unwrap();

        let resp = self.session.get(url, "").await?;

        let result: ArangoSearchViewProperties = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Changes all the properties of an ArangoSearch
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn replace_view_properties(
        &self,
        view_name: &str,
        properties: ArangoSearchViewPropertiesOptions,
    ) -> Result<View, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/view/{}/properties", view_name))
            .unwrap();

        let resp = self
            .session
            .put(url, &serde_json::to_string(&properties)?)
            .await?;

        let result: View = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Partially changes properties of an ArangoSearch View
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn update_view_properties(
        &self,
        view_name: &str,
        properties: ArangoSearchViewPropertiesOptions,
    ) -> Result<View, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/view/{}/properties", view_name))
            .unwrap();

        let resp = self
            .session
            .patch(url, &serde_json::to_string(&properties)?)
            .await?;

        let result: View = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Drops the View identified by view-name.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn drop_view(&self, view_name: &str) -> Result<bool, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/view/{}", view_name))
            .unwrap();

        let resp = self.session.delete(url, "").await?;

        let result: ArangoResult<bool> = deserialize_response(resp.body())?;
        Ok(result.unwrap())
    }

    #[maybe_async]
    pub async fn list_analyzers(&self) -> Result<Vec<AnalyzerInfo>, ClientError> {
        let url = self.base_url.join("_api/analyzer").unwrap();

        let resp = self.session.get(url, "").await?;

        let result: ArangoResult<Vec<AnalyzerInfo>> = deserialize_response(resp.body())?;
        Ok(result.unwrap())
    }

    /// Create an Analyzer with the supplied definition
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_analyzer(
        &self,
        analyzer: AnalyzerInfo,
    ) -> Result<AnalyzerInfo, ClientError> {
        let url = self.base_url.join("_api/analyzer").unwrap();

        let resp = self
            .session
            .post(url, &serde_json::to_string(&analyzer)?)
            .await?;

        let result: AnalyzerInfo = deserialize_response(resp.body())?;
        Ok(result)
    }

    /// Return the Analyzer definition
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn analyzer(&self, analyzer_name: &str) -> Result<AnalyzerInfo, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/analyzer/{}", analyzer_name))
            .unwrap();

        let resp = self.session.get(url, "").await?;

        let result: AnalyzerInfo = deserialize_response(resp.body())?;
        Ok(result)
    }

    ///Removes an Analyzer configuration identified by analyzer_name.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn drop_analyzer(
        &self,
        analyzer_name: &str,
    ) -> Result<AnalyzerDescription, ClientError> {
        let url = self
            .base_url
            .join(&format!("_api/analyzer/{}", analyzer_name))
            .unwrap();

        let resp = self.session.delete(url, "").await?;

        let result: AnalyzerDescription = deserialize_response(resp.body())?;
        Ok(result)
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DatabaseDetails {
    pub name: String,
    pub id: String,
    pub path: String,
    pub is_system: bool,
}