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.Entry;
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.FindOption;
31  import com.linecorp.centraldogma.server.storage.repository.Repository;
32  
33  final class CacheableFindCall extends CacheableCall<Map<String, Entry<?>>> {
34  
35      final Revision revision;
36      final String pathPattern;
37      final Map<FindOption<?>, ?> options;
38      private final int hashCode;
39  
40      CacheableFindCall(Repository repo, Revision revision, String pathPattern, Map<FindOption<?>, ?> options) {
41          super(repo);
42  
43          this.revision = requireNonNull(revision, "revision");
44          this.pathPattern = requireNonNull(pathPattern, "pathPattern");
45          this.options = requireNonNull(options, "options");
46  
47          hashCode = Objects.hash(pathPattern, options) * 31 + System.identityHashCode(repo);
48  
49          assert !revision.isRelative();
50      }
51  
52      @Override
53      protected int weigh(Map<String, Entry<?>> value) {
54          int weight = 0;
55          weight += pathPattern.length();
56          weight += options.size();
57          for (Entry<?> e : value.values()) {
58              weight += e.path().length();
59              if (e.hasContent()) {
60                  weight += e.contentAsText().length();
61              }
62          }
63          return weight;
64      }
65  
66      @Override
67      public CompletableFuture<Map<String, Entry<?>>> execute() {
68          return repo().find(revision, pathPattern, options);
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 CacheableFindCall that = (CacheableFindCall) o;
83          return revision.equals(that.revision) &&
84                 pathPattern.equals(that.pathPattern) &&
85                 options.equals(that.options);
86      }
87  
88      @Override
89      protected void toString(ToStringHelper helper) {
90          helper.add("revision", revision)
91                .add("pathPattern", pathPattern)
92                .add("options", options);
93      }
94  }