Line data Source code
1 : // Copyright (C) 2020 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.patch.diff; 16 : 17 : import static com.google.gerrit.server.patch.DiffUtil.stringSize; 18 : 19 : import com.google.auto.value.AutoValue; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.entities.Project.NameKey; 22 : import com.google.gerrit.proto.Protos; 23 : import com.google.gerrit.server.cache.proto.Cache.ModifiedFilesKeyProto; 24 : import com.google.gerrit.server.cache.serialize.CacheSerializer; 25 : import com.google.gerrit.server.cache.serialize.ObjectIdConverter; 26 : import org.eclipse.jgit.lib.ObjectId; 27 : 28 : /** Cache key for the {@link com.google.gerrit.server.patch.diff.ModifiedFilesCache} */ 29 : @AutoValue 30 105 : public abstract class ModifiedFilesCacheKey { 31 : 32 : /** A specific git project / repository. */ 33 : public abstract Project.NameKey project(); 34 : 35 : /** Returns the old commit ID used in the git tree diff */ 36 : public abstract ObjectId aCommit(); 37 : 38 : /** Returns the new commit ID used in the git tree diff */ 39 : public abstract ObjectId bCommit(); 40 : 41 : /** 42 : * Percentage score used to identify a file as a "rename". A special value of -1 means that the 43 : * computation will ignore renames and rename detection will be disabled. 44 : */ 45 : public abstract int renameScore(); 46 : 47 : public boolean renameDetectionEnabled() { 48 0 : return renameScore() != -1; 49 : } 50 : 51 : /** Returns the size of the object in bytes */ 52 : public int weight() { 53 104 : return stringSize(project().get()) // project 54 : + 20 * 2 // aCommit and bCommit 55 : + 4; // renameScore 56 : } 57 : 58 : public static ModifiedFilesCacheKey.Builder builder() { 59 105 : return new AutoValue_ModifiedFilesCacheKey.Builder(); 60 : } 61 : 62 : @AutoValue.Builder 63 105 : public abstract static class Builder { 64 : 65 : public abstract ModifiedFilesCacheKey.Builder project(NameKey value); 66 : 67 : public abstract ModifiedFilesCacheKey.Builder aCommit(ObjectId value); 68 : 69 : public abstract ModifiedFilesCacheKey.Builder bCommit(ObjectId value); 70 : 71 : public ModifiedFilesCacheKey.Builder disableRenameDetection() { 72 0 : renameScore(-1); 73 0 : return this; 74 : } 75 : 76 : public abstract ModifiedFilesCacheKey.Builder renameScore(int value); 77 : 78 : public abstract ModifiedFilesCacheKey build(); 79 : } 80 : 81 153 : public enum Serializer implements CacheSerializer<ModifiedFilesCacheKey> { 82 153 : INSTANCE; 83 : 84 : @Override 85 : public byte[] serialize(ModifiedFilesCacheKey key) { 86 7 : ObjectIdConverter idConverter = ObjectIdConverter.create(); 87 7 : return Protos.toByteArray( 88 7 : ModifiedFilesKeyProto.newBuilder() 89 7 : .setProject(key.project().get()) 90 7 : .setACommit(idConverter.toByteString(key.aCommit())) 91 7 : .setBCommit(idConverter.toByteString(key.bCommit())) 92 7 : .setRenameScore(key.renameScore()) 93 7 : .build()); 94 : } 95 : 96 : @Override 97 : public ModifiedFilesCacheKey deserialize(byte[] in) { 98 2 : ModifiedFilesKeyProto proto = Protos.parseUnchecked(ModifiedFilesKeyProto.parser(), in); 99 2 : ObjectIdConverter idConverter = ObjectIdConverter.create(); 100 2 : return ModifiedFilesCacheKey.builder() 101 2 : .project(NameKey.parse(proto.getProject())) 102 2 : .aCommit(idConverter.fromByteString(proto.getACommit())) 103 2 : .bCommit(idConverter.fromByteString(proto.getBCommit())) 104 2 : .renameScore(proto.getRenameScore()) 105 2 : .build(); 106 : } 107 : } 108 : }