diff --git a/src/main/kotlin/net/taehui/twilight/system/DB.kt b/src/main/kotlin/net/taehui/twilight/system/DB.kt index 5e51023..f530c7d 100644 --- a/src/main/kotlin/net/taehui/twilight/system/DB.kt +++ b/src/main/kotlin/net/taehui/twilight/system/DB.kt @@ -644,73 +644,71 @@ fun learnComments(futureLearnCommentsStatus: AtomicInteger): CompletableFuture { return logFuture { - Files.newDirectoryStream(TwilightComponent.COMMENT_ENTRY_PATH).use { commentEntryPaths -> - StreamSupport.stream(commentEntryPaths.spliterator(), true).use { parallel -> - parallel.forEach { commentEntryPath -> - pool.connection.use { - Files.list(commentEntryPath).use { commentFilePaths -> - commentFilePaths.forEach { commentFilePath -> - try { - val commentID = - FilenameUtils.removeExtension(commentFilePath.fileName.toString()) - it.prepareStatement( - """ - SELECT Audio_Multiplier - FROM tw_comment - WHERE Comment_ID = ? - """.trimIndent() - ).use { dbStatement -> - dbStatement.setString(1, commentID) - dbStatement.executeQuery().use { rows -> - if (rows.next()) { - val audioMultiplier = rows.getDouble("Audio_Multiplier") - val comment = - XZCompressorInputStream( - Files.readAllBytes(commentFilePath).inputStream() - ).use { - CommentOuterClass.Comment.parseFrom(it) - } - if (comment.audioMultipliersCount > 0) { - it.prepareStatement( - """ - UPDATE tw_comment - SET Lowest_Audio_Multiplier = ?, Highest_Audio_Multiplier = ? - WHERE Comment_ID = ? - """.trimIndent() - ).use { dbStatement -> - dbStatement.setDouble( - 1, - comment.audioMultipliersList.map { it.audioMultiplier } - .plus(audioMultiplier).min() - ) - dbStatement.setDouble( - 2, - comment.audioMultipliersList.map { it.audioMultiplier } - .plus(audioMultiplier).max() - ) - dbStatement.setString(3, commentID) - dbStatement.executeUpdate() - } - } else { - it.prepareStatement( - """ - UPDATE tw_comment - SET Lowest_Audio_Multiplier = Audio_Multiplier, Highest_Audio_Multiplier = Audio_Multiplier - WHERE Comment_ID = ? - """.trimIndent() - ).use { dbStatement -> - dbStatement.setString(1, commentID) - dbStatement.executeUpdate() - } + pool.connection.use { + Files.newDirectoryStream(TwilightComponent.COMMENT_ENTRY_PATH).use { commentEntryPaths -> + commentEntryPaths.forEach { commentEntryPath -> + Files.list(commentEntryPath).use { commentFilePaths -> + commentFilePaths.forEach { commentFilePath -> + try { + val commentID = + FilenameUtils.removeExtension(commentFilePath.fileName.toString()) + it.prepareStatement( + """ + SELECT Audio_Multiplier + FROM tw_comment + WHERE Comment_ID = ? + """.trimIndent() + ).use { dbStatement -> + dbStatement.setString(1, commentID) + dbStatement.executeQuery().use { rows -> + if (rows.next()) { + val audioMultiplier = rows.getDouble("Audio_Multiplier") + val comment = + XZCompressorInputStream( + Files.readAllBytes(commentFilePath).inputStream() + ).use { + CommentOuterClass.Comment.parseFrom(it) + } + if (comment.audioMultipliersCount > 0) { + it.prepareStatement( + """ + UPDATE tw_comment + SET Lowest_Audio_Multiplier = ?, Highest_Audio_Multiplier = ? + WHERE Comment_ID = ? + """.trimIndent() + ).use { dbStatement -> + dbStatement.setDouble( + 1, + comment.audioMultipliersList.map { it.audioMultiplier } + .plus(audioMultiplier).min() + ) + dbStatement.setDouble( + 2, + comment.audioMultipliersList.map { it.audioMultiplier } + .plus(audioMultiplier).max() + ) + dbStatement.setString(3, commentID) + dbStatement.executeUpdate() + } + } else { + it.prepareStatement( + """ + UPDATE tw_comment + SET Lowest_Audio_Multiplier = Audio_Multiplier, Highest_Audio_Multiplier = Audio_Multiplier + WHERE Comment_ID = ? + """.trimIndent() + ).use { dbStatement -> + dbStatement.setString(1, commentID) + dbStatement.executeUpdate() } } } } - } catch (e: Throwable) { - logFault(e) } - futureLearnCommentsStatus.incrementAndGet() + } catch (e: Throwable) { + logFault(e) } + futureLearnCommentsStatus.incrementAndGet() } } } @@ -1082,8 +1080,8 @@ } } - fun setBundle(qwilightSetBundle: JSON.QwilightSetBundle, avatarID: String) { - logValueFuture { + fun setBundle(qwilightSetBundle: JSON.QwilightSetBundle, avatarID: String): CompletableFuture { + return logFuture { pool.connection.use { it.prepareStatement( """ @@ -1148,8 +1146,8 @@ } } - fun setConfigure(qwilightSetConfigure: JSON.QwilightSetConfigure, avatarID: String) { - logValueFuture { + fun setConfigure(qwilightSetConfigure: JSON.QwilightSetConfigure, avatarID: String): CompletableFuture { + return logFuture { pool.connection.use { it.prepareStatement( """ @@ -1169,8 +1167,8 @@ } } - fun setLastDate(avatarID: String) { - logValueFuture { + fun setLastDate(avatarID: String): CompletableFuture { + return logFuture { pool.connection.use { it.prepareStatement( """ @@ -1544,7 +1542,7 @@ } fun getLastDate(avatarID: String): Long { - pool.connection.use { dbComponent -> + return pool.connection.use { dbComponent -> dbComponent.prepareStatement( """ SELECT Last_Date @@ -1554,7 +1552,7 @@ ).use { dbStatement -> dbStatement.setString(1, avatarID) dbStatement.executeQuery().use { - return (if (it.next()) it.getTimestamp("Last_Date") + (if (it.next()) it.getTimestamp("Last_Date") .toInstant() else Instant.now()).toEpochMilli() } } @@ -1710,7 +1708,7 @@ } fun isCrossUbuntu(avatarID: String, ubuntuID: String): Boolean { - pool.connection.use { + return pool.connection.use { it.prepareStatement( """ SELECT * @@ -1720,13 +1718,13 @@ ).use { dbStatement -> dbStatement.setString(1, ubuntuID) dbStatement.setString(2, avatarID) - dbStatement.executeQuery().use { rows -> return rows.next() } + dbStatement.executeQuery().use { rows -> rows.next() } } } } fun getNotifyUbuntu(avatarID: String): Iterable { - pool.connection.use { + return pool.connection.use { it.prepareStatement( """ SELECT Ubuntu, Notify_Ubuntu @@ -1741,7 +1739,7 @@ while (rows.next()) { data.add(rows.getString("Ubuntu")) } - return data + data } } } @@ -2077,20 +2075,20 @@ } fun getNote(qm: Map): CompletableFuture { - return logValueFuture { - val want = qm["want"] ?: "" - val src = qm["src"] ?: "0" - val fit = qm["fit"] ?: "0" - val page = (qm["page"] ?: "1").toInt() - val viewUnit = (qm["viewUnit"] ?: "15").toInt() + val want = qm["want"] ?: "" + val src = qm["src"] ?: "0" + val fit = qm["fit"] ?: "0" + val page = (qm["page"] ?: "1").toInt() + val viewUnit = (qm["viewUnit"] ?: "15").toInt() + val isNotWant = want.isEmpty() - var totalCount = 0 - var highestCount = 0 - var noteCount = 0 - val notes = mutableListOf() + var totalCount = 0 + var highestCount = 0 + var noteCount = 0 + val notes = mutableListOf() + return CompletableFuture.allOf(logFuture { pool.connection.use { db -> - val isNotWant = want.isEmpty() if (isNotWant) { db.prepareStatement( """ @@ -2149,35 +2147,38 @@ } } } - val asStatement = """ - DISTINCT tw_note.Note_ID, Note_Variety, Artist, Title, Genre, Level_Text, Level, ( - SELECT COUNT(DISTINCT Note_ID, Avatar) - FROM tw_comment - WHERE tw_note.Note_ID = tw_comment.Note_ID - ) AS Highest, ( - SELECT COUNT(Comment_ID) - FROM tw_comment - WHERE tw_note.Note_ID = tw_comment.Note_ID - ) AS Total, ( - SELECT MAX(Date) - FROM tw_comment - WHERE tw_note.Note_ID = tw_comment.Note_ID - ) AS Date - """.trimIndent() - val fitStatement = when (fit) { - "0" -> " ORDER BY Highest DESC " - "1" -> " ORDER BY Total DESC " - "2" -> " ORDER BY Date DESC " - "3" -> " ORDER BY Title " - "4" -> " ORDER BY Artist " - "5" -> " ORDER BY Genre " - "6" -> " ORDER BY Level_Text " - else -> " " - } + } + }, logFuture { + val dataStatement = """ + DISTINCT tw_note.Note_ID, Note_Variety, Artist, Title, Genre, Level_Text, Level, ( + SELECT COUNT(DISTINCT Note_ID, Avatar) + FROM tw_comment + WHERE tw_note.Note_ID = tw_comment.Note_ID + ) AS Highest, ( + SELECT COUNT(Comment_ID) + FROM tw_comment + WHERE tw_note.Note_ID = tw_comment.Note_ID + ) AS Total, ( + SELECT MAX(Date) + FROM tw_comment + WHERE tw_note.Note_ID = tw_comment.Note_ID + ) AS Date + """.trimIndent() + val fitStatement = when (fit) { + "0" -> " ORDER BY Highest DESC " + "1" -> " ORDER BY Total DESC " + "2" -> " ORDER BY Date DESC " + "3" -> " ORDER BY Title " + "4" -> " ORDER BY Artist " + "5" -> " ORDER BY Genre " + "6" -> " ORDER BY Level_Text " + else -> " " + } + pool.connection.use { db -> val dbStatement: PreparedStatement? if (isNotWant) { dbStatement = db.prepareStatement( - "SELECT $asStatement FROM tw_note $fitStatement LIMIT ?, ?" + "SELECT $dataStatement FROM tw_note $fitStatement LIMIT ?, ?" ) dbStatement.setInt(1, viewUnit * (page - 1)) dbStatement.setInt(2, viewUnit) @@ -2185,7 +2186,7 @@ dbStatement = when (src) { "0" -> db.prepareStatement( """ - SELECT $asStatement + SELECT $dataStatement FROM tw_note WHERE MATCH(Title) AGAINST(? IN BOOLEAN MODE) $fitStatement LIMIT ?, ? """.trimIndent() @@ -2193,7 +2194,7 @@ "1" -> db.prepareStatement( """ - SELECT $asStatement + SELECT $dataStatement FROM tw_note, tw_comment, tn_avatar WHERE tw_note.Note_ID = tw_comment.Note_ID AND tw_comment.Avatar = tn_avatar.Avatar_ID AND MATCH(Avatar_Name) AGAINST(? IN BOOLEAN MODE) $fitStatement LIMIT ?, ? """.trimIndent() @@ -2201,7 +2202,7 @@ "2" -> db.prepareStatement( """ - SELECT $asStatement + SELECT $dataStatement FROM tw_note WHERE MATCH(Artist) AGAINST(? IN BOOLEAN MODE) $fitStatement LIMIT ?, ? """.trimIndent() @@ -2209,7 +2210,7 @@ "3" -> db.prepareStatement( """ - SELECT $asStatement + SELECT $dataStatement FROM tw_note WHERE MATCH(Genre) AGAINST(? IN BOOLEAN MODE) $fitStatement LIMIT ?, ? """.trimIndent() @@ -2238,13 +2239,13 @@ } } } - - object { - val totalCount = totalCount - val highestCount = highestCount - val noteCount = noteCount - val notes = notes - } + } + }).thenApply { + object { + val totalCount = totalCount + val highestCount = highestCount + val noteCount = noteCount + val notes = notes } } } @@ -2448,9 +2449,9 @@ wantAvatars } } else { - val futures = CompletableFuture.allOf(logValueFuture { + val futures = CompletableFuture.allOf(logFuture { avatarLevels = getAvatarLevels(avatarID) - }, logValueFuture { + }, logFuture { pool.connection.use { db -> db.prepareStatement( """ @@ -2472,7 +2473,7 @@ } } } - }, logValueFuture { + }, logFuture { pool.connection.use { db -> db.prepareStatement( """ @@ -2489,7 +2490,7 @@ } } } - }, logValueFuture { + }, logFuture { pool.connection.use { db -> db.prepareStatement( """ @@ -2506,7 +2507,7 @@ } } } - }, logValueFuture { + }, logFuture { pool.connection.use { db -> val millis = System.currentTimeMillis() for (i in 90 downTo 0) { @@ -2531,7 +2532,7 @@ } } } - }, logValueFuture { + }, logFuture { pool.connection.use { db -> db.prepareStatement( """ @@ -2550,7 +2551,7 @@ } } } - }, logValueFuture { + }, logFuture { val rawAbilities5K = getAbilities5K() val targetAbilities5K = getAbilities(rawAbilities5K).toList() val validAbilities5K = targetAbilities5K.stream().filter { it.second.second }.toList() @@ -2571,7 +2572,7 @@ } else { 1.0 } - }, logValueFuture { + }, logFuture { val rawAbilities7K = getAbilities7K() val targetAbilities7K = getAbilities(rawAbilities7K).toList() val validAbilities7K = targetAbilities7K.stream().filter { it.second.second }.toList() @@ -2592,7 +2593,7 @@ } else { 1.0 } - }, logValueFuture { + }, logFuture { val rawAbilities9K = getAbilities9K() val targetAbilities9K = getAbilities(rawAbilities9K).toList() val validAbilities9K = targetAbilities9K.stream().filter { it.second.second }.toList() @@ -3404,7 +3405,7 @@ val lowestJudgmentConditionModes = mutableListOf() return CompletableFuture.allOf( - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3423,7 +3424,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3442,7 +3443,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3461,7 +3462,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3489,7 +3490,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3515,7 +3516,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3540,7 +3541,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3568,7 +3569,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3596,7 +3597,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3618,7 +3619,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3639,7 +3640,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3660,7 +3661,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3681,7 +3682,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3702,7 +3703,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3723,7 +3724,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3744,7 +3745,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3765,7 +3766,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3786,7 +3787,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """ @@ -3807,7 +3808,7 @@ } } }, - logValueFuture { + logFuture { pool.connection.use { it.prepareStatement( """