Search in sources :

Example 31 with Query

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);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) Test(org.junit.Test)

Example 32 with Query

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);
}
Also used : Query(me.staartvin.statz.database.datatype.Query) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 33 with Query

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.");
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) ArrayList(java.util.ArrayList)

Example 34 with Query

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());
        }
    }
}
Also used : Query(me.staartvin.statz.database.datatype.Query) PlayerStat(me.staartvin.statz.datamanager.player.PlayerStat) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 35 with Query

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;
    }
}
Also used : Query(me.staartvin.statz.database.datatype.Query) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

Query (me.staartvin.statz.database.datatype.Query)44 Test (org.junit.Test)25 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)13 PlayerStat (me.staartvin.statz.datamanager.player.PlayerStat)11 ArrayList (java.util.ArrayList)8 UUID (java.util.UUID)6 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)4 HashMap (java.util.HashMap)2 Material (org.bukkit.Material)2 Player (org.bukkit.entity.Player)2 Inventory (org.bukkit.inventory.Inventory)2 ItemStack (org.bukkit.inventory.ItemStack)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1