Fix 'i' now counting at WorldPieCreator.createSeriesData(Map<String, Long>)

Make the creation of offline players at the import parallel (before: 68608 ms, now: 20320 ms)
This commit is contained in:
Fuzzlemann 2017-08-02 18:46:59 +02:00
parent f705877cbc
commit af91f25827
3 changed files with 41 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package main.java.com.djrapitops.plan.data.handling.importing;
import com.djrapitops.plugin.utilities.player.Fetch;
import com.djrapitops.plugin.utilities.player.IOfflinePlayer;
import com.djrapitops.plugin.utilities.status.ProcessStatus;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.UserData;
@ -14,6 +15,7 @@ import main.java.com.djrapitops.plan.utilities.NewPlayerCreator;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -58,11 +60,16 @@ public abstract class Importer {
public boolean importData(Collection<UUID> uuids, String... args) {
Plan plan = Plan.getInstance();
plan.getAnalysisCache().disableAnalysisTemporarily();
try {
String processName = "Import, " + getClass().getSimpleName();
plan.processStatus().startExecution(processName);
ProcessStatus<Plan> processStatus = plan.processStatus();
DataCacheHandler handler = plan.getHandler();
Database db = plan.getDB();
processStatus.startExecution(processName);
Set<UUID> saved;
try {
saved = db.getSavedUUIDs();
@ -70,30 +77,47 @@ public abstract class Importer {
Log.toLog(this.getClass().getName(), ex);
return false;
}
List<UUID> unSaved = new ArrayList<>(uuids);
unSaved.removeAll(saved);
String createUserObjects = "Creating new UserData objects for: " + unSaved.size();
plan.processStatus().setStatus(processName, createUserObjects);
processStatus.setStatus(processName, createUserObjects);
Map<UUID, IOfflinePlayer> offlinePlayers = Fetch.getIOfflinePlayers().stream().collect(Collectors.toMap(IOfflinePlayer::getUuid, Function.identity()));
Benchmark.start(createUserObjects);
List<IOfflinePlayer> offlineP = unSaved.stream().map(offlinePlayers::get).collect(Collectors.toList());
List<UserData> newUsers = new ArrayList<>();
for (IOfflinePlayer p : offlineP) {
UserData newPlayer = NewPlayerCreator.createNewOfflinePlayer(p);
newPlayer.setLastPlayed(newPlayer.getRegistered());
newUsers.add(newPlayer);
plan.processStatus().setStatus(processName, "Creating new UserData objects: " + newUsers.size() + "/" + unSaved.size());
}
List<IOfflinePlayer> offlineP = unSaved
.stream()
.map(offlinePlayers::get)
.collect(Collectors.toList());
AtomicInteger currentUser = new AtomicInteger(0);
int amount = unSaved.size();
offlineP.parallelStream()
.map(NewPlayerCreator::createNewOfflinePlayer)
.forEach(newPlayer -> {
newPlayer.setLastPlayed(newPlayer.getRegistered());
newUsers.add(newPlayer);
processStatus.setStatus(processName, "Creating new UserData objects: " + currentUser.addAndGet(1) + "/" + amount);
});
Benchmark.stop(createUserObjects);
plan.processStatus().setStatus(processName, "Save new UserData objects (" + unSaved.size() + ")");
processStatus.setStatus(processName, "Save new UserData objects (" + unSaved.size() + ")");
try {
plan.getDB().saveMultipleUserData(newUsers);
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
}
for (UUID uuid : uuids) {
handler.addToPool(importData(uuid, args));
}
plan.processStatus().finishExecution(processName);
} finally {
plan.getAnalysisCache().enableAnalysis();

View File

@ -15,13 +15,16 @@ public class WorldPieCreator {
for (Map.Entry<String, Long> world : worldTimes.entrySet()) {
arrayBuilder.append("{name:'").append(world.getKey())
.append("',y:").append(world.getValue());
if (i == 1) {
arrayBuilder.append(", sliced: true, selected: true");
}
arrayBuilder.append("}");
if (i < size - 1) {
arrayBuilder.append(",");
}
i++;
}
arrayBuilder.append("]");
return arrayBuilder.toString();

View File

@ -235,13 +235,10 @@ public class DatabaseTest {
db.saveUserData(data);
data.addNickname("TestUpdateForSave");
db.saveUserData(data);
DBCallableProcessor process = new DBCallableProcessor() {
@Override
public void process(UserData d) {
System.out.println("\nOriginal: " + data);
System.out.println("Database: " + d);
assertTrue("Not Equals", data.equals(d));
}
DBCallableProcessor process = d -> {
System.out.println("\nOriginal: " + data);
System.out.println("Database: " + d);
assertTrue("Not Equals", data.equals(d));
};
db.giveUserDataToProcessors(data.getUuid(), process);
}