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.gitdiff; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.common.base.Converter; 19 : import com.google.common.base.Enums; 20 : import com.google.gerrit.entities.Patch.ChangeType; 21 : import com.google.gerrit.proto.Protos; 22 : import com.google.gerrit.server.cache.proto.Cache.ModifiedFileProto; 23 : import com.google.gerrit.server.cache.serialize.CacheSerializer; 24 : import com.google.protobuf.Descriptors.FieldDescriptor; 25 : import java.util.Optional; 26 : 27 : /** 28 : * An entity representing a Modified file due to a diff between 2 git trees. This entity contains 29 : * the change type and the old & new paths, but does not include any actual content diff of the 30 : * file. 31 : */ 32 : @AutoValue 33 94 : public abstract class ModifiedFile { 34 : /** 35 : * Returns the change type (i.e. add, delete, modify, rename, etc...) associated with this 36 : * modified file. 37 : */ 38 : public abstract ChangeType changeType(); 39 : 40 : /** 41 : * Returns the old name associated with this file. An empty optional is returned if {@link 42 : * #changeType()} is equal to {@link ChangeType#ADDED}. 43 : */ 44 : public abstract Optional<String> oldPath(); 45 : 46 : /** 47 : * Returns the new name associated with this file. An empty optional is returned if {@link 48 : * #changeType()} is equal to {@link ChangeType#DELETED} 49 : */ 50 : public abstract Optional<String> newPath(); 51 : 52 : public String getDefaultPath() { 53 3 : return newPath().isPresent() ? newPath().get() : oldPath().get(); 54 : } 55 : 56 : public static Builder builder() { 57 94 : return new AutoValue_ModifiedFile.Builder(); 58 : } 59 : 60 : public abstract Builder toBuilder(); 61 : 62 : /** Computes this object's weight, which is its size in bytes. */ 63 : public int weight() { 64 93 : int weight = 1; // the changeType field 65 93 : if (oldPath().isPresent()) { 66 53 : weight += oldPath().get().length(); 67 : } 68 93 : if (newPath().isPresent()) { 69 93 : weight += newPath().get().length(); 70 : } 71 93 : return weight; 72 : } 73 : 74 : @AutoValue.Builder 75 94 : public abstract static class Builder { 76 : 77 : public abstract Builder changeType(ChangeType value); 78 : 79 : public abstract Builder oldPath(Optional<String> value); 80 : 81 : public abstract Builder newPath(Optional<String> value); 82 : 83 : public abstract ModifiedFile build(); 84 : } 85 : 86 5 : enum Serializer implements CacheSerializer<ModifiedFile> { 87 5 : INSTANCE; 88 : 89 5 : private static final Converter<String, ChangeType> CHANGE_TYPE_CONVERTER = 90 5 : Enums.stringConverter(ChangeType.class); 91 : 92 : private static final FieldDescriptor oldPathDescriptor = 93 5 : ModifiedFileProto.getDescriptor().findFieldByNumber(2); 94 : 95 5 : private static final FieldDescriptor newPathDescriptor = 96 5 : ModifiedFileProto.getDescriptor().findFieldByNumber(3); 97 : 98 : @Override 99 : public byte[] serialize(ModifiedFile modifiedFile) { 100 0 : return Protos.toByteArray(toProto(modifiedFile)); 101 : } 102 : 103 : public ModifiedFileProto toProto(ModifiedFile modifiedFile) { 104 5 : ModifiedFileProto.Builder builder = ModifiedFileProto.newBuilder(); 105 5 : builder.setChangeType(CHANGE_TYPE_CONVERTER.reverse().convert(modifiedFile.changeType())); 106 5 : if (modifiedFile.oldPath().isPresent()) { 107 1 : builder.setOldPath(modifiedFile.oldPath().get()); 108 : } 109 5 : if (modifiedFile.newPath().isPresent()) { 110 5 : builder.setNewPath(modifiedFile.newPath().get()); 111 : } 112 5 : return builder.build(); 113 : } 114 : 115 : @Override 116 : public ModifiedFile deserialize(byte[] in) { 117 0 : ModifiedFileProto modifiedFileProto = Protos.parseUnchecked(ModifiedFileProto.parser(), in); 118 0 : return fromProto(modifiedFileProto); 119 : } 120 : 121 : public ModifiedFile fromProto(ModifiedFileProto modifiedFileProto) { 122 1 : ModifiedFile.Builder builder = ModifiedFile.builder(); 123 1 : builder.changeType(CHANGE_TYPE_CONVERTER.convert(modifiedFileProto.getChangeType())); 124 : 125 1 : if (modifiedFileProto.hasField(oldPathDescriptor)) { 126 1 : builder.oldPath(Optional.of(modifiedFileProto.getOldPath())); 127 : } 128 1 : if (modifiedFileProto.hasField(newPathDescriptor)) { 129 1 : builder.newPath(Optional.of(modifiedFileProto.getNewPath())); 130 : } 131 1 : return builder.build(); 132 : } 133 : } 134 : }