1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.armeria.common.util.Exceptions;
28 import com.linecorp.centraldogma.common.EntryNotFoundException;
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 CacheableFindLatestRevCall extends CacheableCall<Revision> {
34
35 static final Revision EMPTY = new Revision(Integer.MIN_VALUE);
36 static final Revision ENTRY_NOT_FOUND = new Revision(Integer.MIN_VALUE);
37
38 private final Revision lastKnownRevision;
39 private final Revision headRevision;
40 private final String pathPattern;
41 private final boolean errorOnEntryNotFound;
42 private final int hashCode;
43
44 CacheableFindLatestRevCall(Repository repo, Revision lastKnownRevision, Revision headRevision,
45 String pathPattern, boolean errorOnEntryNotFound) {
46 super(repo);
47
48 this.lastKnownRevision = requireNonNull(lastKnownRevision, "lastKnownRevision");
49 this.headRevision = requireNonNull(headRevision, "headRevision");
50 this.pathPattern = requireNonNull(pathPattern, "pathPattern");
51 this.errorOnEntryNotFound = errorOnEntryNotFound;
52
53 hashCode = Objects.hash(lastKnownRevision, headRevision, pathPattern, errorOnEntryNotFound) +
54 System.identityHashCode(repo);
55
56 assert !lastKnownRevision.isRelative();
57 }
58
59 @Override
60 protected int weigh(Revision value) {
61 return pathPattern.length();
62 }
63
64 @Override
65 public CompletableFuture<Revision> execute() {
66 return repo().findLatestRevision(lastKnownRevision, pathPattern, errorOnEntryNotFound)
67 .handle((revision, cause) -> {
68 if (cause != null) {
69 cause = Exceptions.peel(cause);
70 if (cause instanceof EntryNotFoundException) {
71 return ENTRY_NOT_FOUND;
72 }
73 return Exceptions.throwUnsafely(cause);
74 }
75 return firstNonNull(revision, EMPTY);
76 });
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 CacheableFindLatestRevCall that = (CacheableFindLatestRevCall) o;
91 return lastKnownRevision.equals(that.lastKnownRevision) &&
92 headRevision.equals(that.headRevision) &&
93 pathPattern.equals(that.pathPattern) &&
94 errorOnEntryNotFound == that.errorOnEntryNotFound;
95 }
96
97 @Override
98 protected void toString(ToStringHelper helper) {
99 helper.add("lastKnownRevision", lastKnownRevision)
100 .add("headRevision", headRevision)
101 .add("pathPattern", pathPattern)
102 .add("errorOnEntryNotFound", errorOnEntryNotFound);
103 }
104 }