Extract an abstract base class for NotesMigration

We want to be able to provide a different implementation for testing
that does not require restarting the server in order to pick up
changes. This will allow us to test rebuilding on a running server,
which fits better with the way existing acceptance tests are set up.

Change-Id: Ie0e30d86db9f951995110a09696decb0fc885381
This commit is contained in:
Dave Borowitz
2016-02-11 16:00:56 -05:00
parent 8bf3bbb0b8
commit 89b9bbc4d0
12 changed files with 137 additions and 90 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2013 The Android Open Source Project
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -14,17 +14,6 @@
package com.google.gerrit.server.notedb;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import java.util.HashSet;
import java.util.Set;
/**
* Holds the current state of the NoteDb migration.
* <p>
@@ -44,77 +33,13 @@ import java.util.Set;
* Changing options quite likely requires re-running {@code RebuildNoteDb}. For
* these reasons, the options remain undocumented.
*/
@Singleton
public class NotesMigration {
private static enum Table {
CHANGES;
public abstract class NotesMigration {
public abstract boolean readChanges();
private String key() {
return name().toLowerCase();
}
}
private static final String NOTEDB = "notedb";
private static final String READ = "read";
private static final String WRITE = "write";
private static void checkConfig(Config cfg) {
Set<String> keys = new HashSet<>();
for (Table t : Table.values()) {
keys.add(t.key());
}
for (String t : cfg.getSubsections(NOTEDB)) {
checkArgument(keys.contains(t.toLowerCase()),
"invalid notedb table: %s", t);
for (String key : cfg.getNames(NOTEDB, t)) {
String lk = key.toLowerCase();
checkArgument(lk.equals(WRITE) || lk.equals(READ),
"invalid notedb key: %s.%s", t, key);
}
boolean write = cfg.getBoolean(NOTEDB, t, WRITE, false);
boolean read = cfg.getBoolean(NOTEDB, t, READ, false);
checkArgument(!(read && !write),
"must have write enabled when read enabled: %s", t);
}
}
public static NotesMigration allEnabled() {
return new NotesMigration(allEnabledConfig());
}
public static Config allEnabledConfig() {
Config cfg = new Config();
setAllEnabledConfig(cfg);
return cfg;
}
public static void setAllEnabledConfig(Config cfg) {
for (Table t : Table.values()) {
cfg.setBoolean(NOTEDB, t.key(), WRITE, true);
cfg.setBoolean(NOTEDB, t.key(), READ, true);
}
}
private final boolean writeChanges;
private final boolean readChanges;
@Inject
NotesMigration(@GerritServerConfig Config cfg) {
checkConfig(cfg);
writeChanges = cfg.getBoolean(NOTEDB, Table.CHANGES.key(), WRITE, false);
readChanges = cfg.getBoolean(NOTEDB, Table.CHANGES.key(), READ, false);
}
public abstract boolean writeChanges();
public boolean enabled() {
return writeChanges()
|| readChanges();
}
public boolean writeChanges() {
return writeChanges;
}
public boolean readChanges() {
return readChanges;
}
}