No notification on known item, Polycentric logging, refs

This commit is contained in:
Kelvin 2023-12-09 18:11:45 +01:00
commit b0a35bcf3f
5 changed files with 19 additions and 11 deletions

View file

@ -17,6 +17,7 @@ import kotlinx.coroutines.*
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.time.OffsetDateTime import java.time.OffsetDateTime
import kotlin.system.measureTimeMillis
class PolycentricCache { class PolycentricCache {
data class CachedOwnedClaims(val ownedClaims: List<OwnedClaim>?, val creationTime: OffsetDateTime = OffsetDateTime.now()) { data class CachedOwnedClaims(val ownedClaims: List<OwnedClaim>?, val creationTime: OffsetDateTime = OffsetDateTime.now()) {
@ -29,8 +30,15 @@ class PolycentricCache {
private val _cache = hashMapOf<PlatformID, CachedOwnedClaims>() private val _cache = hashMapOf<PlatformID, CachedOwnedClaims>()
private val _profileCache = hashMapOf<PublicKey, CachedPolycentricProfile>() private val _profileCache = hashMapOf<PublicKey, CachedPolycentricProfile>()
private val _profileUrlCache = FragmentedStorage.get<CachedPolycentricProfileStorage>("profileUrlCache") private val _profileUrlCache: CachedPolycentricProfileStorage;
private val _scope = CoroutineScope(Dispatchers.IO); private val _scope = CoroutineScope(Dispatchers.IO);
init {
Logger.i(TAG, "Initializing Polycentric cache");
val time = measureTimeMillis {
_profileUrlCache = FragmentedStorage.get<CachedPolycentricProfileStorage>("profileUrlCache")
}
Logger.i(TAG, "Initialized Polycentric cache (${_profileUrlCache.map.size}, ${time}ms)");
}
private val _taskGetProfile = BatchedTaskHandler<PublicKey, CachedPolycentricProfile>(_scope, private val _taskGetProfile = BatchedTaskHandler<PublicKey, CachedPolycentricProfile>(_scope,
{ system -> { system ->
@ -222,7 +230,7 @@ class PolycentricCache {
} }
} }
suspend fun getProfileAsync(id: PlatformID, url: String? = null): CachedPolycentricProfile? { suspend fun getProfileAsync(id: PlatformID, urlNullCache: String? = null): CachedPolycentricProfile? {
if (!StatePolycentric.instance.enabled || id.claimType <= 0) { if (!StatePolycentric.instance.enabled || id.claimType <= 0) {
return CachedPolycentricProfile(null); return CachedPolycentricProfile(null);
} }
@ -243,8 +251,8 @@ class PolycentricCache {
Logger.v(TAG, "getProfileAsync (id: $id) != null (with retrieved valid claims)") Logger.v(TAG, "getProfileAsync (id: $id) != null (with retrieved valid claims)")
return getProfileAsync(claims.ownedClaims.first().system).await() return getProfileAsync(claims.ownedClaims.first().system).await()
} else { } else {
if(url != null) if(urlNullCache != null)
_profileUrlCache.setAndSave(url, PolycentricCache.CachedPolycentricProfile(null)); _profileUrlCache.setAndSave(urlNullCache, PolycentricCache.CachedPolycentricProfile(null));
return null; return null;
} }
} }

View file

@ -99,7 +99,7 @@ class StateCache {
if(existing != null && doUpdate) { if(existing != null && doUpdate) {
_subscriptionCache.update(existing.id!!, serialized); _subscriptionCache.update(existing.id!!, serialized);
return true; return false;
} }
else if(existing == null) { else if(existing == null) {
_subscriptionCache.insert(serialized); _subscriptionCache.insert(serialized);

View file

@ -169,10 +169,10 @@ class StatePolycentric {
} }
} }
fun getChannelUrls(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): List<String> { fun getChannelUrls(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false, doCacheNull: Boolean = false): List<String> {
return getChannelUrlsWithUpdateResult(url, channelId, cacheOnly).second; return getChannelUrlsWithUpdateResult(url, channelId, cacheOnly, doCacheNull).second;
} }
fun getChannelUrlsWithUpdateResult(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false): Pair<Boolean, List<String>> { fun getChannelUrlsWithUpdateResult(url: String, channelId: PlatformID? = null, cacheOnly: Boolean = false, doCacheNull: Boolean = false): Pair<Boolean, List<String>> {
var didUpdate = false; var didUpdate = false;
if (!enabled) { if (!enabled) {
return Pair(false, listOf(url)); return Pair(false, listOf(url));
@ -184,7 +184,7 @@ class StatePolycentric {
if (polycentricCached == null && channelId != null) { if (polycentricCached == null && channelId != null) {
Logger.i("StateSubscriptions", "Get polycentric profile not cached"); Logger.i("StateSubscriptions", "Get polycentric profile not cached");
if(!cacheOnly) { if(!cacheOnly) {
polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId, url) }?.profile; polycentricProfile = runBlocking { PolycentricCache.instance.getProfileAsync(channelId, if(doCacheNull) url else null) }?.profile;
didUpdate = true; didUpdate = true;
} }
} else { } else {

View file

@ -258,7 +258,7 @@ class StateSubscriptions {
var polycentricBudget: Int = 10; var polycentricBudget: Int = 10;
val subUrls = getSubscriptions().parallelStream().map { val subUrls = getSubscriptions().parallelStream().map {
if(usePolycentric) { if(usePolycentric) {
val result = StatePolycentric.instance.getChannelUrlsWithUpdateResult(it.channel.url, it.channel.id, polycentricBudget <= 0); val result = StatePolycentric.instance.getChannelUrlsWithUpdateResult(it.channel.url, it.channel.id, polycentricBudget <= 0, true);
if(result.first) { if(result.first) {
synchronized(lock) { synchronized(lock) {
polycentricBudget--; polycentricBudget--;

@ -1 +1 @@
Subproject commit 8b8fd55f39a5039ed15bee3b7e8e9a6ef5a6f538 Subproject commit fc5d17e19067efc0d28192b43de31f9bc499d288