Skip to content

Commit 6044ded

Browse files
author
Pavlo Nikulin
committed
enable cloud storage prefix
1 parent b7902a3 commit 6044ded

File tree

8 files changed

+95
-15
lines changed

8 files changed

+95
-15
lines changed

core/src/main/kotlin/androidx/build/gradle/core/BuildCacheKey.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ fun BuildCacheKey.blobKey(): String {
3131
// a single `/`.
3232
return hashCode.replace(slashes, "/")
3333
}
34+
35+
fun String.withPrefix(prefix: String?): String {
36+
if (prefix.isNullOrBlank()) return this
37+
38+
val sanitizedPrefix = prefix.trimEnd('/', '\\')
39+
return "$sanitizedPrefix/$this"
40+
}

core/src/main/kotlin/androidx/build/gradle/core/FileSystemStorageService.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,20 @@ import java.nio.file.Files
2626
*/
2727
class FileSystemStorageService(
2828
override val bucketName: String,
29+
private val prefix: String?,
2930
override val isPush: Boolean,
3031
override val isEnabled: Boolean
3132
) : StorageService {
3233

33-
private val location = Files.createTempDirectory("tmp$bucketName").toFile()
34+
private val location: File = createTempDirectory()
35+
36+
private fun createTempDirectory(): File {
37+
val baseDir = prefix?.takeIf { it.isNotBlank() }?.let {
38+
File(it).apply { if (!exists()) mkdirs() }
39+
} ?: File(System.getProperty(JAVA_IO_TMPDIR))
40+
41+
return Files.createTempDirectory(baseDir.toPath(), "tmp$bucketName").toFile()
42+
}
3443

3544
override fun load(cacheKey: String): InputStream? {
3645
if (!isEnabled) {
@@ -84,6 +93,8 @@ class FileSystemStorageService(
8493
}
8594

8695
companion object {
96+
private const val JAVA_IO_TMPDIR = "java.io.tmpdir"
97+
8798
private fun File.deleteRecursively() {
8899
val files = listFiles()
89100
for (file in files) {

core/src/main/kotlin/androidx/build/gradle/core/RemoteGradleBuildCache.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ abstract class RemoteGradleBuildCache : AbstractBuildCache() {
3030
*/
3131
lateinit var bucketName: String
3232

33+
lateinit var prefix: String
34+
3335
/**
3436
* The type of credentials to use to connect to authenticate to your project instance.
3537
*/

core/src/test/kotlin/androidx/build/gradle/core/FileStorageServiceTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ class FileStorageServiceTest {
2424
fun testStoreBlob() {
2525
val storageService = FileSystemStorageService(
2626
bucketName = BUCKET_NAME,
27+
prefix = null,
28+
isPush = true,
29+
isEnabled = true
30+
)
31+
storageService.use {
32+
val cacheKey = "test-store.txt"
33+
val contents = "The quick brown fox jumped over the lazy dog"
34+
val result = storageService.store(cacheKey, contents.toByteArray(Charsets.UTF_8))
35+
assert(result)
36+
}
37+
}
38+
39+
@Test
40+
fun testStoreBlob_withPrefix() {
41+
val storageService = FileSystemStorageService(
42+
bucketName = BUCKET_NAME,
43+
prefix = "test-prefix",
2744
isPush = true,
2845
isEnabled = true
2946
)
@@ -39,6 +56,7 @@ class FileStorageServiceTest {
3956
fun testLoadBlob() {
4057
val storageService = FileSystemStorageService(
4158
bucketName = BUCKET_NAME,
59+
prefix = null,
4260
isPush = true,
4361
isEnabled = true
4462
)
@@ -57,6 +75,7 @@ class FileStorageServiceTest {
5775
fun testStoreBlob_noPushSupport() {
5876
val storageService = FileSystemStorageService(
5977
bucketName = BUCKET_NAME,
78+
prefix = null,
6079
isPush = false,
6180
isEnabled = true
6281
)
@@ -72,6 +91,7 @@ class FileStorageServiceTest {
7291
fun testStoreBlob_disabled() {
7392
val storageService = FileSystemStorageService(
7493
bucketName = BUCKET_NAME,
94+
prefix = null,
7595
isPush = true,
7696
isEnabled = false
7797
)

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheService.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package androidx.build.gradle.gcpbuildcache
1919

2020
import androidx.build.gradle.core.FileSystemStorageService
2121
import androidx.build.gradle.core.blobKey
22+
import androidx.build.gradle.core.withPrefix
2223
import org.gradle.api.logging.Logging
2324
import org.gradle.caching.BuildCacheEntryReader
2425
import org.gradle.caching.BuildCacheEntryWriter
@@ -37,6 +38,7 @@ import java.io.ByteArrayOutputStream
3738
internal class GcpBuildCacheService(
3839
private val projectId: String,
3940
private val bucketName: String,
41+
private val prefix: String?,
4042
gcpCredentials: GcpCredentials,
4143
messageOnAuthenticationFailure: String,
4244
isPush: Boolean,
@@ -46,9 +48,21 @@ internal class GcpBuildCacheService(
4648

4749
private val storageService = if (inTestMode) {
4850
// Use an implementation backed by the File System when in test mode.
49-
FileSystemStorageService(bucketName, isPush, isEnabled)
51+
FileSystemStorageService(
52+
bucketName = bucketName,
53+
prefix = prefix,
54+
isPush = isPush,
55+
isEnabled = isEnabled,
56+
)
5057
} else {
51-
GcpStorageService(projectId, bucketName, gcpCredentials, messageOnAuthenticationFailure, isPush, isEnabled)
58+
GcpStorageService(
59+
projectId = projectId,
60+
bucketName = bucketName,
61+
gcpCredentials = gcpCredentials,
62+
messageOnAuthenticationFailure = messageOnAuthenticationFailure,
63+
isPush = isPush,
64+
isEnabled = isEnabled
65+
)
5266
}
5367

5468
override fun close() {
@@ -57,7 +71,9 @@ internal class GcpBuildCacheService(
5771

5872
override fun load(key: BuildCacheKey, reader: BuildCacheEntryReader): Boolean {
5973
logger.info("Loading ${key.blobKey()}")
60-
val cacheKey = key.blobKey()
74+
val cacheKey = key
75+
.blobKey()
76+
.apply { if (prefix != null) withPrefix(prefix) }
6177
val input = storageService.load(cacheKey) ?: return false
6278
reader.readFrom(input)
6379
return true
@@ -66,7 +82,9 @@ internal class GcpBuildCacheService(
6682
override fun store(key: BuildCacheKey, writer: BuildCacheEntryWriter) {
6783
if (writer.size == 0L) return // do not store empty entries into the cache
6884
logger.info("Storing ${key.blobKey()}")
69-
val cacheKey = key.blobKey()
85+
val cacheKey = key
86+
.blobKey()
87+
.apply { if (prefix != null) withPrefix(prefix) }
7088
val output = ByteArrayOutputStream()
7189
output.use {
7290
writer.writeTo(output)

gcpbuildcache/src/main/kotlin/androidx/build/gradle/gcpbuildcache/GcpBuildCacheServiceFactory.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
3232
.type("GCP-backed")
3333
.config("projectId", buildCache.projectId)
3434
.config("bucketName", buildCache.bucketName)
35+
.config("prefix", buildCache.prefix)
3536
.config("isPushSupported", "${buildCache.isPush}")
3637
.config("isEnabled", "${buildCache.isEnabled}")
3738
.config(
@@ -40,12 +41,13 @@ class GcpBuildCacheServiceFactory : BuildCacheServiceFactory<GcpBuildCache> {
4041
)
4142

4243
val service = GcpBuildCacheService(
43-
buildCache.projectId,
44-
buildCache.bucketName,
45-
buildCache.credentials,
46-
buildCache.messageOnAuthenticationFailure,
47-
buildCache.isPush,
48-
buildCache.isEnabled
44+
projectId = buildCache.projectId,
45+
bucketName = buildCache.bucketName,
46+
prefix = buildCache.prefix,
47+
gcpCredentials = buildCache.credentials,
48+
messageOnAuthenticationFailure = buildCache.messageOnAuthenticationFailure,
49+
isPush = buildCache.isPush,
50+
isEnabled = buildCache.isEnabled
4951
)
5052
service.validateConfiguration()
5153
return service

s3buildcache/src/main/kotlin/androidx/build/gradle/s3buildcache/S3BuildCacheService.kt

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package androidx.build.gradle.s3buildcache
1919

2020
import androidx.build.gradle.core.FileSystemStorageService
2121
import androidx.build.gradle.core.blobKey
22+
import androidx.build.gradle.core.withPrefix
2223
import org.gradle.api.logging.Logging
2324
import org.gradle.caching.BuildCacheEntryReader
2425
import org.gradle.caching.BuildCacheEntryWriter
@@ -42,6 +43,7 @@ class S3BuildCacheService(
4243
credentials: S3Credentials,
4344
region: String,
4445
bucketName: String,
46+
private val prefix: String?,
4547
isPush: Boolean,
4648
isEnabled: Boolean,
4749
reducedRedundancy: Boolean,
@@ -52,23 +54,39 @@ class S3BuildCacheService(
5254
clientOptions(credentials(credentials), region)
5355
}
5456
private val storageService = if (inTestMode) {
55-
FileSystemStorageService(bucketName, isPush, isEnabled)
57+
FileSystemStorageService(
58+
bucketName = bucketName,
59+
prefix = prefix,
60+
isPush = isPush,
61+
isEnabled = isEnabled,
62+
)
5663
} else {
57-
S3StorageService(bucketName, isPush, isEnabled, client, region, reducedRedundancy)
64+
S3StorageService(
65+
bucketName = bucketName,
66+
isPush = isPush,
67+
isEnabled = isEnabled,
68+
client = client,
69+
region = region,
70+
reducedRedundancy = reducedRedundancy
71+
)
5872
}
5973

6074
override fun load(key: BuildCacheKey, reader: BuildCacheEntryReader): Boolean {
6175
logger.info("Loading ${key.blobKey()}")
62-
val cacheKey = key.blobKey()
76+
val cacheKey = key
77+
.blobKey()
78+
.apply { if (prefix != null) withPrefix(prefix) }
6379
val input = storageService.load(cacheKey) ?: return false
6480
reader.readFrom(input)
6581
return true
6682
}
6783

6884
override fun store(key: BuildCacheKey, writer: BuildCacheEntryWriter) {
6985
if (writer.size == 0L) return // do not store empty entries into the cache
86+
val cacheKey = key
87+
.blobKey()
88+
.apply { if (prefix != null) withPrefix(prefix) }
7089
logger.info("Storing ${key.blobKey()}")
71-
val cacheKey = key.blobKey()
7290
val output = ByteArrayOutputStream()
7391
output.use {
7492
writer.writeTo(output)

s3buildcache/src/main/kotlin/androidx/build/gradle/s3buildcache/S3BuildCacheServiceFactory.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class S3BuildCacheServiceFactory : BuildCacheServiceFactory<S3BuildCache> {
3333
.type("AWS-S3-backed")
3434
.config("region", buildCache.region)
3535
.config("bucketName", buildCache.bucketName)
36+
.config("prefix", buildCache.prefix)
3637
.config("reducedRedundancy", "${buildCache.reducedRedundancy}")
3738
.config("isPushSupported", "${buildCache.isPush}")
3839
.config("isEnabled", "${buildCache.isEnabled}")
@@ -41,6 +42,7 @@ class S3BuildCacheServiceFactory : BuildCacheServiceFactory<S3BuildCache> {
4142
val service = S3BuildCacheService(
4243
region = buildCache.region,
4344
bucketName = buildCache.bucketName,
45+
prefix = buildCache.prefix,
4446
isPush = buildCache.isPush,
4547
isEnabled = buildCache.isEnabled,
4648
reducedRedundancy = buildCache.reducedRedundancy,

0 commit comments

Comments
 (0)