2023-03-04 12:19:01 +00:00
|
|
|
package nu.marginalia.loading;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.inject.AbstractModule;
|
2023-08-24 09:55:58 +00:00
|
|
|
import com.google.inject.Inject;
|
|
|
|
import com.google.inject.Provides;
|
|
|
|
import com.google.inject.Singleton;
|
2023-03-04 12:19:01 +00:00
|
|
|
import com.google.inject.name.Names;
|
|
|
|
import nu.marginalia.LanguageModels;
|
|
|
|
import nu.marginalia.WmsaHome;
|
2023-10-14 10:07:40 +00:00
|
|
|
import nu.marginalia.IndexLocations;
|
2024-01-08 18:56:33 +00:00
|
|
|
import nu.marginalia.linkdb.dlinks.DomainLinkDbWriter;
|
2023-10-14 10:07:40 +00:00
|
|
|
import nu.marginalia.storage.FileStorageService;
|
2024-01-08 18:56:33 +00:00
|
|
|
import nu.marginalia.linkdb.docs.DocumentDbWriter;
|
2023-03-04 12:19:01 +00:00
|
|
|
import nu.marginalia.model.gson.GsonFactory;
|
|
|
|
|
2023-08-24 09:55:58 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
2023-03-04 12:19:01 +00:00
|
|
|
import java.nio.file.Path;
|
2023-08-24 09:55:58 +00:00
|
|
|
import java.sql.SQLException;
|
2023-03-04 12:19:01 +00:00
|
|
|
|
2024-01-08 14:53:13 +00:00
|
|
|
import static nu.marginalia.linkdb.LinkdbFileNames.DOCDB_FILE_NAME;
|
|
|
|
import static nu.marginalia.linkdb.LinkdbFileNames.DOMAIN_LINKS_FILE_NAME;
|
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
public class LoaderModule extends AbstractModule {
|
|
|
|
|
2023-07-14 15:08:10 +00:00
|
|
|
public LoaderModule() {
|
2023-03-04 12:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void configure() {
|
2023-07-11 15:08:43 +00:00
|
|
|
bind(Gson.class).toProvider(this::createGson);
|
2023-03-04 12:19:01 +00:00
|
|
|
bind(Path.class).annotatedWith(Names.named("local-index-path")).toInstance(Path.of(System.getProperty("local-index-path", "/vol")));
|
|
|
|
bind(LanguageModels.class).toInstance(WmsaHome.getLanguageModels());
|
|
|
|
}
|
|
|
|
|
2023-08-24 09:55:58 +00:00
|
|
|
@Inject @Provides @Singleton
|
2024-01-08 14:53:13 +00:00
|
|
|
private DocumentDbWriter createLinkdbWriter(FileStorageService service) throws SQLException, IOException {
|
|
|
|
// Migrate
|
|
|
|
Path dbPath = IndexLocations.getLinkdbWritePath(service).resolve(DOCDB_FILE_NAME);
|
2023-10-14 10:07:40 +00:00
|
|
|
|
2024-01-08 14:53:13 +00:00
|
|
|
if (Files.exists(dbPath)) {
|
|
|
|
Files.delete(dbPath);
|
|
|
|
}
|
|
|
|
return new DocumentDbWriter(dbPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Inject @Provides @Singleton
|
|
|
|
private DomainLinkDbWriter createDomainLinkdbWriter(FileStorageService service) throws SQLException, IOException {
|
|
|
|
|
|
|
|
Path dbPath = IndexLocations.getLinkdbWritePath(service).resolve(DOMAIN_LINKS_FILE_NAME);
|
2023-08-24 09:55:58 +00:00
|
|
|
|
|
|
|
if (Files.exists(dbPath)) {
|
|
|
|
Files.delete(dbPath);
|
|
|
|
}
|
2024-01-08 14:53:13 +00:00
|
|
|
|
|
|
|
return new DomainLinkDbWriter(dbPath);
|
2023-08-24 09:55:58 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 12:19:01 +00:00
|
|
|
private Gson createGson() {
|
|
|
|
return GsonFactory.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|