Skip to content

Commit 81ff065

Browse files
authored
Merge pull request #596 from namehillsoftware/bugfix/cached-library-files
[Bugfix] Cache Library Files by Revision
2 parents 8a764b4 + 46e8b94 commit 81ff065

File tree

4 files changed

+138
-2
lines changed

4 files changed

+138
-2
lines changed

projectBlueWater/src/main/java/com/lasthopesoftware/bluewater/client/browsing/files/access/DelegatingLibraryFileProvider.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class DelegatingLibraryFileProvider(inner: ProvideLibraryFiles, policies: Execut
1111
private val promiseLibraryFiles by lazy { policies.applyPolicy<LibraryId, List<ServiceFile>>(inner::promiseFiles) }
1212
private val promiseItemFiles by lazy { policies.applyPolicy<LibraryId, ItemId, List<ServiceFile>>(inner::promiseFiles) }
1313
private val promisePlaylistFiles by lazy { policies.applyPolicy<LibraryId, PlaylistId, List<ServiceFile>>(inner::promiseFiles) }
14-
private val promiseAudioFilesPolicy by lazy { policies.applyPolicy<LibraryId, String, List<ServiceFile>>(inner::promiseAudioFiles) }
14+
private val promiseAudioFilesPolicy by lazy { policies.applyPolicy(inner::promiseAudioFiles) }
1515

1616
override fun promiseFiles(libraryId: LibraryId): Promise<List<ServiceFile>> = promiseLibraryFiles(libraryId)
1717

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.lasthopesoftware.bluewater.client.browsing.files.access
2+
3+
import com.lasthopesoftware.bluewater.client.browsing.files.ServiceFile
4+
import com.lasthopesoftware.bluewater.client.browsing.items.ItemId
5+
import com.lasthopesoftware.bluewater.client.browsing.items.playlists.PlaylistId
6+
import com.lasthopesoftware.bluewater.client.browsing.library.repository.LibraryId
7+
import com.lasthopesoftware.bluewater.client.browsing.library.revisions.CheckRevisions
8+
import com.lasthopesoftware.policies.caching.CachingPolicyFactory
9+
import com.lasthopesoftware.promises.extensions.cancelBackEventually
10+
import com.namehillsoftware.handoff.promises.Promise
11+
12+
class RevisionCachedLibraryFileProvider(
13+
inner: ProvideLibraryFiles,
14+
private val revisionProvider: CheckRevisions,
15+
cachingPolicy: CachingPolicyFactory
16+
) : ProvideLibraryFiles {
17+
private val promiseLibraryFiles by lazy {
18+
cachingPolicy.applyPolicy { id: LibraryId, _: Long -> inner.promiseFiles(id) }
19+
}
20+
21+
private val promiseItemFiles by lazy {
22+
cachingPolicy.applyPolicy { l: LibraryId, i: ItemId, _: Long -> inner.promiseFiles(l, i) }
23+
}
24+
25+
private val promisePlaylistFiles by lazy {
26+
cachingPolicy.applyPolicy { l: LibraryId, p: PlaylistId, _: Long -> inner.promiseFiles(l, p) }
27+
}
28+
29+
private val promiseAudioFilesPolicy by lazy {
30+
cachingPolicy.applyPolicy { l: LibraryId, q: String, _: Long -> inner.promiseAudioFiles(l, q) }
31+
}
32+
33+
override fun promiseFiles(libraryId: LibraryId): Promise<List<ServiceFile>> =
34+
revisionProvider
35+
.promiseRevision(libraryId)
36+
.cancelBackEventually { r -> promiseLibraryFiles(libraryId, r) }
37+
38+
override fun promiseFiles(libraryId: LibraryId, itemId: ItemId): Promise<List<ServiceFile>> =
39+
revisionProvider
40+
.promiseRevision(libraryId)
41+
.cancelBackEventually { r -> promiseItemFiles(libraryId, itemId, r) }
42+
43+
override fun promiseFiles(libraryId: LibraryId, playlistId: PlaylistId): Promise<List<ServiceFile>> =
44+
revisionProvider
45+
.promiseRevision(libraryId)
46+
.cancelBackEventually { r -> promisePlaylistFiles(libraryId, playlistId, r) }
47+
48+
override fun promiseAudioFiles(libraryId: LibraryId, query: String): Promise<List<ServiceFile>> =
49+
revisionProvider
50+
.promiseRevision(libraryId)
51+
.cancelBackEventually { r -> promiseAudioFilesPolicy(libraryId, query, r) }
52+
}

projectBlueWater/src/main/java/com/lasthopesoftware/bluewater/client/connection/libraries/LibraryConnectionDependents.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lasthopesoftware.bluewater.ApplicationDependencies
44
import com.lasthopesoftware.bluewater.client.browsing.files.access.DelegatingLibraryFileProvider
55
import com.lasthopesoftware.bluewater.client.browsing.files.access.LibraryFileProvider
66
import com.lasthopesoftware.bluewater.client.browsing.files.access.ProvideLibraryFiles
7+
import com.lasthopesoftware.bluewater.client.browsing.files.access.RevisionCachedLibraryFileProvider
78
import com.lasthopesoftware.bluewater.client.browsing.files.properties.CachedFilePropertiesProvider
89
import com.lasthopesoftware.bluewater.client.browsing.files.properties.DelegatingFilePropertiesProvider
910
import com.lasthopesoftware.bluewater.client.browsing.files.properties.FilePropertiesProvider
@@ -106,8 +107,9 @@ class LibraryConnectionRegistry(application: ApplicationDependencies) : LibraryC
106107
}
107108

