2023-09-14 08:11:57 +00:00
|
|
|
package nu.marginalia.loading;
|
2023-03-04 12:19:01 +00:00
|
|
|
|
|
|
|
import com.google.inject.Inject;
|
|
|
|
import com.google.inject.Singleton;
|
2023-08-01 13:00:15 +00:00
|
|
|
import lombok.SneakyThrows;
|
2023-10-14 10:07:40 +00:00
|
|
|
import nu.marginalia.IndexLocations;
|
2024-07-27 09:44:13 +00:00
|
|
|
import nu.marginalia.index.journal.IndexJournal;
|
|
|
|
import nu.marginalia.index.journal.IndexJournalSlopWriter;
|
|
|
|
import nu.marginalia.model.processed.SlopDocumentRecord;
|
2023-10-14 10:07:40 +00:00
|
|
|
import nu.marginalia.storage.FileStorageService;
|
2023-03-04 12:19:01 +00:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2024-07-27 09:44:13 +00:00
|
|
|
import java.nio.file.Path;
|
2023-08-28 10:58:18 +00:00
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
|
|
|
|
@Singleton
|
|
|
|
public class LoaderIndexJournalWriter {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(LoaderIndexJournalWriter.class);
|
2024-07-27 09:44:13 +00:00
|
|
|
private final Path journalPath;
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
private IndexJournalSlopWriter currentWriter = null;
|
|
|
|
private long recordsWritten = 0;
|
|
|
|
private int page;
|
2023-09-01 11:52:00 +00:00
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
@Inject
|
2024-06-06 10:54:52 +00:00
|
|
|
public LoaderIndexJournalWriter(FileStorageService fileStorageService) throws IOException {
|
2023-10-14 10:07:40 +00:00
|
|
|
var indexArea = IndexLocations.getIndexConstructionArea(fileStorageService);
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
journalPath = IndexJournal.allocateName(indexArea);
|
|
|
|
page = IndexJournal.numPages(journalPath);
|
2023-07-17 19:20:31 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
switchToNextVersion();
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
logger.info("Creating Journal Writer {}", indexArea);
|
|
|
|
}
|
2023-09-13 14:13:41 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
private void switchToNextVersion() throws IOException {
|
|
|
|
if (currentWriter != null) {
|
|
|
|
currentWriter.close();
|
2023-08-07 10:57:38 +00:00
|
|
|
}
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
currentWriter = new IndexJournalSlopWriter(journalPath, page++);
|
|
|
|
}
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
@SneakyThrows
|
|
|
|
public void putWords(long header, SlopDocumentRecord.KeywordsProjection data)
|
|
|
|
{
|
|
|
|
if (++recordsWritten > 200_000) {
|
|
|
|
recordsWritten = 0;
|
|
|
|
switchToNextVersion();
|
|
|
|
}
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
currentWriter.put(header, data);
|
2023-03-04 12:19:01 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 09:44:13 +00:00
|
|
|
public void close() throws IOException {
|
|
|
|
currentWriter.close();
|
2023-03-04 12:19:01 +00:00
|
|
|
}
|
|
|
|
}
|