diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..6887b9a --- /dev/null +++ b/.editorconfig @@ -0,0 +1,31 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +tab_width = 4 +max_line_length = 120 +insert_final_newline = true + +[*.gradle.kts] +indent_style = tab + +[*.kt] +indent_style = tab +ij_kotlin_name_count_to_use_star_import = 99999 +ij_kotlin_name_count_to_use_star_import_for_members = 99999 + +[*.properties] +indent_style = space +indent_size = 2 + +[*.toml] +indent_style = tab + +[.editorconfig] +indent_style = tab +indent_size = 4 + +[*.sh] +indent_style = tab +indent_size = 4 diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 16d4be0..d1d741b 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -37,7 +37,9 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: body: | - This is an automatic release via `Github Actions`. There will be updated patch notes shortly... + # Changes + - This is an automatic release... + - These notes will be updated shortly :) `build.gradle.kts` ```kts diff --git a/README.md b/README.md index 8779235..9650ce8 100644 --- a/README.md +++ b/README.md @@ -74,12 +74,9 @@ ## Roadmap -- Add support for kotlinx.coroutines - Wrapper for return types -- Add support for FileConnection - Code cleanups - More CI/CD -- Write tests that get ran on commit so that I don't break shit ![Alt](https://repobeats.axiom.co/api/embed/d9732890507abe6f645b1c954e032aea40b39386.svg "Repobeats analytics image") @@ -107,4 +104,4 @@ This project is [MIT](https://github.com/Nopock/Store/blob/main/LICENSE) license *** Database connection system skidded from Growly <3 -

\ No newline at end of file +

