Skip to content

Commit

Permalink
Merge pull request PhantomBot#3529 from gmt2001/patch-762
Browse files Browse the repository at this point in the history
Patch 762
  • Loading branch information
gmt2001 authored Aug 26, 2024
2 parents e28cf84 + 3f10705 commit a7e57ec
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 42 deletions.
8 changes: 4 additions & 4 deletions javascript-source/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,10 @@
handleException('command', e, true);
return;
}
let command = event.getCommand(),
args = event.getArgs(),
subCommand = $.getSubCommandFromArguments(command, args),
isMod = $.checkUserPermission(sender, event.getTags(), $.PERMISSION.Mod);
let command = event.getCommand();
let args = event.getArgs();
let subCommand = $.getSubCommandFromArguments(command, args);
let isMod = $.checkUserPermission(sender, event.getTags(), $.PERMISSION.Mod);

if (isReady === false && ($.equalsIgnoreCase(command, 'pbcore') || $.equalsIgnoreCase(command, $.botName)) && args[0].equalsIgnoreCase('moderate')) {
Packages.tv.phantombot.PhantomBot.instance().getSession().getModerationStatus();
Expand Down
12 changes: 10 additions & 2 deletions source/com/gmt2001/datastore/DataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -45,6 +47,8 @@
import com.gmt2001.datastore2.H2Store2;
import com.gmt2001.datastore2.SQLiteStore2;

import tv.phantombot.PhantomBot;

/**
* Provides access to the database in a key-value store style
*
Expand Down Expand Up @@ -687,11 +691,13 @@ public Optional<SectionVariableValueRecord> OptRecord(SectionVariableValueTable

/**
* Returns the record for the given table, section, and key
* <p>
* If a JOOQ constraint failure occurs due to duplicate (SECTION, VARIABLE), a backup is created, duplicates are dropped, and the SQL constraint is added
*
* @param table the table to search
* @param section a section name. {@code ""} (empty string) for the default section; {@code null} for all sections
* @param key the value of the {@code variable} column to retrieve
* @param isRetry if {@code false}, a {@link TooManyRowsException} triggers a table rebuild; otherwise, the exception is re-thrown up the stack
* @param isRetry if {@code false}, a {@link TooManyRowsException} triggers dropping duplicate data; otherwise, the exception is re-thrown up the stack
* @return an {@link Optional} that may contain a {@link SectionVariableValueRecord} if the row exists
*/
private Optional<SectionVariableValueRecord> OptRecord(SectionVariableValueTable table, String section, String key, boolean isRetry) {
Expand All @@ -707,7 +713,9 @@ private Optional<SectionVariableValueRecord> OptRecord(SectionVariableValueTable
if (isRetry) {
throw ex;
} else {
table.rebuildTable();
String timestamp = LocalDateTime.now(PhantomBot.getTimeZoneId()).format(DateTimeFormatter.ofPattern("ddMMyyyy.hhmmss"));
this.backupDB("before_duplicate_drop_" + table.getName() + "_" + timestamp);
table.dropDuplicateData();
return this.OptRecord(table, section, key, true);
}
}
Expand Down
47 changes: 13 additions & 34 deletions source/com/gmt2001/datastore/SectionVariableValueTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,43 +202,22 @@ public void drop() {
}

/**
* Re-creates the table, de-duplicating data
* Drops duplicate data from the table by (SECTION, VARIABLE), then adds the SQL PRIMARY KEY constraint
* <p>
* Both actions are executed from within a transaction, so a rollback will occur if the SQL PRIMARY KEY constraint fails
*/
void rebuildTable() {
String tempName = this.tableName + "______TEMP";

try {
Datastore2.instance().dslContext().dropTableIfExists(tempName).execute();
} catch (Exception ex) {
com.gmt2001.Console.err.logStackTrace(ex);
}

this.createTable(tempName);

void dropDuplicateData() {
try {
Datastore2.instance().dslContext().transaction(transaction -> {
Optional<Table<?>> tempOTable = transaction.dsl().meta().getTables().stream()
.filter(t -> t.getName().equals(tempName)).findFirst();

if (tempOTable.isPresent()) {
Table<?> tempTable = tempOTable.get();

transaction.dsl().insertInto(tempTable)
.columns(tempTable.field(this.SECTION.getName(), String.class),
tempTable.field(this.VARIABLE.getName(), String.class),
tempTable.field(this.VALUE.getName(), String.class))
.select(
Datastore2.instance().dslContext()
.select(this.SECTION, this.VARIABLE, this.VALUE)
.distinctOn(this.SECTION, this.VARIABLE).from(this))
.execute();

transaction.dsl().dropTable(this.tableName).execute();

transaction.dsl().alterTable(tempName).renameTo(this.tableName).execute();

Datastore2.instance().invalidateTableCache();
}
transaction.dsl().deleteFrom(this)
.where(DSL.row(this.SECTION, this.VARIABLE, this.VALUE).notIn(
transaction.dsl()
.select(this.SECTION, this.VARIABLE, this.VALUE)
.distinctOn(this.SECTION, this.VARIABLE).from(this)))
.execute();

transaction.dsl().alterTable(this)
.add(DSL.primaryKey(this.SECTION, this.VARIABLE)).execute();
});
} catch (Exception ex) {
com.gmt2001.Console.err.logStackTrace(ex);
Expand Down
8 changes: 7 additions & 1 deletion source/com/gmt2001/httpwsserver/HttpServerPageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ public class HttpServerPageHandler extends SimpleChannelInboundHandler<FullHttpR
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (!req.decoderResult().isSuccess()) {
com.gmt2001.Console.debug.println("400 DECODER");
com.gmt2001.Console.err.printStackTrace(req.decoderResult().cause());
Throwable cause = req.decoderResult().cause();
if (cause.getMessage().contains("Content-Length value is not a number")) {
com.gmt2001.Console.warn.println("Unable to decode oversized POST/PUT payload");
com.gmt2001.Console.warn.println(cause.getMessage() + " is greater than " + Long.MAX_VALUE);
} else {
com.gmt2001.Console.err.printStackTrace(cause);
}
sendHttpResponse(ctx, req, prepareHttpResponse(HttpResponseStatus.BAD_REQUEST));
RequestLogger.log(ctx);
return;
Expand Down
6 changes: 5 additions & 1 deletion source/com/gmt2001/httpwsserver/HttpSslRedirectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.PrematureChannelClosureException;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
Expand Down Expand Up @@ -65,7 +66,10 @@ public class HttpSslRedirectHandler extends SimpleChannelInboundHandler<FullHttp
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
if (!req.decoderResult().isSuccess()) {
com.gmt2001.Console.debug.println("400 DECODER");
com.gmt2001.Console.err.printStackTrace(req.decoderResult().cause());
Throwable cause = req.decoderResult().cause();
if (!cause.getClass().isAssignableFrom(PrematureChannelClosureException.class)) {
com.gmt2001.Console.err.printStackTrace(cause);
}
HttpServerPageHandler.sendHttpResponse(ctx, req, HttpServerPageHandler.prepareHttpResponse(HttpResponseStatus.BAD_REQUEST));
return;
}
Expand Down

0 comments on commit a7e57ec

Please sign in to comment.