Skip to content

Commit

Permalink
Support TreeDoc.dedupeNodes(); update TDNode.hashCode() to not includ…
Browse files Browse the repository at this point in the history
…e key
  • Loading branch information
jianwu committed Dec 5, 2022
1 parent bb7db42 commit 29d8064
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -124,7 +124,7 @@
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<version>1.18.24</version>
</path>
</annotationProcessorPaths>
</configuration>
Expand Down Expand Up @@ -182,7 +182,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.21.0</version>
<version>2.12.4</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
Expand Down Expand Up @@ -218,7 +218,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.0</version>
<version>3.4.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down
8 changes: 5 additions & 3 deletions treedoc/src/main/java/org/jsonex/treedoc/TDNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.jsonex.core.util.LangUtil.orElse;
import static org.jsonex.core.util.ListUtil.last;
import static org.jsonex.core.util.ListUtil.map;

/** A Node in TreeDoc */
@RequiredArgsConstructor
Expand Down Expand Up @@ -249,11 +250,11 @@ public StringBuilder toString(StringBuilder sb, boolean includeRootKey, boolean
}

public List<Object> childrenValueAsList() {
return getChildren() == null ? Collections.emptyList() : ListUtil.map(getChildren(), c -> c.getValue());
return getChildren() == null ? Collections.emptyList() : map(getChildren(), c -> c.getValue());
}

public List<List<Object>> childrenValueAsListOfList() {
return getChildren() == null ? Collections.emptyList() : ListUtil.map(getChildren(), c -> c.childrenValueAsList());
return getChildren() == null ? Collections.emptyList() : map(getChildren(), c -> c.childrenValueAsList());
}

@Override public boolean equals(Object o) {
Expand All @@ -267,7 +268,8 @@ public List<List<Object>> childrenValueAsListOfList() {
return Objects.equals(key, tdNode.key) && Objects.equals(value, tdNode.value) && Objects.equals(children, tdNode.children);
}

/** Hash code of value and children, key is not included */
@Override public int hashCode() {
return hash.getOrCompute(() -> Objects.hash(key, value, children));
return hash.getOrCompute(() -> Objects.hash(value, children, map(children, TDNode::getKey)));
}
}
33 changes: 30 additions & 3 deletions treedoc/src/main/java/org/jsonex/treedoc/TreeDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import lombok.experimental.Accessors;

import java.net.URI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import static java.lang.String.format;
import static org.jsonex.core.util.LangUtil.doIfNotNull;
import static org.jsonex.core.util.ListUtil.mapKeys;

Expand Down Expand Up @@ -64,4 +63,32 @@ public static TreeDoc merge(Collection<TDNode> nodes) {

return result;
}

/**
* Dedupe the nodes that are with the same contends by comparing the hash
*/
public void dedupeNodes() {
Map<Integer, TDNode> hashToNodeMap = new LinkedHashMap<>();
Queue<TDNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()) {
TDNode n = queue.poll();
if (n.isLeaf())
continue; // Don't dedupe leaf node and array node
int hash = n.hashCode();
TDNode refNode = hashToNodeMap.get(hash);
if ( refNode != null && refNode.getType() == TDNode.Type.MAP) {
if (refNode.getChild(TDNode.ID_KEY) == null)
refNode.createChild(TDNode.ID_KEY).setValue(format("%x", hash));
n.children.clear();;
n.setType(TDNode.Type.MAP).createChild(TDNode.REF_KEY).setValue(format("#%x", hash));
} else {
hashToNodeMap.put(hash, n);
if (n.hasChildren())
for (TDNode cn : n.children) {
queue.add(cn);
}
}
}
}
}
19 changes: 0 additions & 19 deletions treedoc/src/test/java/org/jsonex/treeedoc/TDPathTest.java

This file was deleted.

17 changes: 17 additions & 0 deletions treedoc/src/test/resources/org/jsonex/treedoc/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"a": 1,
"b": {
"c": [1,2,3,4],
"d": {
"e": 1,
"f": 2
}
},
"e": {
"c": [1, 2, 3, 4],
"d": {
"e": 1,
"f": 2
}
}
}
19 changes: 19 additions & 0 deletions treedoc/src/test/resources/org/jsonex/treedoc/test_deduped.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
a:1,
b:{
c:[
1,
2,
3,
4
],
d:{
e:1,
f:2
},
$id:"5a9c3cc0"
},
e:{
$ref:"#5a9c3cc0"
}
}

0 comments on commit 29d8064

Please sign in to comment.