Merge pull request #2711 from sotovdev/perf_22_12_2016

Concurrency perfomance boost
This commit is contained in:
LevelX2 2016-12-23 19:49:28 +01:00 committed by GitHub
commit d0249716c2

View file

@ -22,6 +22,7 @@ import java.nio.file.AccessDeniedException;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.imageio.IIOImage; import javax.imageio.IIOImage;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam; import javax.imageio.ImageWriteParam;
@ -235,18 +236,20 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
} }
public static boolean checkForNewCards(List<CardInfo> allCards) { public static boolean checkForNewCards(List<CardInfo> allCards) {
TFile file; AtomicBoolean missedCardTFiles = new AtomicBoolean();
for (CardInfo card : allCards) { allCards.parallelStream().forEach(card -> {
if (!missedCardTFiles.get()) {
if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) { if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()) {
CardDownloadData url = new CardDownloadData(card.getName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(), CardDownloadData url = new CardDownloadData(card.getName(), card.getSetCode(), card.getCardNumber(), card.usesVariousArt(),
0, "", "", false, card.isDoubleFaced(), card.isNightCard()); 0, "", "", false, card.isDoubleFaced(), card.isNightCard());
file = new TFile(CardImageUtils.generateImagePath(url)); TFile file = new TFile(CardImageUtils.generateImagePath(url));
if (!file.exists()) { if (!file.exists()) {
return true; missedCardTFiles.set(true);
} }
} }
} }
return false; });
return missedCardTFiles.get();
} }
private void updateCardsToDownload() { private void updateCardsToDownload() {
@ -291,7 +294,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
/** /**
* read all card names and urls * read all card names and urls
*/ */
ArrayList<CardDownloadData> allCardsUrls = new ArrayList<>(); List<CardDownloadData> allCardsUrls = Collections.synchronizedList(new ArrayList<>());
HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls(); HashSet<String> ignoreUrls = SettingsManager.getIntance().getIgnoreUrls();
/** /**
@ -309,7 +312,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
try { try {
offlineMode = true; offlineMode = true;
for (CardInfo card : allCards) { allCards.parallelStream().forEach(card -> {
if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty() if (!card.getCardNumber().isEmpty() && !"0".equals(card.getCardNumber()) && !card.getSetCode().isEmpty()
&& !ignoreUrls.contains(card.getSetCode())) { && !ignoreUrls.contains(card.getSetCode())) {
String cardName = card.getName(); String cardName = card.getName();
@ -349,7 +352,7 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
System.err.println("There was a critical error!"); System.err.println("There was a critical error!");
logger.error("Card has no set name and won't be sent to client:" + card); logger.error("Card has no set name and won't be sent to client:" + card);
} }
} });
allCardsUrls.addAll(getTokenCardUrls()); allCardsUrls.addAll(getTokenCardUrls());
} catch (Exception e) { } catch (Exception e) {
logger.error(e); logger.error(e);
@ -741,21 +744,21 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch"); httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
httpConn.setRequestProperty("Accept-Language", "en-US,en;q=0.8"); httpConn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
break; break;
// ff // ff
case 1: case 1:
httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Accept-Language", "en-US;q=0.5,en;q=0.3"); httpConn.setRequestProperty("Accept-Language", "en-US;q=0.5,en;q=0.3");
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
break; break;
// ie // ie
case 2: case 2:
httpConn.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*"); httpConn.setRequestProperty("Accept", "text/html, application/xhtml+xml, */*");
httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
httpConn.setRequestProperty("Accept-Language", "en-US"); httpConn.setRequestProperty("Accept-Language", "en-US");
httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"); httpConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
break; break;
} }
} }
@ -789,14 +792,16 @@ public class DownloadPictures extends DefaultBoundedRangeModel implements Runnab
bar.setString(String.format("%d of %d cards finished! Please wait! [%.1f Mb]", bar.setString(String.format("%d of %d cards finished! Please wait! [%.1f Mb]",
card, count, mb)); card, count, mb));
} else { } else {
Iterator<CardDownloadData> cardsIterator = DownloadPictures.this.cards.iterator(); List<CardDownloadData> remainingCards = Collections.synchronizedList(new ArrayList<>());
while (cardsIterator.hasNext()) { DownloadPictures.this.cards.parallelStream().forEach(cardDownloadData -> {
CardDownloadData cardDownloadData = cardsIterator.next();
TFile file = new TFile(CardImageUtils.generateImagePath(cardDownloadData)); TFile file = new TFile(CardImageUtils.generateImagePath(cardDownloadData));
if (file.exists()) { if (!file.exists()) {
cardsIterator.remove(); remainingCards.add(cardDownloadData);
}
} }
});
DownloadPictures.this.cards = new ArrayList<>(remainingCards);
count = DownloadPictures.this.cards.size(); count = DownloadPictures.this.cards.size();
if (count == 0) { if (count == 0) {