use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class QueryTest method testNonConflictingQuery1.
// World names are different
@Test
public void testNonConflictingQuery1() {
Query queryA = StatzUtil.makeQuery("UUID", "3657b9cc-2518-4265-ad69-323e11286ce2", "World", "worldName1");
Query queryB = StatzUtil.makeQuery("UUID", "3657b9cc-2518-4265-ad69-323e11286ce2", "World", "worldName2");
checkConflict(queryA, queryB, false);
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class QueryTest method testMultipleConflictingQueries.
@Test
public void testMultipleConflictingQueries() {
Query queryA = StatzUtil.makeQuery("UUID", "3657b9cc-2518-4265-ad69-323e11286ce2", "mob", "COW", "value", 100);
Query queryB = StatzUtil.makeQuery("uuid", "3657b9cc-2518-4265-ad69-323e11286ce2", "mob", "COW", "value", 100);
Query queryC = StatzUtil.makeQuery("uuid", "3657b9cc-2518-4265-ad69-323e11286ce2", "mob", "COW", "value", 200);
Query queryD = StatzUtil.makeQuery("uuid", "3657b9cc-2518-4265-ad69-323e11286ce2", "mob", "COW", "value", 500);
Query queryE = StatzUtil.makeQuery("uuid", "3657b9cc-2518-4265-ad69-323e11286ce2", "mob", "COW", "value", 100);
List<Query> conflictingQueries = new ArrayList<>();
conflictingQueries.add(queryB);
conflictingQueries.add(queryC);
conflictingQueries.add(queryD);
conflictingQueries.add(queryE);
Query nonConflictingQuery = queryA.resolveConflicts(conflictingQueries);
Assert.assertEquals(1000, nonConflictingQuery.getValue(), 0);
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class UpdateDatabaseTask method run.
@Override
public void run() {
if (UpdatePoolManager.isForcingPool) {
// Skip call, as we are still busy.
plugin.debugMessage("Skip database sync as there is still one running.");
return;
}
plugin.debugMessage("Sending local data to database...");
// Set lock so we can't accidentally run two sync tasks at the same time.
UpdatePoolManager.isForcingPool = true;
for (PlayerStat statType : PlayerStat.values()) {
// Grab updates that have happened since the last sync.
List<Query> updates = plugin.getUpdatePoolManager().getUpdateQueriesCopy(statType);
if (updates.isEmpty()) {
continue;
}
// Store queries that have already been converted
List<Query> convertedQueries = new ArrayList<>();
// Queries that should be send to the database
List<Query> resultingQueries = new ArrayList<>();
// Loop over all queries and remove duplicate queries.
for (Iterator<Query> iterator = updates.iterator(); iterator.hasNext(); ) {
// Get a query.
Query query = iterator.next();
// If we've already converted it, skip it.
if (convertedQueries.contains(query)) {
continue;
}
// Remove the current query so it doesn't conflict with itself.
iterator.remove();
// Find queries that conflict with the current query
List<Query> conflictingQueries = query.findConflicts(updates);
// Add queries that were conflicting to converted queries, so we don't count them again.
convertedQueries.addAll(conflictingQueries);
// Calculate sum of all conflicting queries.
Query sumQuery = query.resolveConflicts(conflictingQueries);
// Store the final query
resultingQueries.add(sumQuery);
}
// Update database with new data.
plugin.getDatabaseConnector().setBatchObjects(DatabaseConnector.getTable(statType), resultingQueries, 2);
plugin.getUpdatePoolManager().clearUpdateQueries(statType);
}
// Release lock
UpdatePoolManager.isForcingPool = false;
plugin.debugMessage("Successfully updated database with local data.");
plugin.getLogsManager().writeToLogFile("Updated database with local data.");
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class UpdatePoolManager method printPool.
/**
* Print all pools to the console.
*/
public void printPool() {
if (this.updateQueries.isEmpty()) {
System.out.println("POOL IS EMPTY");
return;
}
System.out.println("PRINT POOL");
System.out.println("------------------------");
for (PlayerStat stat : PlayerStat.values()) {
List<Query> queries = this.getUpdateQueriesCopy(stat);
if (queries == null || queries.isEmpty()) {
System.out.println("[PlayerStat: " + stat + "]: EMPTY");
continue;
}
System.out.println("------------------------");
System.out.println("[PlayerStat: " + stat + "] Size: " + queries.size());
for (Query query : queries) {
System.out.println("------------------------");
StringBuilder builder = new StringBuilder("{");
for (Map.Entry<String, String> entry : query.getEntrySet()) {
builder.append(entry.getKey() + ": " + entry.getValue() + ", ");
}
builder.append("}");
System.out.println(builder.toString());
}
}
}
use of me.staartvin.statz.database.datatype.Query in project Statz by Staartvin.
the class LogManager method writeToLogFile.
/**
* Write query objects of a given statistic to a log file.
* <br>This method will format the queries into human-readable form to write into the log file.
* @param queries Queries to write to log file
* @param stat Statistic that the given queries correspond to.
*/
public void writeToLogFile(List<Query> queries, PlayerStat stat) {
// Creates a new file
logFile = new File(plugin.getDataFolder() + "/logs", "log-" + dateFormatSave + ".txt");
// Create our writer
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(logFile, true));
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
try {
for (Query query : queries) {
out.write("[" + timeFormat.format(new Date()) + "] [PLAYERSTAT: " + stat + "] " + query.getLogString());
out.newLine();
}
} catch (Exception e) {
plugin.debugMessage(ChatColor.RED + "Error when writing to log file!");
}
// close
try {
out.close();
return;
} catch (final IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}
Aggregations