Skip to content

Commit 979e32c

Browse files
committed
Simplify emoticons loading
Clear old emoticons cache after loading new version Remove stream emoticon loader Remove backup emoticon loaders
1 parent f20031e commit 979e32c

17 files changed

Lines changed: 39 additions & 143 deletions

src/main/kotlin/failchat/Dependencies.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,24 @@ import failchat.goodgame.GgApiClient
4242
import failchat.goodgame.GgBadgeHandler
4343
import failchat.goodgame.GgChannel
4444
import failchat.goodgame.GgChatClient
45-
import failchat.goodgame.GgEmoticonBulkLoader
4645
import failchat.goodgame.GgEmoticonHandler
4746
import failchat.goodgame.GgEmoticonLoadConfiguration
47+
import failchat.goodgame.GgEmoticonLoader
4848
import failchat.goodgame.GgViewersCountLoader
4949
import failchat.gui.ChatGuiEventHandler
5050
import failchat.gui.FullGuiEventHandler
5151
import failchat.gui.GuiMode
52+
import failchat.peka2tv.Peka2TvEmoticonLoader
5253
import failchat.peka2tv.Peka2tvApiClient
5354
import failchat.peka2tv.Peka2tvBadgeHandler
5455
import failchat.peka2tv.Peka2tvChatClient
55-
import failchat.peka2tv.Peka2tvEmoticonBulkLoader
5656
import failchat.peka2tv.Peka2tvEmoticonHandler
5757
import failchat.peka2tv.Peka2tvEmoticonLoadConfiguration
5858
import failchat.skin.SkinScanner
5959
import failchat.twitch.BttvApiClient
6060
import failchat.twitch.BttvEmoticonHandler
61-
import failchat.twitch.BttvGlobalEmoticonBulkLoader
6261
import failchat.twitch.BttvGlobalEmoticonLoadConfiguration
62+
import failchat.twitch.BttvGlobalEmoticonLoader
6363
import failchat.twitch.ConfigurationTokenContainer
6464
import failchat.twitch.FfzApiClient
6565
import failchat.twitch.FfzEmoticonHandler
@@ -176,10 +176,7 @@ class Dependencies {
176176
val emoticonStorage = EmoticonStorage()
177177
val emoticonFinder: EmoticonFinder = emoticonStorage
178178
val failchatEmoticonHandler = FailchatEmoticonHandler(emoticonFinder)
179-
val emoticonManager = EmoticonManager(
180-
emoticonStorage,
181-
backgroundExecutorService
182-
)
179+
val emoticonManager = EmoticonManager(emoticonStorage)
183180
val deletedMessagePlaceholderFactory = DeletedMessagePlaceholderFactory(
184181
emoticonFinder,
185182
configuration
@@ -249,7 +246,7 @@ class Dependencies {
249246
objectMapper,
250247
configuration.getString("peka2tv.api-url")
251248
)
252-
val peka2tvEmoticonBulkLoader = Peka2tvEmoticonBulkLoader(peka2tvApiClient)
249+
val peka2tvEmoticonBulkLoader = Peka2TvEmoticonLoader(peka2tvApiClient)
253250
val peka2tvEmoticonLoadConfiguration = Peka2tvEmoticonLoadConfiguration(peka2tvEmoticonBulkLoader)
254251
val peka2tvBadgeHandler = Peka2tvBadgeHandler(badgeFinder)
255252
val peka2tvEmoticonHandler = Peka2tvEmoticonHandler(emoticonFinder)
@@ -274,7 +271,7 @@ class Dependencies {
274271
objectMapper = objectMapper
275272
)
276273
val bttvEmoticonHandler = BttvEmoticonHandler(emoticonFinder)
277-
val bttvGlobalEmoticonBulkLoader = BttvGlobalEmoticonBulkLoader(bttvApiClient)
274+
val bttvGlobalEmoticonBulkLoader = BttvGlobalEmoticonLoader(bttvApiClient)
278275
val bttvGlobalEmoticonLoadConfiguration = BttvGlobalEmoticonLoadConfiguration(bttvGlobalEmoticonBulkLoader)
279276

280277
// FFZ
@@ -370,7 +367,7 @@ class Dependencies {
370367
emoticonsJsUrl = configuration.getString("goodgame.emoticon-js-url"),
371368
objectMapper = objectMapper
372369
)
373-
val ggEmoticonBulkLoader = GgEmoticonBulkLoader(ggApiClient)
370+
val ggEmoticonBulkLoader = GgEmoticonLoader(ggApiClient)
374371
val ggEmoticonLoadConfiguration = GgEmoticonLoadConfiguration(ggEmoticonBulkLoader)
375372
val ggEmoticonHandler = GgEmoticonHandler(emoticonFinder)
376373

src/main/kotlin/failchat/emoticon/ChannelEmoticonUpdater.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ChannelEmoticonUpdater(
4545

4646
val emoticonToId = emoticons.map { EmoticonAndId(it, it.code) }
4747
emoticonStorage.putMapping(Origin.FRANKERFASEZ, emoticonToId)
48-
logger.info("FrankerFaceZ emoticons loaded for channel '{}'", channelName)
48+
logger.info("FrankerFaceZ emoticons loaded for channel '{}', count: {}", channelName, emoticons.size)
4949
}
5050

5151
suspend fun update7tvEmoticons(channelId: Long) {
@@ -58,7 +58,7 @@ class ChannelEmoticonUpdater(
5858

5959
val emoticonToId = emoticons.map { EmoticonAndId(it, it.code) }
6060
emoticonStorage.putMapping(Origin.SEVEN_TV_CHANNEL, emoticonToId)
61-
logger.info("7tv emoticons loaded for channel '{}'", channelId)
61+
logger.info { "7tv emoticons loaded for channel '${channelId}', count: ${emoticons.size}" }
6262
}
6363

6464
}

src/main/kotlin/failchat/emoticon/EmoticonLoadConfiguration.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ interface EmoticonLoadConfiguration<T: Emoticon> {
66

77
val origin: Origin
88

9-
val loadType: LoadType
10-
11-
/** Loaders for the same set of emoticons. Second and the next loaders in the list is used as backup loaders. */
12-
val bulkLoaders: List<EmoticonBulkLoader<T>>
13-
14-
/** Loaders for the same set of emoticons. Second and the next loaders in the list is used as backup loaders. */
15-
val streamLoaders: List<EmoticonStreamLoader<T>>
9+
val loader: EmoticonLoader<T>
1610

1711
val idExtractor: EmoticonIdExtractor<T>
18-
19-
enum class LoadType {
20-
BULK, STREAM
21-
}
2212
}

src/main/kotlin/failchat/emoticon/EmoticonBulkLoader.kt renamed to src/main/kotlin/failchat/emoticon/EmoticonLoader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package failchat.emoticon
33
import failchat.Origin
44
import java.util.concurrent.CompletableFuture
55

6-
interface EmoticonBulkLoader<T : Emoticon> {
6+
interface EmoticonLoader<T : Emoticon> {
77
val origin: Origin
88
fun loadEmoticons(): CompletableFuture<List<T>>
99
}

src/main/kotlin/failchat/emoticon/EmoticonManager.kt

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package failchat.emoticon
22

3-
import failchat.emoticon.EmoticonLoadConfiguration.LoadType.BULK
4-
import failchat.emoticon.EmoticonLoadConfiguration.LoadType.STREAM
5-
import kotlinx.coroutines.flow.consumeAsFlow
6-
import kotlinx.coroutines.flow.map
73
import mu.KotlinLogging
8-
import java.util.concurrent.ScheduledExecutorService
9-
import java.util.concurrent.TimeUnit
10-
import java.util.concurrent.atomic.AtomicInteger
114

125
class EmoticonManager(
13-
private val storage: EmoticonStorage,
14-
private val scheduledExecutorService: ScheduledExecutorService
6+
private val storage: EmoticonStorage
157
) {
168

179
private companion object {
@@ -39,86 +31,37 @@ class EmoticonManager(
3931
val origin = loadConfiguration.origin
4032
val emoticonsInStorage = storage.getCount(origin)
4133

42-
// Load emoticon list via EmoticonBulkLoader or EmoticonStreamLoader and put it in the storage
43-
val loadResult = when (loadConfiguration.loadType) {
44-
BULK -> loadEmoticonBulk(loadConfiguration)
45-
STREAM -> loadEmoticonStream(loadConfiguration)
46-
}
34+
val loadResult = loadEmoticons(loadConfiguration)
4735

4836
when (loadResult) {
4937
is LoadResult.Failure -> {
50-
logger.warn("Failed to load emoticon list for {}. Outdated list will be used, count: {}", origin, emoticonsInStorage)
38+
logger.warn {"Failed to load emoticon list for $origin. Outdated list will be used, count: $emoticonsInStorage" }
5139
}
5240
is LoadResult.Success -> {
53-
logger.info("Emoticon list loaded for {}, count: {}", origin, loadResult.emoticonsLoaded)
41+
logger.info { "Emoticon list loaded for $origin, count: ${loadResult.emoticonsLoaded}" }
5442
}
5543
}
5644
}
5745

58-
private fun <T : Emoticon> loadEmoticonBulk(loadConfiguration: EmoticonLoadConfiguration<T>): LoadResult {
46+
private fun <T : Emoticon> loadEmoticons(loadConfiguration: EmoticonLoadConfiguration<T>): LoadResult {
5947
val origin = loadConfiguration.origin
6048

61-
var loadedSuccessfully = false
62-
var emoticons: List<T> = emptyList()
63-
64-
for (bulkLoader in loadConfiguration.bulkLoaders) {
65-
try {
66-
emoticons = bulkLoader.loadEmoticons().join()
67-
loadedSuccessfully = true
68-
break
69-
} catch (e: Exception) {
70-
logger.warn(e) { "Failed to load emoticon list for $origin via bulk loader $bulkLoader" }
71-
}
72-
}
73-
74-
if (!loadedSuccessfully)
49+
val emoticons = try {
50+
loadConfiguration.loader.loadEmoticons().join()
51+
} catch (e: Exception) {
52+
logger.warn(e) { "Failed to load emoticon list for $origin via bulk loader ${loadConfiguration.loader}" }
7553
return LoadResult.Failure
54+
}
7655

7756
// Put data in storage
7857
val emoticonAndIdMapping = emoticons
7958
.map { EmoticonAndId(it, loadConfiguration.idExtractor.extractId(it)) }
59+
storage.clear(origin)
8060
storage.putMapping(origin, emoticonAndIdMapping)
8161

8262
return LoadResult.Success(emoticons.size)
8363
}
8464

85-
private fun <T : Emoticon> loadEmoticonStream(loadConfiguration: EmoticonLoadConfiguration<T>): LoadResult {
86-
val origin = loadConfiguration.origin
87-
val idExtractor = loadConfiguration.idExtractor
88-
89-
var loadedSuccessfully = false
90-
val count = AtomicInteger()
91-
92-
for (streamLoader in loadConfiguration.streamLoaders) {
93-
count.set(0)
94-
val loggingTask = scheduledExecutorService.scheduleAtFixedRate({
95-
logger.info("Loading {} emoticons, loaded: {}", origin, count.get())
96-
}, 5, 5, TimeUnit.SECONDS)
97-
98-
try {
99-
val emoticonsFlow = streamLoader.loadEmoticons()
100-
.consumeAsFlow()
101-
.map {
102-
count.incrementAndGet()
103-
EmoticonAndId(it, idExtractor.extractId(it))
104-
}
105-
106-
storage.putChannel(origin, emoticonsFlow)
107-
loadedSuccessfully = true
108-
break
109-
} catch (e: Exception) {
110-
logger.warn("Failed to load emoticons for {} via stream loader {}", origin, streamLoader, e)
111-
} finally {
112-
loggingTask.cancel(false)
113-
}
114-
}
115-
116-
if (!loadedSuccessfully)
117-
return LoadResult.Failure
118-
119-
return LoadResult.Success(count.get())
120-
}
121-
12265
private sealed class LoadResult {
12366
class Success(val emoticonsLoaded: Int) : LoadResult()
12467
object Failure : LoadResult()

src/main/kotlin/failchat/emoticon/EmoticonStreamLoader.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/failchat/goodgame/GgEmoticonLoadConfiguration.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package failchat.goodgame
22

33
import failchat.Origin
44
import failchat.emoticon.EmoticonLoadConfiguration
5-
import failchat.emoticon.EmoticonLoadConfiguration.LoadType
6-
import failchat.emoticon.EmoticonStreamLoader
75

8-
class GgEmoticonLoadConfiguration(loader: GgEmoticonBulkLoader) : EmoticonLoadConfiguration<GgEmoticon> {
6+
class GgEmoticonLoadConfiguration(override val loader: GgEmoticonLoader) : EmoticonLoadConfiguration<GgEmoticon> {
97
override val origin = Origin.GOODGAME
10-
override val loadType = LoadType.BULK
11-
override val bulkLoaders = listOf(loader)
12-
override val streamLoaders: List<EmoticonStreamLoader<GgEmoticon>> = emptyList()
138
override val idExtractor = GgEmoticonIdExtractor
149
}

src/main/kotlin/failchat/goodgame/GgEmoticonBulkLoader.kt renamed to src/main/kotlin/failchat/goodgame/GgEmoticonLoader.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package failchat.goodgame
22

33
import failchat.Origin
4-
import failchat.emoticon.EmoticonBulkLoader
4+
import failchat.emoticon.EmoticonLoader
55
import kotlinx.coroutines.CoroutineScope
66
import kotlinx.coroutines.Dispatchers
77
import kotlinx.coroutines.future.future
88
import java.util.concurrent.CompletableFuture
99

10-
class GgEmoticonBulkLoader(private val ggApiClient: GgApiClient) : EmoticonBulkLoader<GgEmoticon> {
10+
class GgEmoticonLoader(private val ggApiClient: GgApiClient) : EmoticonLoader<GgEmoticon> {
1111

1212
override val origin = Origin.GOODGAME
1313

src/main/kotlin/failchat/peka2tv/Peka2tvEmoticonBulkLoader.kt renamed to src/main/kotlin/failchat/peka2tv/Peka2TvEmoticonLoader.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package failchat.peka2tv
22

33
import failchat.Origin.PEKA2TV
4-
import failchat.emoticon.EmoticonBulkLoader
4+
import failchat.emoticon.EmoticonLoader
55
import java.util.concurrent.CompletableFuture
66

7-
class Peka2tvEmoticonBulkLoader(
7+
class Peka2TvEmoticonLoader(
88
private val apiClient: Peka2tvApiClient
9-
) : EmoticonBulkLoader<Peka2tvEmoticon> {
9+
) : EmoticonLoader<Peka2tvEmoticon> {
1010

1111
override val origin = PEKA2TV
1212

src/main/kotlin/failchat/peka2tv/Peka2tvEmoticonLoadConfiguration.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ package failchat.peka2tv
22

33
import failchat.Origin
44
import failchat.emoticon.EmoticonLoadConfiguration
5-
import failchat.emoticon.EmoticonLoadConfiguration.LoadType
6-
import failchat.emoticon.EmoticonStreamLoader
75

8-
class Peka2tvEmoticonLoadConfiguration(loader: Peka2tvEmoticonBulkLoader) : EmoticonLoadConfiguration<Peka2tvEmoticon> {
6+
class Peka2tvEmoticonLoadConfiguration(override val loader: Peka2TvEmoticonLoader) : EmoticonLoadConfiguration<Peka2tvEmoticon> {
97
override val origin = Origin.PEKA2TV
10-
override val loadType = LoadType.BULK
11-
override val bulkLoaders = listOf(loader)
12-
override val streamLoaders: List<EmoticonStreamLoader<Peka2tvEmoticon>> = emptyList()
138
override val idExtractor = Peka2tvEmoticonIdExtractor
149
}

0 commit comments

Comments
 (0)