-
Notifications
You must be signed in to change notification settings - Fork 1
/
CumulativeVersion.java
180 lines (155 loc) · 7.75 KB
/
CumulativeVersion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package qbt.recursive.cv;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.hash.Hasher;
import com.google.gson.JsonElement;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.lang3.tuple.Pair;
import qbt.NormalDependencyType;
import qbt.QbtHashUtils;
import qbt.VcsTreeDigest;
import qbt.manifest.current.PackageMetadata;
import qbt.recursive.rpd.RecursivePackageData;
import qbt.recursive.rpd.RecursivePackageDataCanonicalizer;
public final class CumulativeVersion extends RecursivePackageData<CumulativeVersionNodeData, CumulativeVersion> {
private final CumulativeVersionDigest digest;
private final Map<String, Pair<NormalDependencyType, CumulativeVersion>> normalDependenciesSorted;
private static final String INDENT = " ";
private CumulativeVersion(CumulativeVersionNodeData nodeData, Map<String, Pair<NormalDependencyType, CumulativeVersion>> normalDependencies) {
super(nodeData, normalDependencies);
Hasher b = QbtHashUtils.newHasher();
b = b.putString("SELF\t", Charsets.UTF_8);
b = b.putString(nodeData.packageName, Charsets.UTF_8);
b = b.putString("\t", Charsets.UTF_8);
b = b.putString(nodeData.effectiveTree.getRawDigest().toString(), Charsets.UTF_8);
b = b.putString("\n", Charsets.UTF_8);
b = b.putString("QBT\t", Charsets.UTF_8);
b = b.putString(nodeData.qbtDependency.getRawDigest().toString(), Charsets.UTF_8);
for(Map.Entry<String, String> e : nodeData.qbtEnv.entrySet()) {
b = b.putString("QBTENV\t", Charsets.UTF_8);
b = b.putString(QbtHashUtils.hashFunction().hashString(e.getKey(), Charsets.UTF_8).toString(), Charsets.UTF_8);
b = b.putString("\t", Charsets.UTF_8);
b = b.putString(QbtHashUtils.hashFunction().hashString(e.getValue(), Charsets.UTF_8).toString(), Charsets.UTF_8);
b = b.putString("\n", Charsets.UTF_8);
}
for(Map.Entry<String, String> e : toStringMap(nodeData.metadata)) {
b = b.putString("METADATA\t", Charsets.UTF_8);
b = b.putString(e.getKey(), Charsets.UTF_8);
b = b.putString("\t", Charsets.UTF_8);
b = b.putString(e.getValue(), Charsets.UTF_8);
b = b.putString("\n", Charsets.UTF_8);
}
List<Map.Entry<String, Pair<NormalDependencyType, CumulativeVersion>>> normalDependenciesEntriesSorted = Lists.newArrayList(normalDependencies.entrySet());
Collections.sort(normalDependenciesEntriesSorted, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));
ImmutableMap.Builder<String, Pair<NormalDependencyType, CumulativeVersion>> normalDependenciesSortedBuilder = ImmutableMap.builder();
for(Map.Entry<String, Pair<NormalDependencyType, CumulativeVersion>> e : normalDependenciesEntriesSorted) {
normalDependenciesSortedBuilder.put(e);
}
this.normalDependenciesSorted = normalDependenciesSortedBuilder.build();
for(Map.Entry<String, Pair<NormalDependencyType, CumulativeVersion>> e : normalDependenciesSorted.entrySet()) {
b = b.putString(e.getValue().getLeft().getTag(), Charsets.UTF_8);
b = b.putString("\t", Charsets.UTF_8);
b = b.putString(e.getKey(), Charsets.UTF_8);
b = b.putString("\t", Charsets.UTF_8);
b = b.putString(e.getValue().getRight().getDigest().getRawDigest().toString(), Charsets.UTF_8);
b = b.putString("\n", Charsets.UTF_8);
}
this.digest = new CumulativeVersionDigest(b.hash());
}
private static final RecursivePackageDataCanonicalizer<CumulativeVersionNodeData, CumulativeVersion, CumulativeVersionNodeData> canonicalizer = new RecursivePackageDataCanonicalizer<CumulativeVersionNodeData, CumulativeVersion, CumulativeVersionNodeData>() {
@Override
protected CumulativeVersionNodeData key(CumulativeVersion r) {
return r.result;
}
@Override
protected CumulativeVersion newR(CumulativeVersionNodeData nodeData, Map<String, Pair<NormalDependencyType, CumulativeVersion>> dependencyVersions) {
return new CumulativeVersion(nodeData, dependencyVersions);
}
};
public static CumulativeVersion of(CumulativeVersionNodeData nodeData, Map<String, Pair<NormalDependencyType, CumulativeVersion>> normalDependencies) {
return canonicalizer.transform(new CumulativeVersion(nodeData, ImmutableMap.copyOf(normalDependencies)));
}
public String getPackageName() {
return result.packageName;
}
public VcsTreeDigest getEffectiveTree() {
return result.effectiveTree;
}
public CumulativeVersionDigest getQbtDependency() {
return result.qbtDependency;
}
public PackageMetadata getMetadata() {
return result.metadata;
}
public Map<String, Pair<NormalDependencyType, CumulativeVersion>> getNormalDependencies() {
return normalDependenciesSorted;
}
public CumulativeVersionDigest getDigest() {
return digest;
}
@Override
public String toString() {
return "CumulativeVersion{" + getPackageName() + "@" + digest.getRawDigest() + "}";
}
public String prettyDigest() {
return getPackageName() + "@" + digest.getRawDigest();
}
public List<String> prettyTree() {
ImmutableList.Builder<String> b = ImmutableList.builder();
prettyTree("", b, Sets.<CumulativeVersion>newHashSet());
return b.build();
}
private void prettyTree(String prefix, ImmutableList.Builder<String> b, Set<CumulativeVersion> seen) {
if(!seen.add(this)) {
b.add(prefix + prettyDigest() + "...");
return;
}
b.add(prefix + prettyDigest() + ":");
b.add(prefix + INDENT + "EffectiveTree: " + getEffectiveTree().getRawDigest());
b.add(prefix + INDENT + "QbtVersion: " + getQbtDependency().getRawDigest());
Map<String, String> qbtEnv = result.qbtEnv;
if(!qbtEnv.isEmpty()) {
b.add(prefix + INDENT + "QbtEnv:");
for(Map.Entry<String, String> e : qbtEnv.entrySet()) {
b.add(prefix + INDENT + INDENT + e.getKey() + ": " + e.getValue());
}
}
Collection<Map.Entry<String, String>> metadataMap = toStringMap(getMetadata());
if(!metadataMap.isEmpty()) {
b.add(prefix + INDENT + "Metadata:");
for(Map.Entry<String, String> e : metadataMap) {
b.add(prefix + INDENT + INDENT + e.getKey() + ": " + e.getValue());
}
}
for(NormalDependencyType type : NormalDependencyType.values()) {
boolean first = true;
for(Pair<NormalDependencyType, CumulativeVersion> e : getNormalDependencies().values()) {
if(type != e.getLeft()) {
continue;
}
if(first) {
b.add(prefix + INDENT + type.getTag() + ":");
first = false;
}
e.getRight().prettyTree(prefix + INDENT + INDENT, b, seen);
}
}
}
private static Collection<Map.Entry<String, String>> toStringMap(PackageMetadata metadata) {
JsonElement e = PackageMetadata.SERIALIZER.toJson(metadata.builder());
TreeMap<String, String> m = Maps.newTreeMap();
for(Map.Entry<String, JsonElement> e2 : e.getAsJsonObject().entrySet()) {
m.put(e2.getKey(), e2.getValue().getAsString());
}
return m.entrySet();
}
}