Skip to content

Commit

Permalink
Implemented optional debugging would not 100% recommend not quite sur…
Browse files Browse the repository at this point in the history
…e if it works
  • Loading branch information
nateweisz committed Mar 9, 2023
1 parent 90db509 commit 533b9fd
Show file tree
Hide file tree
Showing 36 changed files with 151 additions and 62 deletions.
85 changes: 60 additions & 25 deletions src/main/kotlin/org/hyrical/store/DataStoreController.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -26,11 +26,14 @@ package org.hyrical.store

import com.google.common.collect.HashBasedTable
import org.hyrical.store.connection.DatabaseConnection
import org.hyrical.store.debugging.RepositoryDebuggingHandler
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.Proxy
import java.util.logging.Logger

/**
* The object that handles creating new [Repository]'s and
Expand All @@ -45,7 +48,9 @@ import org.hyrical.store.type.StorageType
class DataStoreController<T : Storable>(
private val type: StorageType,
val classType: Class<T>,
val connection: DatabaseConnection<*, *>?
val connection: DatabaseConnection<*, *>,
private val debug: Boolean,
private val logger: Logger?
) {

init {
Expand Down Expand Up @@ -76,56 +81,86 @@ class DataStoreController<T : Storable>(
* the specified [StorageType]
*
* @param [type] An objects that implements [Storable] (The type of data to be stored)
* @param [connection] the specified [DatabaseConnection] for the right repository
* @param [debug] If we should log debug statistics (defaulted to false)
* @param [logger] The logger to use for debugging only provide if [debug] is true
*
* @see [DataStoreController]
*/
inline fun <reified T : Storable> of(
type: StorageType,
connection: DatabaseConnection<*, *>? = null
connection: DatabaseConnection<*, *>,
debug: Boolean = false,
logger: Logger? = null
): DataStoreController<T> {
return DataStoreController(type, T::class.java, connection)
return DataStoreController(type, T::class.java, connection, debug, logger)
}

@JvmStatic
fun <T : Storable> of(
type: StorageType,
t: Class<T>,
connection: DatabaseConnection<*, *>? = null
connection: DatabaseConnection<*, *>,
debug: Boolean = false,
logger: Logger? = null
): DataStoreController<T> {
return DataStoreController(type, t, connection)
return DataStoreController(type, t, connection, debug, logger)
}
}

// We do this lazy cause maybe they don't use a repo only async or reactive
val repository: Repository<T> by lazy {
type.build(this, connection)
val objType = type.build(this, connection)

if (debug) {
return@lazy Proxy.newProxyInstance(
objType.javaClass.classLoader,
objType.javaClass.interfaces,
RepositoryDebuggingHandler(objType, logger!!)
) as Repository<T>
}

return@lazy objType
}

val asyncRepository: AsyncRepository<T> by lazy {
type.buildAsync(this, connection)
val objType = type.buildAsync(this, connection)

if (debug) {
return@lazy Proxy.newProxyInstance(
objType.javaClass.classLoader,
objType.javaClass.interfaces,
RepositoryDebuggingHandler(objType, logger!!)
) as AsyncRepository<T>
}

return@lazy objType
}

val reactiveRepository: ReactiveRepository<T> by lazy {
type.buildReactive(this, connection)
val objType = type.buildReactive(this, connection)

if (debug) {
return@lazy Proxy.newProxyInstance(
objType.javaClass.classLoader,
objType.javaClass.interfaces,
RepositoryDebuggingHandler(objType, logger!!)
) as ReactiveRepository<T>
}

return@lazy objType
}

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

var directory: String = ""

/**
* Sets the directory to be used when persisting data
* with the [StorageType.FLAT_FILE]
*
* @param [directory] The directory to be used.
*/
fun bindFlatFileDirectory(directory: String) {
if (type == StorageType.FLAT_FILE) {
this.directory = directory
} else {
throw UnsupportedOperationException("You attempted to bind a flat file directory to a non flat file DataStoreController!!")
if (debug) {
return@lazy Proxy.newProxyInstance(
objType.javaClass.classLoader,
objType.javaClass.interfaces,
RepositoryDebuggingHandler(objType, logger!!)
) as CoroutineRepository<T>
}

return@lazy objType
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/hyrical/store/Storable.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package org.hyrical.store.debugging

import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.util.logging.Logger

class RepositoryDebuggingHandler(private val target: Any, private val logger: Logger) : InvocationHandler {

private val methods = mutableMapOf<String, Method>()

init {
for (method in target.javaClass.declaredMethods) {
methods[method.name] = method
}
}
override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any {
method?.let {
val startTime = System.currentTimeMillis()
val result = methods[it.name]?.invoke(target, *(args ?: emptyArray()))

logger.info("It took ${System.currentTimeMillis() - startTime} ms to execute ${it.name} on the ${target.javaClass.simpleName} class.")

return result!!
} ?: throw IllegalArgumentException("Method parameter is null.")
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
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 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,7 +36,7 @@ import java.util.concurrent.CompletableFuture
class AsyncFlatFileRepository<T : Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) :
AsyncRepository<T> {

val file: File = File(controller.directory, controller.classType.simpleName + ".json").also {
val file: File = File(connection.directory, controller.classType.simpleName + ".json").also {
if (!it.exists()) it.createNewFile()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Nathan Weisz
* Copyright (c) 2023 Nathan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,7 +37,7 @@ import java.io.File
class ReactiveFlatFileRepository<T : Storable>(controller: DataStoreController<T>, val connection: FlatFileConnection) :
ReactiveRepository<T> {

val file: File = File(controller.directory, controller.classType.simpleName + ".json").also {
val file: File = File(connection.directory, controller.classType.simpleName + ".json").also {
if (!it.exists()) it.createNewFile()
}

Expand Down
Loading

0 comments on commit 533b9fd

Please sign in to comment.