-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repo):
CoroutineRepository
Addition 🎉 --release
- Loading branch information
Showing
26 changed files
with
405 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
rootProject.name = "Store" | ||
rootProject.name = "Store" | ||
|
||
dependencyResolutionManagement { | ||
versionCatalogs { | ||
create("libs") { | ||
from(files("libs.versions.toml")) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
src/main/kotlin/org/hyrical/store/connection/flatfile/FlatFileConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/kotlin/org/hyrical/store/repository/AsyncRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/org/hyrical/store/repository/CoroutineRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T : Storable> { | ||
|
||
/** | ||
* @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<T>] A list of the objects saved. | ||
*/ | ||
suspend fun saveMany(vararg objects: T): List<T> | ||
|
||
/** | ||
* @param [keys] A vararg of keys/ids that will be deleted. | ||
*/ | ||
suspend fun deleteMany(vararg keys: String) | ||
|
||
/** | ||
* @return [List<T>] A list of all the objects in the repository. | ||
*/ | ||
suspend fun findAll(): List<T> | ||
} |
1 change: 1 addition & 0 deletions
1
src/main/kotlin/org/hyrical/store/repository/ReactiveRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/main/kotlin/org/hyrical/store/repository/impl/flatfile/AsyncFlatFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/kotlin/org/hyrical/store/repository/impl/flatfile/CoroutineFlatFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T : Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) : CoroutineRepository<T> { | ||
|
||
val cache = mutableMapOf<String, T>() | ||
|
||
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<ArrayList<T>>(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<T> { | ||
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<T> { | ||
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) | ||
} | ||
} | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
src/main/kotlin/org/hyrical/store/repository/impl/flatfile/FlatFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
src/main/kotlin/org/hyrical/store/repository/impl/flatfile/ReactiveFlatFileRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.