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;
|
package me.bearns.fias.service;
|
||||||
|
|
||||||
import me.bearns.fias.domain.FiasVersion;
|
import me.bearns.fias.domain.FiasVersion;
|
||||||
|
import me.bearns.fias.exceptions.DownloadException;
|
||||||
import me.bearns.fias.repository.FiasVersionRepository;
|
import me.bearns.fias.repository.FiasVersionRepository;
|
||||||
import me.bearns.fias.util.DownlodableUpdate;
|
import me.bearns.fias.util.DownlodableUpdate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.AbstractMap;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -22,11 +22,14 @@ public class FiasUpdater implements Updater {
|
|||||||
@Autowired
|
@Autowired
|
||||||
FiasVersionRepository versions;
|
FiasVersionRepository versions;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private static Downloader service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
OnlineVersion clientStub;
|
OnlineVersion clientStub;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() throws DownloadException {
|
||||||
|
|
||||||
//Max value
|
//Max value
|
||||||
FiasVersion dbVersionObj = versions.findTopByOrderByVersionIdDesc();
|
FiasVersion dbVersionObj = versions.findTopByOrderByVersionIdDesc();
|
||||||
@@ -52,7 +55,7 @@ public class FiasUpdater implements Updater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void reload(Long... regions) {
|
public void reload(Long... regions) throws DownloadException {
|
||||||
|
|
||||||
final List<FiasVersion> lastVersion = clientStub.getLastVersion();
|
final List<FiasVersion> lastVersion = clientStub.getLastVersion();
|
||||||
if(lastVersion != null) {
|
if(lastVersion != null) {
|
||||||
@@ -62,29 +65,46 @@ public class FiasUpdater implements Updater {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//transaction from here
|
//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
|
//todo reload flag impl
|
||||||
|
versions.deleteAll();
|
||||||
|
}
|
||||||
|
|
||||||
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
|
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 {
|
try {
|
||||||
//todo process
|
//wait for downloading
|
||||||
processFile(item.getFile(), regions);
|
file = map.get(item).get();
|
||||||
versions.save(item.getVersion());
|
|
||||||
|
|
||||||
//todo save updates
|
//process update
|
||||||
} catch (Exception e) {
|
processArchive(file, regions);
|
||||||
|
//apply this version
|
||||||
|
versions.save(item);
|
||||||
|
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
//todo log
|
//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;
|
package me.bearns.fias.service;
|
||||||
|
|
||||||
|
import me.bearns.fias.exceptions.DownloadException;
|
||||||
|
|
||||||
public interface Updater {
|
public interface Updater {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run online update for fias database
|
* Run online update for fias database
|
||||||
*/
|
*/
|
||||||
public void update();
|
public void update() throws DownloadException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load or reload database for selected regions
|
* Load or reload database for selected regions
|
||||||
* @param regions
|
* @param regions
|
||||||
*/
|
*/
|
||||||
public void reload(Long ... regions);
|
public void reload(Long ... regions) throws DownloadException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package me.bearns.fias.webapi;
|
package me.bearns.fias.webapi;
|
||||||
|
|
||||||
|
import me.bearns.fias.exceptions.DownloadException;
|
||||||
import me.bearns.fias.service.Updater;
|
import me.bearns.fias.service.Updater;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@@ -13,6 +14,11 @@ public class API {
|
|||||||
|
|
||||||
@GetMapping("/update")
|
@GetMapping("/update")
|
||||||
void update(){
|
void update(){
|
||||||
|
try {
|
||||||
service.update();
|
service.update();
|
||||||
|
} catch (DownloadException e) {
|
||||||
|
//TODO
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user