-
Notifications
You must be signed in to change notification settings - Fork 80
/
AtlasResourceLoader.java
507 lines (464 loc) · 18.4 KB
/
AtlasResourceLoader.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package org.openstreetmap.atlas.geography.atlas;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import org.openstreetmap.atlas.exception.CoreException;
import org.openstreetmap.atlas.geography.atlas.builder.text.TextAtlasBuilder;
import org.openstreetmap.atlas.geography.atlas.items.AtlasEntity;
import org.openstreetmap.atlas.geography.atlas.multi.MultiAtlas;
import org.openstreetmap.atlas.geography.atlas.packed.PackedAtlas;
import org.openstreetmap.atlas.geography.atlas.sub.AtlasCutType;
import org.openstreetmap.atlas.streaming.compression.Decompressor;
import org.openstreetmap.atlas.streaming.resource.AbstractResource;
import org.openstreetmap.atlas.streaming.resource.File;
import org.openstreetmap.atlas.streaming.resource.FileSuffix;
import org.openstreetmap.atlas.streaming.resource.Resource;
import org.openstreetmap.atlas.utilities.collections.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Load an {@link Atlas} from a {@link Resource} or an {@link Iterable} of {@link Resource}s. Also
* supports loading based on a resource name filter. To recursively load all {@link Atlas}es in a
* directory, see the {@link AtlasResourceLoader#loadRecursively} method.
*
* @author lcram
*/
public class AtlasResourceLoader
{
public static final Predicate<Resource> HAS_TEXT_ATLAS_EXTENSION = FileSuffix
.resourceFilter(FileSuffix.ATLAS, FileSuffix.TEXT)
.or(FileSuffix.resourceFilter(FileSuffix.ATLAS, FileSuffix.TEXT, FileSuffix.GZIP));
public static final Predicate<Resource> HAS_ATLAS_EXTENSION = FileSuffix
.resourceFilter(FileSuffix.ATLAS)
.or(FileSuffix.resourceFilter(FileSuffix.ATLAS, FileSuffix.GZIP));
private static final Logger logger = LoggerFactory.getLogger(AtlasResourceLoader.class);
private static final Predicate<Resource> CONTENTS_LOOK_LIKE_TEXT_ATLAS = resource ->
{
checkFileExistsAndIsNotDirectory(resource);
setDecompressorFor(resource);
return resource.firstLine().equals(TextAtlasBuilder.getNodesHeader());
};
private Predicate<Resource> resourceFilter;
private Predicate<AtlasEntity> atlasEntityFilter;
private String multiAtlasName;
private static void checkFileExistsAndIsNotDirectory(final Resource resource)
{
if (resource instanceof File)
{
final File fileResource = (File) resource;
if (!fileResource.exists())
{
throw new CoreException("Resource {} was of type File but it could not be found",
resource.getName());
}
else if (fileResource.isDirectory())
{
throw new CoreException(
"Resource {} was of type File but it was a directory. Try loadRecursively instead.",
resource.getName());
}
}
}
private static void setDecompressorFor(final Resource resource)
{
if (FileSuffix.GZIP.matches(resource))
{
if (resource instanceof AbstractResource)
{
((AbstractResource) resource).setDecompressor(Decompressor.GZIP);
}
else
{
throw new CoreException(
"Provide resource was of type {} which does not support decompression.",
resource.getClass().getName());
}
}
}
public AtlasResourceLoader()
{
this.resourceFilter = resource -> true;
this.atlasEntityFilter = null;
}
/**
* Load an {@link Atlas} from the provided {@link Resource}(s). If more than one
* {@link Resource} is provided, the method will utilize the {@link MultiAtlas} to combine them.
* This method will fail with an exception if any of the provided {@link Resource}s do not
* contain a valid binary or text {@link Atlas}. This method should never return null.
*
* @param resources
* the {@link Resource}(s) from which to load
* @return the non-null loaded {@link Atlas}
*/
public Atlas load(final Resource... resources)
{
return load(Iterables.from(resources));
}
/**
* Load an {@link Atlas} from an {@link Iterable} of {@link Resource}s. If more than one
* {@link Resource} is provided, the method will utilize the {@link MultiAtlas} to combine them.
* This method will fail with an exception if any of the provided {@link Resource}s do not
* contain a valid binary or text {@link Atlas}. This method should never return null.
*
* @param resources
* the {@link Iterable} of {@link Resource}s from which to load
* @return the non-null loaded {@link Atlas}
*/
public Atlas load(final Iterable<? extends Resource> resources)
{
final List<Resource> atlasResources = Iterables.stream(resources)
.flatMap(this::upcastAndRemoveNullResources).filter(this.resourceFilter)
.collectToList();
final Optional<Atlas> resultAtlasOptional;
if (atlasResources.isEmpty())
{
throw new CoreException("No loadable Resources were found.");
}
else if (atlasResources.size() == 1)
{
resultAtlasOptional = loadAtlasResource(atlasResources.get(0));
}
else
{
resultAtlasOptional = loadMultipleAtlasResources(atlasResources);
}
if (resultAtlasOptional.isEmpty())
{
throw new CoreException(
"Unable to load atlas from provided Resources. If you are seeing this you likely found a bug with AtlasResourceLoader. Please report it.");
}
// Apply the filter at the end
return applyEntityFilter(resultAtlasOptional.get());
}
/**
* Load an {@link Atlas} from the provided {@link File} {@link Resource}(s). If any of the
* provided {@link File}(s) are directories, the method will recursively descend into the
* directory and include every {@link Atlas} it discovers. It identifies {@link Atlas}es by
* looking for {@link FileSuffix#ATLAS} file extensions. Like with the
* {@link AtlasResourceLoader#load} method, this method will utilize the {@link MultiAtlas} to
* combine the {@link Atlas}es. This method should never return null.
*
* @param resources
* the {@link File} {@link Resource}(s) from which to load
* @return the non-null loaded {@link Atlas}
*/
public Atlas loadRecursively(final Resource... resources)
{
return loadRecursively(Iterables.from(resources));
}
/**
* Load an {@link Atlas} from an {@link Iterable} of {@link File} {@link Resource}s. If any of
* the provided {@link File}(s) are directories, the method will recursively descend into the
* directory and include every {@link Atlas} it discovers. It identifies {@link Atlas}es by
* looking for {@link FileSuffix#ATLAS} file extensions. Like with the
* {@link AtlasResourceLoader#load} method, this method will utilize the {@link MultiAtlas} to
* combine the {@link Atlas}es. This method should never return null.
*
* @param resources
* the {@link Iterable} of {@link File} {@link Resource}s from which to load
* @return the non-null loaded {@link Atlas}
*/
public Atlas loadRecursively(final Iterable<Resource> resources)
{
final List<Resource> atlasResources = Iterables.stream(resources).filter(Objects::nonNull)
.flatMap(this::expandFileOrDirectoryRecursively)
.filter(HAS_ATLAS_EXTENSION.or(HAS_TEXT_ATLAS_EXTENSION))
.filter(this.resourceFilter).collectToList();
final Optional<Atlas> resultAtlasOptional = loadMultipleAtlasResources(atlasResources);
if (!resultAtlasOptional.isPresent())
{
throw new CoreException(
"Unable to load atlas from provided Resources. If you are seeing this you likely found a bug with AtlasResourceLoader. Please report it.");
}
// Apply the filter at the end
return applyEntityFilter(resultAtlasOptional.get());
}
/**
* This safe load method will never throw an exception. If any if the provided {@link Resource}s
* cannot be loaded into an {@link Atlas}, it will simply return an empty {@link Optional}.
*
* @param resources
* the {@link Resource}(s) from which to load
* @return an {@link Optional} wrapping the loaded {@link Atlas} if present
*/
public Optional<Atlas> safeLoad(final Resource... resources)
{
return safeLoad(Iterables.from(resources));
}
/**
* This safe load method will never throw an exception. If any if the provided {@link Resource}s
* cannot be loaded into an {@link Atlas}, it will simply return an empty {@link Optional}.
*
* @param resources
* the {@link Iterable} of {@link Resource}(s) from which to load
* @return an {@link Optional} wrapping the loaded {@link Atlas} if present
*/
public Optional<Atlas> safeLoad(final Iterable<Resource> resources)
{
try
{
return Optional.of(load(resources));
}
catch (final Exception exception)
{
logger.error("Could not load atlas from supplied resources", exception);
return Optional.empty();
}
}
/**
* This safe load method will never throw an exception. If any if the provided {@link Resource}s
* cannot be loaded into an {@link Atlas}, it will simply return an empty {@link Optional}. See
* the documentation for {@link AtlasResourceLoader#loadRecursively(Resource...)} for details on
* how the recursive load works.
*
* @param resources
* the {@link Iterable} of {@link Resource}(s) from which to load
* @return an {@link Optional} wrapping the loaded {@link Atlas} if present
*/
public Optional<Atlas> safeLoadRecursively(final Resource... resources)
{
return safeLoadRecursively(Iterables.from(resources));
}
/**
* This safe load method will never throw an exception. If any if the provided {@link Resource}s
* cannot be loaded into an {@link Atlas}, it will simply return an empty {@link Optional}. See
* the documentation for {@link AtlasResourceLoader#loadRecursively(Resource...)} for details on
* how the recursive load works.
*
* @param resources
* the {@link Iterable} of {@link Resource}(s) from which to load
* @return an {@link Optional} wrapping the loaded {@link Atlas} if present
*/
public Optional<Atlas> safeLoadRecursively(final Iterable<Resource> resources)
{
try
{
return Optional.of(loadRecursively(resources));
}
catch (final Exception exception)
{
logger.error("Could not load atlas from supplied resources", exception);
return Optional.empty();
}
}
/**
* Optionally add an {@link AtlasEntity} filter
*
* @param filter
* filter which {@link AtlasEntity}s to include/exclude in the {@link Atlas}
*/
public void setAtlasEntityFilter(final Predicate<AtlasEntity> filter)
{
this.atlasEntityFilter = filter;
}
/**
* Optionally add a {@link Resource} filter
*
* @param filter
* filter which {@link Resource} to load
*/
public void setResourceFilter(final Predicate<Resource> filter)
{
this.resourceFilter = filter;
}
/**
* Optionally add an {@link AtlasEntity} filter
*
* @param filter
* filter which {@link AtlasEntity}s to include/exclude in the {@link Atlas}
* @return fluent interface requires this be returned
*/
public AtlasResourceLoader withAtlasEntityFilter(final Predicate<AtlasEntity> filter)
{
setAtlasEntityFilter(filter);
return this;
}
/**
* Set the name for the {@link MultiAtlas} that results from the load.
*
* @param multiAtlasName
* the name
* @return instance of {@link AtlasResourceLoader} for method chaining
*/
public AtlasResourceLoader withMultiAtlasName(final String multiAtlasName)
{
this.multiAtlasName = multiAtlasName;
return this;
}
/**
* Optionally add a {@link Resource} filter
*
* @param filter
* filter which {@link Resource} to load
* @return instance of {@link AtlasResourceLoader} for method chaining
*/
public AtlasResourceLoader withResourceFilter(final Predicate<Resource> filter)
{
setResourceFilter(filter);
return this;
}
private Atlas applyEntityFilter(final Atlas atlasToFilter)
{
if (this.atlasEntityFilter != null)
{
final Optional<Atlas> subAtlas = atlasToFilter.subAtlas(this.atlasEntityFilter,
AtlasCutType.SOFT_CUT);
return subAtlas.orElseThrow(
() -> new CoreException("Entity filter resulted in an empty atlas"));
}
return atlasToFilter;
}
private List<Resource> expandFileOrDirectoryRecursively(final Resource resource)
{
if (resource == null)
{
return new ArrayList<>();
}
if (!(resource instanceof File))
{
throw new CoreException("Resource {} was not a File, instead was {}",
resource.getName(), resource.getClass().getName());
}
final File file = (File) resource;
final List<Resource> result = new ArrayList<>();
if (file.isDirectory())
{
file.listFilesRecursively().forEach(child ->
{
if (child.isGzipped())
{
child.setDecompressor(Decompressor.GZIP);
}
result.add(child);
});
}
else
{
result.add(file);
}
return result;
}
private List<Resource> filterForBinaryAtlasResources(final List<Resource> atlasResources)
{
return atlasResources.stream().filter(CONTENTS_LOOK_LIKE_TEXT_ATLAS.negate())
.collect(Collectors.toList());
}
private List<Resource> filterForTextAtlasResources(final List<Resource> atlasResources)
{
return atlasResources.stream().filter(CONTENTS_LOOK_LIKE_TEXT_ATLAS)
.collect(Collectors.toList());
}
private Optional<Atlas> loadAtlasResource(final Resource resource)
{
final Atlas result;
if (resource instanceof File)
{
checkFileExistsAndIsNotDirectory(resource);
}
if (resource.length() == 0L)
{
throw new CoreException("{} {} had zero length!", resource.getClass().getName(),
resource.getName());
}
if (CONTENTS_LOOK_LIKE_TEXT_ATLAS.test(resource))
{
setDecompressorFor(resource);
result = new TextAtlasBuilder().read(resource);
}
else
{
try
{
result = PackedAtlas.load(resource);
}
catch (final Exception exception)
{
throw new CoreException("Failed to load an atlas from {} with name {}",
resource.getClass().getName(), resource.getName(), exception);
}
}
return Optional.ofNullable(result);
}
private Optional<Atlas> loadMultipleAtlasResources(final List<Resource> atlasResources)
{
atlasResources.forEach(resource ->
{
if (resource instanceof File)
{
checkFileExistsAndIsNotDirectory(resource);
}
if (resource.length() == 0L)
{
throw new CoreException("{} {} had zero length!", resource.getClass().getName(),
resource.getName());
}
});
final List<Resource> binaryResources = filterForBinaryAtlasResources(atlasResources);
final List<Resource> textResources = filterForTextAtlasResources(atlasResources);
if (binaryResources.isEmpty() && textResources.isEmpty())
{
throw new CoreException("No loadable Resources were found.");
}
/*
* There are three scenarios that must be handled. 1) There were only binary atlases. 2)
* There was a mix of binary and text atlases. 3) There were only text atlases.
*/
MultiAtlas resultAtlas = null;
if (!binaryResources.isEmpty())
{
resultAtlas = MultiAtlas.loadFromPackedAtlas(binaryResources);
}
if (!textResources.isEmpty())
{
final List<Atlas> textAtlases = loadTextAtlases(textResources);
if (!textAtlases.isEmpty())
{
final MultiAtlas textMultiAtlas = new MultiAtlas(textAtlases);
/*
* In this case, 'resultAtlas' is not null because there was a mix of binary and
* text atlases.
*/
if (resultAtlas != null)
{
resultAtlas = new MultiAtlas(resultAtlas, textMultiAtlas);
}
/*
* For this case, there was no previous resultAtlas since no binary atlases were
* found.
*/
else
{
resultAtlas = textMultiAtlas;
}
}
}
if (this.multiAtlasName != null && resultAtlas != null)
{
resultAtlas.setName(this.multiAtlasName);
}
return Optional.ofNullable(resultAtlas);
}
private List<Atlas> loadTextAtlases(final List<Resource> textAtlasResources)
{
final List<Atlas> textAtlases = new ArrayList<>();
for (final Resource textResource : textAtlasResources)
{
setDecompressorFor(textResource);
final Atlas atlas = new TextAtlasBuilder().read(textResource);
textAtlases.add(atlas);
}
return textAtlases;
}
private List<Resource> upcastAndRemoveNullResources(final Resource resource)
{
final List<Resource> result = new ArrayList<>();
if (resource != null)
{
result.add(resource);
}
return result;
}
}