SitePaths: Convert index_dir to Path

Change-Id: I3e8234845bbde6aab9bf4f7bda3944187539cffb
This commit is contained in:
Dave Borowitz 2015-02-24 14:16:02 -08:00 committed by David Pursehouse
parent bcc2f5c98f
commit 6b37799da1
6 changed files with 54 additions and 47 deletions

View File

@ -89,8 +89,9 @@ import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.Iterator;
@ -223,7 +224,6 @@ public class LuceneChangeIndex implements ChangeIndex {
private final ListeningExecutorService executor;
private final Provider<ReviewDb> db;
private final ChangeData.Factory changeDataFactory;
private final File dir;
private final Schema<ChangeData> schema;
private final QueryBuilder queryBuilder;
private final SubIndex openIndex;
@ -246,11 +246,6 @@ public class LuceneChangeIndex implements ChangeIndex {
this.changeDataFactory = changeDataFactory;
this.schema = schema;
if (base == null) {
dir = LuceneVersionManager.getDir(sitePaths, schema);
} else {
dir = new File(base);
}
Version luceneVersion = checkNotNull(
LUCENE_VERSIONS.get(schema),
"unknown Lucene version for index schema: %s", schema);
@ -271,8 +266,10 @@ public class LuceneChangeIndex implements ChangeIndex {
openIndex = new SubIndex(new RAMDirectory(), "ramOpen", openConfig);
closedIndex = new SubIndex(new RAMDirectory(), "ramClosed", closedConfig);
} else {
openIndex = new SubIndex(new File(dir, CHANGES_OPEN), openConfig);
closedIndex = new SubIndex(new File(dir, CHANGES_CLOSED), closedConfig);
Path dir = base != null ? Paths.get(base)
: LuceneVersionManager.getDir(sitePaths, schema);
openIndex = new SubIndex(dir.resolve(CHANGES_OPEN), openConfig);
closedIndex = new SubIndex(dir.resolve(CHANGES_CLOSED), closedConfig);
}
}

View File

@ -36,8 +36,10 @@ import org.eclipse.jgit.util.FS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.TreeMap;
@ -65,15 +67,16 @@ class LuceneVersionManager implements LifecycleListener {
}
}
static File getDir(SitePaths sitePaths, Schema<ChangeData> schema) {
return new File(sitePaths.index_dir, String.format("%s%04d",
static Path getDir(SitePaths sitePaths, Schema<ChangeData> schema) {
return sitePaths.index_dir.resolve(String.format("%s%04d",
CHANGES_PREFIX, schema.getVersion()));
}
static FileBasedConfig loadGerritIndexConfig(SitePaths sitePaths)
throws ConfigInvalidException, IOException {
FileBasedConfig cfg = new FileBasedConfig(
new File(sitePaths.index_dir, "gerrit_index.config"), FS.detect());
sitePaths.index_dir.resolve("gerrit_index.config").toFile(),
FS.detect());
cfg.load();
return cfg;
}
@ -114,10 +117,10 @@ class LuceneVersionManager implements LifecycleListener {
throw fail(e);
}
if (!sitePaths.index_dir.exists()) {
if (!Files.exists(sitePaths.index_dir)) {
throw new ProvisionException("No index versions ready; run Reindex");
} else if (!sitePaths.index_dir.isDirectory()) {
log.warn("Not a directory: %s", sitePaths.index_dir.getAbsolutePath());
} else if (!Files.exists(sitePaths.index_dir)) {
log.warn("Not a directory: %s", sitePaths.index_dir.toAbsolutePath());
throw new ProvisionException("No index versions ready; run Reindex");
}
@ -167,29 +170,35 @@ class LuceneVersionManager implements LifecycleListener {
private TreeMap<Integer, Version> scanVersions(Config cfg) {
TreeMap<Integer, Version> versions = Maps.newTreeMap();
for (Schema<ChangeData> schema : ChangeSchemas.ALL.values()) {
File f = getDir(sitePaths, schema);
boolean exists = f.exists() && f.isDirectory();
if (f.exists() && !f.isDirectory()) {
log.warn("Not a directory: %s", f.getAbsolutePath());
Path p = getDir(sitePaths, schema);
boolean isDir = Files.isDirectory(p);
if (Files.exists(p) && !isDir) {
log.warn("Not a directory: %s", p.toAbsolutePath());
}
int v = schema.getVersion();
versions.put(v, new Version(schema, v, exists, getReady(cfg, v)));
versions.put(v, new Version(schema, v, isDir, getReady(cfg, v)));
}
for (File f : sitePaths.index_dir.listFiles()) {
if (!f.getName().startsWith(CHANGES_PREFIX)) {
continue;
}
String versionStr = f.getName().substring(CHANGES_PREFIX.length());
Integer v = Ints.tryParse(versionStr);
if (v == null || versionStr.length() != 4) {
log.warn("Unrecognized version in index directory: {}",
f.getAbsolutePath());
continue;
}
if (!versions.containsKey(v)) {
versions.put(v, new Version(null, v, true, getReady(cfg, v)));
try (DirectoryStream<Path> paths =
Files.newDirectoryStream(sitePaths.index_dir)) {
for (Path p : paths) {
String n = p.getFileName().toString();
if (!n.startsWith(CHANGES_PREFIX)) {
continue;
}
String versionStr = n.substring(CHANGES_PREFIX.length());
Integer v = Ints.tryParse(versionStr);
if (v == null || versionStr.length() != 4) {
log.warn("Unrecognized version in index directory: {}",
p.toAbsolutePath());
continue;
}
if (!versions.containsKey(v)) {
versions.put(v, new Version(null, v, true, getReady(cfg, v)));
}
}
} catch (IOException e) {
log.error("Error scanning index directory: " + sitePaths.index_dir, e);
}
return versions;
}

View File

@ -37,8 +37,8 @@ import org.apache.lucene.store.FSDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
@ -56,8 +56,9 @@ class SubIndex {
private final ControlledRealTimeReopenThread<IndexSearcher> reopenThread;
private final Set<NrtFuture> notDoneNrtFutures;
SubIndex(File file, GerritIndexWriterConfig writerConfig) throws IOException {
this(FSDirectory.open(file), file.getName(), writerConfig);
SubIndex(Path path, GerritIndexWriterConfig writerConfig) throws IOException {
this(FSDirectory.open(path.toFile()), path.getFileName().toString(),
writerConfig);
}
SubIndex(Directory dir, final String dirName,

View File

@ -41,7 +41,7 @@ public final class SitePaths {
public final File hooks_dir;
public final File static_dir;
public final File themes_dir;
public final File index_dir;
public final Path index_dir;
public final Path gerrit_sh;
public final Path gerrit_war;
@ -81,7 +81,7 @@ public final class SitePaths {
hooks_dir = new File(site_path, "hooks");
static_dir = new File(site_path, "static");
themes_dir = new File(site_path, "themes");
index_dir = new File(site_path, "index");
index_dir = p.resolve("index");
gerrit_sh = bin_dir.resolve("gerrit.sh");
gerrit_war = bin_dir.resolve("gerrit.war");

View File

@ -25,8 +25,8 @@ import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
class IndexVersionCheck implements LifecycleListener {
@ -34,8 +34,8 @@ class IndexVersionCheck implements LifecycleListener {
SolrChangeIndex.CHANGES_OPEN, ChangeSchemas.getLatest().getVersion(),
SolrChangeIndex.CHANGES_CLOSED, ChangeSchemas.getLatest().getVersion());
public static File solrIndexConfig(SitePaths sitePaths) {
return new File(sitePaths.index_dir, "gerrit_index.config");
public static Path solrIndexConfig(SitePaths sitePaths) {
return sitePaths.index_dir.resolve("gerrit_index.config");
}
private final SitePaths sitePaths;
@ -48,9 +48,9 @@ class IndexVersionCheck implements LifecycleListener {
@Override
public void start() {
// TODO Query schema version from a special meta-document
File file = solrIndexConfig(sitePaths);
Path path = solrIndexConfig(sitePaths);
try {
FileBasedConfig cfg = new FileBasedConfig(file, FS.detect());
FileBasedConfig cfg = new FileBasedConfig(path.toFile(), FS.detect());
cfg.load();
for (Map.Entry<String, Integer> e : SCHEMA_VERSIONS.entrySet()) {
int schemaVersion = cfg.getInt("index", e.getKey(), "schemaVersion", 0);
@ -61,9 +61,9 @@ class IndexVersionCheck implements LifecycleListener {
}
}
} catch (IOException e) {
throw new ProvisionException("unable to read " + file);
throw new ProvisionException("unable to read " + path);
} catch (ConfigInvalidException e) {
throw new ProvisionException("invalid config file " + file);
throw new ProvisionException("invalid config file " + path);
}
}

View File

@ -327,7 +327,7 @@ class SolrChangeIndex implements ChangeIndex, LifecycleListener {
public void markReady(boolean ready) throws IOException {
// TODO Move the schema version information to a special meta-document
FileBasedConfig cfg = new FileBasedConfig(
solrIndexConfig(sitePaths),
solrIndexConfig(sitePaths).toFile(),
FS.detect());
for (Map.Entry<String, Integer> e : SCHEMA_VERSIONS.entrySet()) {
cfg.setInt("index", e.getKey(), "schemaVersion",