Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Make purge collection reactive
Browse files Browse the repository at this point in the history
  • Loading branch information
trema96 committed Jul 13, 2022
1 parent e41a6f9 commit e1a69ec
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/org/taktik/icure/asyncdao/GenericDAO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface GenericDAO<T : Identifiable<String>> : LookupDAO<T> {
suspend fun remove(entity: T): DocIdentifier
fun remove(entities: Collection<T>): Flow<DocIdentifier>
suspend fun purge(entity: T): DocIdentifier
suspend fun purge(entities: Collection<T>)
suspend fun purge(entities: Collection<T>): Flow<DocIdentifier>
fun unRemove(entities: Collection<T>): Flow<DocIdentifier>
suspend fun unRemove(entity: T): DocIdentifier
suspend fun forceInitStandardDesignDocument(updateIfExists: Boolean = true, useVersioning: Boolean = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,10 @@ abstract class CachedDAOImpl<T : StoredDocument>(
}
}

override suspend fun purge(entities: Collection<T>) {
super.purge(entities)
for (entity in entities) {
evictFromCache(entity)
override suspend fun purge(entities: Collection<T>): Flow<DocIdentifier> =
super.purge(entities).onEach {
it.id?.let { id -> evictFromCache(id) }
}
}

override fun <K : Collection<T>> save(newEntity: Boolean?, entities: K) = flow {
val savedEntities = try {
Expand Down
16 changes: 7 additions & 9 deletions src/main/kotlin/org/taktik/icure/asyncdao/impl/GenericDAOImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,15 @@ abstract class GenericDAOImpl<T : StoredDocument>(
log.debug("remove $entities")
}
try {
val entitiesById = entities.associateBy { it.id }
val bulkUpdateResults = client.bulkUpdate(
entities.map {
beforeDelete(it).let {
it.withDeletionDate(System.currentTimeMillis()) as T
}
},
entityClass
).onEach { r ->
entities.firstOrNull { e -> r.id == e.id }?.let { afterDelete(it) }
}
).onEach { r -> entitiesById[r.id]?.let { afterDelete(it) } }
emitAll(bulkUpdateResults.map { DocIdentifier(it.id, it.rev) })
} catch (e: Exception) {
throw PersistenceException("failed to remove entities ", e)
Expand Down Expand Up @@ -291,17 +290,16 @@ abstract class GenericDAOImpl<T : StoredDocument>(
}
}

// This function is not reactive, but it doesn't seem to be used at all anyway...
override suspend fun purge(entities: Collection<T>) {
override suspend fun purge(entities: Collection<T>): Flow<DocIdentifier> = flow {
val client = couchDbDispatcher.getClient(dbInstanceUrl)
if (log.isDebugEnabled) {
log.debug("remove $entities")
}
try {
val bulkDeleteResults = client.bulkDelete(entities.map { beforeDelete(it) }).toList()
for (entity in entities) {
afterDelete(entity)
}
val entitiesById = entities.associateBy { it.id }
val bulkDeleteResults = client.bulkDelete(entities.map { beforeDelete(it) })
.onEach { r -> entitiesById[r.id]?.let { afterDelete(it) } }
emitAll(bulkDeleteResults.map { DocIdentifier(it.id, it.rev) })
} catch (e: Exception) {
throw PersistenceException("failed to remove entities ", e)
}
Expand Down

0 comments on commit e1a69ec

Please sign in to comment.