Merge branch '4.0.0-Connection-Pool-Branch' of https://github.com/Rsl1122/Plan-PlayerAnalytics into 4.0.0-Connection-Pool-Branch

This commit is contained in:
Rsl1122 2017-08-23 23:33:05 +03:00
commit 29542b34b7
25 changed files with 159 additions and 91 deletions

View File

@ -343,6 +343,7 @@ public abstract class Database {
public UserInfoTable getUserInfoTable() {
return userInfoTable;
}
public BasicDataSource getDataSource() {
return dataSource;
}

View File

@ -32,7 +32,6 @@ public class MySQLDB extends SQLDB {
String port = config.getString("Database.MySQL.Port");
String database = config.getString("Database.MySQL.Database");
dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + database + "?rewriteBatchedStatements=true");
String username = config.getString("Database.MySQL.User");

View File

@ -233,12 +233,6 @@ public abstract class SQLDB extends Database {
try {
Benchmark.start("Remove Account");
Log.debug("Database", "Removing Account: " + uuid);
try {
setupDatabase();
} catch (Exception e) {
Log.toLog(this.getClass().getName(), e);
return false;
}
for (Table t : getAllTablesInRemoveOrder()) {
if (!(t instanceof UserIDTable)) {

View File

@ -80,8 +80,9 @@ public class ActionsTable extends UserIDTable {
statement.setLong(4, action.getDate());
statement.setString(5, action.getAdditionalInfo());
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -157,8 +157,8 @@ public class CommandUseTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}
@ -187,8 +187,8 @@ public class CommandUseTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -103,8 +103,9 @@ public class IPsTable extends UserIDTable {
statement.setString(2, ip);
statement.setString(3, geolocation);
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -64,17 +64,14 @@ public class KillsTable extends UserIDTable {
" OR " + columnVictimUserID + " = " + usersTable.statementSelectID);
statement.setString(1, uuid.toString());
statement.setString(2, uuid.toString());
statement.execute();
commit(statement.getConnection());
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -106,9 +103,10 @@ public class KillsTable extends UserIDTable {
statement.setString(5, weapon);
statement.addBatch();
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}
@ -128,7 +126,10 @@ public class KillsTable extends UserIDTable {
" JOIN " + usersTable + " on " + usersIDColumn + "=" + columnVictimUserID +
" WHERE " + columnKillerUserID + "=" + usersTable.statementSelectID);
statement.setString(1, uuid.toString());
set = statement.executeQuery();
commit(statement.getConnection());
while (set.next()) {
long sessionID = set.getLong(columnSessionID);
Session session = sessions.get(sessionID);
@ -142,7 +143,6 @@ public class KillsTable extends UserIDTable {
session.getPlayerKills().add(new KillData(victim, weapon, date));
}
} finally {
endTransaction(statement);
close(set, statement);
}
}

View File

@ -152,9 +152,10 @@ public class NicknamesTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.setString(2, Plan.getServerUUID().toString());
statement.setString(3, displayName);
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -47,18 +47,14 @@ public class SecurityTable extends Table {
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUser + "=?)");
statement.setString(1, user);
statement.execute();
commit(statement.getConnection());
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}

View File

@ -65,7 +65,6 @@ public class ServerTable extends Table {
} else {
updateServerInfo(info);
}
}
private void updateServerInfo(ServerInfo info) throws SQLException {
@ -85,8 +84,9 @@ public class ServerTable extends Table {
statement.setBoolean(4, true);
statement.setInt(5, info.getId());
statement.executeUpdate();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}
@ -116,8 +116,9 @@ public class ServerTable extends Table {
statement.setString(3, webAddress);
statement.setBoolean(4, true);
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -74,6 +74,7 @@ public class SessionsTable extends UserIDTable {
if (sessionID == -1) {
throw new IllegalStateException("Session was not Saved!");
}
db.getWorldTimesTable().saveWorldTimes(uuid, sessionID, session.getWorldTimes());
db.getKillsTable().savePlayerKills(uuid, sessionID, session.getPlayerKills());
}
@ -107,11 +108,11 @@ public class SessionsTable extends UserIDTable {
statement.setLong(3, session.getSessionEnd());
statement.setInt(4, session.getDeaths());
statement.setInt(5, session.getMobKills());
statement.setString(6, Plan.getServerUUID().toString());
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -117,7 +117,9 @@ public class TPSTable extends Table {
statement.setLong(6, tps.getUsedMemory());
statement.setDouble(7, tps.getEntityCount());
statement.setDouble(8, tps.getChunksLoaded());
statement.execute();
commit(statement.getConnection());
} finally {
close(statement);
}
@ -133,9 +135,10 @@ public class TPSTable extends Table {
// More than 2 Months ago.
long fiveWeeks = TimeAmount.MONTH.ms() * 2L;
statement.setLong(1, MiscUtils.getTime() - fiveWeeks);
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -29,17 +29,14 @@ public abstract class UserIDTable extends Table {
statement = prepareStatement("DELETE FROM " + tableName +
" WHERE (" + columnUserID + "=" + usersTable.statementSelectID + ")");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}

View File

@ -70,7 +70,9 @@ public class UserInfoTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.setLong(2, registered);
statement.setString(3, Plan.getServerUUID().toString());
statement.execute();
commit(statement.getConnection());
} finally {
close(statement);
}
@ -100,7 +102,9 @@ public class UserInfoTable extends UserIDTable {
statement.setBoolean(1, opped);
statement.setBoolean(2, banned);
statement.setString(3, uuid.toString());
statement.execute();
commit(statement.getConnection());
} finally {
close(statement);
}

View File

@ -1,7 +1,6 @@
package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.*;
@ -79,16 +78,13 @@ public class UsersTable extends UserIDTable {
try {
statement = prepareStatement("DELETE FROM " + tableName + " WHERE (" + columnUUID + "=?)");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
return true;
} catch (SQLException ex) {
return false;
} finally {
try {
endTransaction(statement);
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
close(statement);
}
}
@ -167,9 +163,10 @@ public class UsersTable extends UserIDTable {
statement.setString(1, uuid.toString());
statement.setLong(2, registered);
statement.setString(3, name);
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}
@ -198,9 +195,10 @@ public class UsersTable extends UserIDTable {
.toString());
statement.setString(1, name);
statement.setString(2, uuid.toString());
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}
@ -231,9 +229,10 @@ public class UsersTable extends UserIDTable {
+ columnTimesKicked + "=" + columnTimesKicked + "+ 1" +
" WHERE " + columnUUID + "=?");
statement.setString(1, uuid.toString());
statement.execute();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -64,6 +64,7 @@ public class VersionTable extends Table {
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " (version) VALUES (" + version + ")");
statement.executeUpdate();
commit(statement.getConnection());
} finally {

View File

@ -101,8 +101,8 @@ public class WorldTable extends Table {
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -98,8 +98,8 @@ public class WorldTimesTable extends UserIDTable {
}
statement.executeBatch();
commit(statement.getConnection());
} finally {
endTransaction(statement);
close(statement);
}
}

View File

@ -1,11 +1,8 @@
package main.java.com.djrapitops.plan.ui.html.graphs;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.utilities.analysis.Point;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@ -27,12 +24,4 @@ public class PlayerActivityGraphCreator {
.collect(Collectors.toList());
return SeriesCreator.seriesGraph(points, true);
}
public static String buildSeriesDataStringSessions(Collection<Session> sessions) {
List<Point> points = sessions.stream()
.map(session -> new Point[]{new Point(session.getSessionStart(), 1), new Point(session.getSessionEnd(), 0)})
.flatMap(Arrays::stream)
.collect(Collectors.toList());
return SeriesCreator.seriesGraph(points, true, false);
}
}

View File

@ -1,5 +1,7 @@
package main.java.com.djrapitops.plan.utilities.analysis;
import com.google.common.base.Objects;
/**
* @author Rsl1122
* @since 3.5.2
@ -25,4 +27,18 @@ public class Point {
public String toString() {
return "{x:" + x + " y:" + y + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
return Double.compare(point.x, x) == 0 &&
Double.compare(point.y, y) == 0;
}
@Override
public int hashCode() {
return Objects.hashCode(x, y);
}
}

View File

@ -60,7 +60,7 @@ public class DatabaseTest {
plan = t.getPlanMock();
db = new SQLiteDB(plan, "debug" + MiscUtils.getTime());
db.init();
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, t.getServerUUID(), "ServerName", ""));
db.getServerTable().saveCurrentServerInfo(new ServerInfo(-1, TestInit.getServerUUID(), "ServerName", ""));
File f = new File(plan.getDataFolder(), "Errors.txt");
rows = FileUtil.lines(f).size();
@ -339,6 +339,7 @@ public class DatabaseTest {
saveTwoWorlds();
saveUserOne();
saveUserTwo();
Session session = new Session(12345L, "", "");
session.endSession(22345L);
session.setWorldTimes(createWorldTimes());
@ -496,8 +497,13 @@ public class DatabaseTest {
assertTrue(usersTable.isRegistered(uuid));
System.out.println("0 " + uuid);
System.out.println("1 " + db.getUsersTable().getSavedUUIDs());
db.removeAccount(uuid);
System.out.println("2 " + db.getUsersTable().getSavedUUIDs());
assertFalse(usersTable.isRegistered(uuid));
assertFalse(userInfoTable.isRegistered(uuid));
assertTrue(nicknamesTable.getNicknames(uuid).isEmpty());

View File

@ -0,0 +1,88 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package test.java.main.java.com.djrapitops.plan.ui.graphs;
import main.java.com.djrapitops.plan.data.Session;
import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.ui.html.graphs.*;
import main.java.com.djrapitops.plan.utilities.analysis.Point;
import org.junit.Before;
import org.junit.Test;
import test.java.utils.RandomData;
import java.util.*;
import static junit.framework.TestCase.assertEquals;
/**
* @author Fuzzlemann
*/
public class GraphTest {
private List<TPS> tpsList = new ArrayList<>();
private List<Session> sessionList = new ArrayList<>();
private Map<String, Integer> geoList = new HashMap<>();
private Map<String, Long> worldTimes = new HashMap<>();
private List<Point> points = new ArrayList<>();
@Before
public void setUp() {
for (int i = 0; i < 10; i++) {
tpsList.add(new TPS(i, i, i, i, i, i, i));
sessionList.add(new Session(i, (long) i, (long) i, i, i));
geoList.put(String.valueOf(i), i);
worldTimes.put(String.valueOf(i), (long) i);
}
points = RandomData.randomPoints();
}
@Test
public void testGraphCreators() {
assertEquals(CPUGraphCreator.buildSeriesDataString(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(PlayerActivityGraphCreator.buildSeriesDataString(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(PunchCardGraphCreator.createDataSeries(sessionList), "[{x:3600000, y:3, z:14, marker: { radius:14}},]");
assertEquals(RamGraphCreator.buildSeriesDataString(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(TPSGraphCreator.buildSeriesDataString(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(WorldLoadGraphCreator.buildSeriesDataStringChunks(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(WorldLoadGraphCreator.buildSeriesDataStringEntities(tpsList), "[[0,0.0],[9,9.0]]");
assertEquals(WorldMapCreator.createDataSeries(geoList), "[{'code':'1','value':1},{'code':'2','value':2},{'code':'3','value':3},{'code':'4','value':4},{'code':'5','value':5},{'code':'6','value':6},{'code':'7','value':7},{'code':'8','value':8},{'code':'9','value':9}]");
assertEquals(WorldPieCreator.createSeriesData(worldTimes), "[{name:'0',y:0},{name:'1',y:1, sliced: true, selected: true},{name:'2',y:2},{name:'3',y:3},{name:'4',y:4},{name:'5',y:5},{name:'6',y:6},{name:'7',y:7},{name:'8',y:8},{name:'9',y:9}]");
}
@Test
public void testSeriesCreator() {
String result = SeriesCreator.seriesGraph(points, false, false);
String[] splittedResult = result.split(",");
Map<String, String> expected = new LinkedHashMap<>();
String key = null;
for (String resultString : splittedResult) {
resultString = resultString.replaceAll("[\\[\\]]", "");
if (key == null) {
key = resultString;
} else {
expected.put(key, resultString);
key = null;
}
}
int i2 = 0;
for (Map.Entry<String, String> entry : expected.entrySet()) {
String expectedX = entry.getKey();
String expectedY = entry.getValue();
Point point = points.get(i2);
assertEquals("Given X does not match expected X", expectedX, String.valueOf((long) point.getX()));
assertEquals("Given Y does not match expected Y", expectedY, String.valueOf(point.getY()));
i2++;
}
}
}

View File

@ -20,22 +20,13 @@ import org.powermock.modules.junit4.PowerMockRunner;
@PrepareForTest(JavaPlugin.class)
public class NewPlayerCreatorTest {
/**
*
*/
public NewPlayerCreatorTest() {
}
/**
*
*/
@Before
public void setUp() throws Exception {
}
/**
*
*/
@After
public void tearDown() {
}

View File

@ -21,9 +21,9 @@ public class PassEncryptTest {
@Before
public void setUp() throws Exception {
for (int i = 0; i < 20; i++) {
String password = RandomData.randomString(RandomData.randomInt(1, 50));
String password = RandomData.randomString(RandomData.randomInt(1, 20));
PASSWORD_MAP.put(password, PassEncryptUtil.createHash(password));
};
}
}
@Test

View File

@ -23,18 +23,12 @@ import static org.powermock.api.mockito.PowerMockito.when;
*/
public class MockUtils {
/**
* @return
*/
public static World mockWorld() {
World mockWorld = Mockito.mock(World.class);
when(mockWorld.toString()).thenReturn("World");
return mockWorld;
}
/**
* @return
*/
public static IPlayer mockIPlayer() {
return Fetch.wrapBukkit(mockPlayer());
}
@ -54,16 +48,10 @@ public class MockUtils {
return p;
}
/**
* @return
*/
public static UUID getPlayerUUID() {
return UUID.fromString("45b0dfdb-f71d-4cf3-8c21-27c9d4c651db");
}
/**
* @return
*/
public static IPlayer mockIPlayer2() {
return Fetch.wrapBukkit(mockPlayer2());
}
@ -83,9 +71,6 @@ public class MockUtils {
return p;
}
/**
* @return
*/
public static UUID getPlayer2UUID() {
return UUID.fromString("ec94a954-1fa1-445b-b09b-9b698519af80");
}
@ -97,9 +82,6 @@ public class MockUtils {
return uuids;
}
/**
* @return
*/
public static IPlayer mockBrokenPlayer() {
Player p = PowerMockito.mock(Player.class);
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL);
@ -114,9 +96,6 @@ public class MockUtils {
return Fetch.wrapBukkit(p);
}
/**
* @return
*/
public static CommandSender mockConsoleSender() {
return PowerMockito.mock(CommandSender.class);
}