-
Notifications
You must be signed in to change notification settings - Fork 7
/
countNodes-offline-oakRun14andOlder.groovy
70 lines (62 loc) · 2.2 KB
/
countNodes-offline-oakRun14andOlder.groovy
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
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicInteger
import org.apache.jackrabbit.oak.api.Type
import org.apache.jackrabbit.oak.plugins.segment.SegmentBlob
import org.apache.jackrabbit.oak.spi.state.NodeState
def countNodes(NodeState n, deep = false, String path = "/", Integer flush = 50000, AtomicInteger count = new AtomicInteger(0), AtomicInteger binaries = new AtomicInteger(0), root = true) {
if(root) {
println "Counting nodes in tree ${path}"
}
cnt = count.incrementAndGet()
if (cnt % flush == 0) println(" " + cnt)
def propname = "";
try {
try {
for(prop in n.getProperties()) {
propname = prop.getName();
if(prop.getType() == Type.BINARY || prop.getType() == Type.BINARIES) {
for(b in prop.getValue(Type.BINARIES)) {
binaries.incrementAndGet()
if(deep) {
InputStream s = b.getNewStream();
try {
byte[] buffer = new byte[1024];
int l = s.read(buffer, 0, buffer.length);
} finally {
s.close();
}
} else if(b instanceof SegmentBlob) {
if(!((SegmentBlob)b).isExternal()) {
b.length()
}
} else {
b.length()
}
}
} else {
if(prop.isArray()) {
for(sf in prop.getValue(prop.getType())) {
// do nothing - we just need to read all values
}
} else {
prop.getValue(prop.getType());
}
}
}
} catch(ex) {
println "warning unable to read node ${path}@" + propname + " : " + ex.getMessage()
}
propname = "";
for(child in n.getChildNodeEntries()) {
countNodes(child.getNodeState(), deep, path + child.getName() + "/", flush, count, binaries, false)
}
} catch(e) {
println "warning unable to read node ${path} : " + e.getMessage()
}
if(root) {
println "Total nodes in tree ${path}: ${cnt}"
println "Total binaries in tree ${path}: ${binaries.get()}"
}
return cnt
}
countNodes(session.workingNode)