forked from dbs-leipzig/gradoop
/
MetaDataParser.java
441 lines (416 loc) · 15.2 KB
/
MetaDataParser.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Copyright © 2014 - 2018 Leipzig University (Database Research Group)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradoop.flink.io.impl.csv.metadata;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.gradoop.common.model.impl.id.GradoopId;
import org.gradoop.common.model.impl.properties.Property;
import org.gradoop.common.model.impl.properties.PropertyValue;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Responsible for creating a {@link MetaData} instance from its string representation.
*/
public class MetaDataParser {
/**
* Used to separate property meta data.
*/
private static final String PROPERTY_DELIMITER = ",";
/**
* Used to separate list items
*/
private static final String LIST_DELIMITER = ", ";
/**
* Used to separate property tokens (property-key, property-type)
*/
private static final String PROPERTY_TOKEN_DELIMITER = ":";
/**
* Used to map a type string to its corresponding parsing function
*/
private static final Map<String, Function<String, Object>> TYPE_PARSER_MAP = getTypeParserMap();
/**
* Creates the type - parser function mapping of static property TYPE_PARSER_MAP
*
* @return a HashMap containing the mapping of a type string to its corresponding parsing function
*/
private static Map<String, Function<String, Object>> getTypeParserMap() {
Map<String, Function<String, Object>> map = new HashMap<>();
map.put(TypeString.SHORT.getTypeString(), Short::parseShort);
map.put(TypeString.INTEGER.getTypeString(), Integer::parseInt);
map.put(TypeString.LONG.getTypeString(), Long::parseLong);
map.put(TypeString.FLOAT.getTypeString(), Float::parseFloat);
map.put(TypeString.DOUBLE.getTypeString(), Double::parseDouble);
map.put(TypeString.BOOLEAN.getTypeString(), Boolean::parseBoolean);
map.put(TypeString.STRING.getTypeString(), s -> s);
map.put(TypeString.BIGDECIMAL.getTypeString(), BigDecimal::new);
map.put(TypeString.GRADOOPID.getTypeString(), GradoopId::fromString);
map.put(TypeString.MAP.getTypeString(), MetaDataParser::parseMapProperty);
map.put(TypeString.LIST.getTypeString(), MetaDataParser::parseListProperty);
map.put(TypeString.LOCALDATE.getTypeString(), LocalDate::parse);
map.put(TypeString.LOCALTIME.getTypeString(), LocalTime::parse);
map.put(TypeString.LOCALDATETIME.getTypeString(), LocalDateTime::parse);
map.put(TypeString.NULL.getTypeString(), MetaDataParser::parseNullProperty);
return Collections.unmodifiableMap(map);
}
/**
* Creates a {@link MetaData} object from the specified lines. The specified tuple is already
* separated into the label and the
*
* @param metaDataStrings (label, meta-data) tuples
* @return Meta Data object
*/
public static MetaData create(List<Tuple3<String, String, String>> metaDataStrings) {
Map<Tuple2<String, String>, List<PropertyMetaData>> metaDataMap
= new HashMap<>(metaDataStrings.size());
for (Tuple3<String, String, String> tuple : metaDataStrings) {
List<PropertyMetaData> propertyMetaDataList;
if (tuple.f2.length() > 0) {
String[] propertyStrings = tuple.f2.split(PROPERTY_DELIMITER);
propertyMetaDataList = new ArrayList<>(propertyStrings.length);
for (String propertyString : propertyStrings) {
String[] propertyMetadata = propertyString.split(PROPERTY_TOKEN_DELIMITER, 2);
String[] propertyTypeTokens = propertyMetadata[1].split(PROPERTY_TOKEN_DELIMITER);
if (propertyTypeTokens.length == 2 &&
propertyTypeTokens[0].equals(TypeString.LIST.getTypeString())) {
// it's a list with one additional data type (type of list items)
propertyMetaDataList.add(new PropertyMetaData(
propertyMetadata[0], propertyMetadata[1], getListValueParser(propertyTypeTokens[1])));
} else if (propertyTypeTokens.length == 3 &&
propertyTypeTokens[0].equals(TypeString.MAP.getTypeString())) {
// it's a map with two additional data types (key type + value type)
propertyMetaDataList.add(
new PropertyMetaData(
propertyMetadata[0],
propertyMetadata[1],
getMapValueParser(propertyTypeTokens[1], propertyTypeTokens[2])
)
);
} else {
propertyMetaDataList.add(new PropertyMetaData(
propertyMetadata[0], propertyMetadata[1], getValueParser(propertyMetadata[1])));
}
}
} else {
propertyMetaDataList = new ArrayList<>(0);
}
metaDataMap.put(new Tuple2<>(tuple.f0, tuple.f1), propertyMetaDataList);
}
return new MetaData(metaDataMap);
}
/**
* Returns the property meta data string for the specified property. The string consists of the
* property key and the property value type, e.g. "foo:int".
*
* @param property property
* @return property meta data
*/
public static String getPropertyMetaData(Property property) {
return String.format("%s%s%s",
property.getKey(), PROPERTY_TOKEN_DELIMITER, getTypeString(property.getValue()));
}
/**
* Sorts and concatenates the specified meta data entries.
*
* @param propertyMetaDataSet set of property meta data strings
* @return sorted and concatenated string
*/
public static String getPropertiesMetaData(Set<String> propertyMetaDataSet) {
return propertyMetaDataSet.stream()
.sorted()
.collect(Collectors.joining(PROPERTY_DELIMITER));
}
/**
* Creates a parsing function for the given property type.
*
* @param type property type
* @return parsing function
*/
private static Function<String, Object> getValueParser(String type) {
type = type.toLowerCase();
if (TYPE_PARSER_MAP.containsKey(type)) {
return TYPE_PARSER_MAP.get(type);
} else {
throw new IllegalArgumentException("Type " + type + " is not supported");
}
}
/**
* Creates a parsing function for list property type.
*
* @param listItemType string representation of the list item type, e.g. "String"
* @return parsing function
*/
private static Function<String, Object> getListValueParser(String listItemType) {
final String itemType = listItemType.toLowerCase();
// check the validity of the list item type
if (!TYPE_PARSER_MAP.containsKey(itemType)) {
throw new TypeNotPresentException(itemType, null);
}
return s -> parseListProperty(s, TYPE_PARSER_MAP.get(itemType));
}
/**
* Creates a parsing function for map property type.
*
* @param keyType string representation of the map key type, e.g. "String"
* @param valueType string representation of the map value type, e.g. "Double"
* @return parsing function
*/
private static Function<String, Object> getMapValueParser(String keyType, String valueType) {
final String keyTypeLowerCase = keyType.toLowerCase();
// check the validity of the map key type
if (!TYPE_PARSER_MAP.containsKey(keyTypeLowerCase)) {
throw new TypeNotPresentException(keyTypeLowerCase, null);
}
final String valueTypeLowerCase = valueType.toLowerCase();
// check the validity of the map value type
if (!TYPE_PARSER_MAP.containsKey(valueTypeLowerCase)) {
throw new TypeNotPresentException(keyTypeLowerCase, null);
}
return s -> parseMapProperty(
s,
TYPE_PARSER_MAP.get(keyTypeLowerCase),
TYPE_PARSER_MAP.get(valueTypeLowerCase)
);
}
/**
* Parse function to translate string representation of a List to a list of PropertyValues
* Every PropertyValue has the type "string", because there is no parser for the items given
* Use {@link #parseListProperty(String, Function)} to specify a parsing function
*
* @param s the string to parse as list, e.g. "[myString1, myString2]"
* @return the list represented by the argument
*/
private static Object parseListProperty(String s) {
// no item type given, so use string as type
s = s.replace("[", "").replace("]", "");
return Arrays.stream(s.split(LIST_DELIMITER))
.map(PropertyValue::create)
.collect(Collectors.toList());
}
/**
* Parse function to translate string representation of a List to a list of PropertyValues
*
* @param s the string to parse as list, e.g. "[myString1, myString2]"
* @param itemParser the function to parse the list items
* @return the list represented by the argument
*/
private static Object parseListProperty(String s, Function<String, Object> itemParser) {
s = s.replace("[", "").replace("]", "");
return Arrays.stream(s.split(LIST_DELIMITER))
.map(itemParser)
.map(PropertyValue::create)
.collect(Collectors.toList());
}
/**
* Parse function to translate string representation of a Map to a Map with key and value of
* type PropertyValue. Every PropertyValue (key and value) has the type "string", because there
* are no parsers for the keys and values given. Use
* {@link #parseMapProperty(String, Function, Function)} to specify both parsing functions.
*
* @param s the string to parse as map, e.g. "{myString1=myValue1, myString2=myValue2}"
* @return the map represented by the argument
*/
private static Object parseMapProperty(String s) {
// no key type and value type given, so use string as types
s = s.replace("{", "").replace("}", "");
return Arrays.stream(s.split(LIST_DELIMITER))
.map(st -> st.split("="))
.collect(Collectors.toMap(e -> PropertyValue.create(e[0]), e -> PropertyValue.create(e[1])));
}
/**
* Parse function to translate string representation of a Map to a Map with
* key and value of type PropertyValue
*
* @param s the string to parse as map, e.g. "{myString1=myValue1, myString2=myValue2}"
* @param keyParser the function to parse the keys
* @param valueParser the function to parse the values
* @return the map represented by the argument
*/
private static Object parseMapProperty(
String s,
Function<String, Object> keyParser,
Function<String, Object> valueParser
) {
s = s.replace("{", "").replace("}", "");
return Arrays.stream(s.split(LIST_DELIMITER))
.map(st -> st.split("="))
.map(strings -> {
Object[] objects = new Object[2];
objects[0] = keyParser.apply(strings[0]);
objects[1] = valueParser.apply(strings[1]);
return objects;
})
.collect(Collectors.toMap(e -> PropertyValue.create(e[0]), e -> PropertyValue.create(e[1])));
}
/**
* Parse function to create null from the null string representation.
*
* @param nullString The string representing null.
* @throws IllegalArgumentException The string that is passed has to represent null.
* @return Returns null
*/
private static Object parseNullProperty(String nullString) throws IllegalArgumentException {
if (nullString != null && nullString.equalsIgnoreCase("null")) {
return null;
} else {
throw new IllegalArgumentException("Only null represents a null string.");
}
}
/**
* Returns the type string for the specified property value.
*
* @param propertyValue property value
* @return property type string
*/
public static String getTypeString(PropertyValue propertyValue) {
if (propertyValue.isNull()) {
return TypeString.NULL.getTypeString();
} else if (propertyValue.isShort()) {
return TypeString.SHORT.getTypeString();
} else if (propertyValue.isInt()) {
return TypeString.INTEGER.getTypeString();
} else if (propertyValue.isLong()) {
return TypeString.LONG.getTypeString();
} else if (propertyValue.isFloat()) {
return TypeString.FLOAT.getTypeString();
} else if (propertyValue.isDouble()) {
return TypeString.DOUBLE.getTypeString();
} else if (propertyValue.isBoolean()) {
return TypeString.BOOLEAN.getTypeString();
} else if (propertyValue.isString()) {
return TypeString.STRING.getTypeString();
} else if (propertyValue.isBigDecimal()) {
return TypeString.BIGDECIMAL.getTypeString();
} else if (propertyValue.isGradoopId()) {
return TypeString.GRADOOPID.getTypeString();
} else if (propertyValue.isMap()) {
// map type string is map:{keyType}:{valueType}
return TypeString.MAP.getTypeString() +
PROPERTY_TOKEN_DELIMITER +
getTypeString(propertyValue.getMap().keySet().iterator().next()) +
PROPERTY_TOKEN_DELIMITER +
getTypeString(propertyValue.getMap().values().iterator().next());
} else if (propertyValue.isList()) {
// list type string is list:{itemType}
return TypeString.LIST.getTypeString() +
PROPERTY_TOKEN_DELIMITER +
getTypeString(propertyValue.getList().get(0));
} else if (propertyValue.isDate()) {
return TypeString.LOCALDATE.getTypeString();
} else if (propertyValue.isTime()) {
return TypeString.LOCALTIME.getTypeString();
} else if (propertyValue.isDateTime()) {
return TypeString.LOCALDATETIME.getTypeString();
} else {
throw new IllegalArgumentException("Type " + propertyValue.getType() + " is not supported");
}
}
/**
* Supported type strings for the CSV format.
*/
private enum TypeString {
/**
* Null type
*/
NULL("null"),
/**
* Boolean type
*/
BOOLEAN("boolean"),
/**
* Short type
*/
SHORT("short"),
/**
* Integer type
*/
INTEGER("int"),
/**
* Long type
*/
LONG("long"),
/**
* Float type
*/
FLOAT("float"),
/**
* Double type
*/
DOUBLE("double"),
/**
* String type
*/
STRING("string"),
/**
* BigDecimal type
*/
BIGDECIMAL("bigdecimal"),
/**
* GradoopId type
*/
GRADOOPID("gradoopid"),
/**
* Map type
*/
MAP("map"),
/**
* List type
*/
LIST("list"),
/**
* LocalDate type
*/
LOCALDATE("localdate"),
/**
* LocalTime type
*/
LOCALTIME("localtime"),
/**
* LocalDateTime type
*/
LOCALDATETIME("localdatetime");
/**
* String representation
*/
private String typeString;
/**
* Constructor
*
* @param typeString string representation
*/
TypeString(String typeString) {
this.typeString = typeString;
}
/**
* Returns the type string
*
* @return type string
*/
public String getTypeString() {
return typeString;
}
}
}