Line data Source code
1 : // Copyright (C) 2011 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git; 16 : 17 : import com.google.common.cache.Cache; 18 : import com.google.gerrit.entities.Project; 19 : import com.google.gerrit.server.cache.CacheModule; 20 : import com.google.gerrit.server.cache.serialize.StringCacheSerializer; 21 : import com.google.inject.Inject; 22 : import com.google.inject.Module; 23 : import com.google.inject.Singleton; 24 : import com.google.inject.name.Named; 25 : import java.util.concurrent.ExecutionException; 26 : import org.eclipse.jgit.lib.ObjectId; 27 : 28 : @Singleton 29 : public class TagCache { 30 : private static final String CACHE_NAME = "git_tags"; 31 : 32 : public static Module module() { 33 152 : return new CacheModule() { 34 : @Override 35 : protected void configure() { 36 152 : persist(CACHE_NAME, String.class, TagSetHolder.class) 37 152 : .version(1) 38 152 : .keySerializer(StringCacheSerializer.INSTANCE) 39 152 : .valueSerializer(TagSetHolder.Serializer.INSTANCE); 40 152 : bind(TagCache.class); 41 152 : } 42 : }; 43 : } 44 : 45 : private final Cache<String, TagSetHolder> cache; 46 : 47 : @Inject 48 143 : TagCache(@Named(CACHE_NAME) Cache<String, TagSetHolder> cache) { 49 143 : this.cache = cache; 50 143 : } 51 : 52 : /** 53 : * Advise the cache that a reference fast-forwarded. 54 : * 55 : * <p>This operation is not necessary, the cache will automatically detect changes made to 56 : * references and update itself on demand. However, this method may allow the cache to update more 57 : * quickly and reuse the caller's computation of the fast-forward status of a branch. 58 : * 59 : * @param name project the branch is contained in. 60 : * @param refName the branch name. 61 : * @param oldValue the old value, before the fast-forward. The cache will only update itself if it 62 : * is still using this old value. 63 : * @param newValue the current value, after the fast-forward. 64 : */ 65 : public void updateFastForward( 66 : Project.NameKey name, String refName, ObjectId oldValue, ObjectId newValue) { 67 : // Be really paranoid and null check everything. This method should 68 : // never fail with an exception. Some of these references can be null 69 : // (e.g. not all projects are cached, or the cache is not current). 70 : // 71 68 : TagSetHolder holder = cache.getIfPresent(name.get()); 72 68 : if (holder != null) { 73 5 : TagSet tags = holder.getTagSet(); 74 5 : if (tags != null) { 75 5 : if (tags.updateFastForward(refName, oldValue, newValue)) { 76 5 : cache.put(name.get(), holder); 77 : } 78 : } 79 : } 80 68 : } 81 : 82 : public TagSetHolder get(Project.NameKey name) { 83 : try { 84 9 : return cache.get(name.get(), () -> new TagSetHolder(name)); 85 0 : } catch (ExecutionException e) { 86 0 : throw new IllegalStateException(e); 87 : } 88 : } 89 : 90 : void put(Project.NameKey name, TagSetHolder tags) { 91 9 : cache.put(name.get(), tags); 92 9 : } 93 : }