RefCache: Use java.util.Optional

Change-Id: Iab5f1873c45f719a298f3e955cf206286b54a1ae
This commit is contained in:
Dave Borowitz
2016-10-13 15:38:41 -04:00
parent fe672b8690
commit e4231bdb9a
9 changed files with 19 additions and 21 deletions

View File

@@ -17,8 +17,6 @@ package com.google.gerrit.server.git;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Optional;
import org.eclipse.jgit.lib.BatchRefUpdate; import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
@@ -28,6 +26,7 @@ import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* Collection of {@link ReceiveCommand}s that supports multiple updates per ref. * Collection of {@link ReceiveCommand}s that supports multiple updates per ref.
@@ -96,7 +95,7 @@ public class ChainedReceiveCommands implements RefCache {
if (cmd != null) { if (cmd != null) {
return !cmd.getNewId().equals(ObjectId.zeroId()) return !cmd.getNewId().equals(ObjectId.zeroId())
? Optional.of(cmd.getNewId()) ? Optional.of(cmd.getNewId())
: Optional.<ObjectId>absent(); : Optional.empty();
} }
return refCache.get(refName); return refCache.get(refName);
} }

View File

@@ -14,11 +14,10 @@
package com.google.gerrit.server.git; package com.google.gerrit.server.git;
import com.google.common.base.Optional;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
/** /**
* Simple short-lived cache of individual refs read from a repo. * Simple short-lived cache of individual refs read from a repo.

View File

@@ -14,8 +14,6 @@
package com.google.gerrit.server.git; package com.google.gerrit.server.git;
import com.google.common.base.Optional;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase; import org.eclipse.jgit.lib.RefDatabase;
@@ -24,6 +22,7 @@ import org.eclipse.jgit.lib.Repository;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** {@link RefCache} backed directly by a repository. */ /** {@link RefCache} backed directly by a repository. */
public class RepoRefCache implements RefCache { public class RepoRefCache implements RefCache {
@@ -42,9 +41,7 @@ public class RepoRefCache implements RefCache {
return id; return id;
} }
Ref ref = refdb.exactRef(refName); Ref ref = refdb.exactRef(refName);
id = ref != null id = Optional.ofNullable(ref).map(Ref::getObjectId);
? Optional.of(ref.getObjectId())
: Optional.<ObjectId>absent();
ids.put(refName, id); ids.put(refName, id);
return id; return id;
} }

View File

