IndexingBase.java
/*
* Copyright © 2025 The CTAN Team and individual authors
*
* This file is distributed under the 3-clause BSD license.
* See file LICENSE for details.
*/
package org.ctan.site.services.search.base;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.lucene.store.FSDirectory;
import org.ctan.site.CtanConfiguration.CtanConfig;
import org.ctan.site.CtanConfiguration.IndexConfig;
import org.ctan.site.services.util.NullCheck;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.Getter;
import lombok.NonNull;
/**
* The class <code>IndexingBase</code> contains the base class for indexing.
*
* @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
*/
public abstract class IndexingBase {
/**
* The field <code>indexBase</code> contains the cached base directory for
* the Lucene index files.
*/
@Getter
private File indexBase;
/**
* This is the constructor for <code>IndexingBase</code>.
*
* @param indexBase the base directory
*/
@SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW")
public IndexingBase(@NonNull File indexBase) {
this.indexBase = indexBase;
}
/**
* This is the constructor for <code>IndexingBase</code>.
*
* @param config the configuration
* @throws FileNotFoundException in case of an improper base directory
*/
@SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW")
public IndexingBase(@NonNull IndexConfig config)
throws FileNotFoundException {
var dir = config.getDirectory();
NullCheck.isNotNullObject(dir, "config.directory");
this.indexBase = new File(config.getDirectory());
}
/**
* The method <code>checkIndexDirectory</code> provides means to check
* whether the index exists.
*
* @param dir the directory denoting the base directory for the index
* @return the argument
* @throws FileNotFoundException in case of an error
*/
protected File checkIndexDirectory(File dir)
throws FileNotFoundException {
if (!dir.isDirectory()) {
throw new FileNotFoundException("directory is no index directory: "
+ dir.getName());
}
for (var it : IndexType.values()) {
for (var locale : CtanConfig.LOCALES) {
if (!indexPath(it, locale).toFile().exists()) {
throw new FileNotFoundException("missing index directory: "
+ directory(it, locale));
}
}
}
return dir;
}
/**
* The method <code>directory</code> provides means to get the name of the
* directory.
*
* @param type the type
* @param locale the language
* @return the directory name
*/
protected String directory(IndexType type, String locale) {
return type.getKey() + "-" + locale;
}
/**
* The method <code>indexPath</code> provides means to get the index path.
*
* @param type the type
* @param locale the language
* @return the path
*/
protected Path indexPath(IndexType type, String locale) {
return Path.of(indexBase + "/" + directory(type, locale));
}
/**
* The method <code>open</code> provides means to open an index.
*
* @param type the type
* @param locale the language
* @return the directory
* @throws IOException in case of an I/O error
*/
protected FSDirectory open(IndexType type, String locale)
throws IOException {
return FSDirectory.open(indexPath(type, locale));
}
/**
* The method <code>open</code> provides means to open an index.
*
* @param index the index
* @return the directory
* @throws IOException in case of an I/O error
*/
protected FSDirectory open(String index) throws IOException {
return FSDirectory.open(Path.of(indexBase + "/" + index));
}
}