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.google.common.base.MoreObjects.firstNonNull;
20  import static java.util.Objects.requireNonNull;
21  
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.Query;
29  import com.linecorp.centraldogma.common.Revision;
30  import com.linecorp.centraldogma.server.internal.storage.repository.CacheableCall;
31  import com.linecorp.centraldogma.server.storage.repository.Repository;
32  
33  final class CacheableQueryCall extends CacheableCall<Entry<?>> {
34  
35      static final Entry<?> EMPTY = Entry.ofDirectory(new Revision(Integer.MAX_VALUE), "/");
36  
37      final Revision revision;
38      final Query<?> query;
39      final int hashCode;
40  
41      CacheableQueryCall(Repository repo, Revision revision, Query<?> query) {
42          super(repo);
43          this.revision = requireNonNull(revision, "revision");
44          this.query = requireNonNull(query, "query");
45  
46          hashCode = Objects.hash(revision, query) * 31 + System.identityHashCode(repo);
47  
48          assert !revision.isRelative();
49      }
50  
51      @Override
52      protected int weigh(Entry<?> value) {
53          int weight = 0;
54          weight += query.path().length();
55          for (String e : query.expressions()) {
56              weight += e.length();
57          }
58          if (value != null && value.hasContent()) {
59              weight += value.contentAsText().length();
60          }
61          return weight;
62      }
63  
64      @Override
65      public CompletableFuture<Entry<?>> execute() {
66          return repo().getOrNull(revision, query).thenApply(e -> firstNonNull(e, EMPTY));
67      }
68  
69      @Override
70      public int hashCode() {
71          return hashCode;
72      }
73  
74      @Override
75      public boolean equals(Object o) {
76          if (!super.equals(o)) {
77              return false;
78          }
79  
80          final CacheableQueryCall that = (CacheableQueryCall) o;
81          return revision.equals(that.revision) &&
82                 query.equals(that.query);
83      }
84  
85      @Override
86      protected void toString(ToStringHelper helper) {
87          helper.add("revision", revision)
88                .add("query", query);
89      }
90  }