Add ChangeCache implementation to scan changes from git

The purpose of ChangeCache is to resolve a project name to a list of
changes. This is a natural location for the scanChanges implementation
we added to SiteIndexer. However, most users in a running server will
still just want use the secondary index lookup, as it's faster. So we
extract an interface and provide two implementations.

Change-Id: I66c24b337cdc6aa787f450f8e0b2dfa17b0cdf97
This commit is contained in:
Dave Borowitz
2015-01-22 14:32:59 -08:00
parent 2fa1732dc5
commit a2b7358dfe
8 changed files with 257 additions and 119 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2012 The Android Open Source Project
// Copyright (C) 2015 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,89 +14,11 @@
package com.google.gerrit.server.git;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.cache.CacheModule;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.InternalChangeQuery;
import com.google.gerrit.server.util.OneOffRequestContext;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
@Singleton
public class ChangeCache implements GitReferenceUpdatedListener {
private static final Logger log =
LoggerFactory.getLogger(ChangeCache.class);
private static final String ID_CACHE = "changes";
public static Module module() {
return new CacheModule() {
@Override
protected void configure() {
cache(ID_CACHE,
Project.NameKey.class,
new TypeLiteral<List<Change>>() {})
.maximumWeight(0)
.loader(Loader.class);
}
};
}
private final LoadingCache<Project.NameKey, List<Change>> cache;
@Inject
ChangeCache(@Named(ID_CACHE) LoadingCache<Project.NameKey, List<Change>> cache) {
this.cache = cache;
}
List<Change> get(Project.NameKey name) {
try {
return cache.get(name);
} catch (ExecutionException e) {
log.warn("Cannot fetch changes for " + name, e);
return Collections.emptyList();
}
}
@Override
public void onGitReferenceUpdated(GitReferenceUpdatedListener.Event event) {
if (event.getRefName().startsWith(RefNames.REFS_CHANGES)) {
cache.invalidate(new Project.NameKey(event.getProjectName()));
}
}
static class Loader extends CacheLoader<Project.NameKey, List<Change>> {
private final OneOffRequestContext requestContext;
private final Provider<InternalChangeQuery> queryProvider;
@Inject
Loader(OneOffRequestContext requestContext,
Provider<InternalChangeQuery> queryProvider) {
this.requestContext = requestContext;
this.queryProvider = queryProvider;
}
@Override
public List<Change> load(Project.NameKey key) throws Exception {
try (AutoCloseable ctx = requestContext.open()) {
return Collections.unmodifiableList(
ChangeData.asChanges(queryProvider.get().byProject(key)));
}
}
}
public interface ChangeCache {
public List<Change> get(Project.NameKey name);
}