Update proccessing, remove unnessesary functional code
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package me.bearns.fias.exceptions;
|
||||
|
||||
public class DownloadException extends Exception {
|
||||
public DownloadException(Exception e) {
|
||||
super(e);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,16 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.domain.FiasVersion;
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
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.Map;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -22,11 +22,14 @@ public class FiasUpdater implements Updater {
|
||||
@Autowired
|
||||
FiasVersionRepository versions;
|
||||
|
||||
@Autowired
|
||||
private static Downloader service;
|
||||
|
||||
@Autowired
|
||||
OnlineVersion clientStub;
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
public void update() throws DownloadException {
|
||||
|
||||
//Max value
|
||||
FiasVersion dbVersionObj = versions.findTopByOrderByVersionIdDesc();
|
||||
@@ -52,7 +55,7 @@ public class FiasUpdater implements Updater {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reload(Long... regions) {
|
||||
public void reload(Long... regions) throws DownloadException {
|
||||
|
||||
final List<FiasVersion> lastVersion = clientStub.getLastVersion();
|
||||
if(lastVersion != null) {
|
||||
@@ -62,29 +65,46 @@ public class FiasUpdater implements Updater {
|
||||
}
|
||||
|
||||
//transaction from here
|
||||
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) {
|
||||
private void processUpdates(List<FiasVersion> updates, boolean reloadFlag, Long ... regions) throws DownloadException {
|
||||
|
||||
if(reloadFlag) {
|
||||
//todo reload flag impl
|
||||
versions.deleteAll();
|
||||
}
|
||||
|
||||
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
|
||||
|
||||
final Stream<DownlodableUpdate> stream = reloadFlag ? updates.stream().map(DownlodableUpdate::processReload) : updates.stream().map(DownlodableUpdate::processUpdate);
|
||||
Map<FiasVersion, Future<File>> map = new HashMap<>();
|
||||
|
||||
for (DownlodableUpdate item : stream.collect(Collectors.toList())) {
|
||||
//start download
|
||||
updates.forEach(u -> map.put(u, service.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl())));
|
||||
|
||||
//strict order
|
||||
for (FiasVersion item : updates) {
|
||||
|
||||
final File file;
|
||||
try {
|
||||
//todo process
|
||||
processFile(item.getFile(), regions);
|
||||
versions.save(item.getVersion());
|
||||
//wait for downloading
|
||||
file = map.get(item).get();
|
||||
|
||||
//todo save updates
|
||||
} catch (Exception e) {
|
||||
//process update
|
||||
processArchive(file, regions);
|
||||
//apply this version
|
||||
versions.save(item);
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
//todo log
|
||||
break;
|
||||
throw new DownloadException(e);
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
//todo log
|
||||
throw new DownloadException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void processFile(File file, Long ... regions) {
|
||||
private void processArchive(File file, Long ... regions) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,18 @@
|
||||
package me.bearns.fias.service;
|
||||
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
|
||||
public interface Updater {
|
||||
|
||||
|
||||
/**
|
||||
* Run online update for fias database
|
||||
*/
|
||||
public void update();
|
||||
public void update() throws DownloadException;
|
||||
|
||||
/**
|
||||
* Load or reload database for selected regions
|
||||
* @param regions
|
||||
*/
|
||||
public void reload(Long ... regions);
|
||||
public void reload(Long ... regions) throws DownloadException;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package me.bearns.fias.webapi;
|
||||
|
||||
import me.bearns.fias.exceptions.DownloadException;
|
||||
import me.bearns.fias.service.Updater;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -13,6 +14,11 @@ public class API {
|
||||
|
||||
@GetMapping("/update")
|
||||
void update(){
|
||||
try {
|
||||
service.update();
|
||||
} catch (DownloadException e) {
|
||||
//TODO
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user