Downloader implementation
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
package me.bearns.fias.service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
public interface Downloader {
|
||||||
|
|
||||||
|
public Future<InputStream> download(String url);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package me.bearns.fias.service;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
public class DownloaderImpl implements Downloader {
|
||||||
|
|
||||||
|
private static final ExecutorService pool = Executors.newCachedThreadPool();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Future<InputStream> download(String url) {
|
||||||
|
|
||||||
|
CompletableFuture<InputStream> completableFuture
|
||||||
|
= new CompletableFuture<>();
|
||||||
|
|
||||||
|
pool.submit(() -> {
|
||||||
|
InputStream is = null;
|
||||||
|
//todo
|
||||||
|
|
||||||
|
completableFuture.complete(is);
|
||||||
|
});
|
||||||
|
|
||||||
|
return completableFuture;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,10 @@ import me.bearns.fias.repository.FiasVersionRepository;
|
|||||||
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.InputStream;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FiasUpdater implements Updater {
|
public class FiasUpdater implements Updater {
|
||||||
@@ -14,7 +17,9 @@ public class FiasUpdater implements Updater {
|
|||||||
FiasVersionRepository versions;
|
FiasVersionRepository versions;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
OnlineVersion downloadService;
|
OnlineVersion clientStub;
|
||||||
|
@Autowired
|
||||||
|
Downloader downloader;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
@@ -26,12 +31,12 @@ public class FiasUpdater implements Updater {
|
|||||||
|
|
||||||
final Long dbVersion = dbVersionObj.getVersionId();
|
final Long dbVersion = dbVersionObj.getVersionId();
|
||||||
|
|
||||||
final long lastOnlineVersion = downloadService.getLastVersionId();
|
final long lastOnlineVersion = clientStub.getLastVersionId();
|
||||||
|
|
||||||
if(lastOnlineVersion > dbVersion) {
|
if(lastOnlineVersion > dbVersion) {
|
||||||
//need update
|
//need update
|
||||||
|
|
||||||
final List<FiasVersion> updates = downloadService.getVersionsAfter(dbVersion);
|
final List<FiasVersion> updates = clientStub.getVersionsAfter(dbVersion);
|
||||||
|
|
||||||
if(updates != null && !updates.isEmpty()) {
|
if(updates != null && !updates.isEmpty()) {
|
||||||
processUpdates(updates, false);
|
processUpdates(updates, false);
|
||||||
@@ -45,7 +50,7 @@ public class FiasUpdater implements Updater {
|
|||||||
@Override
|
@Override
|
||||||
public void reload(Long... regions) {
|
public void reload(Long... regions) {
|
||||||
|
|
||||||
final List<FiasVersion> lastVersion = downloadService.getLastVersion();
|
final List<FiasVersion> lastVersion = clientStub.getLastVersion();
|
||||||
if(lastVersion != null) {
|
if(lastVersion != null) {
|
||||||
processUpdates(lastVersion, true, regions);
|
processUpdates(lastVersion, true, regions);
|
||||||
}
|
}
|
||||||
@@ -57,14 +62,9 @@ public class FiasUpdater implements Updater {
|
|||||||
|
|
||||||
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
|
updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId()));
|
||||||
|
|
||||||
//pre-fetch files if update multiple version
|
//just for test
|
||||||
|
final Stream<Future<InputStream>> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl()));
|
||||||
|
|
||||||
for (FiasVersion update : updates) {
|
|
||||||
|
|
||||||
final String url = reloadFlag ? update.getFiasCompleteXmlUrl() : update.getFiasDeltaXmlUrl();
|
|
||||||
|
|
||||||
//run update here
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user