mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-04-19 14:33:04 +03:00
[FEATURE] Оптимизация алгоритма.
This commit is contained in:
parent
6a76321f20
commit
f16a781fcf
1 changed files with 30 additions and 7 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import java.io.IOError;
|
import java.io.IOError;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
@ -10,28 +11,50 @@
|
||||||
import pro.gravit.utils.helper.IOHelper;
|
import pro.gravit.utils.helper.IOHelper;
|
||||||
|
|
||||||
public final class UpdateData {
|
public final class UpdateData {
|
||||||
|
private static final class Pair {
|
||||||
|
private int cnt = 0;
|
||||||
|
private long size = 0;
|
||||||
|
}
|
||||||
public static final int COUNT_LIMIT = 4;
|
public static final int COUNT_LIMIT = 4;
|
||||||
public static final long AVG_LIMIT = IOHelper.MB*4;
|
public static final long AVG_LIMIT = IOHelper.MB*4;
|
||||||
public static final long TOTAL_LIMIT = IOHelper.MB*16;
|
public static final long TOTAL_LIMIT = IOHelper.MB*16;
|
||||||
|
public static final double SIMPLE_DOWNLOAD_SIZE_COFF = 0.75D;
|
||||||
|
public static final double SIMPLE_DOWNLOAD_FILE_COPF = 0.5D;
|
||||||
|
|
||||||
private UpdateData() {
|
private UpdateData() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean needsZip(Pair p) {
|
||||||
|
long avg = p.size/(long)p.cnt;
|
||||||
|
return p.size < TOTAL_LIMIT && avg < AVG_LIMIT && p.cnt > COUNT_LIMIT;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean needsZip(HashedDir hDir) {
|
public static boolean needsZip(HashedDir hDir) {
|
||||||
AtomicLong size = new AtomicLong();
|
return needsZip(count(hDir));
|
||||||
AtomicInteger cnt = new AtomicInteger();
|
}
|
||||||
|
|
||||||
|
public static boolean needsZipUpdate(HashedDir origd, HashedDir entryd) {
|
||||||
|
Pair orig = count(origd);
|
||||||
|
Pair entry = count(entryd);
|
||||||
|
if (!needsZip(orig)) return false;
|
||||||
|
double coffSize = BigDecimal.valueOf(entry.size).divide(BigDecimal.valueOf(orig.size)).doubleValue();
|
||||||
|
double coffCnt = BigDecimal.valueOf(entry.cnt).divide(BigDecimal.valueOf(orig.cnt)).doubleValue();
|
||||||
|
return coffSize >= SIMPLE_DOWNLOAD_SIZE_COFF && SIMPLE_DOWNLOAD_FILE_COPF <= coffCnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Pair count(HashedDir hDir) {
|
||||||
|
final Pair pair = new Pair();
|
||||||
try {
|
try {
|
||||||
hDir.walk(IOHelper.CROSS_SEPARATOR, (p,n,e) -> {
|
hDir.walk(IOHelper.CROSS_SEPARATOR, (p,n,e) -> {
|
||||||
if (e.getType().equals(HashedEntry.Type.FILE)) {
|
if (e.getType().equals(HashedEntry.Type.FILE)) {
|
||||||
cnt.incrementAndGet();
|
pair.cnt++;
|
||||||
size.addAndGet(e.size());
|
pair.size += e.size();
|
||||||
}
|
}
|
||||||
return HashedDir.WalkAction.CONTINUE;
|
return HashedDir.WalkAction.CONTINUE;
|
||||||
});
|
});
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IOError(e); // never happen
|
throw new IOError(e); // never happen
|
||||||
}
|
}
|
||||||
long avg = size.get()/(long)cnt.get();
|
return pair;
|
||||||
return size.get() < TOTAL_LIMIT && avg < AVG_LIMIT && cnt.get() > COUNT_LIMIT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue