Refactoring base EOSE managers to provide better logging
This commit is contained in:
@@ -141,10 +141,10 @@ fun debugState(context: Context) {
|
|||||||
LocalCache.addressables
|
LocalCache.addressables
|
||||||
.sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory() ?: 0L })
|
.sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory() ?: 0L })
|
||||||
|
|
||||||
qttNotes.toList().sortedByDescending { bytesNotes.get(it.first) }.forEach { (kind, qtt) ->
|
qttNotes.toList().sortedByDescending { bytesNotes[it.first] }.forEach { (kind, qtt) ->
|
||||||
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesNotes.get(kind)?.div((1024 * 1024))}MB ")
|
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesNotes.get(kind)?.div((1024 * 1024))}MB ")
|
||||||
}
|
}
|
||||||
qttAddressables.toList().sortedByDescending { bytesNotes.get(it.first) }.forEach { (kind, qtt) ->
|
qttAddressables.toList().sortedByDescending { bytesNotes[it.first] }.forEach { (kind, qtt) ->
|
||||||
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesAddressables.get(kind)?.div((1024 * 1024))}MB ")
|
Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesAddressables.get(kind)?.div((1024 * 1024))}MB ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-3
@@ -25,17 +25,29 @@ import com.vitorpamplona.amethyst.isDebug
|
|||||||
import com.vitorpamplona.ammolite.relays.BundledUpdate
|
import com.vitorpamplona.ammolite.relays.BundledUpdate
|
||||||
import com.vitorpamplona.ammolite.relays.datasources.SubscriptionController
|
import com.vitorpamplona.ammolite.relays.datasources.SubscriptionController
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.client.single.newSubId
|
||||||
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
abstract class BaseEoseManager<T>(
|
abstract class BaseEoseManager<T>(
|
||||||
val client: NostrClient,
|
val client: NostrClient,
|
||||||
val allKeys: () -> Set<T>,
|
val allKeys: () -> Set<T>,
|
||||||
) {
|
) {
|
||||||
val orchestrator = SubscriptionController(client)
|
protected val logTag: String = this.javaClass.simpleName
|
||||||
|
|
||||||
|
private val orchestrator = SubscriptionController(client)
|
||||||
|
|
||||||
abstract fun updateSubscriptions(keys: Set<T>)
|
abstract fun updateSubscriptions(keys: Set<T>)
|
||||||
|
|
||||||
fun printStats() = orchestrator.printStats(this.javaClass.simpleName)
|
fun printStats() = orchestrator.printStats(logTag)
|
||||||
|
|
||||||
|
fun newSubscriptionId() = if (isDebug) logTag + newSubId() else newSubId()
|
||||||
|
|
||||||
|
fun getSubscription(subId: String) = orchestrator.getSub(subId)
|
||||||
|
|
||||||
|
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null) = orchestrator.requestNewSubscription(newSubscriptionId(), onEOSE)
|
||||||
|
|
||||||
|
fun dismissSubscription(subId: String) = orchestrator.dismissSubscription(subId)
|
||||||
|
|
||||||
// Refreshes observers in batches.
|
// Refreshes observers in batches.
|
||||||
private val bundler = BundledUpdate(300, Dispatchers.Default)
|
private val bundler = BundledUpdate(300, Dispatchers.Default)
|
||||||
@@ -56,7 +68,7 @@ abstract class BaseEoseManager<T>(
|
|||||||
bundler.cancel()
|
bundler.cancel()
|
||||||
orchestrator.destroy()
|
orchestrator.destroy()
|
||||||
if (isDebug) {
|
if (isDebug) {
|
||||||
Log.d("${this.javaClass.simpleName}", "Destroy, Unsubscribe")
|
Log.d(logTag, "Destroy, Unsubscribe")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -62,7 +62,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
open fun newSub(key: T): Subscription =
|
open fun newSub(key: T): Subscription =
|
||||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
requestNewSubscription { time, relayUrl ->
|
||||||
newEose(key, relayUrl, time)
|
newEose(key, relayUrl, time)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
|||||||
key: String,
|
key: String,
|
||||||
subId: String,
|
subId: String,
|
||||||
) {
|
) {
|
||||||
orchestrator.dismissSubscription(subId)
|
dismissSubscription(subId)
|
||||||
userSubscriptionMap.remove(key)
|
userSubscriptionMap.remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +80,7 @@ abstract class PerUniqueIdEoseManager<T>(
|
|||||||
return if (subId == null) {
|
return if (subId == null) {
|
||||||
newSub(key).also { userSubscriptionMap[id] = it.id }
|
newSub(key).also { userSubscriptionMap[id] = it.id }
|
||||||
} else {
|
} else {
|
||||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[id] = it.id }
|
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[id] = it.id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-3
@@ -28,6 +28,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
|
|||||||
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter
|
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.groupByRelay
|
import com.vitorpamplona.quartz.nip01Core.relay.client.pool.groupByRelay
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
|
import kotlin.collections.distinctBy
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This query type creates a new relay subscription for every logged-in
|
* This query type creates a new relay subscription for every logged-in
|
||||||
@@ -57,7 +58,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
|||||||
) = latestEOSEs.newEose(user(key), list(key), relay, time)
|
) = latestEOSEs.newEose(user(key), list(key), relay, time)
|
||||||
|
|
||||||
open fun newSub(key: T): Subscription =
|
open fun newSub(key: T): Subscription =
|
||||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
requestNewSubscription { time, relayUrl ->
|
||||||
newEose(key, relayUrl, time)
|
newEose(key, relayUrl, time)
|
||||||
if (invalidateAfterEose) {
|
if (invalidateAfterEose) {
|
||||||
invalidateFilters()
|
invalidateFilters()
|
||||||
@@ -68,7 +69,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
|||||||
key: User,
|
key: User,
|
||||||
subId: String,
|
subId: String,
|
||||||
) {
|
) {
|
||||||
orchestrator.dismissSubscription(subId)
|
dismissSubscription(subId)
|
||||||
userSubscriptionMap.remove(key)
|
userSubscriptionMap.remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +79,7 @@ abstract class PerUserAndFollowListEoseManager<T>(
|
|||||||
return if (subId == null) {
|
return if (subId == null) {
|
||||||
newSub(key).also { userSubscriptionMap[user] = it.id }
|
newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||||
} else {
|
} else {
|
||||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -56,7 +56,7 @@ abstract class PerUserEoseManager<T>(
|
|||||||
) = latestEOSEs.newEose(user(key), relay, time)
|
) = latestEOSEs.newEose(user(key), relay, time)
|
||||||
|
|
||||||
open fun newSub(key: T): Subscription =
|
open fun newSub(key: T): Subscription =
|
||||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
requestNewSubscription { time, relayUrl ->
|
||||||
newEose(key, relayUrl, time)
|
newEose(key, relayUrl, time)
|
||||||
if (invalidateAfterEose) {
|
if (invalidateAfterEose) {
|
||||||
invalidateFilters()
|
invalidateFilters()
|
||||||
@@ -67,7 +67,7 @@ abstract class PerUserEoseManager<T>(
|
|||||||
key: User,
|
key: User,
|
||||||
subId: String,
|
subId: String,
|
||||||
) {
|
) {
|
||||||
orchestrator.dismissSubscription(subId)
|
dismissSubscription(subId)
|
||||||
userSubscriptionMap.remove(key)
|
userSubscriptionMap.remove(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ abstract class PerUserEoseManager<T>(
|
|||||||
return if (subId == null) {
|
return if (subId == null) {
|
||||||
newSub(key).also { userSubscriptionMap[user] = it.id }
|
newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||||
} else {
|
} else {
|
||||||
orchestrator.getSub(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
getSubscription(subId) ?: newSub(key).also { userSubscriptionMap[user] = it.id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -56,7 +56,7 @@ abstract class SingleSubEoseManager<T>(
|
|||||||
) = latestEOSEs.newEose(relay, time)
|
) = latestEOSEs.newEose(relay, time)
|
||||||
|
|
||||||
val sub =
|
val sub =
|
||||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
requestNewSubscription { time, relayUrl ->
|
||||||
newEose(relayUrl, time)
|
newEose(relayUrl, time)
|
||||||
if (invalidateAfterEose) {
|
if (invalidateAfterEose) {
|
||||||
invalidateFilters()
|
invalidateFilters()
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ abstract class SingleSubNoEoseCacheEoseManager<T>(
|
|||||||
val invalidateAfterEose: Boolean = false,
|
val invalidateAfterEose: Boolean = false,
|
||||||
) : BaseEoseManager<T>(client, allKeys) {
|
) : BaseEoseManager<T>(client, allKeys) {
|
||||||
val sub =
|
val sub =
|
||||||
orchestrator.requestNewSubscription { time, relayUrl ->
|
requestNewSubscription { time, relayUrl ->
|
||||||
if (invalidateAfterEose) {
|
if (invalidateAfterEose) {
|
||||||
invalidateFilters()
|
invalidateFilters()
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -38,7 +38,7 @@ class AccountGiftWrapsEoseManager(
|
|||||||
client: NostrClient,
|
client: NostrClient,
|
||||||
allKeys: () -> Set<AccountQueryState>,
|
allKeys: () -> Set<AccountQueryState>,
|
||||||
) : PerUserEoseManager<AccountQueryState>(client, allKeys) {
|
) : PerUserEoseManager<AccountQueryState>(client, allKeys) {
|
||||||
override fun user(query: AccountQueryState) = query.account.userProfile()
|
override fun user(key: AccountQueryState) = key.account.userProfile()
|
||||||
|
|
||||||
override fun updateFilter(
|
override fun updateFilter(
|
||||||
key: AccountQueryState,
|
key: AccountQueryState,
|
||||||
|
|||||||
+4
-1
@@ -78,7 +78,10 @@ class SubscriptionController(
|
|||||||
|
|
||||||
fun getSub(subId: String) = subscriptions.get(subId)
|
fun getSub(subId: String) = subscriptions.get(subId)
|
||||||
|
|
||||||
fun requestNewSubscription(onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null): Subscription = Subscription(onEose = onEOSE).also { subscriptions.put(it.id, it) }
|
fun requestNewSubscription(
|
||||||
|
subId: String,
|
||||||
|
onEOSE: ((Long, NormalizedRelayUrl) -> Unit)? = null,
|
||||||
|
): Subscription = Subscription(subId, onEose = onEOSE).also { subscriptions.put(it.id, it) }
|
||||||
|
|
||||||
fun dismissSubscription(subId: String) = getSub(subId)?.let { dismissSubscription(it) }
|
fun dismissSubscription(subId: String) = getSub(subId)?.let { dismissSubscription(it) }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user