-
Notifications
You must be signed in to change notification settings - Fork 7
/
countNodes-oak16andLater.groovy
161 lines (145 loc) · 5.31 KB
/
countNodes-oak16andLater.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
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
//Adaptation of @stillalex's script from here https://gist.github.com/stillalex/06303f8cc1d3780d3eab4c72575883ae
//This version works with Oak 1.6 and later versions
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicInteger
import org.apache.jackrabbit.oak.api.Type
import org.apache.jackrabbit.oak.spi.state.NodeState
import org.apache.jackrabbit.oak.spi.state.NodeStore
import org.apache.jackrabbit.oak.commons.PathUtils
import com.google.common.collect.Lists
import java.util.List
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("countNodes.groovy");
def countNodes(NodeState n, deep = false, String path = "/", Integer flush = 1000, AtomicInteger count = new AtomicInteger(0), AtomicInteger binaries = new AtomicInteger(0), root = true) {
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("countNodes.groovy");
if(root) {
log.info("Counting nodes in tree ${path}");
}
cnt = count.incrementAndGet()
if (cnt % flush == 0) log.info(" " + cnt)
try {
for(prop in n.getProperties()) {
try {
if(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 {
b.length();
}
binaries.incrementAndGet();
}
} else if(prop.getType() == Type.BINARY) {
def b = prop.getValue(Type.BINARY);
if(deep) {
InputStream s = b.getNewStream();
try {
byte[] buffer = new byte[1024];
int l = s.read(buffer, 0, buffer.length);
} finally {
s.close();
}
} else {
b.length();
}
binaries.incrementAndGet();
} else {
// Check regular properties for missing segments
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(e) {
log.error("warning unable to read node properties ${path} ${prop.name}: " + e.getMessage())
//org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(e, out)
}
}
try {
for(child in n.getChildNodeEntries()) {
try {
countNodes(child.getNodeState(), deep, path + "/" + child.getName(), flush, count, binaries, false)
} catch(e) {
log.error("warning unable to read child node ${path} : " + e.getMessage())
//org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(e, out)
}
}
} catch(e) {
log.error("warning unable to read child entries ${path} : " + e.getMessage())
//org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(e, out)
}
} catch(e) {
log.error("warning unable to read node ${path} : " + e.getMessage())
//org.codehaus.groovy.runtime.StackTraceUtils.printSanitizedStackTrace(e, out)
}
if(root) {
log.info("Total nodes in tree ${path}: ${cnt}");
log.info("Total binaries in tree ${path}: ${binaries.get()}");
}
return cnt
}
def countNodes(session, path, deep) {
NodeStore nstore = session.getRootNode().sessionDelegate.root.store
def rs = nstore.root
def rnb = rs.builder()
def nb = rnb;
List<String> elements = Lists.newArrayList();
PathUtils.elements(path).each{String element ->
if (PathUtils.denotesParent(element)) {
if (!elements.isEmpty()) {
elements.remove(elements.size() - 1);
}
} else if (!PathUtils.denotesCurrent(element)) {
elements.add(element);
}
}
elements.each {
if(it.size() > 0) {
nb = nb.getChildNode(it)
}
}
countNodes(nb.getNodeState(), deep, path)
}
def countNodes(path) {
def repo = osgi.getService(org.apache.sling.jcr.api.SlingRepository)
def session = repo.loginAdministrative(null)
try {
countNodes(session, path, true)
} finally {
session.logout()
}
}
log.info("Running node counter");
// Or only check the async oak indexes
t1 = Thread.start("countNodes /oak:index",{countNodes("/oak:index")})
t2 = Thread.start("countNodes /content", {countNodes("/content")})
t3 = Thread.start("countNodes /var", {countNodes("/var")})
t4 = Thread.start("countNodes /etc", {countNodes("/etc")})
t5 = Thread.start("countNodes /tmp", {countNodes("/tmp")})
t6 = Thread.start("countNodes /conf", {countNodes("/conf")})
t7 = Thread.start("countNodes /jcr:system", {countNodes("/jcr:system")})
t8 = Thread.start("countNodes /apps", {countNodes("/apps")})
t9 = Thread.start("countNodes /libs", {countNodes("/libs")})
t10 = Thread.start("countNodes /home", {countNodes("/home")})
log.info("Done starting countNodes threads");
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
t6.join();
t7.join();
t8.join();
t9.join();
t10.join();
log.info("Done running countNodes");
null