Make persistent storage per server base.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-03-12 21:40:55 +01:00
parent fa7b8b1c88
commit 334ffbf5e9
4 changed files with 28 additions and 6 deletions

View File

@ -11,10 +11,14 @@ internal const val STORAGE_DIR_NAME = "persistent_storage"
/**
* Provides access to permanent file based storage.
*
* [serverId] is currently active server. Should be unique per server so stored data will not
* interfere with other server data.
*
* Look at [org.moire.ultrasonic.cache.serializers] package for available [Serializer]s.
*/
class PermanentFileStorage(
private val directories: Directories,
private val serverId: String,
private val debug: Boolean = false
) {
private val serializationContext = object : SerializationContext {
@ -60,7 +64,10 @@ class PermanentFileStorage(
private fun getFile(name: String) = File(getStorageDir(), "$name.ser")
private fun getStorageDir() = File(directories.getInternalDataDir(), STORAGE_DIR_NAME).apply {
if (!exists()) mkdirs()
private fun getStorageDir(): File {
val mainDir = File(directories.getInternalDataDir(), STORAGE_DIR_NAME)
val serverDir = File(mainDir, serverId)
if (!serverDir.exists()) serverDir.mkdirs()
return serverDir
}
}

View File

@ -21,6 +21,8 @@ abstract class BaseStorageTest {
protected lateinit var mockDirectories: Directories
protected lateinit var storage: PermanentFileStorage
open val serverId: String = ""
@Before
fun setUp() {
mockDirectories = mock<Directories> {
@ -28,7 +30,7 @@ abstract class BaseStorageTest {
on { getInternalCacheDir() } `it returns` tempFileRule.newFolder(INTERNAL_CACHE_FOLDER)
on { getExternalCacheDir() } `it returns` tempFileRule.newFolder(EXTERNAL_CACHE_FOLDER)
}
storage = PermanentFileStorage(mockDirectories, true)
storage = PermanentFileStorage(mockDirectories, serverId, true)
}
protected val storageDir get() = File(mockDirectories.getInternalDataDir(), STORAGE_DIR_NAME)

View File

@ -6,17 +6,22 @@ import org.amshove.kluent.`should equal`
import org.junit.Test
import org.moire.ultrasonic.cache.serializers.musicFolderSerializer
import org.moire.ultrasonic.domain.MusicFolder
import java.io.File
/**
* Integration test for [PermanentFileStorage].
*/
class PermanentFileStorageTest : BaseStorageTest() {
override val serverId: String
get() = "some-server-id"
@Test
fun `Should create storage dir if it is not exist`() {
val item = MusicFolder("1", "2")
storage.store("test", item, musicFolderSerializer)
storageDir.exists() `should equal to` true
getServerStorageDir().exists() `should equal to` true
}
@Test
@ -26,7 +31,7 @@ class PermanentFileStorageTest : BaseStorageTest() {
storage.store(name, item, musicFolderSerializer)
val storageFiles = storageDir.listFiles()
val storageFiles = getServerStorageDir().listFiles()
storageFiles.size `should equal to` 1
storageFiles[0].name `should contain` name
}
@ -62,7 +67,7 @@ class PermanentFileStorageTest : BaseStorageTest() {
storage.clearAll()
storageDir.listFiles().size `should equal to` 0
getServerStorageDir().listFiles().size `should equal to` 0
}
@Test
@ -71,4 +76,6 @@ class PermanentFileStorageTest : BaseStorageTest() {
loadedItem `should equal` null
}
private fun getServerStorageDir() = File(storageDir, serverId)
}

View File

@ -114,7 +114,13 @@ public class MusicServiceFactory {
}
private static PermanentFileStorage getPermanentFileStorage(final Context context) {
return new PermanentFileStorage(getDirectories(context), BuildConfig.DEBUG);
final SharedPreferences preferences = Util.getPreferences(context);
int instance = preferences.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
String serverUrl = preferences.getString(
Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
String serverId = String.valueOf(Math.abs((serverUrl + instance).hashCode()));
return new PermanentFileStorage(getDirectories(context), serverId, BuildConfig.DEBUG);
}
private static Directories getDirectories(final Context context) {