2021-03-07 15:50:11 +01:00
|
|
|
/*
|
|
|
|
* FileSponge
|
|
|
|
* Copyright (C) 2021 Andrea Cavalli
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.warp.filesponge;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2021-03-09 00:35:24 +01:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2021-06-02 02:16:13 +02:00
|
|
|
import org.warp.commonutils.log.Logger;
|
|
|
|
import org.warp.commonutils.log.LoggerFactory;
|
2021-03-07 15:50:11 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
2021-05-05 17:31:50 +02:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2021-03-07 15:50:11 +01:00
|
|
|
|
2021-03-07 15:51:54 +01:00
|
|
|
public class FileSponge implements URLsHandler {
|
2021-03-07 15:50:11 +01:00
|
|
|
|
2021-03-09 00:35:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(FileSponge.class);
|
|
|
|
|
|
|
|
public static final int BLOCK_SIZE = 1024 * 1024; // 1 MiB
|
2021-03-07 15:50:11 +01:00
|
|
|
|
|
|
|
private final Set<URLsHandler> urlsHandlers = new ConcurrentHashMap<URLsHandler, Object>().keySet(new Object());
|
|
|
|
|
|
|
|
private final Set<URLsDiskHandler> cacheAccess = new ConcurrentHashMap<URLsDiskHandler, Object>().keySet(new Object());
|
|
|
|
private final Set<URLsWriter> cacheWrite = new ConcurrentHashMap<URLsWriter, Object>().keySet(new Object());
|
|
|
|
|
2021-03-07 15:51:54 +01:00
|
|
|
public FileSponge() {
|
2021-03-07 15:50:11 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Mono<Void> registerSource(URLsHandler urLsHandler) {
|
|
|
|
return Mono.fromRunnable(() -> urlsHandlers.add(urLsHandler));
|
|
|
|
}
|
|
|
|
|
|
|
|
public <T extends URLsDiskHandler & URLsWriter> Mono<Void> registerCache(T urlsCache) {
|
|
|
|
return Mono.fromRunnable(() -> {
|
|
|
|
cacheAccess.add(urlsCache);
|
|
|
|
cacheWrite.add(urlsCache);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Flux<DataBlock> requestContent(URL url) {
|
2021-03-09 00:35:24 +01:00
|
|
|
AtomicBoolean alreadyPrintedDebug = new AtomicBoolean(false);
|
2021-03-07 15:50:11 +01:00
|
|
|
return Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(cacheAccess::stream)
|
2021-03-08 13:42:52 +01:00
|
|
|
.map(urlsHandler -> urlsHandler.requestContent(url))
|
|
|
|
.collectList()
|
2021-03-09 00:35:24 +01:00
|
|
|
.flatMapMany(monos -> FileSpongeUtils.firstWithValueFlux(monos))
|
|
|
|
.doOnNext(dataBlock -> {
|
|
|
|
if (alreadyPrintedDebug.compareAndSet(false, true)) {
|
|
|
|
logger.debug("File \"{}\" content has been found in the cache", url);
|
|
|
|
}
|
|
|
|
})
|
2021-03-07 15:50:11 +01:00
|
|
|
.switchIfEmpty(Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(urlsHandlers::stream)
|
2021-03-09 00:35:24 +01:00
|
|
|
.doOnSubscribe(s -> logger.debug("Downloading file \"{}\" content", url))
|
2021-03-08 13:42:52 +01:00
|
|
|
.map(urlsHandler -> urlsHandler
|
2021-03-07 15:50:11 +01:00
|
|
|
.requestContent(url)
|
|
|
|
.flatMapSequential(dataBlock -> Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(cacheWrite::stream)
|
2021-03-07 15:50:11 +01:00
|
|
|
.flatMapSequential(cw -> cw.writeContentBlock(url, dataBlock))
|
|
|
|
.then(Mono.just(dataBlock))
|
|
|
|
)
|
|
|
|
)
|
2021-03-08 13:42:52 +01:00
|
|
|
.collectList()
|
2021-03-09 00:35:24 +01:00
|
|
|
.flatMapMany(monos -> FileSpongeUtils.firstWithValueFlux(monos))
|
|
|
|
.doOnComplete(() -> logger.debug("Downloaded file \"{}\" content", url))
|
2021-03-07 15:50:11 +01:00
|
|
|
)
|
2021-05-05 17:31:50 +02:00
|
|
|
.distinct(DataBlock::getId)
|
2021-06-06 02:24:21 +02:00
|
|
|
|
|
|
|
.doOnDiscard(DataBlock.class, DataBlock::release)
|
|
|
|
.doOnDiscard(Flux.class, f -> {
|
|
|
|
//noinspection unchecked
|
|
|
|
Flux<DataBlock> flux = (Flux<DataBlock>) f;
|
|
|
|
flux.doOnNext(DataBlock::release).subscribeOn(Schedulers.single()).subscribe();
|
|
|
|
});
|
2021-03-07 15:50:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Mono<Metadata> requestMetadata(URL url) {
|
2021-03-08 13:42:52 +01:00
|
|
|
return Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(cacheAccess::stream)
|
2021-03-08 13:42:52 +01:00
|
|
|
.map(urlsHandler -> urlsHandler.requestMetadata(url))
|
|
|
|
.collectList()
|
2021-03-09 00:35:24 +01:00
|
|
|
.flatMap(monos -> FileSpongeUtils.firstWithValueMono(monos))
|
|
|
|
.doOnSuccess(metadata -> {
|
|
|
|
if (metadata != null) {
|
|
|
|
logger.debug("File \"{}\" metadata has been found in the cache", url);
|
|
|
|
}
|
|
|
|
})
|
2021-03-07 15:50:11 +01:00
|
|
|
.switchIfEmpty(Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(urlsHandlers::stream)
|
2021-03-09 00:35:24 +01:00
|
|
|
.doOnSubscribe(s -> logger.debug("Downloading file \"{}\" metadata", url))
|
2021-03-08 13:42:52 +01:00
|
|
|
.map(urlsHandler -> urlsHandler
|
2021-03-07 15:50:11 +01:00
|
|
|
.requestMetadata(url)
|
|
|
|
.flatMap(dataBlock -> Flux
|
2021-06-06 02:24:21 +02:00
|
|
|
.fromStream(cacheWrite::stream)
|
2021-03-07 15:50:11 +01:00
|
|
|
.flatMapSequential(cw -> cw.writeMetadata(url, dataBlock))
|
|
|
|
.then(Mono.just(dataBlock))
|
|
|
|
)
|
|
|
|
)
|
2021-03-08 13:42:52 +01:00
|
|
|
.collectList()
|
2021-03-09 00:35:24 +01:00
|
|
|
.flatMap(monos -> FileSpongeUtils.firstWithValueMono(monos))
|
|
|
|
.doOnSuccess(s -> {
|
|
|
|
if (s != null) {
|
|
|
|
logger.debug("Downloaded file \"{}\" metadata", url);
|
|
|
|
} else {
|
|
|
|
logger.debug("File \"{}\" metadata has not been found anywhere", url);
|
|
|
|
}
|
|
|
|
})
|
2021-03-08 13:42:52 +01:00
|
|
|
);
|
|
|
|
}
|
2021-03-07 15:50:11 +01:00
|
|
|
}
|