@@ -566,7 +566,7 @@ public class ChangeNotes extends AbstractChangeNotes<ChangeNotes> {
@Override @Override
protected ObjectId readRef(Repository repo) throws IOException { protected ObjectId readRef(Repository repo) throws IOException {
return refs != null return refs != null
? refs.get(getRefName()).orNull() ? refs.get(getRefName()).orElse(null)
: super.readRef(repo); : super.readRef(repo);
} }

View File

@@ -22,7 +22,6 @@ import static java.util.Comparator.comparing;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@@ -40,6 +39,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
/** /**
* The state of all relevant NoteDb refs across all repos corresponding to a * The state of all relevant NoteDb refs across all repos corresponding to a

View File

@@ -21,7 +21,6 @@ import static com.google.gerrit.reviewdb.client.RefNames.REFS_DRAFT_COMMENTS;
import static com.google.gerrit.server.notedb.NoteDbTable.CHANGES; import static com.google.gerrit.server.notedb.NoteDbTable.CHANGES;
import com.google.auto.value.AutoValue; import com.google.auto.value.AutoValue;
import com.google.common.base.Optional;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@@ -58,6 +57,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
/** /**
@@ -362,7 +362,7 @@ public class NoteDbUpdateManager implements AutoCloseable {
StagedResult r = StagedResult.create( StagedResult r = StagedResult.create(
e.getKey(), e.getKey(),
NoteDbChangeState.Delta.create( NoteDbChangeState.Delta.create(
e.getKey(), Optional.<ObjectId>absent(), e.getValue()), e.getKey(), Optional.empty(), e.getValue()),
changeRepo, allUsersRepo); changeRepo, allUsersRepo);
checkState(r.changeCommands().isEmpty(), checkState(r.changeCommands().isEmpty(),
"should not have change commands when updating only drafts: %s", r); "should not have change commands when updating only drafts: %s", r);
@@ -550,7 +550,7 @@ public class NoteDbUpdateManager implements AutoCloseable {
for (Map.Entry<String, Collection<U>> e : all.asMap().entrySet()) { for (Map.Entry<String, Collection<U>> e : all.asMap().entrySet()) {
String refName = e.getKey(); String refName = e.getKey();
Collection<U> updates = e.getValue(); Collection<U> updates = e.getValue();
ObjectId old = or.cmds.get(refName).or(ObjectId.zeroId()); ObjectId old = or.cmds.get(refName).orElse(ObjectId.zeroId());
// Only actually write to the ref if one of the updates explicitly allows // Only actually write to the ref if one of the updates explicitly allows
// us to do so, i.e. it is known to represent a new change. This avoids // us to do so, i.e. it is known to represent a new change. This avoids
// writing partial change meta if the change hasn't been backfilled yet. // writing partial change meta if the change hasn't been backfilled yet.

View File

@@ -19,10 +19,10 @@ import static com.google.common.base.Preconditions.checkState;
import static com.google.gerrit.reviewdb.client.RefNames.changeMetaRef; import static com.google.gerrit.reviewdb.client.RefNames.changeMetaRef;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_HASHTAGS; import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_HASHTAGS;
import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_PATCH_SET; import static com.google.gerrit.server.notedb.ChangeNoteUtil.FOOTER_PATCH_SET;
import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import com.google.common.base.Optional;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@@ -87,6 +87,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
public class ChangeRebuilderImpl extends ChangeRebuilder { public class ChangeRebuilderImpl extends ChangeRebuilder {

View File

@@ -14,7 +14,6 @@
package com.google.gerrit.server.notedb.rebuild; package com.google.gerrit.server.notedb.rebuild;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -25,6 +24,7 @@ import com.google.gwtorm.server.OrmException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern; import java.util.regex.Pattern;
class StatusChangeEvent extends Event { class StatusChangeEvent extends Event {
@@ -40,7 +40,7 @@ class StatusChangeEvent extends Event {
Change change, Change noteDbChange) { Change change, Change noteDbChange) {
String msg = message.getMessage(); String msg = message.getMessage();
if (msg == null) { if (msg == null) {
return Optional.absent(); return Optional.empty();
} }
for (Map.Entry<Change.Status, Pattern> e : PATTERNS.entrySet()) { for (Map.Entry<Change.Status, Pattern> e : PATTERNS.entrySet()) {
if (e.getValue().matcher(msg).matches()) { if (e.getValue().matcher(msg).matches()) {
@@ -48,7 +48,7 @@ class StatusChangeEvent extends Event {
message, change, noteDbChange, e.getKey())); message, change, noteDbChange, e.getKey()));
} }
} }
return Optional.absent(); return Optional.empty();
} }
private final Change change; private final Change change;

View File

@@ -18,9 +18,9 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.notedb.NoteDbChangeState.applyDelta; import static com.google.gerrit.server.notedb.NoteDbChangeState.applyDelta;
import static com.google.gerrit.server.notedb.NoteDbChangeState.parse; import static com.google.gerrit.server.notedb.NoteDbChangeState.parse;
import static org.eclipse.jgit.lib.ObjectId.zeroId; import static org.eclipse.jgit.lib.ObjectId.zeroId;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@@ -33,6 +33,8 @@ import com.google.gwtorm.server.StandardKeyEncoder;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.junit.Test; import org.junit.Test;
import java.util.Optional;
/** Unit tests for {@link NoteDbChangeState}. */ /** Unit tests for {@link NoteDbChangeState}. */
public class NoteDbChangeStateTest { public class NoteDbChangeStateTest {
static { static {
@@ -134,7 +136,7 @@ public class NoteDbChangeStateTest {
// Static factory methods to avoid type arguments when using as method args. // Static factory methods to avoid type arguments when using as method args.
private static Optional<ObjectId> noMetaId() { private static Optional<ObjectId> noMetaId() {
return Optional.absent(); return Optional.empty();
} }
private static Optional<ObjectId> metaId(ObjectId id) { private static Optional<ObjectId> metaId(ObjectId id) {