108109
override val libraryFilesProvider by lazy {
109-
DelegatingLibraryFileProvider(
110+
RevisionCachedLibraryFileProvider(
110111
LibraryFileProvider(application.libraryConnectionProvider),
112+
revisionProvider,
111113
object : CachingPolicyFactory() {
112114
override fun <Input : Any, Output> getCache(): CachePromiseFunctions<Input, Output> =
113115
LruPromiseCache(maxLibraryFiles)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.lasthopesoftware.bluewater.client.browsing.files.access.GivenLibraryFiles.AndTheLibraryRevisionChanges
2+
3+
import com.lasthopesoftware.bluewater.client.browsing.files.ServiceFile
4+
import com.lasthopesoftware.bluewater.client.browsing.files.access.RevisionCachedLibraryFileProvider
5+
import com.lasthopesoftware.bluewater.client.browsing.library.repository.LibraryId
6+
import com.lasthopesoftware.bluewater.shared.promises.extensions.toExpiringFuture
7+
import com.lasthopesoftware.policies.caching.PermanentCachePolicy
8+
import com.lasthopesoftware.promises.extensions.toPromise
9+
import io.mockk.every
10+
import io.mockk.mockk
11+
import org.assertj.core.api.Assertions.assertThat
12+
import org.junit.jupiter.api.BeforeAll
13+
import org.junit.jupiter.api.Test
14+
15+
class `When the getting the files` {
16+
17+
companion object {
18+
private val libraryId = LibraryId(980)
19+
}
20+
21+
private val services by lazy {
22+
RevisionCachedLibraryFileProvider(
23+
mockk {
24+
every { promiseFiles(libraryId) } returns listOf(
25+
ServiceFile("645"),
26+
ServiceFile("501.5"),
27+
ServiceFile("CUN7AXL"),
28+
).toPromise() andThen listOf(
29+
ServiceFile("R6pgbzfqS9"),
30+
ServiceFile("7Wtg3R3xi7"),
31+
).toPromise()
32+
},
33+
mockk {
34+
every { promiseRevision(libraryId) } returns 993L.toPromise() andThen 993L.toPromise() andThen 781L.toPromise()
35+
},
36+
PermanentCachePolicy,
37+
)
38+
}
39+
40+
private var initialFiles: List<ServiceFile>? = null
41+
private var cachedFiles: List<ServiceFile>? = null
42+
private var filesAfterRevision: List<ServiceFile>? = null
43+
44+
@BeforeAll
45+
fun act() {
46+
initialFiles = services.promiseFiles(libraryId).toExpiringFuture().get()
47+
cachedFiles = services.promiseFiles(libraryId).toExpiringFuture().get()
48+
filesAfterRevision = services.promiseFiles(libraryId).toExpiringFuture().get()
49+
}
50+
51+
@Test
52+
fun `then the initial files are correct`() {
53+
assertThat(initialFiles).isEqualTo(
54+
listOf(
55+
ServiceFile("645"),
56+
ServiceFile("501.5"),
57+
ServiceFile("CUN7AXL"),
58+
)
59+
)
60+
}
61+
62+
@Test
63+
fun `then the cached files are correct`() {
64+
assertThat(cachedFiles).isEqualTo(
65+
listOf(
66+
ServiceFile("645"),
67+
ServiceFile("501.5"),
68+
ServiceFile("CUN7AXL"),
69+
)
70+
)
71+
}
72+
73+
@Test
74+
fun `then the files change after the revision changes`() {
75+
assertThat(filesAfterRevision).isEqualTo(
76+
listOf(
77+
ServiceFile("R6pgbzfqS9"),
78+
ServiceFile("7Wtg3R3xi7"),
79+
)
80+
)
81+
}
82+
}

0 commit comments

Comments
 (0)