Hide implementation under typealias.

Though it is not working for java interop.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-03-18 17:30:55 +01:00
parent 8076956786
commit 72743346dc
8 changed files with 68 additions and 50 deletions

View File

@ -6,6 +6,8 @@ import com.twitter.serial.stream.Serial
import com.twitter.serial.stream.bytebuffer.ByteBufferSerial
import java.io.File
typealias DomainEntitySerializer<T> = Serializer<T>
internal const val STORAGE_DIR_NAME = "persistent_storage"
/**
@ -14,7 +16,7 @@ internal const val STORAGE_DIR_NAME = "persistent_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.
* Look at [org.moire.ultrasonic.cache.serializers] package for available [DomainEntitySerializer]s.
*/
class PermanentFileStorage(
private val directories: Directories,
@ -34,7 +36,7 @@ class PermanentFileStorage(
fun <T> store(
name: String,
objectToStore: T,
objectSerializer: Serializer<T>
objectSerializer: DomainEntitySerializer<T>
) {
val storeFile = getFile(name)
if (!storeFile.exists()) storeFile.createNewFile()
@ -46,7 +48,7 @@ class PermanentFileStorage(
*/
fun <T> load(
name: String,
objectDeserializer: Serializer<T>
objectDeserializer: DomainEntitySerializer<T>
): T? {
val storeFile = getFile(name)
if (!storeFile.exists()) return null

View File

@ -8,14 +8,12 @@ import com.twitter.serial.serializer.SerializationContext
import com.twitter.serial.stream.SerializerDefs
import com.twitter.serial.stream.SerializerInput
import com.twitter.serial.stream.SerializerOutput
import org.moire.ultrasonic.cache.DomainEntitySerializer
import org.moire.ultrasonic.domain.Artist
private const val SERIALIZER_VERSION = 1
/**
* Serializer/deserializer for [Artist] domain entity.
*/
val artistSerializer get() = object : ObjectSerializer<Artist>(SERIALIZER_VERSION) {
private val artistSerializer get() = object : ObjectSerializer<Artist>(SERIALIZER_VERSION) {
override fun serializeObject(
context: SerializationContext,
output: SerializerOutput<out SerializerOutput<*>>,
@ -54,7 +52,14 @@ val artistSerializer get() = object : ObjectSerializer<Artist>(SERIALIZER_VERSIO
}
}
/**
* Serializer/deserializer for [Artist] domain entity.
*/
fun getArtistsSerializer(): DomainEntitySerializer<Artist> = artistSerializer
private val artistListSerializer = CollectionSerializers.getListSerializer(artistSerializer)
/**
* Serializer/deserializer for list of [Artist] domain entities.
*/
val artistListSerializer = CollectionSerializers.getListSerializer(artistSerializer)
fun getArtistListSerializer(): DomainEntitySerializer<List<Artist>> = artistListSerializer

View File

@ -2,28 +2,27 @@
@file:JvmName("DomainSerializers")
package org.moire.ultrasonic.cache.serializers
import com.twitter.serial.serializer.CollectionSerializers
import com.twitter.serial.serializer.ObjectSerializer
import com.twitter.serial.serializer.SerializationContext
import com.twitter.serial.stream.SerializerInput
import com.twitter.serial.stream.SerializerOutput
import org.moire.ultrasonic.cache.DomainEntitySerializer
import org.moire.ultrasonic.domain.Artist
import org.moire.ultrasonic.domain.Indexes
private const val SERIALIZATION_VERSION = 1
val indexesSerializer get() = object : ObjectSerializer<Indexes>(SERIALIZATION_VERSION) {
private val indexesSerializer get() = object : ObjectSerializer<Indexes>(SERIALIZATION_VERSION) {
override fun serializeObject(
context: SerializationContext,
output: SerializerOutput<out SerializerOutput<*>>,
item: Indexes
) {
val artistListSerializer = getArtistListSerializer()
output.writeLong(item.lastModified)
.writeString(item.ignoredArticles)
.writeObject<MutableList<Artist>>(context, item.shortcuts,
CollectionSerializers.getListSerializer(artistSerializer))
.writeObject<MutableList<Artist>>(context, item.artists,
CollectionSerializers.getListSerializer(artistSerializer))
.writeObject<MutableList<Artist>>(context, item.shortcuts, artistListSerializer)
.writeObject<MutableList<Artist>>(context, item.artists, artistListSerializer)
}
override fun deserializeObject(
@ -33,12 +32,17 @@ val indexesSerializer get() = object : ObjectSerializer<Indexes>(SERIALIZATION_V
): Indexes? {
if (versionNumber != SERIALIZATION_VERSION) return null
val artistListDeserializer = getArtistListSerializer()
val lastModified = input.readLong()
val ignoredArticles = input.readString() ?: return null
val shortcutsList = input.readObject(context,
CollectionSerializers.getListSerializer(artistSerializer)) ?: return null
val artistsList = input.readObject(context,
CollectionSerializers.getListSerializer(artistSerializer)) ?: return null
return Indexes(lastModified, ignoredArticles, shortcutsList, artistsList)
val shortcutsList = input.readObject(context, artistListDeserializer) ?: return null
val artistsList = input.readObject(context, artistListDeserializer) ?: return null
return Indexes(lastModified, ignoredArticles, shortcutsList.toMutableList(),
artistsList.toMutableList())
}
}
/**
* Get serializer/deserializer for [Indexes] entity.
*/
fun getIndexesSerializer(): DomainEntitySerializer<Indexes> = indexesSerializer

View File

@ -7,14 +7,12 @@ import com.twitter.serial.serializer.ObjectSerializer
import com.twitter.serial.serializer.SerializationContext
import com.twitter.serial.stream.SerializerInput
import com.twitter.serial.stream.SerializerOutput
import org.moire.ultrasonic.cache.DomainEntitySerializer
import org.moire.ultrasonic.domain.MusicFolder
private const val SERIALIZATION_VERSION = 1
/**
* Serializer/deserializer for [MusicFolder] domain entity.
*/
val musicFolderSerializer = object : ObjectSerializer<MusicFolder>(SERIALIZATION_VERSION) {
private val musicFolderSerializer = object : ObjectSerializer<MusicFolder>(SERIALIZATION_VERSION) {
override fun serializeObject(
context: SerializationContext,
@ -37,7 +35,16 @@ val musicFolderSerializer = object : ObjectSerializer<MusicFolder>(SERIALIZATION
}
}
/**
* Serializer/deserializer for [MusicFolder] domain entity.
*/
fun getMusicFolderSerializer(): DomainEntitySerializer<MusicFolder> = musicFolderSerializer
private val musicFolderListSerializer =
CollectionSerializers.getListSerializer(musicFolderSerializer)
/**
* Serializer/deserializer for [List] of [MusicFolder] items.
*/
val musicFolderListSerializer = CollectionSerializers.getListSerializer(musicFolderSerializer)
fun getMusicFolderListSerializer(): DomainEntitySerializer<List<MusicFolder>> =
musicFolderListSerializer

View File

@ -4,7 +4,7 @@ import org.amshove.kluent.`should contain`
import org.amshove.kluent.`should equal to`
import org.amshove.kluent.`should equal`
import org.junit.Test
import org.moire.ultrasonic.cache.serializers.musicFolderSerializer
import org.moire.ultrasonic.cache.serializers.getMusicFolderSerializer
import org.moire.ultrasonic.domain.MusicFolder
import java.io.File
@ -18,7 +18,7 @@ class PermanentFileStorageTest : BaseStorageTest() {
@Test
fun `Should create storage dir if it is not exist`() {
val item = MusicFolder("1", "2")
storage.store("test", item, musicFolderSerializer)
storage.store("test", item, getMusicFolderSerializer())
storageDir.exists() `should equal to` true
getServerStorageDir().exists() `should equal to` true
@ -29,7 +29,7 @@ class PermanentFileStorageTest : BaseStorageTest() {
val item = MusicFolder("1", "23")
val name = "some-name"
storage.store(name, item, musicFolderSerializer)
storage.store(name, item, getMusicFolderSerializer())
val storageFiles = getServerStorageDir().listFiles()
storageFiles.size `should equal to` 1
@ -40,9 +40,9 @@ class PermanentFileStorageTest : BaseStorageTest() {
fun `Should deserialize stored object`() {
val item = MusicFolder("some", "nice")
val name = "some-name"
storage.store(name, item, musicFolderSerializer)
storage.store(name, item, getMusicFolderSerializer())
val loadedItem = storage.load(name, musicFolderSerializer)
val loadedItem = storage.load(name, getMusicFolderSerializer())
loadedItem `should equal` item
}
@ -52,18 +52,18 @@ class PermanentFileStorageTest : BaseStorageTest() {
val name = "some-nice-name"
val item1 = MusicFolder("1", "1")
val item2 = MusicFolder("2", "2")
storage.store(name, item1, musicFolderSerializer)
storage.store(name, item2, musicFolderSerializer)
storage.store(name, item1, getMusicFolderSerializer())
storage.store(name, item2, getMusicFolderSerializer())
val loadedItem = storage.load(name, musicFolderSerializer)
val loadedItem = storage.load(name, getMusicFolderSerializer())
loadedItem `should equal` item2
}
@Test
fun `Should clear all files when clearAll is called`() {
storage.store("name1", MusicFolder("1", "1"), musicFolderSerializer)
storage.store("name2", MusicFolder("2", "2"), musicFolderSerializer)
storage.store("name1", MusicFolder("1", "1"), getMusicFolderSerializer())
storage.store("name2", MusicFolder("2", "2"), getMusicFolderSerializer())
storage.clearAll()
@ -72,7 +72,7 @@ class PermanentFileStorageTest : BaseStorageTest() {
@Test
fun `Should return null if serialized file not available`() {
val loadedItem = storage.load("some-name", musicFolderSerializer)
val loadedItem = storage.load("some-name", getMusicFolderSerializer())
loadedItem `should equal` null
}

View File

@ -13,7 +13,7 @@ class ArtistSerializerTest : BaseStorageTest() {
fun `Should correctly serialize Artist object`() {
val item = Artist("id", "name", "index", "coverArt", 1, 0)
storage.store("some-name", item, artistSerializer)
storage.store("some-name", item, getArtistsSerializer())
validateSerializedData()
}
@ -22,9 +22,9 @@ class ArtistSerializerTest : BaseStorageTest() {
fun `Should correctly deserialize Artist object`() {
val itemName = "some-name"
val item = Artist("id", "name", "index", "coverArt", null, 0)
storage.store(itemName, item, artistSerializer)
storage.store(itemName, item, getArtistsSerializer())
val loadedItem = storage.load(itemName, artistSerializer)
val loadedItem = storage.load(itemName, getArtistsSerializer())
loadedItem `should equal` item
}
@ -36,7 +36,7 @@ class ArtistSerializerTest : BaseStorageTest() {
Artist(id = "2", name = "some")
)
storage.store("some-name", itemsList, artistListSerializer)
storage.store("some-name", itemsList, getArtistListSerializer())
validateSerializedData()
}
@ -48,9 +48,9 @@ class ArtistSerializerTest : BaseStorageTest() {
Artist(id = "1"),
Artist(id = "2", name = "some")
)
storage.store(name, itemsList, artistListSerializer)
storage.store(name, itemsList, getArtistListSerializer())
val loadedItems = storage.load(name, artistListSerializer)
val loadedItems = storage.load(name, getArtistListSerializer())
loadedItems `should equal` itemsList
}

View File

@ -18,7 +18,7 @@ class IndexesSerializerTest : BaseStorageTest() {
Artist("233", "some")
))
storage.store("some-name", item, indexesSerializer)
storage.store("some-name", item, getIndexesSerializer())
validateSerializedData()
}
@ -31,9 +31,9 @@ class IndexesSerializerTest : BaseStorageTest() {
), mutableListOf(
Artist("233", "some")
))
storage.store(name, item, indexesSerializer)
storage.store(name, item, getIndexesSerializer())
val loadedItem = storage.load(name, indexesSerializer)
val loadedItem = storage.load(name, getIndexesSerializer())
loadedItem `should equal` item
}

View File

@ -13,7 +13,7 @@ class MusicFolderSerializerTest : BaseStorageTest() {
fun `Should correctly serialize MusicFolder object`() {
val item = MusicFolder("Music", "Folder")
storage.store("some-name", item, musicFolderSerializer)
storage.store("some-name", item, getMusicFolderSerializer())
validateSerializedData()
}
@ -22,9 +22,9 @@ class MusicFolderSerializerTest : BaseStorageTest() {
fun `Should correctly deserialize MusicFolder object`() {
val name = "name"
val item = MusicFolder("some", "none")
storage.store(name, item, musicFolderSerializer)
storage.store(name, item, getMusicFolderSerializer())
val loadedItem = storage.load(name, musicFolderSerializer)
val loadedItem = storage.load(name, getMusicFolderSerializer())
loadedItem `should equal` item
}
@ -36,7 +36,7 @@ class MusicFolderSerializerTest : BaseStorageTest() {
MusicFolder("2", "2")
)
storage.store("some-name", itemsList, musicFolderListSerializer)
storage.store("some-name", itemsList, getMusicFolderListSerializer())
validateSerializedData()
}
@ -48,9 +48,9 @@ class MusicFolderSerializerTest : BaseStorageTest() {
MusicFolder("1", "1"),
MusicFolder("2", "2")
)
storage.store(name, itemsList, musicFolderListSerializer)
storage.store(name, itemsList, getMusicFolderListSerializer())
val loadedItem = storage.load(name, musicFolderListSerializer)
val loadedItem = storage.load(name, getMusicFolderListSerializer())
loadedItem `should equal` itemsList
}