|
1 | 1 | package failchat.emoticon |
2 | 2 |
|
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 |
7 | 3 | import mu.KotlinLogging |
8 | | -import java.util.concurrent.ScheduledExecutorService |
9 | | -import java.util.concurrent.TimeUnit |
10 | | -import java.util.concurrent.atomic.AtomicInteger |
11 | 4 |
|
12 | 5 | class EmoticonManager( |
13 | | - private val storage: EmoticonStorage, |
14 | | - private val scheduledExecutorService: ScheduledExecutorService |
| 6 | + private val storage: EmoticonStorage |
15 | 7 | ) { |
16 | 8 |
|
17 | 9 | private companion object { |
@@ -39,86 +31,37 @@ class EmoticonManager( |
39 | 31 | val origin = loadConfiguration.origin |
40 | 32 | val emoticonsInStorage = storage.getCount(origin) |
41 | 33 |
|
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) |
47 | 35 |
|
48 | 36 | when (loadResult) { |
49 | 37 | 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" } |
51 | 39 | } |
52 | 40 | 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}" } |
54 | 42 | } |
55 | 43 | } |
56 | 44 | } |
57 | 45 |
|
58 | | - private fun <T : Emoticon> loadEmoticonBulk(loadConfiguration: EmoticonLoadConfiguration<T>): LoadResult { |
| 46 | + private fun <T : Emoticon> loadEmoticons(loadConfiguration: EmoticonLoadConfiguration<T>): LoadResult { |
59 | 47 | val origin = loadConfiguration.origin |
60 | 48 |
|
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}" } |
75 | 53 | return LoadResult.Failure |
| 54 | + } |
76 | 55 |
|
77 | 56 | // Put data in storage |
78 | 57 | val emoticonAndIdMapping = emoticons |
79 | 58 | .map { EmoticonAndId(it, loadConfiguration.idExtractor.extractId(it)) } |
| 59 | + storage.clear(origin) |
80 | 60 | storage.putMapping(origin, emoticonAndIdMapping) |
81 | 61 |
|
82 | 62 | return LoadResult.Success(emoticons.size) |
83 | 63 | } |
84 | 64 |
|
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 | | - |
122 | 65 | private sealed class LoadResult { |
123 | 66 | class Success(val emoticonsLoaded: Int) : LoadResult() |
124 | 67 | object Failure : LoadResult() |
|
0 commit comments