From 0afc7a563d751bf761aff4eeec4c1317cfcdce08 Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 3 Sep 2020 10:47:53 +0100 Subject: [PATCH] Add Store.open() method It automatically determines whether to use a DiskStore or FlatFileStore. Signed-off-by: Graham --- .../main/java/dev/openrs2/cache/DiskStore.kt | 2 +- .../src/main/java/dev/openrs2/cache/Store.kt | 16 ++++++++++++++ .../test/java/dev/openrs2/cache/StoreTest.kt | 21 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cache/src/test/java/dev/openrs2/cache/StoreTest.kt diff --git a/cache/src/main/java/dev/openrs2/cache/DiskStore.kt b/cache/src/main/java/dev/openrs2/cache/DiskStore.kt index 54a8f278de..4a2006e25d 100644 --- a/cache/src/main/java/dev/openrs2/cache/DiskStore.kt +++ b/cache/src/main/java/dev/openrs2/cache/DiskStore.kt @@ -462,7 +462,7 @@ public class DiskStore private constructor( private const val INDEX_BUFFER_SIZE = INDEX_ENTRY_SIZE * 1000 private const val DATA_BUFFER_SIZE = BLOCK_SIZE * 10 - private fun dataPath(root: Path): Path { + internal fun dataPath(root: Path): Path { return root.resolve("main_file_cache.dat2") } diff --git a/cache/src/main/java/dev/openrs2/cache/Store.kt b/cache/src/main/java/dev/openrs2/cache/Store.kt index 3063d4aabd..62e4baafb1 100644 --- a/cache/src/main/java/dev/openrs2/cache/Store.kt +++ b/cache/src/main/java/dev/openrs2/cache/Store.kt @@ -5,6 +5,8 @@ import java.io.Closeable import java.io.FileNotFoundException import java.io.Flushable import java.io.IOException +import java.nio.file.Files +import java.nio.file.Path /** * A low-level interface for reading and writing raw groups directly to and @@ -118,5 +120,19 @@ public interface Store : Flushable, Closeable { * The maximum length of a group's contents in bytes. */ public const val MAX_GROUP_SIZE: Int = (1 shl 24) - 1 + + /** + * Opens a [Store], automatically detecting the type based on the + * presence or absence of the `main_file_cache.dat2` file. + * @param root the store's root directory. + * @throws IOException if an underlying I/O error occurs. + */ + public fun open(root: Path): Store { + return if (Files.isRegularFile(DiskStore.dataPath(root))) { + DiskStore.open(root) + } else { + FlatFileStore.open(root) + } + } } } diff --git a/cache/src/test/java/dev/openrs2/cache/StoreTest.kt b/cache/src/test/java/dev/openrs2/cache/StoreTest.kt new file mode 100644 index 0000000000..1ca84057e3 --- /dev/null +++ b/cache/src/test/java/dev/openrs2/cache/StoreTest.kt @@ -0,0 +1,21 @@ +package dev.openrs2.cache + +import java.nio.file.Paths +import kotlin.test.Test +import kotlin.test.assertTrue + +object StoreTest { + private val DISK_ROOT = Paths.get(StoreTest::class.java.getResource("disk-store/empty").toURI()) + private val FLAT_FILE_ROOT = Paths.get(StoreTest::class.java.getResource("flat-file-store/empty").toURI()) + + @Test + fun testOpen() { + Store.open(DISK_ROOT).use { store -> + assertTrue(store is DiskStore) + } + + Store.open(FLAT_FILE_ROOT).use { store -> + assertTrue(store is FlatFileStore) + } + } +}