@@ -3,58 +3,70 @@ package com.github.lamba92.dragalialost.core.datasource
3
3
import com.github.lamba92.dragalialost.data.datasource.GamepediaDatasourceCache
4
4
import com.github.lamba92.dragalialost.data.datasource.queries.*
5
5
import com.github.lamba92.dragalialost.data.rawresponses.*
6
+ import com.mongodb.MongoWriteException
6
7
import kotlinx.coroutines.flow.asFlow
7
8
import kotlinx.coroutines.flow.collect
8
9
import kotlinx.coroutines.flow.onEach
9
10
import kotlinx.coroutines.runBlocking
11
+ import kotlinx.serialization.Serializable
12
+ import org.litote.kmongo.coroutine.CoroutineClient
13
+ import org.litote.kmongo.coroutine.CoroutineCollection
10
14
import org.litote.kmongo.coroutine.CoroutineDatabase
11
15
12
- class MongoDBGamepediaCache private constructor(db : CoroutineDatabase ) : GamepediaDatasourceCache {
16
+ class MongoDBGamepediaCache private constructor(
17
+ private val client : CoroutineClient ,
18
+ db : CoroutineDatabase
19
+ ) : GamepediaDatasourceCache {
13
20
14
21
companion object {
15
22
16
- private suspend fun createCollections (db : CoroutineDatabase ) {
17
- db.createCollection(" adventurers" )
18
- db.createCollection(" dragons" )
19
- db.createCollection(" wyrmprints" )
20
- db.createCollection(" weapons" )
21
- db.createCollection(" abilities" )
22
- db.createCollection(" coAbilities" )
23
- db.createCollection(" skillsById" )
24
- db.createCollection(" skillsByName" )
25
- db.createCollection(" abilityLimitedGroup" )
26
- db.createCollection(" abilityGroup" )
27
- db.createCollection(" adventurerIcons" )
28
- db.createCollection(" adventurerPortraits" )
29
- db.createCollection(" dragonIcons" )
30
- db.createCollection(" dragonPortraits" )
31
- db.createCollection(" wyrmprintIcons" )
32
- db.createCollection(" wyrmprintPortraits" )
33
- db.createCollection(" abilityIcons" )
34
- db.createCollection(" coAbilityIcons" )
35
- db.createCollection(" skillIcons" )
36
- }
23
+ private suspend fun createCollections (db : CoroutineDatabase ) = listOf (
24
+ " adventurers" , " dragons" , " wyrmprints" , " weapons" , " abilities" ,
25
+ " coAbilities" , " skillsById" , " skillsByName" , " abilityLimitedGroup" , " abilityGroup" , " adventurerIcons" ,
26
+ " adventurerPortraits" , " dragonIcons" , " dragonPortraits" , " wyrmprintIcons" , " wyrmprintPortraits" ,
27
+ " abilityIcons" , " coAbilityIcons" , " skillIcons"
28
+ ).filter { it !in db.listCollectionNames() }
29
+ .forEach { db.createCollection(it) }
37
30
38
- suspend fun initialize (db : CoroutineDatabase ): MongoDBGamepediaCache {
31
+ suspend fun initialize (db : CoroutineDatabase , client : CoroutineClient ): MongoDBGamepediaCache {
39
32
createCollections(db)
40
- return MongoDBGamepediaCache (db)
33
+ return MongoDBGamepediaCache (client, db)
41
34
}
42
35
43
- fun initializeBlocking (db : CoroutineDatabase ) =
36
+ fun initializeBlocking (db : CoroutineDatabase , client : CoroutineClient ) =
44
37
runBlocking {
45
- initialize(db)
38
+ initialize(db, client )
46
39
}
47
40
}
48
41
42
+ @Serializable
49
43
private data class AdventurerDocument (val _id : String , val data : AdventurerJSON )
44
+
45
+ @Serializable
50
46
private data class DragonDocument (val _id : String , val data : DragonJSON )
47
+
48
+ @Serializable
51
49
private data class WyrmprintDocument (val _id : String , val data : WyrmprintJSON )
50
+
51
+ @Serializable
52
52
private data class WeaponDocument (val _id : String , val data : WeaponJSON )
53
+
54
+ @Serializable
53
55
private data class AbilityDocument (val _id : String , val data : AbilityJSON )
56
+
57
+ @Serializable
54
58
private data class CoAbilityDocument (val _id : String , val data : CoAbilityJSON )
59
+
60
+ @Serializable
55
61
private data class SkillDocument (val _id : String , val data : SkillJSON )
62
+
63
+ @Serializable
56
64
private data class AbilityLimitedGroupDocument (val _id : String , val data : AbilityLimitedGroupJSON )
65
+
66
+ @Serializable
57
67
private data class AbilityGroupDocument (val _id : String , val data : AbilityGroupJSON )
68
+
69
+ @Serializable
58
70
private data class ImageInfoDocument (val _id : String , val data : ImageInfoJSON )
59
71
60
72
private val adventurersCollection =
@@ -155,10 +167,10 @@ class MongoDBGamepediaCache private constructor(db: CoroutineDatabase) : Gameped
155
167
abilityGroupsCollection.findOneById(id)?.data
156
168
157
169
override suspend fun getAdventurerIconById (id : String , variationId : String , rarity : Int ) =
158
- adventurerIconsCollection.findOne (" ${id} _${variationId} _$rarity " )?.data
170
+ adventurerIconsCollection.findOneById (" ${id} _${variationId} _$rarity " )?.data
159
171
160
172
override suspend fun getAdventurerPortraitById (id : String , variationId : String , rarity : Int ) =
161
- adventurerPortraitsCollection.findOne (" ${id} _${variationId} _$rarity " )?.data
173
+ adventurerPortraitsCollection.findOneById (" ${id} _${variationId} _$rarity " )?.data
162
174
163
175
override suspend fun getDragonIconById (id : String ) =
164
176
dragonIconsCollection.findOneById(id)?.data
@@ -221,53 +233,69 @@ class MongoDBGamepediaCache private constructor(db: CoroutineDatabase) : Gameped
221
233
) =
222
234
true
223
235
236
+ // private suspend fun <T> CoroutineClient.transaction(action: suspend (ClientSession) -> T) =
237
+ // startSession().let {
238
+ // it.startTransaction()
239
+ // val r = action(it)
240
+ // it.commitTransactionAndAwait()
241
+ // r
242
+ // }
243
+
244
+ private suspend fun <T : Any > CoroutineCollection<T>.insertOrUpdate (id : Any , document : T ) {
245
+ try {
246
+ insertOne(document)
247
+ } catch (e: MongoWriteException ) {
248
+ updateOneById(id, document)
249
+ }
250
+ }
251
+
224
252
override suspend fun cacheAbilityGroupsByGroupId (groupId : String , data : AbilityGroupJSON ): Boolean {
225
- abilityGroupsCollection.updateOneById (groupId, AbilityGroupDocument (groupId, data))
253
+ abilityGroupsCollection.insertOrUpdate (groupId, AbilityGroupDocument (groupId, data))
226
254
return true
227
255
}
228
256
229
257
override suspend fun cacheAdventurerByIds (id : String , variationId : String , data : AdventurerJSON ): Boolean {
230
- adventurersCollection.updateOneById (" ${id} _$variationId " , AdventurerDocument (" ${id} _$variationId " , data))
258
+ adventurersCollection.insertOrUpdate (" ${id} _$variationId " , AdventurerDocument (" ${id} _$variationId " , data))
231
259
return true
232
260
}
233
261
234
262
override suspend fun cacheDragonById (id : String , data : DragonJSON ): Boolean {
235
- dragonsCollection.updateOneById (id, DragonDocument (id, data))
263
+ dragonsCollection.insertOrUpdate (id, DragonDocument (id, data))
236
264
return true
237
265
}
238
266
239
267
override suspend fun cacheWyrmprintById (id : String , data : WyrmprintJSON ): Boolean {
240
- wyrmprintsCollection.updateOneById (id, WyrmprintDocument (id, data))
268
+ wyrmprintsCollection.insertOrUpdate (id, WyrmprintDocument (id, data))
241
269
return true
242
270
}
243
271
244
272
override suspend fun cacheWeaponById (id : String , data : WeaponJSON ): Boolean {
245
- weaponsCollection.updateOneById (id, WeaponDocument (id, data))
273
+ weaponsCollection.insertOrUpdate (id, WeaponDocument (id, data))
246
274
return true
247
275
}
248
276
249
277
override suspend fun cacheAbilityById (id : String , data : AbilityJSON ): Boolean {
250
- abilitiesCollection.updateOneById (id, AbilityDocument (id, data))
278
+ abilitiesCollection.insertOrUpdate (id, AbilityDocument (id, data))
251
279
return true
252
280
}
253
281
254
282
override suspend fun cacheCoAbilityById (id : String , data : CoAbilityJSON ): Boolean {
255
- coAbilitiesCollection.updateOneById (id, CoAbilityDocument (id, data))
283
+ coAbilitiesCollection.insertOrUpdate (id, CoAbilityDocument (id, data))
256
284
return true
257
285
}
258
286
259
287
override suspend fun cacheSkillById (id : String , data : SkillJSON ): Boolean {
260
- skillsByIdCollection.updateOneById (id, SkillDocument (id, data))
288
+ skillsByIdCollection.insertOrUpdate (id, SkillDocument (id, data))
261
289
return true
262
290
}
263
291
264
292
override suspend fun cacheSkillByName (name : String , data : SkillJSON ): Boolean {
265
- skillsByNameCollection.updateOneById (name, SkillDocument (name, data))
293
+ skillsByNameCollection.insertOrUpdate (name, SkillDocument (name, data))
266
294
return true
267
295
}
268
296
269
297
override suspend fun cacheAbilityLimitedGroupById (id : String , data : AbilityLimitedGroupJSON ): Boolean {
270
- abilityLimitedGroupsCollection.updateOneById (id, AbilityLimitedGroupDocument (id, data))
298
+ abilityLimitedGroupsCollection.insertOrUpdate (id, AbilityLimitedGroupDocument (id, data))
271
299
return true
272
300
}
273
301
@@ -298,37 +326,37 @@ class MongoDBGamepediaCache private constructor(db: CoroutineDatabase) : Gameped
298
326
}
299
327
300
328
override suspend fun cacheDragonIconById (id : String , data : ImageInfoJSON ): Boolean {
301
- dragonIconsCollection.updateOneById (id, ImageInfoDocument (id, data))
329
+ dragonIconsCollection.insertOrUpdate (id, ImageInfoDocument (id, data))
302
330
return true
303
331
}
304
332
305
333
override suspend fun cacheDragonPortraitById (id : String , data : ImageInfoJSON ): Boolean {
306
- dragonPortraitsCollection.updateOneById (id, ImageInfoDocument (id, data))
334
+ dragonPortraitsCollection.insertOrUpdate (id, ImageInfoDocument (id, data))
307
335
return true
308
336
}
309
337
310
338
override suspend fun cacheWyrmprintIconByIds (id : String , vestige : Int , data : ImageInfoJSON ): Boolean {
311
- wyrmprintIconsCollection.updateOneById (" ${id} _$vestige " , ImageInfoDocument (" ${id} _$vestige " , data))
339
+ wyrmprintIconsCollection.insertOrUpdate (" ${id} _$vestige " , ImageInfoDocument (" ${id} _$vestige " , data))
312
340
return true
313
341
}
314
342
315
343
override suspend fun cacheWyrmprintPortraitByIds (id : String , vestige : Int , data : ImageInfoJSON ): Boolean {
316
- wyrmprintPortraitsCollection.updateOneById (" ${id} _$vestige " , ImageInfoDocument (" ${id} _$vestige " , data))
344
+ wyrmprintPortraitsCollection.insertOrUpdate (" ${id} _$vestige " , ImageInfoDocument (" ${id} _$vestige " , data))
317
345
return true
318
346
}
319
347
320
348
override suspend fun cacheAbilityIconByFileName (fileName : String , data : ImageInfoJSON ): Boolean {
321
- abilityIconsCollection.updateOneById (fileName, ImageInfoDocument (fileName, data))
349
+ abilityIconsCollection.insertOrUpdate (fileName, ImageInfoDocument (fileName, data))
322
350
return true
323
351
}
324
352
325
353
override suspend fun cacheCoAbilityIconByFileName (fileName : String , data : ImageInfoJSON ): Boolean {
326
- coAbilityIconsCollection.updateOneById (fileName, ImageInfoDocument (fileName, data))
354
+ coAbilityIconsCollection.insertOrUpdate (fileName, ImageInfoDocument (fileName, data))
327
355
return true
328
356
}
329
357
330
358
override suspend fun cacheSkillIconByIconName (fileName : String , data : ImageInfoJSON ): Boolean {
331
- skillIconsCollection.updateOneById (fileName, ImageInfoDocument (fileName, data))
359
+ skillIconsCollection.insertOrUpdate (fileName, ImageInfoDocument (fileName, data))
332
360
return true
333
361
}
334
362
0 commit comments