Skip to content

Commit 070c629

Browse files
lvcarobfrank
authored andcommitted
New IterableGraph interface to make easier working with edges (#2228)
Implemented `toList()`, `getFirstOrNull()`, and `size()` (cherry picked from commit 0fce213)
1 parent 8ca85be commit 070c629

File tree

10 files changed

+102
-36
lines changed

10 files changed

+102
-36
lines changed

engine/src/main/java/com/arcadedb/graph/GraphEngine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public void deleteVertex(final VertexInternal vertex) {
439439
vertex.getDatabase().getSchema().getBucketById(vertex.getIdentity().getBucketId()).deleteRecord(vertex.getIdentity());
440440
}
441441

442-
public Iterable<Edge> getEdges(final VertexInternal vertex) {
442+
public IterableGraph<Edge> getEdges(final VertexInternal vertex) {
443443
final MultiIterator<Edge> result = new MultiIterator<>();
444444

445445
final EdgeLinkedList outEdges = getEdgeHeadChunk(vertex, Vertex.DIRECTION.OUT);
@@ -453,7 +453,7 @@ public Iterable<Edge> getEdges(final VertexInternal vertex) {
453453
return result;
454454
}
455455

456-
public Iterable<Edge> getEdges(final VertexInternal vertex, final Vertex.DIRECTION direction, final String... edgeTypes) {
456+
public IterableGraph<Edge> getEdges(final VertexInternal vertex, final Vertex.DIRECTION direction, final String... edgeTypes) {
457457
if (direction == null)
458458
throw new IllegalArgumentException("Direction is null");
459459

@@ -486,15 +486,15 @@ public Iterable<Edge> getEdges(final VertexInternal vertex, final Vertex.DIRECTI
486486
default:
487487
throw new IllegalArgumentException("Invalid direction " + direction);
488488
}
489-
return Collections.emptyList();
489+
return IterableGraph.emptyList();
490490
}
491491

492492
/**
493493
* Returns all the connected vertices, both directions, any edge type.
494494
*
495495
* @return An iterator of PVertex instances
496496
*/
497-
public Iterable<Vertex> getVertices(final VertexInternal vertex) {
497+
public IterableGraph<Vertex> getVertices(final VertexInternal vertex) {
498498
final MultiIterator<Vertex> result = new MultiIterator<>();
499499

500500
final EdgeLinkedList outEdges = getEdgeHeadChunk(vertex, Vertex.DIRECTION.OUT);
@@ -516,7 +516,7 @@ public Iterable<Vertex> getVertices(final VertexInternal vertex) {
516516
*
517517
* @return An iterator of PVertex instances
518518
*/
519-
public Iterable<Vertex> getVertices(final VertexInternal vertex, final Vertex.DIRECTION direction, final String... edgeTypes) {
519+
public IterableGraph<Vertex> getVertices(final VertexInternal vertex, final Vertex.DIRECTION direction, final String... edgeTypes) {
520520
if (direction == null)
521521
throw new IllegalArgumentException("Direction is null");
522522

@@ -549,7 +549,7 @@ public Iterable<Vertex> getVertices(final VertexInternal vertex, final Vertex.DI
549549
default:
550550
throw new IllegalArgumentException("Invalid direction " + direction);
551551
}
552-
return Collections.emptyList();
552+
return IterableGraph.emptyList();
553553
}
554554

555555
public boolean isVertexConnectedTo(final VertexInternal vertex, final Identifiable toVertex) {

engine/src/main/java/com/arcadedb/graph/ImmutableVertex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,22 +152,22 @@ public long countEdges(final DIRECTION direction, final String edgeType) {
152152
}
153153

154154
@Override
155-
public Iterable<Edge> getEdges() {
155+
public IterableGraph<Edge> getEdges() {
156156
return database.getGraphEngine().getEdges(getMostUpdatedVertex(this));
157157
}
158158

159159
@Override
160-
public Iterable<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
160+
public IterableGraph<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
161161
return database.getGraphEngine().getEdges(getMostUpdatedVertex(this), direction, edgeTypes);
162162
}
163163

164164
@Override
165-
public Iterable<Vertex> getVertices() {
165+
public IterableGraph<Vertex> getVertices() {
166166
return database.getGraphEngine().getVertices(getMostUpdatedVertex(this));
167167
}
168168

169169
@Override
170-
public Iterable<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
170+
public IterableGraph<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
171171
return database.getGraphEngine().getVertices(getMostUpdatedVertex(this), direction, edgeTypes);
172172
}
173173

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright © 2021-present Arcade Data Ltd (info@arcadedata.com)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com)
17+
* SPDX-License-Identifier: Apache-2.0
18+
*/
19+
package com.arcadedb.graph;
20+
21+
import com.arcadedb.utility.MultiIterator;
22+
23+
import java.util.*;
24+
25+
/**
26+
* Iterable implementation with utility methods to browse and count elements.
27+
*
28+
* @author Luca Garulli (l.garulli@arcadedata.it)
29+
*/
30+
public interface IterableGraph<T> extends Iterable<T> {
31+
static <T> IterableGraph<T> emptyList() {
32+
return () -> Collections.emptyIterator();
33+
}
34+
35+
default T getFirstOrNull() {
36+
if (this instanceof List<?> list)
37+
return list.isEmpty() ? null : (T) list.getFirst();
38+
39+
for (final T item : this)
40+
return (T) item;
41+
return null;
42+
}
43+
44+
default List<T> toList() {
45+
final List<T> result = new ArrayList<>();
46+
for (final T item : this) {
47+
result.add(item);
48+
}
49+
return result;
50+
}
51+
52+
default int size() {
53+
if (this instanceof Collection<?> coll)
54+
return coll.size();
55+
else if (this instanceof MultiIterator<T> it)
56+
return (int) it.countEntries();
57+
58+
int count = 0;
59+
for (final T item : this)
60+
count++;
61+
return count;
62+
}
63+
}

engine/src/main/java/com/arcadedb/graph/MutableVertex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,22 @@ public long countEdges(final DIRECTION direction, final String edgeType) {
164164
}
165165

166166
@Override
167-
public Iterable<Edge> getEdges() {
167+
public IterableGraph<Edge> getEdges() {
168168
return database.getGraphEngine().getEdges(this);
169169
}
170170

171171
@Override
172-
public Iterable<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
172+
public IterableGraph<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
173173
return database.getGraphEngine().getEdges(this, direction, edgeTypes);
174174
}
175175

176176
@Override
177-
public Iterable<Vertex> getVertices() {
177+
public IterableGraph<Vertex> getVertices() {
178178
return database.getGraphEngine().getVertices(this);
179179
}
180180

181181
@Override
182-
public Iterable<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
182+
public IterableGraph<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
183183
return database.getGraphEngine().getVertices(this, direction, edgeTypes);
184184
}
185185

engine/src/main/java/com/arcadedb/graph/SynchronizedVertex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,22 @@ public synchronized long countEdges(final DIRECTION direction, final String edge
9090
}
9191

9292
@Override
93-
public synchronized Iterable<Edge> getEdges() {
93+
public synchronized IterableGraph<Edge> getEdges() {
9494
return delegate.getEdges();
9595
}
9696

9797
@Override
98-
public synchronized Iterable<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
98+
public synchronized IterableGraph<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
9999
return delegate.getEdges(direction, edgeTypes);
100100
}
101101

102102
@Override
103-
public synchronized Iterable<Vertex> getVertices() {
103+
public synchronized IterableGraph<Vertex> getVertices() {
104104
return delegate.getVertices();
105105
}
106106

107107
@Override
108-
public synchronized Iterable<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
108+
public synchronized IterableGraph<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
109109
return delegate.getVertices(direction, edgeTypes);
110110
}
111111

engine/src/main/java/com/arcadedb/graph/Vertex.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ enum DIRECTION {
5757

5858
long countEdges(DIRECTION direction, String edgeType);
5959

60-
Iterable<Edge> getEdges();
60+
IterableGraph<Edge> getEdges();
6161

62-
Iterable<Edge> getEdges(DIRECTION direction, String... edgeTypes);
62+
IterableGraph<Edge> getEdges(DIRECTION direction, String... edgeTypes);
6363

6464
/**
6565
* Returns all the connected vertices, both directions, any edge type.
6666
*
6767
* @return An iterator of PIndexCursorEntry entries
6868
*/
69-
Iterable<Vertex> getVertices();
69+
IterableGraph<Vertex> getVertices();
7070

7171
/**
7272
* Returns the connected vertices.
@@ -75,7 +75,7 @@ enum DIRECTION {
7575
*
7676
* @return An iterator of PIndexCursorEntry entries
7777
*/
78-
Iterable<Vertex> getVertices(DIRECTION direction, String... edgeTypes);
78+
IterableGraph<Vertex> getVertices(DIRECTION direction, String... edgeTypes);
7979

8080
boolean isConnectedTo(Identifiable toVertex);
8181

engine/src/main/java/com/arcadedb/utility/MultiIterator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@
1919
package com.arcadedb.utility;
2020

2121
import com.arcadedb.exception.TimeoutException;
22+
import com.arcadedb.graph.IterableGraph;
2223

2324
import java.lang.reflect.*;
2425
import java.util.*;
2526

2627
/**
2728
* Iterator that allow to iterate against multiple collection of elements.
2829
*/
29-
public class MultiIterator<T> implements ResettableIterator<T>, Iterable<T> {
30+
public class MultiIterator<T> implements ResettableIterator<T>, IterableGraph<T> {
3031
private List<Object> sources;
3132
private Iterator<?> sourcesIterator;
3233
private Iterator<T> partialIterator;

network/src/main/java/com/arcadedb/remote/RemoteImmutableVertex.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.arcadedb.database.RID;
2424
import com.arcadedb.graph.Edge;
2525
import com.arcadedb.graph.ImmutableLightEdge;
26+
import com.arcadedb.graph.IterableGraph;
2627
import com.arcadedb.graph.MutableEdge;
2728
import com.arcadedb.graph.Vertex;
2829
import com.arcadedb.schema.EdgeType;
@@ -60,22 +61,22 @@ public long countEdges(final DIRECTION direction, final String edgeType) {
6061
}
6162

6263
@Override
63-
public Iterable<Edge> getEdges() {
64+
public IterableGraph<Edge> getEdges() {
6465
return internal.getEdges(DIRECTION.BOTH);
6566
}
6667

6768
@Override
68-
public Iterable<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
69+
public IterableGraph<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
6970
return internal.getEdges(direction, edgeTypes);
7071
}
7172

7273
@Override
73-
public Iterable<Vertex> getVertices() {
74+
public IterableGraph<Vertex> getVertices() {
7475
return internal.getVertices(DIRECTION.BOTH);
7576
}
7677

7778
@Override
78-
public Iterable<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
79+
public IterableGraph<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
7980
return internal.getVertices(direction, edgeTypes);
8081
}
8182

network/src/main/java/com/arcadedb/remote/RemoteMutableVertex.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.arcadedb.exception.RecordNotFoundException;
2727
import com.arcadedb.graph.Edge;
2828
import com.arcadedb.graph.ImmutableLightEdge;
29+
import com.arcadedb.graph.IterableGraph;
2930
import com.arcadedb.graph.MutableEdge;
3031
import com.arcadedb.graph.MutableVertex;
3132
import com.arcadedb.graph.Vertex;
@@ -154,22 +155,22 @@ public long countEdges(final DIRECTION direction, final String edgeType) {
154155
}
155156

156157
@Override
157-
public Iterable<Edge> getEdges() {
158+
public IterableGraph<Edge> getEdges() {
158159
return internal.getEdges(DIRECTION.BOTH);
159160
}
160161

161162
@Override
162-
public Iterable<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
163+
public IterableGraph<Edge> getEdges(final DIRECTION direction, final String... edgeTypes) {
163164
return internal.getEdges(DIRECTION.BOTH, edgeTypes);
164165
}
165166

166167
@Override
167-
public Iterable<Vertex> getVertices() {
168+
public IterableGraph<Vertex> getVertices() {
168169
return internal.getVertices(DIRECTION.BOTH);
169170
}
170171

171172
@Override
172-
public Iterable<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
173+
public IterableGraph<Vertex> getVertices(final DIRECTION direction, final String... edgeTypes) {
173174
return internal.getVertices(direction, edgeTypes);
174175
}
175176

network/src/main/java/com/arcadedb/remote/RemoteVertex.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.arcadedb.database.RID;
2323
import com.arcadedb.graph.Edge;
2424
import com.arcadedb.graph.ImmutableLightEdge;
25+
import com.arcadedb.graph.IterableGraph;
2526
import com.arcadedb.graph.MutableEdge;
2627
import com.arcadedb.graph.Vertex;
2728
import com.arcadedb.query.sql.executor.ResultSet;
@@ -47,7 +48,6 @@ protected RemoteVertex(final Vertex vertex, final RemoteDatabase remoteDatabase)
4748
}
4849

4950
public long countEdges(final Vertex.DIRECTION direction, final String edgeType) {
50-
5151
StringBuilder query = new StringBuilder("select " + direction.toString().toLowerCase(Locale.ENGLISH) + "(");
5252
if (edgeType != null)
5353
query.append("'").append(edgeType).append("'");
@@ -57,11 +57,11 @@ public long countEdges(final Vertex.DIRECTION direction, final String edgeType)
5757
return resultSet.next().<Number>getProperty("count").longValue();
5858
}
5959

60-
public Iterable<Edge> getEdges() {
60+
public IterableGraph<Edge> getEdges() {
6161
return getEdges(Vertex.DIRECTION.BOTH);
6262
}
6363

64-
public Iterable<Edge> getEdges(final Vertex.DIRECTION direction, final String... edgeTypes) {
64+
public IterableGraph<Edge> getEdges(final Vertex.DIRECTION direction, final String... edgeTypes) {
6565
final ResultSet resultSet = fetch("E", direction, edgeTypes);
6666
return () -> new Iterator<>() {
6767
@Override
@@ -76,11 +76,11 @@ public Edge next() {
7676
};
7777
}
7878

79-
public Iterable<Vertex> getVertices() {
79+
public IterableGraph<Vertex> getVertices() {
8080
return getVertices(Vertex.DIRECTION.BOTH);
8181
}
8282

83-
public Iterable<Vertex> getVertices(final Vertex.DIRECTION direction, final String... edgeTypes) {
83+
public IterableGraph<Vertex> getVertices(final Vertex.DIRECTION direction, final String... edgeTypes) {
8484
final ResultSet resultSet = fetch("", direction, edgeTypes);
8585
return () -> new Iterator<>() {
8686
@Override

0 commit comments

Comments
 (0)