Skip to content

Commit

Permalink
feat(repo): CoroutineRepository Addition 🎉 --release
Browse files Browse the repository at this point in the history
  • Loading branch information
nateweisz committed Feb 4, 2023
1 parent c689b42 commit 289779c
Show file tree
Hide file tree
Showing 26 changed files with 405 additions and 45 deletions.
31 changes: 31 additions & 0 deletions .editorconfig
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
4 changes: 3 additions & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -107,4 +104,4 @@ This project is [MIT](https://github.com/Nopock/Store/blob/main/LICENSE) license
***

Database connection system skidded from Growly <3
<p></p>
<p></p>
22 changes: 8 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id("java-library")
}

group = "org.hyrical"
group = "org.hyrical.store"
version = "1.0"


Expand All @@ -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 {
Expand All @@ -44,9 +44,3 @@ publishing {
}
}
}

configurations.all {
resolutionStrategy {
force("com.google.code.gson:gson:2.10.1")
}
}
13 changes: 13 additions & 0 deletions libs.versions.toml
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" }
10 changes: 9 additions & 1 deletion settings.gradle.kts
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"))
}
}
}
7 changes: 5 additions & 2 deletions src/main/kotlin/org/hyrical/store/DataStoreController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +52,10 @@ class DataStoreController<T : Storable>(private val type: StorageType, val class
type.buildReactive(this, connection)
}

val coroutineRepository: CoroutineRepository<T> by lazy {
type.buildCoroutine(this, connection)
}

var directory: String = ""

/**
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/org/hyrical/store/Storable.kt
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hyrical.store.repository

import org.hyrical.store.DataStoreController
import org.hyrical.store.Storable
import java.util.concurrent.CompletableFuture

Expand Down
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>
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/hyrical/store/repository/Repository.kt
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T : Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) : AsyncRepository<T> {

Expand Down
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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<T : Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) : Repository<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -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<T: Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) : ReactiveRepository<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T : Storable>(private val controller: DataStoreController<T>, val connection: MongoConnection) : AsyncRepository<T> {
Expand Down
Loading

0 comments on commit 289779c

Please sign in to comment.