Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Up the max filesize to 25m * Upgrade Kotlin and add mysql-socket-factory * Redistribute the Database files and add JDBCDatabase * Add an option to print the config out * Add an option for the max workerExecuteTime
- Loading branch information
1 parent
05d9b7f
commit 5475ca9
Showing
15 changed files
with
363 additions
and
210 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
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
129 changes: 129 additions & 0 deletions
129
src/main/kotlin/org/abimon/eternalJukebox/data/database/HikariDatabase.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,129 @@ | ||
package org.abimon.eternalJukebox.data.database | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import org.abimon.eternalJukebox.EternalJukebox | ||
import org.abimon.eternalJukebox.objects.ClientInfo | ||
import org.abimon.eternalJukebox.objects.JukeboxAccount | ||
import org.abimon.eternalJukebox.objects.JukeboxInfo | ||
import org.abimon.visi.io.errPrintln | ||
import java.sql.Connection | ||
|
||
abstract class HikariDatabase : IDatabase { | ||
abstract val ds: HikariDataSource | ||
|
||
override fun provideAudioTrackOverride(id: String, clientInfo: ClientInfo?): String? = use { connection -> | ||
val select = connection.prepareStatement("SELECT * FROM overrides WHERE id=?;") | ||
select.setString(1, id) | ||
select.execute() | ||
|
||
val results = select.resultSet | ||
if (results.next()) | ||
return@use results.getString("url") | ||
return@use null | ||
} | ||
|
||
override fun storeAudioTrackOverride(id: String, newURL: String, clientInfo: ClientInfo?) { | ||
use { connection -> | ||
val insert = connection.prepareStatement("INSERT INTO overrides (id, url) VALUES (?, ?) ON DUPLICATE KEY UPDATE url=VALUES(url);") | ||
insert.setString(1, id) | ||
insert.setString(2, newURL) | ||
|
||
insert.execute() | ||
} | ||
} | ||
|
||
override fun provideAccountForID(accountID: String, clientInfo: ClientInfo?): JukeboxAccount? { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun provideAccountForGoogleID(googleID: String, clientInfo: ClientInfo?): JukeboxAccount? { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun storeAccount(clientInfo: ClientInfo?, account: JukeboxAccount) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun providePopularSongs(service: String, count: Int, clientInfo: ClientInfo?): List<JukeboxInfo> { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun makeSongPopular(service: String, id: String, clientInfo: ClientInfo?) { | ||
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun provideShortURL(params: Array<String>, clientInfo: ClientInfo?): String = use { connection -> | ||
val joined = buildString { | ||
for (param in params) { | ||
if (length > 4096) | ||
break | ||
|
||
if (isNotBlank()) | ||
append('&') | ||
|
||
append(param) | ||
} | ||
} | ||
|
||
val select = connection.prepareStatement("SELECT * FROM short_urls WHERE params=?;") | ||
select.setString(1, joined) | ||
select.execute() | ||
|
||
val results = select.resultSet | ||
|
||
if (results.first()) | ||
return@use results.getString("id") | ||
|
||
val id = obtainNewShortID(connection) | ||
|
||
val insert = connection.prepareStatement("INSERT INTO short_urls (id, params) VALUES (?, ?);") | ||
insert.setString(1, id) | ||
insert.setString(2, joined) | ||
insert.execute() | ||
|
||
return@use id | ||
} | ||
|
||
override fun expandShortURL(id: String, clientInfo: ClientInfo?): Array<String>? = use { connection -> | ||
val select = connection.prepareStatement("SELECT * FROM short_urls WHERE id=?;") | ||
select.setString(1, id) | ||
select.execute() | ||
|
||
val results = select.resultSet | ||
if (results.first()) | ||
return@use results.getString("params").split("&").toTypedArray() | ||
|
||
return@use null | ||
} | ||
|
||
fun obtainNewShortID(connection: Connection): String { | ||
val preparedSelect = connection.prepareStatement("SELECT * FROM short_urls WHERE id=?") | ||
val range = (0 until 4) | ||
(0 until 4096).forEach { | ||
val id = buildString { for (i in range) append(EternalJukebox.BASE_64_URL[EternalJukebox.secureRandom.nextInt(64)]) } | ||
preparedSelect.setString(1, id) | ||
preparedSelect.execute() | ||
preparedSelect.resultSet.use { | ||
if (!it.isBeforeFirst) | ||
return@obtainNewShortID id | ||
} | ||
|
||
println("Generated $id, no success") | ||
} | ||
|
||
errPrintln("We've run out of new short IDs to send. This is bad.") | ||
|
||
throw IllegalStateException("Run out of IDs") | ||
} | ||
|
||
infix fun <T> use(op: (Connection) -> T): T = ds.connection.use(op) | ||
|
||
fun initialise() { | ||
use { connection -> | ||
// connection.createStatement().execute("USE $databaseName") | ||
|
||
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS overrides (id VARCHAR(64) PRIMARY KEY NOT NULL, url VARCHAR(8192) NOT NULL);") | ||
connection.createStatement().execute("CREATE TABLE IF NOT EXISTS short_urls (id VARCHAR(16) PRIMARY KEY NOT NULL, params VARCHAR(4096) NOT NULL);") | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/kotlin/org/abimon/eternalJukebox/data/database/IDatabase.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
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/org/abimon/eternalJukebox/data/database/JDBCDatabase.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,31 @@ | ||
package org.abimon.eternalJukebox.data.database | ||
|
||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
|
||
object JDBCDatabase: HikariDatabase() { | ||
override val ds: HikariDataSource | ||
|
||
init { | ||
val config = HikariConfig() | ||
config.jdbcUrl = databaseOptions["jdbcUrl"]?.toString() ?: throw IllegalStateException("jdbcUrl was not provided!") | ||
|
||
config.username = databaseOptions["username"]?.toString() | ||
config.password = databaseOptions["password"]?.toString() | ||
|
||
val cloudSqlInstance = databaseOptions["cloudSqlInstance"]?.toString() | ||
|
||
if(cloudSqlInstance != null) { | ||
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory") | ||
config.addDataSourceProperty("cloudSqlInstance", cloudSqlInstance) | ||
} | ||
|
||
config.addDataSourceProperty("cachePrepStmts", databaseOptions["cachePrepStmts"]?.toString() ?: "true") | ||
config.addDataSourceProperty("prepStmtCacheSize", databaseOptions["prepStmtCacheSize"]?.toString() ?: "250") | ||
config.addDataSourceProperty("prepStmtCacheSqlLimit", databaseOptions["prepStmtCacheSqlLimit"]?.toString() ?: "2048") | ||
|
||
ds = HikariDataSource(config) | ||
|
||
initialise() | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/org/abimon/eternalJukebox/data/storage/IStorage.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
Oops, something went wrong.