LCOV - code coverage report
Current view: top level - server/patch/gitdiff - GitModifiedFilesCacheKey.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 28 92.9 %
Date: 2022-11-19 15:00:39 Functions: 9 10 90.0 %

          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 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.GitModifiedFilesKeyProto;
      24             : import com.google.gerrit.server.cache.serialize.CacheSerializer;
      25             : import com.google.gerrit.server.cache.serialize.ObjectIdConverter;
      26             : import com.google.gerrit.server.patch.DiffUtil;
      27             : import java.io.IOException;
      28             : import org.eclipse.jgit.lib.ObjectId;
      29             : import org.eclipse.jgit.revwalk.RevWalk;
      30             : 
      31             : /** Cache key for the {@link GitModifiedFilesCache}. */
      32             : @AutoValue
      33         105 : public abstract class GitModifiedFilesCacheKey {
      34             : 
      35             :   /** A specific git project / repository. */
      36             :   public abstract Project.NameKey project();
      37             : 
      38             :   /**
      39             :    * The git SHA-1 {@link ObjectId} of the first git tree object for which the diff should be
      40             :    * computed. If equals to {@link ObjectId#zeroId()}, a null tree is used for the diff scan, and
      41             :    * {@link #bTree()} is treated as an added tree.
      42             :    */
      43             :   public abstract ObjectId aTree();
      44             : 
      45             :   /**
      46             :    * The git SHA-1 {@link ObjectId} of the second git tree object for which the diff should be
      47             :    * computed.
      48             :    */
      49             :   public abstract ObjectId bTree();
      50             : 
      51             :   /**
      52             :    * Percentage score used to identify a file as a rename. This value is only available if {@link
      53             :    * #renameDetection()} is true. Otherwise, this method will return -1.
      54             :    *
      55             :    * <p>This value will be used to set the rename score of {@link
      56             :    * org.eclipse.jgit.diff.DiffFormatter#getRenameDetector()}.
      57             :    */
      58             :   public abstract int renameScore();
      59             : 
      60             :   /** Returns true if rename detection was set for this key. */
      61             :   public boolean renameDetection() {
      62         104 :     return renameScore() != -1;
      63             :   }
      64             : 
      65             :   public static GitModifiedFilesCacheKey create(
      66             :       Project.NameKey project, ObjectId aCommit, ObjectId bCommit, int renameScore, RevWalk rw)
      67             :       throws IOException {
      68           4 :     ObjectId aTree = DiffUtil.getTreeId(rw, aCommit);
      69           4 :     ObjectId bTree = DiffUtil.getTreeId(rw, bCommit);
      70           4 :     return builder().project(project).aTree(aTree).bTree(bTree).renameScore(renameScore).build();
      71             :   }
      72             : 
      73             :   public static Builder builder() {
      74         105 :     return new AutoValue_GitModifiedFilesCacheKey.Builder();
      75             :   }
      76             : 
      77             :   /** Returns the size of the object in bytes */
      78             :   public int weight() {
      79         104 :     return stringSize(project().get())
      80             :         + 20 * 2 // old and new tree IDs
      81             :         + 4; // rename score
      82             :   }
      83             : 
      84             :   @AutoValue.Builder
      85         105 :   public abstract static class Builder {
      86             : 
      87             :     public abstract Builder project(NameKey value);
      88             : 
      89             :     public abstract Builder aTree(ObjectId value);
      90             : 
      91             :     public abstract Builder bTree(ObjectId value);
      92             : 
      93             :     public abstract Builder renameScore(int value);
      94             : 
      95             :     public Builder disableRenameDetection() {
      96           0 :       renameScore(-1);
      97           0 :       return this;
      98             :     }
      99             : 
     100             :     public abstract GitModifiedFilesCacheKey build();
     101             :   }
     102             : 
     103         153 :   public enum Serializer implements CacheSerializer<GitModifiedFilesCacheKey> {
     104         153 :     INSTANCE;
     105             : 
     106             :     @Override
     107             :     public byte[] serialize(GitModifiedFilesCacheKey key) {
     108           7 :       ObjectIdConverter idConverter = ObjectIdConverter.create();
     109           7 :       return Protos.toByteArray(
     110           7 :           GitModifiedFilesKeyProto.newBuilder()
     111           7 :               .setProject(key.project().get())
     112           7 :               .setATree(idConverter.toByteString(key.aTree()))
     113           7 :               .setBTree(idConverter.toByteString(key.bTree()))
     114           7 :               .setRenameScore(key.renameScore())
     115           7 :               .build());
     116             :     }
     117             : 
     118             :     @Override
     119             :     public GitModifiedFilesCacheKey deserialize(byte[] in) {
     120           2 :       GitModifiedFilesKeyProto proto = Protos.parseUnchecked(GitModifiedFilesKeyProto.parser(), in);
     121           2 :       ObjectIdConverter idConverter = ObjectIdConverter.create();
     122           2 :       return GitModifiedFilesCacheKey.builder()
     123           2 :           .project(NameKey.parse(proto.getProject()))
     124           2 :           .aTree(idConverter.fromByteString(proto.getATree()))
     125           2 :           .bTree(idConverter.fromByteString(proto.getBTree()))
     126           2 :           .renameScore(proto.getRenameScore())
     127           2 :           .build();
     128             :     }
     129             :   }
     130             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750