(fs) interface cleanup

This commit is contained in:
Viktor Lofgren 2023-10-20 12:24:18 +02:00
parent 7b5ec6b98f
commit 584bb3a648
3 changed files with 25 additions and 21 deletions

View File

@ -491,6 +491,17 @@ public class FileStorageService {
public List<FileStorageId> getActiveFileStorages(FileStorageType type) throws SQLException {
return getActiveFileStorages(node, type);
}
public Optional<FileStorageId> getOnlyActiveFileStorage(FileStorageType type) throws SQLException {
return getOnlyActiveFileStorage(node, type);
}
public Optional<FileStorageId> getOnlyActiveFileStorage(int nodeId, FileStorageType type) throws SQLException {
var storages = getActiveFileStorages(nodeId, type);
if (storages.size() > 1) {
throw new IllegalStateException("Expected [0,1] instances of FileStorage with type " + type + ", found " + storages.size());
}
return storages.stream().findFirst();
}
public List<FileStorageId> getActiveFileStorages(int nodeId, FileStorageType type) throws SQLException
{

View File

@ -159,13 +159,13 @@ public class ControlNodeService {
private Object triggerAutoRecrawl(Request request, Response response) throws SQLException {
int nodeId = Integer.parseInt(request.params("id"));
var toCrawl = fileStorageService.getActiveFileStorages(nodeId, FileStorageType.CRAWL_DATA);
if (toCrawl.size() != 1)
throw new IllegalStateException();
var toCrawl = fileStorageService.getOnlyActiveFileStorage(nodeId, FileStorageType.CRAWL_DATA);
executorClient.triggerRecrawl(Context.fromRequest(request),
executorClient.triggerRecrawl(
Context.fromRequest(request),
nodeId,
toCrawl.get(0));
toCrawl.orElseThrow(AssertionError::new)
);
return redirectToOverview(request);
}
@ -173,13 +173,11 @@ public class ControlNodeService {
private Object triggerAutoProcess(Request request, Response response) throws SQLException {
int nodeId = Integer.parseInt(request.params("id"));
var toConvert = fileStorageService.getActiveFileStorages(nodeId, FileStorageType.CRAWL_DATA);
if (toConvert.size() != 1)
throw new IllegalStateException();
var toConvert = fileStorageService.getOnlyActiveFileStorage(nodeId, FileStorageType.CRAWL_DATA);
executorClient.triggerConvert(Context.fromRequest(request),
nodeId,
toConvert.get(0));
toConvert.orElseThrow(AssertionError::new));
return redirectToOverview(request);
}
@ -187,11 +185,11 @@ public class ControlNodeService {
private Object triggerLoadSelected(Request request, Response response) throws SQLException {
int nodeId = Integer.parseInt(request.params("id"));
var toLoad = fileStorageService.getActiveFileStorages(nodeId, FileStorageType.PROCESSED_DATA);
var toLoadStorages = fileStorageService.getActiveFileStorages(nodeId, FileStorageType.PROCESSED_DATA);
executorClient.loadProcessedData(Context.fromRequest(request),
nodeId,
new LoadParameters(toLoad)
new LoadParameters(toLoadStorages)
);
return redirectToOverview(request);

View File

@ -20,21 +20,14 @@ import nu.marginalia.process.log.WorkLog;
import nu.marginalia.service.module.ServiceConfiguration;
import nu.marginalia.storage.FileStorageService;
import nu.marginalia.storage.model.FileStorageBaseType;
import nu.marginalia.storage.model.FileStorageId;
import nu.marginalia.storage.model.FileStorageType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.UUID;
import java.util.zip.GZIPOutputStream;
@Singleton
public class TransferDomainsActor extends AbstractActorPrototype {
@ -89,7 +82,7 @@ public class TransferDomainsActor extends AbstractActorPrototype {
Ensure preconditions are met
""")
public Message init(Message message) throws Exception {
var storages = storageService.getActiveFileStorages(FileStorageType.CRAWL_DATA);
var storages = storageService.getOnlyActiveFileStorage(FileStorageType.CRAWL_DATA);
// Ensure crawl data exists to receive into
if (storages.isEmpty()) {
@ -100,7 +93,6 @@ public class TransferDomainsActor extends AbstractActorPrototype {
"Crawl Data"
);
storageService.enableFileStorage(storage.id());
}
return message;
@ -114,7 +106,10 @@ public class TransferDomainsActor extends AbstractActorPrototype {
"""
)
public Message transferData(Message message) throws Exception {
var storageId = storageService.getActiveFileStorages(FileStorageType.CRAWL_DATA).get(0);
var storageId = storageService
.getOnlyActiveFileStorage(FileStorageType.CRAWL_DATA)
.orElseThrow(AssertionError::new); // This Shouldn't Happen (tm)
var storage = storageService.getStorage(storageId);
var spec = executorClient.getTransferSpec(Context.internal(), message.sourceNode, message.count);