Add Store.open() method

It automatically determines whether to use a DiskStore or FlatFileStore.

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 4 years ago
parent 5a20b75f4f
commit 0afc7a563d
  1. 2
      cache/src/main/java/dev/openrs2/cache/DiskStore.kt
  2. 16
      cache/src/main/java/dev/openrs2/cache/Store.kt
  3. 21
      cache/src/test/java/dev/openrs2/cache/StoreTest.kt

@ -462,7 +462,7 @@ public class DiskStore private constructor(
private const val INDEX_BUFFER_SIZE = INDEX_ENTRY_SIZE * 1000 private const val INDEX_BUFFER_SIZE = INDEX_ENTRY_SIZE * 1000
private const val DATA_BUFFER_SIZE = BLOCK_SIZE * 10 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") return root.resolve("main_file_cache.dat2")
} }

@ -5,6 +5,8 @@ import java.io.Closeable
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.io.Flushable import java.io.Flushable
import java.io.IOException 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 * 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. * The maximum length of a group's contents in bytes.
*/ */
public const val MAX_GROUP_SIZE: Int = (1 shl 24) - 1 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)
}
}
} }
} }

@ -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)
}
}
}
Loading…
Cancel
Save