1   /*
2    * Copyright 2018 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.internal.Util.validateJsonFilePath;
20  import static com.linecorp.centraldogma.server.internal.storage.repository.RepositoryCache.logger;
21  import static java.util.Objects.requireNonNull;
22  
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.concurrent.CompletableFuture;
26  
27  import com.google.common.base.MoreObjects.ToStringHelper;
28  
29  import com.linecorp.centraldogma.common.MergeQuery;
30  import com.linecorp.centraldogma.common.MergeSource;
31  import com.linecorp.centraldogma.common.MergedEntry;
32  import com.linecorp.centraldogma.common.Revision;
33  import com.linecorp.centraldogma.server.storage.repository.AbstractCacheableCall;
34  import com.linecorp.centraldogma.server.storage.repository.Repository;
35  
36  final class CacheableMergeQueryCall<T> extends AbstractCacheableCall<MergedEntry<T>> {
37  
38      private final Revision revision;
39      private final MergeQuery<T> query;
40      private final int hashCode;
41  
42      CacheableMergeQueryCall(Repository repo, Revision revision, MergeQuery<T> query) {
43          super(repo);
44          this.revision = requireNonNull(revision, "revision");
45          this.query = requireNonNull(query, "query");
46          // Only JSON files can currently be merged.
47          query.mergeSources().forEach(path -> validateJsonFilePath(path.path(), "path"));
48  
49          hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo);
50  
51          assert !revision.isRelative();
52      }
53  
54      @Override
55      public int weigh(MergedEntry<T> value) {
56          int weight = 0;
57          final List<MergeSource> mergeSources = query.mergeSources();
58          weight += mergeSources.size();
59          for (MergeSource mergeSource : mergeSources) {
60              weight += mergeSource.path().length();
61          }
62          final List<String> expressions = query.expressions();
63          weight += expressions.size();
64          for (String expression : expressions) {
65              weight += expression.length();
66          }
67          if (value != null) {
68              weight += value.contentAsText().length();
69          }
70          return weight;
71      }
72  
73      @Override
74      public CompletableFuture<MergedEntry<T>> execute() {
75          logger.debug("Cache miss: {}", this);
76          return repo().mergeFiles(revision, query);
77      }
78  
79      @Override
80      public int hashCode() {
81          return hashCode;
82      }
83  
84      @Override
85      public boolean equals(Object o) {
86          if (!super.equals(o)) {
87              return false;
88          }
89  
90          final CacheableMergeQueryCall<?> that = (CacheableMergeQueryCall<?>) o;
91          return revision.equals(that.revision) &&
92                 query.equals(that.query);
93      }
94  
95      @Override
96      protected void toString(ToStringHelper helper) {
97          helper.add("revision", revision)
98                .add("query", query);
99      }
100 }