diff --git a/build.gradle.kts b/build.gradle.kts index 8983053..8a541cf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { id("java-library") } -group = "org.hyrical" +group = "org.hyrical.store" version = "1.0" @@ -16,13 +16,13 @@ repositories { } dependencies { - compileOnly("redis.clients:jedis:4.3.1") - compileOnly("org.mongodb:mongo-java-driver:3.12.11") - implementation("org.slf4j:slf4j-api:2.0.6") - testImplementation(kotlin("test")) - compileOnly("io.projectreactor:reactor-core:3.5.2") - implementation("com.google.code.gson:gson:2.10.1") - // https://mvnrepository.com/artifact/com.google.code.gson/gson + compileOnly(libs.jedis) + compileOnly(libs.mongo) + compileOnly(libs.reactor.core) + implementation(libs.gson) + compileOnly(libs.coroutines) + + testImplementation(kotlin("test")) } tasks.test { @@ -44,9 +44,3 @@ publishing { } } } - -configurations.all { - resolutionStrategy { - force("com.google.code.gson:gson:2.10.1") - } -} \ No newline at end of file diff --git a/libs.versions.toml b/libs.versions.toml new file mode 100644 index 0000000..7e763af --- /dev/null +++ b/libs.versions.toml @@ -0,0 +1,13 @@ +[versions] +redis = "4.3.1" +mongo = "3.12.11" +reactor-core = "3.5.2" +gson = "2.10.1" +coroutines = "1.5.2" + +[libraries] +jedis = { module = "redis.clients:jedis", version.ref = "redis" } +mongo = { module = "org.mongodb:mongo-java-driver", version.ref = "mongo" } +reactor-core = { module = "io.projectreactor:reactor-core", version.ref = "reactor-core" } +gson = { module = "com.google.code.gson:gson", version.ref = "gson" } +coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } diff --git a/settings.gradle.kts b/settings.gradle.kts index 7559bd3..0c57c4d 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1 +1,9 @@ -rootProject.name = "Store" \ No newline at end of file +rootProject.name = "Store" + +dependencyResolutionManagement { + versionCatalogs { + create("libs") { + from(files("libs.versions.toml")) + } + } +} diff --git a/src/main/kotlin/org/hyrical/store/DataStoreController.kt b/src/main/kotlin/org/hyrical/store/DataStoreController.kt index efcbf9b..b14b1ef 100644 --- a/src/main/kotlin/org/hyrical/store/DataStoreController.kt +++ b/src/main/kotlin/org/hyrical/store/DataStoreController.kt @@ -2,11 +2,10 @@ package org.hyrical.store import org.hyrical.store.connection.DatabaseConnection import org.hyrical.store.repository.AsyncRepository +import org.hyrical.store.repository.CoroutineRepository import org.hyrical.store.repository.ReactiveRepository import org.hyrical.store.repository.Repository import org.hyrical.store.type.StorageType -import java.lang.reflect.ParameterizedType -import java.util.UUID /** * The object that handles creating new [Repository]'s and @@ -53,6 +52,10 @@ class DataStoreController(private val type: StorageType, val class type.buildReactive(this, connection) } + val coroutineRepository: CoroutineRepository by lazy { + type.buildCoroutine(this, connection) + } + var directory: String = "" /** diff --git a/src/main/kotlin/org/hyrical/store/Storable.kt b/src/main/kotlin/org/hyrical/store/Storable.kt index 4c3cbc0..576f6b6 100644 --- a/src/main/kotlin/org/hyrical/store/Storable.kt +++ b/src/main/kotlin/org/hyrical/store/Storable.kt @@ -1,7 +1,5 @@ package org.hyrical.store -import com.google.gson.annotations.SerializedName - /** * The base interface that all classes stored by [DataStoreController]'s * must implement. diff --git a/src/main/kotlin/org/hyrical/store/connection/flatfile/FlatFileConnection.kt b/src/main/kotlin/org/hyrical/store/connection/flatfile/FlatFileConnection.kt index 2c4c527..0edf08b 100644 --- a/src/main/kotlin/org/hyrical/store/connection/flatfile/FlatFileConnection.kt +++ b/src/main/kotlin/org/hyrical/store/connection/flatfile/FlatFileConnection.kt @@ -1,6 +1,5 @@ package org.hyrical.store.connection.flatfile -import com.mongodb.client.MongoDatabase import org.hyrical.store.connection.DatabaseConnection import java.io.File diff --git a/src/main/kotlin/org/hyrical/store/repository/AsyncRepository.kt b/src/main/kotlin/org/hyrical/store/repository/AsyncRepository.kt index 80d7f55..14e8073 100644 --- a/src/main/kotlin/org/hyrical/store/repository/AsyncRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/AsyncRepository.kt @@ -1,5 +1,6 @@ package org.hyrical.store.repository +import org.hyrical.store.DataStoreController import org.hyrical.store.Storable import java.util.concurrent.CompletableFuture diff --git a/src/main/kotlin/org/hyrical/store/repository/CoroutineRepository.kt b/src/main/kotlin/org/hyrical/store/repository/CoroutineRepository.kt new file mode 100644 index 0000000..98ad8cb --- /dev/null +++ b/src/main/kotlin/org/hyrical/store/repository/CoroutineRepository.kt @@ -0,0 +1,50 @@ +package org.hyrical.store.repository + +import org.hyrical.store.DataStoreController +import org.hyrical.store.Storable + +/** + * The base repository for all [Storable] objects, + * initiated by a [DataStoreController] + * + * @author Nopox + * @since 2/3/23 + */ +interface CoroutineRepository { + + /** + * @param [id] The ID of the [T] object that you are searching for. + * + * @return [T?] The [T] object if found else null. + */ + suspend fun search(id: String): T? + + /** + * @param [t] The object to save. + * + * @return [T] The object saved. + */ + suspend fun save(t: T): T + + /** + * @param [id] The ID of the [T] object to delete. + */ + suspend fun delete(id: String) + + /** + * @param [objects] A vararg of [T]'s that need to be saved. + * + * @return [List] A list of the objects saved. + */ + suspend fun saveMany(vararg objects: T): List + + /** + * @param [keys] A vararg of keys/ids that will be deleted. + */ + suspend fun deleteMany(vararg keys: String) + + /** + * @return [List] A list of all the objects in the repository. + */ + suspend fun findAll(): List +} \ No newline at end of file diff --git a/src/main/kotlin/org/hyrical/store/repository/ReactiveRepository.kt b/src/main/kotlin/org/hyrical/store/repository/ReactiveRepository.kt index 2626f64..53a43b2 100644 --- a/src/main/kotlin/org/hyrical/store/repository/ReactiveRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/ReactiveRepository.kt @@ -1,5 +1,6 @@ package org.hyrical.store.repository +import org.hyrical.store.DataStoreController import org.hyrical.store.Storable import reactor.core.publisher.Flux import reactor.core.publisher.Mono diff --git a/src/main/kotlin/org/hyrical/store/repository/Repository.kt b/src/main/kotlin/org/hyrical/store/repository/Repository.kt index e454ee3..0990882 100644 --- a/src/main/kotlin/org/hyrical/store/repository/Repository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/Repository.kt @@ -1,7 +1,7 @@ package org.hyrical.store.repository -import org.hyrical.store.Storable import org.hyrical.store.DataStoreController +import org.hyrical.store.Storable /** * The base repository for all [Storable] objects, diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/AsyncFlatFileRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/AsyncFlatFileRepository.kt index bb5db2c..586d827 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/AsyncFlatFileRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/AsyncFlatFileRepository.kt @@ -1,15 +1,13 @@ package org.hyrical.store.repository.impl.flatfile +import com.google.gson.reflect.TypeToken import org.hyrical.store.DataStoreController import org.hyrical.store.Storable +import org.hyrical.store.connection.flatfile.FlatFileConnection import org.hyrical.store.repository.AsyncRepository import org.hyrical.store.serializers.Serializers import java.io.File -import java.io.FileReader -import java.util.ArrayList import java.util.concurrent.CompletableFuture -import com.google.gson.reflect.TypeToken -import org.hyrical.store.connection.flatfile.FlatFileConnection class AsyncFlatFileRepository(controller: DataStoreController, val connection: FlatFileConnection) : AsyncRepository { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/CoroutineFlatFileRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/CoroutineFlatFileRepository.kt new file mode 100644 index 0000000..c3c0e38 --- /dev/null +++ b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/CoroutineFlatFileRepository.kt @@ -0,0 +1,76 @@ +package org.hyrical.store.repository.impl.flatfile + +import com.google.gson.reflect.TypeToken +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.hyrical.store.DataStoreController +import org.hyrical.store.Storable +import org.hyrical.store.connection.flatfile.FlatFileConnection +import org.hyrical.store.repository.CoroutineRepository +import org.hyrical.store.serializers.Serializers + +class CoroutineFlatFileRepository(controller: DataStoreController, val connection: FlatFileConnection) : CoroutineRepository { + + val cache = mutableMapOf() + + init { + // Read the file and deserialize the contents into the cache map + val jsonString = connection.useResourceWithReturn { + readText() + } + val type = TypeToken.getParameterized(ArrayList::class.java, controller.classType).type + val objects = Serializers.activeSerializer.deserialize>(jsonString, type) + objects?.forEach { obj -> cache[obj.identifier] = obj } + } + + override suspend fun search(id: String): T? { + return cache[id] + } + + override suspend fun save(t: T): T { + return withContext(Dispatchers.IO) { + cache[t.identifier] = t + persistToFile() + t + } + } + + override suspend fun delete(id: String) { + withContext(Dispatchers.IO) { + cache.remove(id) + persistToFile() + } + } + + override suspend fun saveMany(vararg objects: T): List { + return withContext(Dispatchers.IO) { + objects.forEach { obj -> cache[obj.identifier] = obj } + persistToFile() + objects.toList() + } + } + + override suspend fun deleteMany(vararg keys: String) { + withContext(Dispatchers.IO) { + keys.forEach { key -> cache.remove(key) } + persistToFile() + } + } + + override suspend fun findAll(): List { + return withContext(Dispatchers.IO) { + cache.values.toList() + } + } + + private fun persistToFile() { + // Serialize the cache map and write it to the file + val jsonString = Serializers.activeSerializer.serialize(cache.values) + + connection.useResource { + if (jsonString != null) { + writeText(jsonString) + } + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/FlatFileRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/FlatFileRepository.kt index d531790..d93ec3a 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/FlatFileRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/FlatFileRepository.kt @@ -1,13 +1,11 @@ package org.hyrical.store.repository.impl.flatfile +import com.google.gson.reflect.TypeToken import org.hyrical.store.DataStoreController import org.hyrical.store.Storable +import org.hyrical.store.connection.flatfile.FlatFileConnection import org.hyrical.store.repository.Repository import org.hyrical.store.serializers.Serializers -import java.io.File -import java.util.ArrayList -import com.google.gson.reflect.TypeToken -import org.hyrical.store.connection.flatfile.FlatFileConnection class FlatFileRepository(controller: DataStoreController, val connection: FlatFileConnection) : Repository { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/ReactiveFlatFileRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/ReactiveFlatFileRepository.kt index d7c1eed..af289d7 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/ReactiveFlatFileRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/flatfile/ReactiveFlatFileRepository.kt @@ -1,16 +1,14 @@ package org.hyrical.store.repository.impl.flatfile +import com.google.gson.reflect.TypeToken import org.hyrical.store.DataStoreController import org.hyrical.store.Storable +import org.hyrical.store.connection.flatfile.FlatFileConnection import org.hyrical.store.repository.ReactiveRepository import org.hyrical.store.serializers.Serializers import reactor.core.publisher.Flux import reactor.core.publisher.Mono import java.io.File -import java.io.FileReader -import java.util.ArrayList -import com.google.gson.reflect.TypeToken -import org.hyrical.store.connection.flatfile.FlatFileConnection class ReactiveFlatFileRepository(controller: DataStoreController, val connection: FlatFileConnection) : ReactiveRepository { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/AsyncMongoRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/AsyncMongoRepository.kt index 93875ed..acc33da 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/AsyncMongoRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/AsyncMongoRepository.kt @@ -8,7 +8,6 @@ import org.hyrical.store.Storable import org.hyrical.store.connection.mongo.MongoConnection import org.hyrical.store.repository.AsyncRepository import org.hyrical.store.serializers.Serializers -import java.lang.UnsupportedOperationException import java.util.concurrent.CompletableFuture class AsyncMongoRepository(private val controller: DataStoreController, val connection: MongoConnection) : AsyncRepository { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/CoroutineMongoRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/CoroutineMongoRepository.kt new file mode 100644 index 0000000..8b4025a --- /dev/null +++ b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/CoroutineMongoRepository.kt @@ -0,0 +1,82 @@ +package org.hyrical.store.repository.impl.mongodb + +import com.mongodb.client.MongoCollection +import com.mongodb.client.model.Filters +import com.mongodb.client.model.UpdateOptions +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.bson.Document +import org.hyrical.store.DataStoreController +import org.hyrical.store.Storable +import org.hyrical.store.connection.mongo.MongoConnection +import org.hyrical.store.repository.CoroutineRepository +import org.hyrical.store.serializers.Serializers + +class CoroutineMongoRepository(private val controller: DataStoreController, val connection: MongoConnection) : CoroutineRepository { + + private val collection: MongoCollection = connection.useResourceWithReturn { + this.getCollection(controller.classType.simpleName) + } ?: throw UnsupportedOperationException("You did not provide a mongodatabase connection when initiating the owning DataStoreContrller.") + + /** + * @param [id] The ID of the [T] object that you are searching for. + * + * @return [T?] The [T] object if found else null. + */ + override suspend fun search(id: String): T? { + return withContext(Dispatchers.IO) { + Serializers.activeSerializer.deserialize(collection.find(Filters.eq("_id", id)).first()?.toJson(), controller.classType) + } + } + + /** + * @param [id] The ID of the [T] object to delete. + */ + override suspend fun delete(id: String) { + withContext(Dispatchers.IO) { + collection.deleteOne(Filters.eq("_id", id)) + } + } + + /** + * @param [keys] A vararg of keys/ids that will be deleted. + */ + override suspend fun deleteMany(vararg keys: String) { + withContext(Dispatchers.IO) { + collection.deleteMany(Filters.`in`("_id", keys)) + } + } + + /** + * @return [List] A list of all the objects in the repository. + */ + override suspend fun findAll(): List { + return withContext(Dispatchers.IO) { + collection.find().map { Serializers.activeSerializer.deserialize(it.toJson(), controller.classType)!! } + .toList() + } + } + + /** + * @return [List] A list of all the objects in the repository. + */ + override suspend fun saveMany(vararg objects: T): List { + return withContext(Dispatchers.IO) { + return@withContext objects.toList().onEach { + save(it) + } + } + } + + /** + * @param [t] The object to save. + * + * @return [T] The object saved. + */ + override suspend fun save(t: T): T { + return withContext(Dispatchers.IO) { + collection.updateOne(Filters.eq("_id", t.identifier), Document("\$set", Document.parse(Serializers.activeSerializer.serialize(t))), UpdateOptions().upsert(true)) + return@withContext t + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/MongoRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/MongoRepository.kt index ef3d72c..95cc98f 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/MongoRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/MongoRepository.kt @@ -9,7 +9,6 @@ import org.hyrical.store.Storable import org.hyrical.store.connection.mongo.MongoConnection import org.hyrical.store.repository.Repository import org.hyrical.store.serializers.Serializers -import java.lang.UnsupportedOperationException class MongoRepository(private val controller: DataStoreController, val connection: MongoConnection) : Repository { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/ReactiveMongoRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/ReactiveMongoRepository.kt index ef91846..2a18252 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/ReactiveMongoRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/mongodb/ReactiveMongoRepository.kt @@ -11,7 +11,6 @@ import org.hyrical.store.repository.ReactiveRepository import org.hyrical.store.serializers.Serializers import reactor.core.publisher.Flux import reactor.core.publisher.Mono -import java.lang.UnsupportedOperationException class ReactiveMongoRepository(private val controller: DataStoreController, val connection: MongoConnection) : ReactiveRepository { val collection: MongoCollection = connection.useResourceWithReturn { diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/redis/CoroutineRedisRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/redis/CoroutineRedisRepository.kt new file mode 100644 index 0000000..030fed9 --- /dev/null +++ b/src/main/kotlin/org/hyrical/store/repository/impl/redis/CoroutineRedisRepository.kt @@ -0,0 +1,90 @@ +package org.hyrical.store.repository.impl.redis + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.hyrical.store.DataStoreController +import org.hyrical.store.Storable +import org.hyrical.store.connection.redis.RedisConnection +import org.hyrical.store.repository.CoroutineRepository +import org.hyrical.store.serializers.Serializers + +class CoroutineRedisRepository(private val controller: DataStoreController, val connection: RedisConnection) : CoroutineRepository { + + private val serializer = Serializers.activeSerializer + + private val id = controller.classType.simpleName + + /** + * @param [id] The ID of the [T] object that you are searching for. + * + * @return [T?] The [T] object if found else null. + */ + override suspend fun search(id: String): T? { + return withContext(Dispatchers.IO){ + connection.useResourceWithReturn { + serializer.deserialize(hget(this@CoroutineRedisRepository.id, id), controller.classType) + } + } + } + + /** + * @param [id] The ID of the [T] object to delete. + */ + override suspend fun delete(id: String) { + return withContext(Dispatchers.IO){ + connection.useResource { + hdel(this@CoroutineRedisRepository.id, id) + } + } + } + + /** + * @param [keys] A vararg of keys/ids that will be deleted. + */ + override suspend fun deleteMany(vararg keys: String) { + return withContext(Dispatchers.IO) { + connection.useResource { + hdel(this@CoroutineRedisRepository.id, *keys) + } + } + } + + /** + * @return [List] A list of all the objects in the repository. + */ + override suspend fun findAll(): List { + return withContext(Dispatchers.IO) { + connection.useResourceWithReturn { + hgetAll(this@CoroutineRedisRepository.id).values.map { serializer.deserialize(it, controller.classType)!! } + }!! + } + } + + /** + * @param [objects] A vararg of [T]'s that need to be saved. + * + * @return [List] A list of the objects saved. + */ + override suspend fun saveMany(vararg objects: T): List { + return withContext(Dispatchers.IO) { + connection.useResource { + hmset(this@CoroutineRedisRepository.id, objects.associate { it.identifier to serializer.serialize(it) }) + } + objects.toList() + } + } + + /** + * @param [t] The object to save. + * + * @return [T] The object saved. + */ + override suspend fun save(t: T): T { + return withContext(Dispatchers.IO) { + connection.useResource { + hset(this@CoroutineRedisRepository.id, t.identifier, serializer.serialize(t)) + } + t + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/org/hyrical/store/repository/impl/redis/ReactiveRedisRepository.kt b/src/main/kotlin/org/hyrical/store/repository/impl/redis/ReactiveRedisRepository.kt index 5a18f86..82cd7b8 100644 --- a/src/main/kotlin/org/hyrical/store/repository/impl/redis/ReactiveRedisRepository.kt +++ b/src/main/kotlin/org/hyrical/store/repository/impl/redis/ReactiveRedisRepository.kt @@ -3,10 +3,10 @@ package org.hyrical.store.repository.impl.redis import org.hyrical.store.DataStoreController import org.hyrical.store.Storable import org.hyrical.store.connection.redis.RedisConnection +import org.hyrical.store.repository.ReactiveRepository import org.hyrical.store.serializers.Serializers import reactor.core.publisher.Flux import reactor.core.publisher.Mono -import org.hyrical.store.repository.ReactiveRepository class ReactiveRedisRepository(private val controller: DataStoreController, val connection: RedisConnection) : ReactiveRepository { diff --git a/src/main/kotlin/org/hyrical/store/serializers/impl/GsonSerializer.kt b/src/main/kotlin/org/hyrical/store/serializers/impl/GsonSerializer.kt index ded1805..16dc9ec 100644 --- a/src/main/kotlin/org/hyrical/store/serializers/impl/GsonSerializer.kt +++ b/src/main/kotlin/org/hyrical/store/serializers/impl/GsonSerializer.kt @@ -1,5 +1,6 @@ package org.hyrical.store.serializers.impl +import com.google.gson.Gson import com.google.gson.GsonBuilder import com.google.gson.LongSerializationPolicy import org.hyrical.store.serializers.Serializer @@ -7,7 +8,7 @@ import java.lang.reflect.Type class GsonSerializer : Serializer() { - val gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls().create() + private val gson: Gson = GsonBuilder().setLongSerializationPolicy(LongSerializationPolicy.STRING).serializeNulls().create() override fun deserialize(json: String?, type: Class): T? { if (json == null) return null diff --git a/src/main/kotlin/org/hyrical/store/type/StorageType.kt b/src/main/kotlin/org/hyrical/store/type/StorageType.kt index 72fe0e8..f29f741 100644 --- a/src/main/kotlin/org/hyrical/store/type/StorageType.kt +++ b/src/main/kotlin/org/hyrical/store/type/StorageType.kt @@ -7,17 +7,21 @@ import org.hyrical.store.connection.flatfile.FlatFileConnection import org.hyrical.store.connection.mongo.MongoConnection import org.hyrical.store.connection.redis.RedisConnection import org.hyrical.store.repository.AsyncRepository +import org.hyrical.store.repository.CoroutineRepository import org.hyrical.store.repository.ReactiveRepository import org.hyrical.store.repository.Repository import org.hyrical.store.repository.impl.flatfile.AsyncFlatFileRepository +import org.hyrical.store.repository.impl.flatfile.CoroutineFlatFileRepository import org.hyrical.store.repository.impl.flatfile.FlatFileRepository import org.hyrical.store.repository.impl.flatfile.ReactiveFlatFileRepository import org.hyrical.store.repository.impl.mongodb.AsyncMongoRepository +import org.hyrical.store.repository.impl.mongodb.CoroutineMongoRepository import org.hyrical.store.repository.impl.mongodb.MongoRepository import org.hyrical.store.repository.impl.mongodb.ReactiveMongoRepository import org.hyrical.store.repository.impl.redis.AsyncRedisRepository -import org.hyrical.store.repository.impl.redis.RedisRepository +import org.hyrical.store.repository.impl.redis.CoroutineRedisRepository import org.hyrical.store.repository.impl.redis.ReactiveRedisRepository +import org.hyrical.store.repository.impl.redis.RedisRepository /** * The type of storage to be used whilst persisting data. @@ -39,6 +43,10 @@ enum class StorageType { override fun buildReactive(controller: DataStoreController, connection: DatabaseConnection<*, *>?): ReactiveRepository { return ReactiveMongoRepository(controller, connection as MongoConnection) } + + override fun buildCoroutine(controller: DataStoreController, connection: DatabaseConnection<*, *>?): CoroutineRepository { + return CoroutineMongoRepository(controller, connection as MongoConnection) + } }, REDIS() { @@ -53,6 +61,10 @@ enum class StorageType { override fun buildReactive(controller: DataStoreController, connection: DatabaseConnection<*, *>?): ReactiveRepository { return ReactiveRedisRepository(controller, connection as RedisConnection) } + + override fun buildCoroutine(controller: DataStoreController, connection: DatabaseConnection<*, *>?): CoroutineRepository { + return CoroutineRedisRepository(controller, connection as RedisConnection) + } }, FLAT_FILE() { @@ -67,6 +79,10 @@ enum class StorageType { override fun buildReactive(controller: DataStoreController, connection: DatabaseConnection<*, *>?): ReactiveRepository { return ReactiveFlatFileRepository(controller, connection as FlatFileConnection) } + + override fun buildCoroutine(controller: DataStoreController, connection: DatabaseConnection<*, *>?): CoroutineRepository { + return CoroutineFlatFileRepository(controller, connection as FlatFileConnection) + } }; /** @@ -89,4 +105,11 @@ enum class StorageType { * @param [controller] The owning [DataStoreController] */ abstract fun buildReactive(controller: DataStoreController, connection: DatabaseConnection<*, *>?): ReactiveRepository + + /** + * Builds and initiates the [ReactiveRepository] + * + * @param [controller] The owning [DataStoreController] + */ + abstract fun buildCoroutine(controller: DataStoreController, connection: DatabaseConnection<*, *>?): CoroutineRepository } \ No newline at end of file diff --git a/src/test/kotlin/org/hyrical/store/tests/obj/UserTest.kt b/src/test/kotlin/org/hyrical/store/tests/obj/UserTest.kt index f6db84f..8d38f1b 100644 --- a/src/test/kotlin/org/hyrical/store/tests/obj/UserTest.kt +++ b/src/test/kotlin/org/hyrical/store/tests/obj/UserTest.kt @@ -1,7 +1,7 @@ package org.hyrical.store.tests.obj import org.hyrical.store.Storable -import java.util.UUID +import java.util.* data class UserTest( override val identifier: String,