Skip to content

Commit b1801a9

Browse files
committed
[models, types] Change collections to not require static instances
1 parent fbe5482 commit b1801a9

File tree

4 files changed

+51
-34
lines changed

4 files changed

+51
-34
lines changed

arangodb-models/src/model_builders/build_db.rs

-4
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,6 @@ fn build_db_document_impl(
688688
&self.db_rev
689689
}
690690

691-
fn collection() -> Arc<Self::Collection> {
692-
Self::Collection::instance()
693-
}
694-
695691
#is_all_missing_method_tokens
696692

697693
// SETTERS --------------------------------------------------------

arangodb-types/src/traits/collection.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ pub trait DBCollection: Send + Sync {
4545
&self.db_info().database
4646
}
4747

48-
/// Returns the instance associated with the collection.
49-
fn instance() -> Arc<Self>;
50-
5148
// METHODS ----------------------------------------------------------------
5249

5350
/// Checks whether a document exists in the DB by its key.
@@ -393,7 +390,7 @@ pub trait DBCollection: Send + Sync {
393390
Ok(())
394391
}
395392

396-
/// Drops the collection.
393+
/// Drops the colle ction.
397394
async fn drop_collection(&self) -> Result<(), Box<dyn Error>> {
398395
let db_info = self.db_collection().await?;
399396
db_info.drop().await?;

arangodb-types/src/traits/document.rs

+36-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::error::Error;
22
use std::hash::Hash;
3-
use std::sync::Arc;
43

54
use arangors::document::options::{InsertOptions, OverwriteMode, RemoveOptions, UpdateOptions};
65
use arangors::document::response::DocumentResponse;
@@ -37,8 +36,6 @@ pub trait DBDocument:
3736

3837
fn db_rev(&self) -> &Option<ArcStr>;
3938

40-
fn collection() -> Arc<Self::Collection>;
41-
4239
/// Whether all the fields are missing or not.
4340
fn is_all_missing(&self) -> bool;
4441

@@ -60,8 +57,11 @@ pub trait DBDocument:
6057
/// Inserts a new document.
6158
///
6259
/// WARN: returns the whole document.
63-
async fn insert(mut self, overwrite: bool) -> Result<Self, Box<dyn Error>> {
64-
let collection = Self::collection();
60+
async fn insert(
61+
mut self,
62+
overwrite: bool,
63+
collection: &Self::Collection,
64+
) -> Result<Self, Box<dyn Error>> {
6565
let db_collection = collection.db_collection().await?;
6666

6767
loop {
@@ -91,8 +91,11 @@ pub trait DBDocument:
9191
}
9292

9393
/// Inserts a new document ignoring the result.
94-
async fn insert_and_ignore(mut self, overwrite: bool) -> Result<Self::Key, Box<dyn Error>> {
95-
let collection = Self::collection();
94+
async fn insert_and_ignore(
95+
mut self,
96+
overwrite: bool,
97+
collection: &Self::Collection,
98+
) -> Result<Self::Key, Box<dyn Error>> {
9699
let db_collection = collection.db_collection().await?;
97100

98101
loop {
@@ -121,8 +124,11 @@ pub trait DBDocument:
121124
/// Updates the element and returns its updated value.
122125
///
123126
/// WARN: returns the whole document.
124-
async fn update(&self, merge_objects: bool) -> Result<Self, Box<dyn Error>> {
125-
let collection = Self::collection();
127+
async fn update(
128+
&self,
129+
merge_objects: bool,
130+
collection: &Self::Collection,
131+
) -> Result<Self, Box<dyn Error>> {
126132
let db_collection = collection.db_collection().await?;
127133

128134
let ignore_rev = self.db_rev().is_none();
@@ -165,8 +171,11 @@ pub trait DBDocument:
165171
}
166172

167173
/// Updates the element ignoring the result.
168-
async fn update_and_ignore(&self, merge_objects: bool) -> Result<(), Box<dyn Error>> {
169-
let collection = Self::collection();
174+
async fn update_and_ignore(
175+
&self,
176+
merge_objects: bool,
177+
collection: &Self::Collection,
178+
) -> Result<(), Box<dyn Error>> {
170179
let db_collection = collection.db_collection().await?;
171180

172181
let ignore_rev = self.db_rev().is_none();
@@ -208,8 +217,11 @@ pub trait DBDocument:
208217
/// Inserts a new document or updates it if it already exists.
209218
///
210219
/// WARN: returns the whole document.
211-
async fn insert_or_update(mut self, merge_objects: bool) -> Result<Self, Box<dyn Error>> {
212-
let collection = Self::collection();
220+
async fn insert_or_update(
221+
mut self,
222+
merge_objects: bool,
223+
collection: &Self::Collection,
224+
) -> Result<Self, Box<dyn Error>> {
213225
let db_collection = collection.db_collection().await?;
214226

215227
loop {
@@ -243,8 +255,8 @@ pub trait DBDocument:
243255
async fn insert_or_update_and_ignore(
244256
mut self,
245257
merge_objects: bool,
258+
collection: &Self::Collection,
246259
) -> Result<Self::Key, Box<dyn Error>> {
247-
let collection = Self::collection();
248260
let db_collection = collection.db_collection().await?;
249261

250262
loop {
@@ -273,8 +285,11 @@ pub trait DBDocument:
273285
}
274286

275287
/// Removes the element returning the old value.
276-
async fn remove(&self, rev: Option<ArcStr>) -> Result<Self, Box<dyn Error>> {
277-
let collection = Self::collection();
288+
async fn remove(
289+
&self,
290+
rev: Option<ArcStr>,
291+
collection: &Self::Collection,
292+
) -> Result<Self, Box<dyn Error>> {
278293
let db_collection = collection.db_collection().await?;
279294

280295
let key = self
@@ -314,8 +329,11 @@ pub trait DBDocument:
314329
}
315330

316331
/// Removes the element ignoring the result.
317-
async fn remove_and_ignore(&self, rev: Option<ArcStr>) -> Result<(), Box<dyn Error>> {
318-
let collection = Self::collection();
332+
async fn remove_and_ignore(
333+
&self,
334+
rev: Option<ArcStr>,
335+
collection: &Self::Collection,
336+
) -> Result<(), Box<dyn Error>> {
319337
let db_collection = collection.db_collection().await?;
320338

321339
let key = self

arangodb-types/src/utilities/db_mutex/mod.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct BDMutexGuardInner<T: 'static + DBSynchronizedDocument<'static>> {
3434
elements: HashSet<T::Key>,
3535
change_flag: DBUuid,
3636
alive_job: Option<JoinHandle<()>>,
37+
collection: Arc<T::Collection>,
3738
}
3839

3940
impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
@@ -45,6 +46,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
4546
node_id: &ArcStr,
4647
fields: Option<&T>,
4748
timeout: Option<u64>,
49+
collection: &Arc<T::Collection>,
4850
) -> Result<(T, BDMutexGuard<T>), DBMutexError> {
4951
let time_out = timeout.map(|v| DBDateTime::now().after_seconds(v));
5052
let mut checked_doc_exists = false;
@@ -56,7 +58,8 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
5658
}
5759

5860
// Prepare filter.
59-
let (mut list, mutex) = Self::acquire_list(&[key.clone()], node_id, fields).await?;
61+
let (mut list, mutex) =
62+
Self::acquire_list(&[key.clone()], node_id, fields, collection).await?;
6063

6164
let value = list.pop().unwrap();
6265

@@ -67,7 +70,6 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
6770
// Check the document exists and exit if not.
6871
// This prevents waiting until timeout when the document
6972
// is not present in the DB.
70-
let collection = T::collection();
7173
let exists_in_db = collection.exists_by_key(key).await?;
7274

7375
if !exists_in_db {
@@ -94,6 +96,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
9496
keys: &[T::Key],
9597
node_id: &ArcStr,
9698
fields: Option<&T>,
99+
collection: &Arc<T::Collection>,
97100
) -> Result<(Vec<Option<T>>, BDMutexGuard<T>), Box<dyn Error>> {
98101
// Shortcut for empty sets.
99102
if keys.is_empty() {
@@ -105,12 +108,12 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
105108
elements: HashSet::new(),
106109
change_flag: DBUuid::new(),
107110
alive_job: Some(tokio::spawn(async {})),
111+
collection: collection.clone(),
108112
})),
109113
},
110114
));
111115
}
112116

113-
let collection = T::collection();
114117
let collection_name = T::Collection::name();
115118
let mutex_path = DBDocumentField::Mutex.path();
116119

@@ -183,6 +186,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
183186
elements: result_ids,
184187
change_flag,
185188
alive_job: None,
189+
collection: collection.clone(),
186190
})),
187191
};
188192

@@ -224,8 +228,8 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
224228
limits: Option<AqlLimit>,
225229
node_id: &ArcStr,
226230
fields: Option<&T>,
231+
collection: &Arc<T::Collection>,
227232
) -> Result<(Vec<T>, BDMutexGuard<T>), Box<dyn Error>> {
228-
let collection = T::collection();
229233
let collection_name = T::Collection::name();
230234
let mutex_path = DBDocumentField::Mutex.path();
231235

@@ -303,6 +307,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
303307
elements: result_ids,
304308
change_flag,
305309
alive_job: None,
310+
collection: collection.clone(),
306311
})),
307312
};
308313

@@ -380,6 +385,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
380385
elements: new_elements,
381386
change_flag: DBUuid::new(),
382387
alive_job: Some(tokio::spawn(async {})),
388+
collection: lock.collection.clone(),
383389
})),
384390
}
385391
} else {
@@ -389,6 +395,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
389395
elements: new_elements,
390396
change_flag: lock.change_flag.clone(),
391397
alive_job: None,
398+
collection: lock.collection.clone(),
392399
})),
393400
};
394401

