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