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