FileSponge/src/main/java/org/warp/filesponge/FileSponge.java

170 lines
5.7 KiB
Java
Raw Normal View History

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;
2021-06-29 23:49:52 +02:00
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
2021-09-17 20:38:28 +02:00
import java.util.ArrayList;
import java.util.List;
2021-03-07 15:50:11 +01:00
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
2021-03-09 00:35:24 +01:00
import java.util.concurrent.atomic.AtomicBoolean;
2022-01-04 12:59:21 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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
2022-01-04 12:59:21 +01:00
private static final Logger logger = LogManager.getLogger(FileSponge.class);
2021-03-09 00:35:24 +01:00
public static final int BLOCK_SIZE = 1024 * 1024; // 1 MiB
2021-03-07 15:50:11 +01:00
2021-06-29 23:49:52 +02:00
private final Object structuresLock = new Object();
private volatile ObjectOpenHashSet<URLsHandler> urlsHandlers = ObjectOpenHashSet.of();
private volatile ObjectOpenHashSet<URLsDiskHandler> cacheAccess = ObjectOpenHashSet.of();
private volatile ObjectOpenHashSet<URLsWriter> cacheWrite = ObjectOpenHashSet.of();
2021-03-07 15:50:11 +01:00
2021-03-07 15:51:54 +01:00
public FileSponge() {
2021-03-07 15:50:11 +01:00
}
public Mono<Void> registerSource(URLsHandler urLsHandler) {
2021-06-29 23:49:52 +02:00
return Mono
.<Void>fromRunnable(() -> {
synchronized (structuresLock) {
var clone = urlsHandlers.clone();
clone.add(urLsHandler);
this.urlsHandlers = clone;
}
})
.subscribeOn(Schedulers.boundedElastic());
2021-03-07 15:50:11 +01:00
}
public <T extends URLsDiskHandler & URLsWriter> Mono<Void> registerCache(T urlsCache) {
2021-06-29 23:49:52 +02:00
return Mono
.<Void>fromRunnable(() -> {
synchronized (structuresLock) {
2021-09-17 20:42:35 +02:00
var cacheAccessClone = this.cacheAccess.clone();
2021-06-29 23:49:52 +02:00
cacheAccessClone.add(urlsCache);
this.cacheAccess = cacheAccessClone;
var cacheWriteClone = cacheWrite.clone();
cacheWriteClone.add(urlsCache);
this.cacheWrite = cacheWriteClone;
}
})
.subscribeOn(Schedulers.boundedElastic());
2021-03-07 15:50:11 +01:00
}
@Override
public Flux<DataBlock> requestContent(URL url) {
2021-03-09 00:35:24 +01:00
AtomicBoolean alreadyPrintedDebug = new AtomicBoolean(false);
2021-09-17 20:38:28 +02:00
return Mono
.fromCallable(() -> {
2021-09-17 20:42:35 +02:00
var ca = this.cacheAccess;
List<Flux<DataBlock>> contentRequests = new ArrayList<>(ca.size());
for (URLsDiskHandler urlsHandler : ca) {
2021-09-17 20:38:28 +02:00
contentRequests.add(urlsHandler.requestContent(url));
}
return contentRequests;
})
2021-06-29 23:49:52 +02:00
.flatMapMany(FileSpongeUtils::firstWithValueFlux)
2021-03-09 00:35:24 +01:00
.doOnNext(dataBlock -> {
if (alreadyPrintedDebug.compareAndSet(false, true)) {
logger.debug("File \"{}\" content has been found in the cache", url);
}
})
2021-09-17 20:38:28 +02:00
.switchIfEmpty(Mono
.fromCallable(() -> {
logger.debug("Downloading file \"{}\" content", url);
2021-09-17 20:42:35 +02:00
var uh = this.urlsHandlers;
List<Flux<DataBlock>> contentRequestsAndCaching = new ArrayList<>(uh.size());
for (URLsHandler urlsHandler : uh) {
2021-09-17 20:38:28 +02:00
contentRequestsAndCaching.add(urlsHandler
.requestContent(url)
.flatMapSequential(dataBlock -> {
2021-09-17 20:42:35 +02:00
var cw = this.cacheWrite;
List<Mono<Void>> cacheWriteActions = new ArrayList<>(cw.size());
for (URLsWriter urlsWriter : cw) {
2021-09-17 20:38:28 +02:00
cacheWriteActions.add(urlsWriter.writeContentBlock(url, dataBlock));
}
return Mono.when(cacheWriteActions).thenReturn(dataBlock);
})
);
}
return contentRequestsAndCaching;
})
2021-06-29 23:49:52 +02:00
.flatMapMany(FileSpongeUtils::firstWithValueFlux)
2021-03-09 00:35:24 +01:00
.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
2021-09-03 02:23:19 +02:00
.doOnDiscard(DataBlock.class, DataBlock::close);
2021-03-07 15:50:11 +01:00
}
@Override
public Mono<Metadata> requestMetadata(URL url) {
2021-09-17 20:38:28 +02:00
return Mono
.fromCallable(() -> {
2021-09-17 20:42:35 +02:00
var ca = this.cacheAccess;
List<Mono<Metadata>> metadataRequests = new ArrayList<>(ca.size());
for (URLsDiskHandler urlsHandler : ca) {
2021-09-17 20:38:28 +02:00
metadataRequests.add(urlsHandler.requestMetadata(url));
}
return metadataRequests;
})
2021-06-29 23:49:52 +02:00
.flatMap(FileSpongeUtils::firstWithValueMono)
2021-03-09 00:35:24 +01:00
.doOnSuccess(metadata -> {
if (metadata != null) {
logger.debug("File \"{}\" metadata has been found in the cache", url);
}
})
2021-09-17 20:38:28 +02:00
.switchIfEmpty(Mono
.fromCallable(() -> {
logger.debug("Downloading file \"{}\" metadata", url);
2021-09-17 20:42:35 +02:00
var uh = this.urlsHandlers;
List<Mono<Metadata>> metadataRequestsAndCaching = new ArrayList<>(uh.size());
for (URLsHandler urlsHandler : uh) {
2021-09-17 20:38:28 +02:00
metadataRequestsAndCaching.add(urlsHandler
.requestMetadata(url)
.flatMap(meta -> {
2021-09-17 20:42:35 +02:00
var cw = this.cacheWrite;
List<Mono<Void>> cacheWriteActions = new ArrayList<>(cw.size());
for (URLsWriter urlsWriter : cw) {
2021-09-17 20:38:28 +02:00
cacheWriteActions.add(urlsWriter.writeMetadata(url, meta));
}
return Mono.when(cacheWriteActions).thenReturn(meta);
})
);
}
return metadataRequestsAndCaching;
})
2021-06-29 23:49:52 +02:00
.flatMap(FileSpongeUtils::firstWithValueMono)
2021-03-09 00:35:24 +01:00
.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
}