1   /*
2    * Copyright 2017 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  
17  package com.linecorp.centraldogma.server.internal.storage.repository.cache;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.List;
22  import java.util.Objects;
23  import java.util.concurrent.CompletableFuture;
24  
25  import com.google.common.base.MoreObjects.ToStringHelper;
26  
27  import com.linecorp.centraldogma.common.Commit;
28  import com.linecorp.centraldogma.common.Revision;
29  import com.linecorp.centraldogma.server.internal.storage.repository.CacheableCall;
30  import com.linecorp.centraldogma.server.storage.repository.Repository;
31  
32  final class CacheableHistoryCall extends CacheableCall<List<Commit>> {
33  
34      final Revision from;
35      final Revision to;
36      final String pathPattern;
37      final int maxCommits;
38      final int hashCode;
39  
40      CacheableHistoryCall(Repository repo, Revision from, Revision to, String pathPattern, int maxCommits) {
41          super(repo);
42  
43          this.from = requireNonNull(from, "from");
44          this.to = requireNonNull(to, "to");
45          this.pathPattern = requireNonNull(pathPattern, "pathPattern");
46          this.maxCommits = maxCommits;
47  
48          hashCode = (Objects.hash(from, to, pathPattern) * 31 + maxCommits) * 31 + System.identityHashCode(repo);
49  
50          assert !from.isRelative();
51          assert !to.isRelative();
52      }
53  
54      @Override
55      protected int weigh(List<Commit> value) {
56          int weight = 0;
57          weight += pathPattern.length();
58          for (Commit c : value) {
59              weight += c.author().name().length() + c.author().email().length() +
60                        c.summary().length() + c.detail().length();
61          }
62          return weight;
63      }
64  
65      @Override
66      public CompletableFuture<List<Commit>> execute() {
67          return repo().history(from, to, pathPattern, maxCommits);
68      }
69  
70      @Override
71      public int hashCode() {
72          return hashCode;
73      }
74  
75      @Override
76      public boolean equals(Object o) {
77          if (!super.equals(o)) {
78              return false;
79          }
80  
81          final CacheableHistoryCall that = (CacheableHistoryCall) o;
82          return from.equals(that.from) &&
83                 to.equals(that.to) &&
84                 pathPattern.equals(that.pathPattern) &&
85                 maxCommits == that.maxCommits;
86      }
87  
88      @Override
89      protected void toString(ToStringHelper helper) {
90          helper.add("from", from)
91                .add("to", to)
92                .add("pathPattern", pathPattern)
93                .add("maxCommits", maxCommits);
94      }
95  }