Update proccessing
This commit is contained in:
@@ -2,13 +2,18 @@ package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import me.bearns.fias.repository.FiasVersionRepository;
|
||||
import me.bearns.fias.util.DownlodableUpdate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
@@ -19,8 +24,6 @@ public class FiasUpdater implements Updater {
|
||||
|
||||
@Autowired
|
||||
OnlineVersion clientStub;
|
||||
@Autowired
|
||||
Downloader downloader;
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
@@ -65,33 +68,23 @@ public class FiasUpdater implements Updater {
|
||||
|
||||
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
|
||||
|
||||
//just for test
|
||||
final Stream<Future<File>> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()));
|
||||
final Stream<DownlodableUpdate> stream = reloadFlag ? updates.stream().map(DownlodableUpdate::processReload) : updates.stream().map(DownlodableUpdate::processUpdate);
|
||||
|
||||
processDownloads(stream, regions);
|
||||
for (DownlodableUpdate item : stream.collect(Collectors.toList())) {
|
||||
try {
|
||||
//todo process
|
||||
processFile(item.getFile(), regions);
|
||||
versions.save(item.getVersion());
|
||||
|
||||
//todo save updates
|
||||
|
||||
}
|
||||
|
||||
private void processDownloads(Stream<Future<File>> futureStream, Long ... regions) {
|
||||
|
||||
final Stream<File> fileStream = futureStream.map(fileFuture -> {
|
||||
try {
|
||||
return fileFuture.get();
|
||||
} catch (Exception e) {
|
||||
//todo log
|
||||
return null;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
//throw exception
|
||||
assert fileStream.allMatch(Objects::nonNull);
|
||||
|
||||
fileStream.forEachOrdered(f -> {
|
||||
//todo
|
||||
|
||||
});
|
||||
private void processFile(File file, Long ... regions) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package me.bearns.fias.util;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import me.bearns.fias.service.Downloader;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class DownlodableUpdate {
|
||||
|
||||
@Autowired
|
||||
private static Downloader service;
|
||||
|
||||
private final FiasVersion update;
|
||||
private final Future<File> file;
|
||||
|
||||
private static DownlodableUpdate process(FiasVersion ver, boolean reload) {
|
||||
return new DownlodableUpdate(ver, service.download(reload ? ver.getFiasCompleteXmlUrl() : ver.getFiasDeltaXmlUrl()));
|
||||
}
|
||||
|
||||
public static DownlodableUpdate processReload(FiasVersion ver){
|
||||
return process(ver, true);
|
||||
}
|
||||
|
||||
public static DownlodableUpdate processUpdate(FiasVersion ver){
|
||||
return process(ver, false);
|
||||
}
|
||||
|
||||
private DownlodableUpdate(FiasVersion update, Future<File> file) {
|
||||
this.update = update;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public File getFile() throws ExecutionException, InterruptedException {
|
||||
return file.get();
|
||||
}
|
||||
|
||||
public FiasVersion getVersion() {
|
||||
return update;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user