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.Objects;
22  import java.util.concurrent.CompletableFuture;
23  
24  import com.google.common.base.MoreObjects.ToStringHelper;
25  
26  import com.linecorp.centraldogma.common.Change;
27  import com.linecorp.centraldogma.common.Query;
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 CacheableSingleDiffCall extends CacheableCall<Change<?>> {
33  
34      final Revision from;
35      final Revision to;
36      final Query<?> query;
37      final int hashCode;
38  
39      CacheableSingleDiffCall(Repository repo, Revision from, Revision to, Query<?> query) {
40          super(repo);
41  
42          this.from = requireNonNull(from, "from");
43          this.to = requireNonNull(to, "to");
44          this.query = requireNonNull(query, "query");
45  
46          hashCode = Objects.hash(from, to, query) * 31 + System.identityHashCode(repo);
47  
48          assert !from.isRelative();
49          assert !to.isRelative();
50      }
51  
52      @Override
53      protected int weigh(Change<?> value) {
54          int weight = 0;
55          weight += query.path().length();
56          for (String e : query.expressions()) {
57              weight += e.length();
58          }
59          weight += value.path().length();
60          final String content = value.contentAsText();
61          if (content != null) {
62              weight += content.length();
63          }
64          return weight;
65      }
66  
67      @Override
68      public CompletableFuture<Change<?>> execute() {
69          return repo().diff(from, to, query);
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 CacheableSingleDiffCall that = (CacheableSingleDiffCall) o;
84          return from.equals(that.from) &&
85                 to.equals(that.to) &&
86                 query.equals(that.query);
87      }
88  
89      @Override
90      protected void toString(ToStringHelper helper) {
91          helper.add("from", from)
92                .add("to", to)
93                .add("query", query);
94      }
95  }