1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal.storage.repository.git;
17  
18  import java.util.List;
19  import java.util.Objects;
20  import java.util.concurrent.CompletableFuture;
21  
22  import javax.annotation.Nullable;
23  
24  import org.eclipse.jgit.attributes.Attribute;
25  import org.eclipse.jgit.diff.DiffEntry;
26  import org.eclipse.jgit.revwalk.RevTree;
27  
28  import com.google.common.base.MoreObjects.ToStringHelper;
29  
30  import com.linecorp.centraldogma.server.internal.storage.repository.CacheableCall;
31  import com.linecorp.centraldogma.server.storage.repository.Repository;
32  
33  final class CacheableCompareTreesCall extends CacheableCall<List<DiffEntry>> {
34  
35      private static final int SHA1_LEN = 20;
36  
37      @Nullable
38      private final RevTree treeA;
39      @Nullable
40      private final RevTree treeB;
41      private final int hashCode;
42  
43      CacheableCompareTreesCall(Repository repo, @Nullable RevTree treeA, @Nullable RevTree treeB) {
44          super(repo);
45  
46          this.treeA = treeA;
47          this.treeB = treeB;
48          hashCode = Objects.hash(treeA, treeB) * 31 + System.identityHashCode(repo);
49      }
50  
51      @Override
52      protected int weigh(List<DiffEntry> value) {
53          int weight = SHA1_LEN * 2;
54          for (DiffEntry e : value) {
55              if (e.getOldId() != null) {
56                  weight += SHA1_LEN;
57              }
58              if (e.getNewId() != null) {
59                  weight += SHA1_LEN;
60              }
61              if (e.getOldPath() != null) {
62                  weight += e.getOldPath().length();
63              }
64              if (e.getNewPath() != null) {
65                  weight += e.getNewPath().length();
66              }
67              final Attribute attr = e.getDiffAttribute();
68              if (attr != null) {
69                  if (attr.getKey() != null) {
70                      weight += attr.getKey().length();
71                  }
72                  if (attr.getValue() != null) {
73                      weight += attr.getValue().length();
74                  }
75              }
76          }
77          return weight;
78      }
79  
80      /**
81       * Never invoked because {@link GitRepository} produces the value of this call.
82       */
83      @Override
84      public CompletableFuture<List<DiffEntry>> execute() {
85          throw new IllegalStateException();
86      }
87  
88      @Override
89      public int hashCode() {
90          return hashCode;
91      }
92  
93      @Override
94      public boolean equals(Object o) {
95          if (!super.equals(o)) {
96              return false;
97          }
98  
99          final CacheableCompareTreesCall that = (CacheableCompareTreesCall) o;
100         return Objects.equals(treeA, that.treeA) &&
101                Objects.equals(treeB, that.treeB);
102     }
103 
104     @Override
105     protected void toString(ToStringHelper helper) {
106         helper.add("treeA", treeA != null ? treeA.getName() : null)
107               .add("treeB", treeB != null ? treeB.getName() : null);
108     }
109 }