Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 2573dbb

Browse files
committed
refactor(op): Better naming + removed collection init
- added `Op.invoke(Monop)` and `Op.enqueue(Monop)` - added `Operation.enqueue(Monop)` - added `Monop.get(String)` BREAKING-CHANGE: - renamed `Monop.invoke(...)` to `Monop.enqueue(...)` - removed `MonopCollection.init(...)` and `MonopCollection.initOnce(...)` - renamed `Op.invoke()` to `Op.createOperation()` - Bultin Ops and Operations `collection` field type changed from `MonopCollection` to `String`
1 parent a630de0 commit 2573dbb

7 files changed

Lines changed: 122 additions & 142 deletions

File tree

op/src/main/kotlin/Monop.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ interface Monop {
6868
*
6969
* @since 2.0.0
7070
*/
71-
operator fun invoke(operations: List<Operation<*>>)
71+
fun enqueue(operations: List<Operation<*>>)
7272

7373
/**
7474
* The global monop instance.
@@ -156,8 +156,8 @@ operator fun Monop.minusAssign(operator: Operator) {
156156
*
157157
* @since 2.0.0
158158
*/
159-
operator fun Monop.invoke(vararg operations: Operation<*>) {
160-
this(operations.asList())
159+
fun Monop.enqueue(vararg operations: Operation<*>) {
160+
this.enqueue(operations.asList())
161161
}
162162

163163
/**
@@ -170,4 +170,14 @@ operator fun Monop.get(collection: MonopCollection): MongoCollection {
170170
return database[collection.name]
171171
}
172172

173+
/**
174+
* A shortcut to get a [MongoCollection] instance
175+
* from a monop instance.
176+
*
177+
* @since 2.0.0
178+
*/
179+
operator fun Monop.get(collection: String): MongoCollection {
180+
return database[collection]
181+
}
182+
173183
/* ============= ------------------ ============= */

op/src/main/kotlin/MonopCollection.kt

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,20 @@ package org.cufy.monop
1717

1818
import org.cufy.bson.*
1919
import org.cufy.mongodb.*
20-
import java.util.*
2120

2221
/* ============= ------------------ ============= */
2322

2423
/**
2524
* A convenient class that holds bare minimal
2625
* data needed for using some collection.
2726
*
28-
* The important parts of this class are [name]
29-
* which is the name of the collection and [init]
30-
* which is a function to be invoked when the
31-
* collection is about to be used.
32-
*
3327
* @author LSafer
3428
* @since 2.0.0
3529
*/
3630
open class MonopCollection(val name: String) {
37-
private var dejaVu = Collections.newSetFromMap<Monop>(WeakHashMap())
38-
3931
override fun toString(): String {
4032
return "MonopCollection($name)"
4133
}
42-
43-
/**
44-
* Called only once per [monop] instance
45-
* and only before this instance being used.
46-
*
47-
* Call [initOnce] instead.
48-
*/
49-
protected open suspend fun init(monop: Monop) {}
50-
51-
/**
52-
* If not initialized, initialize this
53-
* instance with the given [monop].
54-
*
55-
* @since 2.0.0
56-
*/
57-
suspend fun initOnce(monop: Monop) {
58-
if (!dejaVu.add(monop))
59-
return
60-
61-
init(monop)
62-
}
6334
}
6435

6536
/* ============= ------------------ ============= */
@@ -77,7 +48,7 @@ fun MonopCollection.deleteOne(
7748
filter: BsonDocument,
7849
options: DeleteOptions = DeleteOptions()
7950
): DeleteOneOp {
80-
return DeleteOneOp(this, filter, options)
51+
return DeleteOneOp(name, filter, options)
8152
}
8253

8354
/**
@@ -143,7 +114,7 @@ fun MonopCollection.deleteOneById(
143114
fun MonopCollection.deleteMany(
144115
filter: BsonDocument,
145116
options: DeleteOptions = DeleteOptions()
146-
) = DeleteManyOp(this, filter, options)
117+
) = DeleteManyOp(name, filter, options)
147118

148119
/**
149120
* Create a [DeleteManyOp] with the given arguments.
@@ -174,7 +145,7 @@ fun MonopCollection.insertOne(
174145
document: BsonDocument,
175146
options: InsertOneOptions = InsertOneOptions()
176147
): InsertOneOp {
177-
return InsertOneOp(this, document, options)
148+
return InsertOneOp(name, document, options)
178149
}
179150

180151
/**
@@ -206,7 +177,7 @@ fun MonopCollection.insertMany(
206177
documents: List<BsonDocument>,
207178
options: InsertManyOptions = InsertManyOptions()
208179
): InsertManyOp {
209-
return InsertManyOp(this, documents, options)
180+
return InsertManyOp(name, documents, options)
210181
}
211182

212183
/**
@@ -240,7 +211,7 @@ fun MonopCollection.updateOne(
240211
update: BsonDocument,
241212
options: UpdateOptions = UpdateOptions()
242213
): UpdateOneOp {
243-
return UpdateOneOp(this, filter, update, options)
214+
return UpdateOneOp(name, filter, update, options)
244215
}
245216

246217
/**
@@ -276,7 +247,7 @@ fun MonopCollection.updateOne(
276247
update: List<BsonDocument>,
277248
options: UpdateOptions = UpdateOptions()
278249
): UpdateOneOp {
279-
return UpdateOneOp(this, filter, update.toBsonArray(), options)
250+
return UpdateOneOp(name, filter, update.toBsonArray(), options)
280251
}
281252

282253
/**
@@ -392,7 +363,7 @@ fun MonopCollection.updateMany(
392363
update: BsonDocument,
393364
options: UpdateOptions = UpdateOptions()
394365
): UpdateManyOp {
395-
return UpdateManyOp(this, filter, update, options)
366+
return UpdateManyOp(name, filter, update, options)
396367
}
397368

398369
/**
@@ -428,7 +399,7 @@ fun MonopCollection.updateMany(
428399
update: List<BsonDocument>,
429400
options: UpdateOptions = UpdateOptions()
430401
): UpdateManyOp {
431-
return UpdateManyOp(this, filter, update.toBsonArray(), options)
402+
return UpdateManyOp(name, filter, update.toBsonArray(), options)
432403
}
433404

434405
/**
@@ -464,7 +435,7 @@ fun MonopCollection.replaceOne(
464435
replacement: BsonDocument,
465436
options: ReplaceOptions = ReplaceOptions()
466437
): ReplaceOneOp {
467-
return ReplaceOneOp(this, filter, replacement, options)
438+
return ReplaceOneOp(name, filter, replacement, options)
468439
}
469440

470441
/**
@@ -538,7 +509,7 @@ fun MonopCollection.bulkWrite(
538509
requests: List<WriteModel>,
539510
options: BulkWriteOptions = BulkWriteOptions()
540511
): BulkWriteOp {
541-
return BulkWriteOp(this, requests, options)
512+
return BulkWriteOp(name, requests, options)
542513
}
543514

544515
/**
@@ -570,7 +541,7 @@ fun MonopCollection.count(
570541
filter: BsonDocument = EmptyBsonDocument,
571542
options: CountOptions = CountOptions()
572543
): CountOp {
573-
return CountOp(this, filter, options)
544+
return CountOp(name, filter, options)
574545
}
575546

576547
/**
@@ -600,7 +571,7 @@ fun MonopCollection.count(
600571
fun MonopCollection.estimatedCount(
601572
options: EstimatedCountOptions = EstimatedCountOptions()
602573
): EstimatedCountOp {
603-
return EstimatedCountOp(this, options)
574+
return EstimatedCountOp(name, options)
604575
}
605576

606577
/**
@@ -630,7 +601,7 @@ fun MonopCollection.findOneAndDelete(
630601
filter: BsonDocument,
631602
options: FindOneAndDeleteOptions = FindOneAndDeleteOptions()
632603
): FindOneAndDeleteOp {
633-
return FindOneAndDeleteOp(this, filter, options)
604+
return FindOneAndDeleteOp(name, filter, options)
634605
}
635606

636607
/**
@@ -699,7 +670,7 @@ fun MonopCollection.findOneAndReplace(
699670
replacement: BsonDocument,
700671
options: FindOneAndReplaceOptions = FindOneAndReplaceOptions()
701672
): FindOneAndReplaceOp {
702-
return FindOneAndReplaceOp(this, filter, replacement, options)
673+
return FindOneAndReplaceOp(name, filter, replacement, options)
703674
}
704675

705676
/**
@@ -775,7 +746,7 @@ fun MonopCollection.findOneAndUpdate(
775746
update: BsonDocument,
776747
options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()
777748
): FindOneAndUpdateOp {
778-
return FindOneAndUpdateOp(this, filter, update, options)
749+
return FindOneAndUpdateOp(name, filter, update, options)
779750
}
780751

781752
/**
@@ -811,7 +782,7 @@ fun MonopCollection.findOneAndUpdate(
811782
update: List<BsonDocument>,
812783
options: FindOneAndUpdateOptions = FindOneAndUpdateOptions()
813784
): FindOneAndUpdateOp {
814-
return FindOneAndUpdateOp(this, filter, update.toBsonArray(), options)
785+
return FindOneAndUpdateOp(name, filter, update.toBsonArray(), options)
815786
}
816787

817788
/**
@@ -925,7 +896,7 @@ fun MonopCollection.find(
925896
filter: BsonDocument = EmptyBsonDocument,
926897
options: FindOptions = FindOptions()
927898
): FindOp {
928-
return FindOp(this, filter, options)
899+
return FindOp(name, filter, options)
929900
}
930901

931902
/**
@@ -1027,7 +998,7 @@ fun MonopCollection.aggregate(
1027998
pipeline: List<BsonDocument>,
1028999
options: AggregateOptions = AggregateOptions()
10291000
): AggregateOp {
1030-
return AggregateOp(this, pipeline, options)
1001+
return AggregateOp(name, pipeline, options)
10311002
}
10321003

10331004
/**
@@ -1061,7 +1032,7 @@ fun MonopCollection.distinct(
10611032
filter: BsonDocument = EmptyBsonDocument,
10621033
options: DistinctOptions = DistinctOptions()
10631034
): DistinctOp {
1064-
return DistinctOp(this, field, filter, options)
1035+
return DistinctOp(name, field, filter, options)
10651036
}
10661037

10671038
/**

0 commit comments

Comments
 (0)