Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal suspend fun <Item, PrimaryKey, SortKey> Queryable.iterateWithChunk(
var consumedKeys = mapOf<SortKey, Set<PrimaryKey>>()
var nextRange = initialRange
while (takeNext) {
val results = openCursor(nextRange).map { cursor ->
val results = openCursor(nextRange, autoContinue = true).map { cursor ->
cursor.value.unsafeCast<Item>()
}.filter {
// すでに処理した項目はスキップする
Expand Down Expand Up @@ -80,7 +80,7 @@ internal suspend fun <Item, PrimaryKey> Queryable.deleteWithChunk(
var takeNext = true
var deleted = 0L
while (takeNext) {
val results = openCursor(query)
val results = openCursor(query, autoContinue = true)
.take(
limit?.let { (it - deleted).coerceAtMost(chunkSize) } ?: chunkSize
).map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ internal class KottageIndexeddbItemEventRepository : KottageItemEventRepository
store.index("item_event_created_at")
.openCursor(
// created_at DESC
direction = Cursor.Direction.Previous
direction = Cursor.Direction.Previous,
autoContinue = true,
).map { cursor ->
cursor.value.unsafeCast<Item_event>().created_at.toLong()
}.firstOrNull()
Expand Down Expand Up @@ -176,9 +177,11 @@ internal class KottageIndexeddbItemEventRepository : KottageItemEventRepository
bound(
arrayOf(itemType),
arrayOf(itemType, emptyArray<Any>())
)
),
autoContinue = false,
).take(limit).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand All @@ -187,9 +190,11 @@ internal class KottageIndexeddbItemEventRepository : KottageItemEventRepository
transaction.store { store ->
store.index("item_event_created_at").openCursor(
// created_at < createdAt
upperBound(createdAt.toDouble(), true)
upperBound(createdAt.toDouble(), true),
autoContinue = false,
).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand All @@ -201,9 +206,11 @@ internal class KottageIndexeddbItemEventRepository : KottageItemEventRepository
bound(
arrayOf(itemType),
arrayOf(itemType, emptyArray<Any>())
)
),
autoContinue = false,
).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand All @@ -215,9 +222,11 @@ internal class KottageIndexeddbItemEventRepository : KottageItemEventRepository
bound(
arrayOf(listType),
arrayOf(listType, emptyArray<Any>())
)
),
autoContinue = false,
).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.juul.indexeddb.Key
import com.juul.indexeddb.ObjectStore
import com.juul.indexeddb.WriteTransaction
import com.juul.indexeddb.bound
import com.juul.indexeddb.only
import com.juul.indexeddb.lowerBound
import com.juul.indexeddb.only
import io.github.irgaly.kottage.data.indexeddb.extension.jso
import io.github.irgaly.kottage.data.indexeddb.schema.entity.Item_list
import io.github.irgaly.kottage.data.indexeddb.schema.entity.Item_list_stats
Expand Down Expand Up @@ -153,15 +153,17 @@ internal class KottageIndexeddbItemListRepository : KottageItemListRepository {
arrayOf(type),
arrayOf(type, beforeExpireAt.toDouble()),
upperOpen = true
)
),
autoContinue = true,
)
} else {
store.index("item_list_type_expire_at").openCursor(
// type = type
bound(
arrayOf(type),
arrayOf(type, emptyArray<Any>())
)
),
autoContinue = true,
)
}).map { cursor ->
cursor.value.unsafeCast<Item_list>()
Expand All @@ -185,7 +187,8 @@ internal class KottageIndexeddbItemListRepository : KottageItemListRepository {
): Long {
return transaction.store { store ->
store.index("item_list_type").openCursor(
Key(type)
Key(type),
autoContinue = true,
).count { cursor ->
(cursor.value.unsafeCast<Item_list>().item_key == null)
}.toLong()
Expand Down Expand Up @@ -224,9 +227,11 @@ internal class KottageIndexeddbItemListRepository : KottageItemListRepository {
bound(
arrayOf(type),
arrayOf(type, emptyArray<Any>())
)
),
autoContinue = false,
).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ internal class KottageIndexeddbItemRepository : KottageItemRepository {
): List<ItemStats> {
return transaction.statsStore { store ->
// SQLite 側に合わせて index なしで総当たり処理
store.openCursor().map { cursor ->
store.openCursor(autoContinue = true).map { cursor ->
cursor.value.unsafeCast<Item_stats>()
}.filter { stats ->
((stats.count.toLong() <= 0L) && (stats.event_count.toLong() <= 0))
Expand All @@ -260,9 +260,11 @@ internal class KottageIndexeddbItemRepository : KottageItemRepository {
transaction.store { store ->
store.index("item_type").openCursor(
// type = itemType
Key(itemType)
Key(itemType),
autoContinue = false,
).collect { cursor ->
cursor.delete()
cursor.`continue`()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ package io.github.irgaly.kottage.internal.repository
* * external types https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-js-export/
* * Kotlin indexeddb
* * https://github.com/JuulLabs/indexeddb
* * v0.3.0 Issue
* * cursor Flow の collect 内で他の indexxeddb 非同期処理を実行すると
* collect 処理が最後まで進む前に cursor.continue() が実行され、次の collect 処理が並列で走ってしまう
* * Transaction 内の並列処理は整合性が保てないため、cursor Flow 内では非同期処理を使わないようにする
* * 例外として: cursor.delete() だけの実行なら許可する
*/
internal class KottageIndexeddbRepositoryFactory: KottageRepositoryFactory {
override suspend fun createItemRepository(): KottageItemRepository {
Expand Down
Loading