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.Map;
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.Change;
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 CacheableMultiDiffCall extends CacheableCall<Map<String, Change<?>>> {
33  
34      final Revision from;
35      final Revision to;
36      final String pathPattern;
37      final int hashCode;
38  
39      CacheableMultiDiffCall(Repository repo, Revision from, Revision to, String pathPattern) {
40          super(repo);
41  
42          this.from = requireNonNull(from, "from");
43          this.to = requireNonNull(to, "to");
44          this.pathPattern = requireNonNull(pathPattern, "pathPattern");
45  
46          hashCode = Objects.hash(from, to, pathPattern) * 31 + System.identityHashCode(repo);
47  
48          assert !from.isRelative();
49          assert !to.isRelative();
50      }
51  
52      @Override
53      protected int weigh(Map<String, Change<?>> value) {
54          int weight = 0;
55          weight += pathPattern.length();
56          for (Change<?> e : value.values()) {
57              weight += e.path().length();
58              final String content = e.contentAsText();
59              if (content != null) {
60                  weight += content.length();
61              }
62          }
63          return weight;
64      }
65  
66      @Override
67      public CompletableFuture<Map<String, Change<?>>> execute() {
68          return repo().diff(from, to, pathPattern);
69      }
70  
71      @Override
72      public int hashCode() {
73          return hashCode;
74      }
75  
76      @Override
77      public boolean equals(Object o) {
78          if (!super.equals(o)) {
79              return false;
80          }
81  
82          final CacheableMultiDiffCall that = (CacheableMultiDiffCall) o;
83          return from.equals(that.from) &&
84                 to.equals(that.to) &&
85                 pathPattern.equals(that.pathPattern);
86      }
87  
88      @Override
89      protected void toString(ToStringHelper helper) {
90          helper.add("from", from)
91                .add("to", to)
92                .add("pathPattern", pathPattern);
93      }
94  }