@@ -427,7 +434,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
427434
return;
428435
}
429436

430-
let collection = T::collection();
437+
let collection = &lock.collection;
431438
let node_id = &lock.node_id;
432439
let now = DBDateTime::now();
433440
let expiration = now.after_seconds(MUTEX_EXPIRATION);
@@ -519,7 +526,7 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
519526
return;
520527
}
521528

522-
let collection = T::collection();
529+
let collection = &lock.collection;
523530
let node_id = &lock.node_id;
524531
let keys = &lock.elements;
525532

@@ -590,12 +597,11 @@ impl<T: 'static + DBSynchronizedDocument<'static>> BDMutexGuard<T> {
590597
}
591598
}
592599

593-
pub async fn release_all_mutexes(node_id: &str) {
600+
pub async fn release_all_mutexes(node_id: &str, collection: &Arc<T::Collection>) {
594601
// FOR i IN <collection>
595602
// FILTER i.<mutex.node> == <node>
596603
// UPDATE i WITH { <mutex>: null } IN <collection> OPTIONS { mergeObjects: true, keepNulls: false, ignoreErrors: true }
597604
let mutex_path = DBDocumentField::Mutex.path();
598-
let collection = T::collection();
599605
let collection_name = T::Collection::name();
600606
let mut aql = AqlBuilder::new_for_in_collection(AQL_DOCUMENT_ID, collection_name);
601607
aql.filter_step(

0 commit comments

Comments
 (0)