-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathPropertyAnalyzer.java
More file actions
165 lines (150 loc) · 6.14 KB
/
Copy pathPropertyAnalyzer.java
File metadata and controls
165 lines (150 loc) · 6.14 KB
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
package org.neo4j.tool;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.helpers.collection.MapUtil;
import java.io.File;
import java.lang.reflect.Array;
import java.util.*;
/**
* @author mh
* @since 28.07.11
*/
public class PropertyAnalyzer {
static class PropertyInfo {
String name;
int count;
int emptyCount;
Set<String> types=new HashSet<String>();
long min, max;
int minCount, maxCount;
PropertyInfo(String name) {
this.name = name;
}
public void update(PropertyContainer pc) {
count++;
final Object value = pc.getProperty(name);
final Class<?> type = value.getClass();
types.add(type.getSimpleName());
final int size = toSize(value);
minCount = Math.min(minCount, size);
maxCount = Math.max(maxCount, size);
final long length = toNumber(value);
min = Math.min(min, length);
max = Math.max(max, length);
if (isDefaultValue(value)) {
emptyCount++;
}
}
private long toNumber(Object value) {
if (value instanceof Number) {
return ((Number)value).longValue();
}
if (value instanceof String) {
return ((String) value).length();
}
if (value instanceof Boolean && ((Boolean)value)) {
return 1;
}
if (value.getClass().isArray()) {
long sum=0;
for (int i = Array.getLength(value)-1;i>=0;i--) {
sum += toNumber(Array.get(value,i));
}
return sum;
}
return 0;
}
private int toSize(Object value) {
if (value.getClass().isArray()) {
return Array.getLength(value);
}
return 1;
}
@Override
public String toString() {
return ""+name+" "+ count+" "+ emptyCount+" "+ types+" "+minCount+" "+maxCount+" "+min+" "+max;
}
public int getEmptyCount() {
return emptyCount;
}
public int getCount() {
return count;
}
}
public static Map<String,String> config() {
return (Map) MapUtil.map(
"neostore.nodestore.db.mapped_memory", "100M",
"neostore.relationshipstore.db.mapped_memory", "500M",
"neostore.propertystore.db.mapped_memory", "300M",
"neostore.propertystore.db.strings.mapped_memory", "1G",
"neostore.propertystore.db.arrays.mapped_memory", "300M",
"neostore.propertystore.db.index.keys.mapped_memory", "100M",
"neostore.propertystore.db.index.mapped_memory", "100M",
"allow_store_upgrade","true",
"cache_type", "weak"
);
}
public static void main(String[] args) {
final GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File(args[0])).setConfig(config()).newGraphDatabase();
int withoutProps=0, nodes = 0, rels = 0;
Map<String,PropertyInfo> props=new HashMap<String, PropertyInfo>();
for (Node node : db.getAllNodes()) {
nodes ++;
withoutProps += analyzeProperties(props, node);
for (Relationship relationship : node.getRelationships(Direction.OUTGOING)) {
rels ++;
withoutProps += analyzeProperties(props, relationship);
}
if (nodes % 1000 == 0) {
System.out.print(".");
if (nodes % 100000 == 0)
System.out.println(" "+nodes);
}
}
db.shutdown();
outputEmptyCounts(withoutProps, props, nodes, rels);
}
private static int analyzeProperties(Map<String, PropertyInfo> props, PropertyContainer propertyContainer) {
boolean hasProps = false;
for (String property : propertyContainer.getPropertyKeys()) {
hasProps=true;
update(props, property,propertyContainer);
}
return hasProps ? 0 : 1;
}
private static void update(Map<String, PropertyInfo> props, String property, PropertyContainer pc) {
PropertyInfo info = props.get(property);
if (info==null) {
info = new PropertyInfo(property);
props.put(property, info);
}
info.update(pc);
}
private static void outputEmptyCounts(int withoutProps, Map<String, PropertyInfo> props, int nodes, int rels) {
System.out.println();
int emptyCount=0, allCount = 0;
for (PropertyInfo info : props.values()) {
emptyCount += info.getEmptyCount();
allCount += info.getCount();
System.out.println(info);
}
System.out.printf("%d of %d empty properties %d nodes %d rels pc w/o props %d%n", emptyCount, allCount,nodes,rels,withoutProps);
}
private static boolean isDefaultValue(Object property) {
if (property==null) return true;
if (property instanceof String) return ((String)property).isEmpty();
if (property instanceof Number) return ((Number)property).longValue() == 0 || ((Number)property).doubleValue() == 0.0D;
if (property instanceof Character) return (Character) property == 0;
if (property instanceof Boolean) return !(Boolean) property;
if (property instanceof String[]) return ((String[])property).length==0;
if (property instanceof int[]) return ((int[])property).length==0;
if (property instanceof long[]) return ((long[])property).length==0;
if (property instanceof boolean[]) return ((boolean[])property).length==0;
if (property instanceof char[]) return ((char[])property).length==0;
if (property instanceof byte[]) return ((byte[])property).length==0;
if (property instanceof float[]) return ((float[])property).length==0;
if (property instanceof double[]) return ((double[])property).length==0;
System.out.println("property = " + property);
return false;
}
}