forked from neo4j-contrib/neo4j-apoc-procedures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.java
1013 lines (903 loc) · 39.8 KB
/
Util.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package apoc.util;
import apoc.ApocConfig;
import apoc.Pools;
import apoc.convert.Convert;
import apoc.export.util.CountingInputStream;
import apoc.result.VirtualNode;
import apoc.result.VirtualRelationship;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.collections.api.iterator.LongIterator;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotInTransactionException;
import org.neo4j.graphdb.QueryExecutionException;
import org.neo4j.graphdb.QueryExecutionType;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.TransactionTerminatedException;
import org.neo4j.internal.helpers.collection.Iterators;
import org.neo4j.internal.helpers.collection.Pair;
import org.neo4j.internal.kernel.api.procs.ProcedureCallContext;
import org.neo4j.internal.kernel.api.security.SecurityContext;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.Log;
import org.neo4j.procedure.TerminationGuard;
import org.neo4j.values.storable.CoordinateReferenceSystem;
import org.neo4j.values.storable.PointValue;
import org.neo4j.values.storable.Values;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.PrimitiveIterator;
import java.util.Scanner;
import java.util.Set;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.lang.model.SourceVersion;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotInTransactionException;
import org.neo4j.graphdb.QueryExecutionException;
import org.neo4j.graphdb.QueryExecutionType;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.TransactionTerminatedException;
import org.neo4j.internal.helpers.collection.Iterators;
import org.neo4j.internal.helpers.collection.Pair;
import org.neo4j.internal.kernel.api.procs.ProcedureCallContext;
import org.neo4j.internal.kernel.api.security.SecurityContext;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.Log;
import org.neo4j.procedure.TerminationGuard;
import static apoc.ApocConfig.apocConfig;
import static apoc.export.cypher.formatter.CypherFormatterUtils.formatProperties;
import static apoc.export.cypher.formatter.CypherFormatterUtils.formatToString;
import static apoc.util.DateFormatUtil.getOrCreate;
import static java.lang.String.format;
import static org.eclipse.jetty.util.URIUtil.encodePath;
/**
* @author mh
* @since 24.04.16
*/
public class Util {
public static final Label[] NO_LABELS = new Label[0];
public static final String NODE_COUNT = "MATCH (n) RETURN count(*) as result";
public static final String REL_COUNT = "MATCH ()-->() RETURN count(*) as result";
public static final String COMPILED = "interpreted"; // todo handle enterprise properly
public static final String ERROR_BYTES_OR_STRING = "Only byte[] or url String allowed";
public static String labelString(List<String> labelNames) {
return labelNames.stream().map(Util::quote).collect(Collectors.joining(":"));
}
public static String labelString(Node n) {
return joinLabels(n.getLabels(), ":");
}
public static String joinLabels(Iterable<Label> labels, String s) {
return StreamSupport.stream(labels.spliterator(), false).map(Label::name).collect(Collectors.joining(s));
}
public static List<String> labelStrings(Node n) {
return StreamSupport.stream(n.getLabels().spliterator(),false).map(Label::name).sorted().collect(Collectors.toList());
}
public static Label[] labels(Object labelNames) {
if (labelNames==null) return NO_LABELS;
if (labelNames instanceof List) {
Set names = new LinkedHashSet((List) labelNames); // Removing duplicates
Label[] labels = new Label[names.size()];
int i = 0;
for (Object l : names) {
if (l==null) continue;
labels[i++] = Label.label(l.toString());
}
if (i <= labels.length) return Arrays.copyOf(labels,i);
return labels;
}
return new Label[]{Label.label(labelNames.toString())};
}
public static RelationshipType type(Object type) {
if (type == null) throw new RuntimeException("No relationship-type provided");
return RelationshipType.withName(type.toString());
}
@SuppressWarnings("unchecked")
public static LongStream ids(Object ids) {
if (ids == null) return LongStream.empty();
if (ids instanceof Number) return LongStream.of(((Number)ids).longValue());
if (ids instanceof Node) return LongStream.of(((Node)ids).getId());
if (ids instanceof Relationship) return LongStream.of(((Relationship)ids).getId());
if (ids instanceof Collection) {
Collection<Object> coll = (Collection<Object>) ids;
return coll.stream().mapToLong( (o) -> ((Number)o).longValue());
}
if (ids instanceof Iterable) {
Spliterator<Object> spliterator = ((Iterable) ids).spliterator();
return StreamSupport.stream(spliterator,false).mapToLong( (o) -> ((Number)o).longValue());
}
throw new RuntimeException("Can't convert "+ids.getClass()+" to a stream of long ids");
}
public static Stream<Object> stream(Object values) {
return Convert.convertToList(values).stream();
}
public static Stream<Node> nodeStream(Transaction tx, Object ids) {
return stream(ids).map(id -> node(tx, id));
}
public static Node node(Transaction tx, Object id) {
if (id instanceof Node) return rebind(tx, (Node)id);
if (id instanceof Number) return tx.getNodeById(((Number)id).longValue());
throw new RuntimeException("Can't convert "+id.getClass()+" to a Node");
}
public static Stream<Relationship> relsStream(Transaction tx, Object ids) {
return stream(ids).map(id -> relationship(tx, id));
}
public static Relationship relationship(Transaction tx, Object id) {
if (id instanceof Relationship) return rebind(tx, (Relationship)id);
if (id instanceof Number) return tx.getRelationshipById(((Number)id).longValue());
throw new RuntimeException("Can't convert "+id.getClass()+" to a Relationship");
}
public static double doubleValue(Entity pc, String prop, Number defaultValue) {
return toDouble(pc.getProperty(prop, defaultValue));
}
public static double doubleValue(Entity pc, String prop) {
return doubleValue(pc, prop, 0);
}
public static Direction parseDirection(String direction) {
if (null == direction) {
return Direction.BOTH;
}
try {
return Direction.valueOf(direction.toUpperCase());
} catch (Exception e) {
throw new RuntimeException(format("Cannot convert value '%s' to Direction. Legal values are '%s'",
direction, Arrays.toString(Direction.values())));
}
}
public static RelationshipType[] toRelTypes(List<String> relTypeStrings) {
RelationshipType[] relTypes = new RelationshipType[relTypeStrings.size()];
for (int i = 0; i < relTypes.length; i++) {
relTypes[i] = RelationshipType.withName(relTypeStrings.get(i));
}
return relTypes;
}
public static <T> T retryInTx(Log log, GraphDatabaseService db, Function<Transaction, T> function, long retry, long maxRetries, Consumer<Long> callbackForRetry) {
try (Transaction tx = db.beginTx()) {
T result = function.apply(tx);
tx.commit();
return result;
} catch (Exception e) {
if (retry >= maxRetries) throw e;
if (log!=null) {
log.warn("Retrying operation %d of %d", retry, maxRetries);
}
callbackForRetry.accept(retry);
Util.sleep(100);
return retryInTx(log, db, function, retry + 1, maxRetries, callbackForRetry);
}
}
public static <T> Future<T> inTxFuture(Log log,
ExecutorService pool,
GraphDatabaseService db,
Function<Transaction, T> function,
long maxRetries,
Consumer<Long> callbackForRetry,
Consumer<Void> callbackAction) {
try {
return pool.submit(() -> {
try {
return retryInTx(log, db, function, 0, maxRetries, callbackForRetry);
} finally {
callbackAction.accept(null);
}
});
} catch (Exception e) {
throw new RuntimeException("Error executing in separate transaction", e);
}
}
public static <T> Future<T> inTxFuture(ExecutorService pool, GraphDatabaseService db, Function<Transaction, T> function) {
return inTxFuture(null, pool, db, function, 0, _ignored -> {}, _ignored -> {});
}
public static <T> T inTx(GraphDatabaseService db, Pools pools, Function<Transaction, T> function) {
try {
return inTxFuture(pools.getDefaultExecutorService(), db, function).get();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
// TODO: consider dropping the exception to logs
throw new RuntimeException("Error executing in separate transaction: "+e.getMessage(), e);
}
}
public static <T> T inThread(Pools pools, Callable<T> callable) {
try {
return inFuture(pools, callable).get();
} catch (Exception e) {
throw new RuntimeException("Error executing in separate thread: "+e.getMessage(), e);
}
}
public static <T> Future<T> inFuture(Pools pools, Callable<T> callable) {
return pools.getDefaultExecutorService().submit(callable);
}
public static Double toDouble(Object value) {
if (value == null) return null;
if (value instanceof Number) return ((Number) value).doubleValue();
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException e) {
return null;
}
}
public static Map<String, Object> subMap(Map<String, ?> params, String prefix) {
Map<String, Object> config = new HashMap<>(10);
int len = prefix.length() + (prefix.isEmpty() || prefix.endsWith(".") ? 0 : 1);
for (Map.Entry<String, ?> entry : params.entrySet()) {
String key = entry.getKey();
if (key.startsWith(prefix)) {
config.put(key.substring(len), entry.getValue());
}
}
return config;
}
public static Long toLong(Object value) {
if (value == null) return null;
if (value instanceof Number) return ((Number)value).longValue();
try {
String s = value.toString();
if (s.contains(".")) return (long)Double.parseDouble(s);
return Long.parseLong(s);
} catch (NumberFormatException e) {
return null;
}
}
public static Integer toInteger(Object value) {
if (value == null) return null;
if (value instanceof Number) return ((Number)value).intValue();
try {
String s = value.toString();
if (s.contains(".")) return (int)Double.parseDouble(s);
return Integer.parseInt(value.toString());
} catch (NumberFormatException e) {
return null;
}
}
public static URLConnection openUrlConnection(String url, Map<String, Object> headers) throws IOException {
URL src = new URL(url);
URLConnection con = src.openConnection();
con.setRequestProperty("User-Agent", "APOC Procedures for Neo4j");
if (headers != null) {
Object method = headers.get("method");
if (method != null && con instanceof HttpURLConnection) {
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod(method.toString());
http.setChunkedStreamingMode(1024*1024);
http.setInstanceFollowRedirects(true);
}
headers.forEach((k,v) -> con.setRequestProperty(k, v == null ? "" : v.toString()));
}
// con.setDoInput(true);
con.setConnectTimeout(apocConfig().getInt("apoc.http.timeout.connect",10_000));
con.setReadTimeout(apocConfig().getInt("apoc.http.timeout.read",60_000));
return con;
}
public static boolean isRedirect(HttpURLConnection con) throws IOException {
int code = con.getResponseCode();
boolean isRedirectCode = code >= 300 && code < 400;
if (isRedirectCode) {
URL location = new URL(con.getHeaderField("Location"));
String oldProtocol = con.getURL().getProtocol();
String protocol = location.getProtocol();
if (!protocol.equals(oldProtocol) && !protocol.startsWith(oldProtocol)) { // we allow http -> https redirect and similar
throw new RuntimeException("The redirect URI has a different protocol: " + location.toString());
}
}
return isRedirectCode;
}
private static void writePayload(URLConnection con, String payload) throws IOException {
if (payload == null) return;
con.setDoOutput(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(),"UTF-8"));
writer.write(payload);
writer.close();
}
private static String handleRedirect(URLConnection con, String url) throws IOException {
if (!(con instanceof HttpURLConnection)) return url;
if (!isRedirect(((HttpURLConnection)con))) return url;
return con.getHeaderField("Location");
}
public static CountingInputStream openInputStream(Object input, Map<String, Object> headers, String payload, String compressionAlgo) throws IOException {
if (input instanceof String) {
String urlAddress = (String) input;
if (urlAddress.contains("!") && (urlAddress.contains(".zip") || urlAddress.contains(".tar") || urlAddress.contains(".tgz"))) {
return getStreamCompressedFile(urlAddress, headers, payload);
}
StreamConnection sc = getStreamConnection(urlAddress, headers, payload);
return sc.toCountingInputStream();
} else if (input instanceof byte[]) {
return FileUtils.getInputStreamFromBinary((byte[]) input, compressionAlgo);
} else {
throw new RuntimeException(ERROR_BYTES_OR_STRING);
}
}
private static CountingInputStream getStreamCompressedFile(String urlAddress, Map<String, Object> headers, String payload) throws IOException {
StreamConnection sc;
InputStream stream;
String[] tokens = urlAddress.split("!");
urlAddress = tokens[0];
String zipFileName;
if(tokens.length == 2) {
zipFileName = tokens[1];
sc = getStreamConnection(urlAddress, headers, payload);
stream = getFileStreamIntoCompressedFile(sc.getInputStream(), zipFileName);
}else
throw new IllegalArgumentException("filename can't be null or empty");
return new CountingInputStream(stream, sc.getLength());
}
private static StreamConnection getStreamConnection(String urlAddress, Map<String, Object> headers, String payload) throws IOException {
return FileUtils.SupportedProtocols
.from(urlAddress)
.getStreamConnection(urlAddress, headers, payload);
}
private static InputStream getFileStreamIntoCompressedFile(InputStream is, String fileName) throws IOException {
try (ZipInputStream zip = new ZipInputStream(is)) {
ZipEntry zipEntry;
while ((zipEntry = zip.getNextEntry()) != null) {
if (!zipEntry.isDirectory() && zipEntry.getName().equals(fileName)) {
return new ByteArrayInputStream(IOUtils.toByteArray(zip));
}
}
}
return null;
}
public static StreamConnection readHttpInputStream(String urlAddress, Map<String, Object> headers, String payload) throws IOException {
ApocConfig.apocConfig().checkReadAllowed(urlAddress);
URLConnection con = openUrlConnection(urlAddress, headers);
writePayload(con, payload);
String newUrl = handleRedirect(con, urlAddress);
if (newUrl != null && !urlAddress.equals(newUrl)) {
con.getInputStream().close();
return readHttpInputStream(newUrl, headers, payload);
}
return new StreamConnection.UrlStreamConnection(con);
}
public static boolean toBoolean(Object value) {
if ((value == null || value instanceof Number && (((Number) value).longValue()) == 0L || value instanceof String && (value.equals("") || ((String) value).equalsIgnoreCase("false") || ((String) value).equalsIgnoreCase("no")|| ((String) value).equalsIgnoreCase("0"))|| value instanceof Boolean && value.equals(false))) {
return false;
}
return true;
}
public static String encodeUrlComponent(String value) {
try {
return URLEncoder.encode(value,"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unsupported character set utf-8");
}
}
public static String toJson(Object value) {
try {
return JsonUtil.OBJECT_MAPPER.writeValueAsString(value);
} catch (IOException e) {
throw new RuntimeException("Can't convert "+value+" to JSON");
}
}
public static <T> T fromJson(String value, Class<T> type) {
try {
return JsonUtil.OBJECT_MAPPER.readValue(value,type);
} catch (IOException e) {
throw new RuntimeException("Can't convert "+value+" from JSON");
}
}
public static Stream<List<Object>> partitionSubList(List<Object> data, int partitions) {
return partitionSubList(data,partitions,null);
}
public static Stream<List<Object>> partitionSubList(List<Object> data, int partitions, List<Object> tombstone) {
if (partitions==0) partitions=1;
List<Object> list = new ArrayList<>(data);
int total = list.size();
int batchSize = Math.max((int)Math.ceil((double)total / partitions),1);
Stream<List<Object>> stream = IntStream.range(0, partitions).parallel()
.mapToObj((part) -> list.subList(Math.min(part * batchSize, total), Math.min((part + 1) * batchSize, total)))
.filter(partition -> !partition.isEmpty());
return tombstone == null ? stream : Stream.concat(stream,Stream.of(tombstone));
}
public static Long runNumericQuery(Transaction tx, String query, Map<String, Object> params) {
if (params == null) params = Collections.emptyMap();
try (ResourceIterator<Long> it = tx.execute(query,params).<Long>columnAs("result")) {
return it.next();
}
}
public static long nodeCount(Transaction tx) {
return runNumericQuery(tx,NODE_COUNT,null);
}
public static long relCount(Transaction tx) {
return runNumericQuery(tx,REL_COUNT,null);
}
public static LongStream toLongStream(LongIterator it) {
PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
@Override
public boolean hasNext() {
return it.hasNext();
}
@Override
public long nextLong() {
return it.next();
}
};
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
public static String readResourceFile(String name) {
InputStream is = Util.class.getClassLoader().getResourceAsStream(name);
return new Scanner(is).useDelimiter("\\Z").next();
}
@SuppressWarnings("unchecked")
public static Map<String,Object> readMap(String value) {
try {
return JsonUtil.OBJECT_MAPPER.readValue(value, Map.class);
} catch (IOException e) {
throw new RuntimeException("Couldn't read as JSON "+value);
}
}
public static <T> List<T> take(Iterator<T> iterator, int batchsize) {
List<T> result = new ArrayList<>(batchsize);
while (iterator.hasNext() && batchsize-- > 0) {
result.add(iterator.next());
}
return result;
}
public static Map<String, Object> merge(Map<String, Object> first, Map<String, Object> second) {
if (second == null || second.isEmpty()) return first == null ? Collections.EMPTY_MAP : first;
if (first == null || first.isEmpty()) return second == null ? Collections.EMPTY_MAP : second;
Map<String,Object> combined = new HashMap<>(first);
combined.putAll(second);
return combined;
}
public static <T> Map<String, T> map(T ... values) {
Map<String, T> map = new LinkedHashMap<>();
for (int i = 0; i < values.length; i+=2) {
if (values[i] == null) continue;
map.put(values[i].toString(),values[i+1]);
}
return map;
}
public static <T> Map<String, T> map(List<T> pairs) {
Map<String, T> res = new LinkedHashMap<>(pairs.size() / 2);
Iterator<T> it = pairs.iterator();
while (it.hasNext()) {
Object key = it.next();
T value = it.next();
if (key != null) res.put(key.toString(), value);
}
return res;
}
public static <T,R> List<R> map(Stream<T> stream, Function<T,R> mapper) {
return stream.map(mapper).collect(Collectors.toList());
}
public static <T,R> List<R> map(Collection<T> collection, Function<T,R> mapper) {
return map(collection.stream(), mapper);
}
public static Map<String, Object> mapFromLists(List<String> keys, List<Object> values) {
if (keys == null || values == null || keys.size() != values.size())
throw new RuntimeException("keys and values lists have to be not null and of same size");
if (keys.isEmpty()) return Collections.emptyMap();
if (keys.size()==1) return Collections.singletonMap(keys.get(0),values.get(0));
ListIterator<Object> it = values.listIterator();
Map<String, Object> res = new LinkedHashMap<>(keys.size());
for (String key : keys) {
res.put(key,it.next());
}
return res;
}
public static Map<String, Object> mapFromPairs(List<List<Object>> pairs) {
if (pairs.isEmpty()) return Collections.emptyMap();
Map<String,Object> map = new LinkedHashMap<>(pairs.size());
for (List<Object> pair : pairs) {
if (pair.isEmpty()) continue;
Object key = pair.get(0);
if (key==null) continue;
Object value = pair.size() >= 2 ? pair.get(1) : null;
map.put(key.toString(),value);
}
return map;
}
public static String cleanUrl(String url) {
try {
URL source = new URL(url);
String file = source.getFile();
if (source.getRef() != null) file += "#"+source.getRef();
return new URL(source.getProtocol(),source.getHost(),source.getPort(),file).toString();
} catch (MalformedURLException mfu) {
return String.format("invalid URL (%s)", url);
}
}
public static <T> T getFuture(Future<T> f, Map<String, Long> errorMessages, AtomicInteger errors, T errorValue) {
try {
T t = f.get();
return t;
} catch (Exception e) {
errors.incrementAndGet();
errorMessages.compute(e.getMessage(),(s, i) -> i == null ? 1 : i + 1);
return errorValue;
}
}
public static <T> T getFutureOrCancel(Future<T> f, Map<String, Long> errorMessages, AtomicInteger errors, T errorValue) {
try {
if (f.isDone()) return f.get();
else {
f.cancel(false);
errors.incrementAndGet();
}
} catch (Exception e) {
errors.incrementAndGet();
errorMessages.compute(e.getMessage(),(s, i) -> i == null ? 1 : i + 1);
}
return errorValue;
}
public static boolean isSumOutOfRange(long... numbers) {
try {
sumLongs(numbers).longValueExact();
return false;
} catch (ArithmeticException ae) {
return true;
}
}
public static BigInteger sumLongs(long... numbers) {
return LongStream.of(numbers)
.mapToObj(BigInteger::valueOf)
.reduce(BigInteger.ZERO, (x, y) -> x.add(y));
}
public static void logErrors(String message, Map<String, Long> errors, Log log) {
if (!errors.isEmpty()) {
log.warn(message);
errors.forEach((k, v) -> log.warn("%d times: %s",v,k));
}
}
public static void checkAdmin(SecurityContext securityContext, ProcedureCallContext callContext, String procedureName) {
if (!securityContext.allowExecuteAdminProcedure(callContext.id()).allowsAccess()) throw new RuntimeException("This procedure "+ procedureName +" is only available to admin users");
}
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
/* ignore */
}
}
public static String quote(String var) {
return SourceVersion.isIdentifier(var) && !var.contains("$") ? var : '`' + var + '`';
}
public static String sanitizeAndQuote(String var) {
return quote(var.replaceAll("`", ""));
}
public static String param(String var) {
return var.charAt(0) == '$' ? var : '$'+quote(var);
}
public static String withMapping(Stream<String> columns, Function<String, String> withMapping) {
String with = columns.map(withMapping).collect(Collectors.joining(","));
return with.isEmpty() ? with : " WITH "+with+" ";
}
public static boolean isWriteableInstance(GraphDatabaseAPI db) {
try {
try {
Class hadb = Class.forName("org.neo4j.kernel.ha.HighlyAvailableGraphDatabase");
boolean isSlave = hadb.isInstance(db) && !((Boolean)hadb.getMethod("isMaster").invoke(db));
if (isSlave) return false;
} catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
/* ignore */
}
String role = db.executeTransactionally("CALL dbms.cluster.role($databaseName)",
Collections.singletonMap("databaseName", db.databaseName()),
result -> Iterators.single(result.columnAs("role")));
return role.equalsIgnoreCase("LEADER");
} catch(QueryExecutionException e) {
if (e.getStatusCode().equalsIgnoreCase("Neo.ClientError.Procedure.ProcedureNotFound")) return true;
throw e;
}
}
/**
* Given a context related to the procedure invocation this method checks if the transaction is terminated in some way
*
* @param db
* @return
*
*/
public static boolean transactionIsTerminated(TerminationGuard db) {
try {
db.check();
return false;
} catch (TransactionTerminatedException | NotInTransactionException tge) {
return true;
}
}
public static void waitForFutures(List<Future> futures) {
for (Future future : futures) {
try {
if (future != null) future.get();
} catch (InterruptedException | ExecutionException e) {
// ignore
}
}
}
public static void removeFinished(List<Future> futures) {
if (futures.size() > 25) {
futures.removeIf(Future::isDone);
}
}
public static void close(AutoCloseable closeable, Consumer<Exception> onErrror) {
try {
if (closeable!=null) closeable.close();
} catch (Exception e) {
// ignore
if (onErrror != null) {
onErrror.accept(e);
}
}
}
public static void close(AutoCloseable closeable) {
close(closeable, null);
}
public static boolean isNotNullOrEmpty(String s) {
return s!=null && s.trim().length()!=0;
}
public static boolean isNullOrEmpty(String s) {
return s==null || s.trim().length()==0;
}
public static Map<String, String> getRequestParameter(String parameters){
Map<String, String> params = null;
if(Objects.nonNull(parameters)){
params = new HashMap<>();
String[] queryStrings = parameters.split("&");
for (String query : queryStrings) {
String[] parts = query.split("=");
if (parts.length == 2) {
params.put(parts[0], parts[1]);
}
}
}
return params;
}
public static boolean classExists(String className) {
try {
Class.forName(className);
return true;
} catch(ClassNotFoundException cnfe) {
return false;
}
}
public static <T> T createInstanceOrNull(String className) {
try {
return (T)Class.forName(className).getDeclaredConstructor().newInstance();
} catch (IllegalAccessException | InstantiationException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) {
return null;
}
}
public static Optional<String> getLoadUrlByConfigFile(String loadType, String key, String suffix){
key = Optional.ofNullable(key)
.map(s ->
Stream.of("apoc", loadType, s, suffix).collect(Collectors.joining("."))
)
.orElse(StringUtils.EMPTY);
String value = apocConfig().getString(key);
return Optional.ofNullable(value);
}
public static String dateFormat(TemporalAccessor value, String format){
return getFormat(format).format(value);
}
public static Duration durationParse(String value) {
return Duration.parse(value);
}
public static DateTimeFormatter getFormat(String format) {
return getOrCreate(format);
}
public static char parseCharFromConfig(Map<String, Object> config, String key, char defaultValue) {
String separator = (String) config.getOrDefault(key, "");
if (separator == null || separator.isEmpty()) {
return defaultValue;
}
if ("TAB".equals(separator)) {
return '\t';
}
// "NONE" is used to resolve cases like issue #1376.
// That is, when I have a line like "VER: AX\GEARBOX\ASSEMBLY" and I don't want to convert it in "VER: AXGEARBOXASSEMBLY"
if ("NONE".equals(separator)) {
return '\0';
}
return separator.charAt(0);
}
public static Map<String, Object> flattenMap(Map<String, Object> map) {
return flattenMap(map, null);
}
public static Map<String, Object> flattenMap(Map<String, Object> map, String prefix) {
return map.entrySet().stream()
.flatMap(entry -> {
String key;
if (prefix != null && !prefix.isEmpty()) {
key = prefix + "." + entry.getKey();
} else {
key = entry.getKey();
}
Object value = entry.getValue();
if (value instanceof Map) {
return flattenMap((Map<String, Object>) value, key).entrySet().stream();
} else {
Map.Entry<String, Object> newEntry = new AbstractMap.SimpleEntry(key, entry.getValue());
return Stream.of(newEntry);
}
})
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
}
public static Node rebind(Transaction tx, Node node) {
return node instanceof VirtualNode ? node : tx.getNodeById(node.getId());
}
public static Relationship rebind(Transaction tx, Relationship rel) {
return rel instanceof VirtualRelationship ? rel : tx.getRelationshipById(rel.getId());
}
public static <T extends Entity> T rebind(Transaction tx, T e) {
if (e instanceof Node) {
return (T) rebind(tx, (Node) e);
} else {
return (T) rebind(tx, (Relationship) e);
}
}
public static <T extends Entity> List<T> rebind(List<T> entities, Transaction tx) {
return entities.stream()
.map(n -> Util.rebind(tx, n))
.collect(Collectors.toList());
}
public static Node mergeNode(Transaction tx, Label primaryLabel, Label addtionalLabel,
Pair<String, Object>... pairs) {
Node node = Iterators.singleOrNull(tx.findNodes(primaryLabel, pairs[0].first(), pairs[0].other()).stream()
.filter(n -> addtionalLabel == null || n.hasLabel(addtionalLabel))
.filter( n -> {
for (int i=1; i<pairs.length; i++) {
if (!Objects.deepEquals(pairs[i].other(), n.getProperty(pairs[i].first(), null))) {
return false;
}
}
return true;
})
.iterator());
if (node==null) {
Label[] labels = addtionalLabel == null ?
new Label[]{primaryLabel} :
new Label[]{primaryLabel, addtionalLabel};
node = tx.createNode(labels);
for (int i=0; i<pairs.length; i++) {
node.setProperty(pairs[i].first(), pairs[i].other());
}
}
return node;
}
public static <T> Set<T> intersection(Collection<T> a, Collection<T> b) {
if (a == null || b == null) {
return Collections.emptySet();
}
Set<T> intersection = new HashSet<>(a);
intersection.retainAll(b);
return intersection;
}
public static void validateQuery(GraphDatabaseService db, String statement, QueryExecutionType.QueryType... supportedQueryTypes) {
final boolean isValid = db.executeTransactionally("EXPLAIN " + statement, Collections.emptyMap(), result ->
supportedQueryTypes == null || supportedQueryTypes.length == 0 || Stream.of(supportedQueryTypes)
.anyMatch(sqt -> sqt.equals(result.getQueryExecutionType().queryType())));
if (!isValid) {
throw new RuntimeException("Supported query types for the operation are " + Arrays.toString(supportedQueryTypes));
}
}
/**
* all new threads being created within apoc should be daemon threads to allow graceful termination, so use this whenever you need a thread
* @param target
* @return
*/
public static Thread newDaemonThread(Runnable target) {
Thread thread = new Thread(target);
thread.setDaemon(true);
return thread;
}
public static String encodeUserColonPassToBase64(String userPass) {
return new String(Base64.getEncoder().encode((userPass).getBytes()));
}
public static Map<String, Object> extractCredentialsIfNeeded(String url, boolean failOnError) {
try {
URI uri = new URI(encodePath(url));
String authInfo = uri.getUserInfo();
if (null != authInfo) {
String[] parts = authInfo.split(":");
if (2 == parts.length) {
String token = encodeUserColonPassToBase64(authInfo);
return MapUtil.map("Authorization", "Basic " + token);
}
}
} catch (Exception e) {
if(!failOnError)
return Collections.emptyMap();
else
throw new RuntimeException(e);
}
return Collections.emptyMap();
}
public static boolean isSelfRel(Relationship rel) {
return rel.getStartNodeId() == rel.getEndNodeId();
}
public static String toCypherMap(Map<String, Object> map) {
final StringBuilder builder = formatProperties(map);
return "{" + formatToString(builder) + "}";
}
public static PointValue toPoint(Map<String, Object> pointMap, Map<String, Object> defaultPointMap) {
double x;
double y;
Double z = null;
final CoordinateReferenceSystem crs = CoordinateReferenceSystem.byName((String) getOrDefault(pointMap, defaultPointMap, "crs"));
// It does not depend on the prefix of crs, I could also pass a point({x: 56.7, y: 12.78, crs: 'wgs-84'})
final boolean isLatitudePresent = pointMap.containsKey("latitude") || (!pointMap.containsKey("x") && defaultPointMap.containsKey("latitude"));
final boolean isCoord3D = crs.getName().endsWith("-3d");
if (isLatitudePresent) {
x = Util.toDouble(getOrDefault(pointMap, defaultPointMap, "longitude"));
y = Util.toDouble(getOrDefault(pointMap, defaultPointMap, "latitude"));
if (isCoord3D) {
z = Util.toDouble(getOrDefault(pointMap, defaultPointMap, "height"));
}
} else {
x = Util.toDouble(getOrDefault(pointMap, defaultPointMap, "x"));