Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getNodeByStoreId and getEdgeByStoreId methods to Graph #198

Merged
merged 2 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/main/java/org/gephi/graph/api/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* create a <em>GraphModel</em> with custom configuration.
* <p>
* Create instances by using the builder:
*
*
* <pre>
* Configuration config = Configuration.builder().build();
* </pre>
Expand Down Expand Up @@ -260,7 +260,7 @@ public boolean isEnableObservers() {
* for each type.
* <p>
* Default is <code>true</code>.
*
*
* @param enableAutoEdgeTypeRegistration enable auto edge type registration
* @return this builder
*/
Expand All @@ -281,7 +281,7 @@ public boolean isEnableAutoEdgeTypeRegistration() {
* properties aren't needed, disabling them can save memory.
* <p>
* Default is <code>true</code>.
*
*
* @param enableNodeProperties enable node properties
* @return this builder
*/
Expand All @@ -302,7 +302,7 @@ public boolean isEnableNodeProperties() {
* properties aren't needed, disabling them can save memory.
* <p>
* Default is <code>true</code>.
*
*
* @param enableEdgeProperties enable edge properties
* @return this builder
*/
Expand All @@ -325,7 +325,7 @@ public boolean isEnableEdgeProperties() {
* The spatial index can be retrieved from {@link GraphModel#getSpatialIndex()}.
* <p>
* Default is <code>false</code>.
*
*
* @param enableSpatialIndex enable edge properties
* @return this builder
*/
Expand All @@ -349,7 +349,7 @@ public boolean isEnableSpatialIndex() {
* return results.
* <p>
* Default is <code>true</code>.
*
*
* @param enableIndexNodes enable node attribute indexing
* @return this builder
*/
Expand All @@ -373,7 +373,7 @@ public boolean isEnableIndexNodes() {
* return results.
* <p>
* Default is <code>true</code>.
*
*
* @param enableIndexEdges enable edge attribute indexing
* @return this builder
*/
Expand Down Expand Up @@ -416,7 +416,7 @@ public boolean isEnableIndexTime() {
* If disabled, only a single edge of a given type can exist between two nodes.
* <p>
* Default is <code>false</code>.
*
*
* @param enableParallelEdgesSameType enable parallel edges of the same type
* @return this builder
*/
Expand Down Expand Up @@ -586,7 +586,7 @@ public Boolean getEdgeWeightColumn() {
/**
* Sets whether to create an edge weight column.
* <p>
*
*
* @deprecated Use {@link #builder()} instead.
*
* @param edgeWeightColumn edge weight column
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/gephi/graph/api/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ public interface Graph {
*/
public Node getNode(Object id);

/**
* Gets a node given its store id.
*
* @param storeId the store id
* @return the node, or null if not found
*/
public Node getNodeByStoreId(int storeId);

/**
* Returns true if a node with <em>id</em> as identifier exists.
*
Expand All @@ -147,6 +155,14 @@ public interface Graph {
*/
public Edge getEdge(Object id);

/**
* Gets an edge given its store id.
*
* @param storeId the store id
* @return the edge, or null if not found
*/
public Edge getEdgeByStoreId(int storeId);

/**
* Returns true if an edge with <em>id</em> as identifier exists.
*
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/gephi/graph/api/GraphModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* </ul>
* <p>
* New instances can be obtained via the embedded factory:
*
*
* <pre>
* GraphModel model = GraphModel.Factory.newInstance();
* </pre>
Expand All @@ -49,7 +49,7 @@
* Configuration configuration = Configuration.builder().build();
* GraphModel model = GraphModel.Factory.newInstance(configuration);
* </pre>
*
*
* This API revolves around a set of simple concepts. A <code>GraphModel</code>
* encapsulate all elements and metadata associated with a graph structure. In
* other words it's a single graph, but it also contains configuration, indices,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gephi/graph/api/SpatialIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

/**
* Object to query the nodes and edges of the graph in a spatial context.
*
*
* @author Eduardo Ramos
*/
public interface SpatialIndex {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/gephi/graph/api/package.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<html>
<head></head>
<html>
<head></head>
<body>
Complete API description, where
Complete API description, where
<code>GraphModel</code>
is the entry point.
is the entry point.
</body>
</html>
</html>
8 changes: 4 additions & 4 deletions src/main/java/org/gephi/graph/api/types/package.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head></head>
<html>
<head></head>
<body>
Custom types the API supports, in addition of primitive and arrays.
Custom types the API supports, in addition of primitive and arrays.
</body>
</html>
</html>
8 changes: 8 additions & 0 deletions src/main/java/org/gephi/graph/impl/EdgeStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,14 @@ public EdgeImpl get(int id) {
return blocks[id / GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE].get(id);
}

// Only used for Graph.getEdgeByStoreId
public EdgeImpl getForGetByStoreId(int id) {
if (id < 0 || !isValidIndex(id)) {
return null;
}
return blocks[id / GraphStoreConfiguration.EDGESTORE_BLOCK_SIZE].get(id);
}

public EdgeImpl get(final Object id) {
checkNonNullObject(id);

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/gephi/graph/impl/GraphStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ public NodeImpl getNode(final Object id) {
}
}

@Override
public NodeImpl getNodeByStoreId(final int id) {
autoReadLock();
try {
return nodeStore.getForGetByStoreId(id);
} finally {
autoReadUnlock();
}
}

@Override
public boolean hasNode(final Object id) {
return getNode(id) != null;
Expand All @@ -212,6 +222,16 @@ public EdgeImpl getEdge(final Object id) {
}
}

@Override
public EdgeImpl getEdgeByStoreId(final int id) {
autoReadLock();
try {
return edgeStore.getForGetByStoreId(id);
} finally {
autoReadUnlock();
}
}

@Override
public boolean hasEdge(final Object id) {
return getEdge(id) != null;
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/gephi/graph/impl/GraphViewDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,20 @@ public Node getNode(Object id) {
}
}

@Override
public Node getNodeByStoreId(int id) {
graphStore.autoReadLock();
try {
NodeImpl node = graphStore.getNodeByStoreId(id);
if (node != null && view.containsNode(node)) {
return node;
}
return null;
} finally {
graphStore.autoReadUnlock();
}
}

@Override
public boolean hasNode(final Object id) {
return getNode(id) != null;
Expand All @@ -337,6 +351,20 @@ public Edge getEdge(Object id) {
}
}

@Override
public Edge getEdgeByStoreId(int id) {
graphStore.autoReadLock();
try {
EdgeImpl edge = graphStore.getEdgeByStoreId(id);
if (edge != null && view.containsEdge(edge)) {
return edge;
}
return null;
} finally {
graphStore.autoReadUnlock();
}
}

@Override
public boolean hasEdge(final Object id) {
return getEdge(id) != null;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/gephi/graph/impl/NodeStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ public NodeImpl get(final int id) {
return blocks[id / GraphStoreConfiguration.NODESTORE_BLOCK_SIZE].get(id);
}

// Only used for Graph.getNodeByStoreId
public NodeImpl getForGetByStoreId(int id) {
if (id < 0 || !isValidIndex(id)) {
return null;
}
return blocks[id / GraphStoreConfiguration.NODESTORE_BLOCK_SIZE].get(id);
}

public NodeImpl get(final Object id) {
int index = dictionary.getInt(id);
if (index != NodeStore.NULL_ID) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/gephi/graph/impl/NodesQuadTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Adapted from https://bitbucket.org/C3/quadtree/wiki/Home
*
* TODO: unit tests!!
*
*
* @author Eduardo Ramos
*/
public class NodesQuadTree {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/gephi/graph/impl/UndirectedDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ public Node getNode(Object id) {
return store.getNode(id);
}

@Override
public Node getNodeByStoreId(int id) {
return store.getNodeByStoreId(id);
}

@Override
public boolean hasNode(final Object id) {
return store.hasNode(id);
Expand All @@ -120,6 +125,11 @@ public Edge getEdge(Object id) {
return store.getEdge(id);
}

@Override
public Edge getEdgeByStoreId(int storeId) {
return store.getEdgeByStoreId(storeId);
}

@Override
public boolean hasEdge(final Object id) {
return store.hasEdge(id);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/gephi/graph/spi/package.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head></head>
<html>
<head></head>
<body>
SPI interfaces clients can implement to extend the API.
SPI interfaces clients can implement to extend the API.
</body>
</html>
</html>
9 changes: 9 additions & 0 deletions src/test/java/org/gephi/graph/impl/BasicGraphStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public Node getNode(Object id) {
return nodeStore.get(id);
}

public Node getNodeByStoreId(int storeId) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasNode(Object id) {
return nodeStore.get(id) != null;
Expand All @@ -219,6 +223,11 @@ public Edge getEdge(Object id) {
return edgeStore.get(id);
}

@Override
public Edge getEdgeByStoreId(int storeId) {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public boolean hasEdge(Object id) {
return edgeStore.get(id) != null;
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/gephi/graph/impl/GraphStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,24 @@ public void testGetNode() {
Assert.assertFalse(graphStore.hasNode("bar"));
}

@Test
public void testGetNodeByStoreId() {
GraphStore graphStore = GraphGenerator.generateTinyGraphStore();
for (Node node : graphStore.getNodes().toArray()) {
Assert.assertNotNull(graphStore.getNodeByStoreId(node.getStoreId()));
}
}

@Test
public void testGetNodeByStoreIdIsNull() {
GraphStore graphStore = GraphGenerator.generateTinyGraphStore();
for (Node node : graphStore.getNodes().toArray()) {
int storeId = node.getStoreId();
graphStore.removeNode(node);
Assert.assertNull(graphStore.getNodeByStoreId(storeId));
}
}

@Test
public void testGetEdge() {
GraphStore graphStore = GraphGenerator.generateTinyGraphStore();
Expand All @@ -629,6 +647,23 @@ public void testGetEdge() {
Assert.assertFalse(graphStore.hasEdge("bar"));
}

@Test
public void testGetEdgeByStoreId() {
GraphStore graphStore = GraphGenerator.generateTinyGraphStore();
for (Edge edge : graphStore.getEdges().toArray()) {
Assert.assertNotNull(graphStore.getEdgeByStoreId(edge.getStoreId()));
}
}

@Test
public void testGetEdgeByStoreIdIsNull() {
GraphStore graphStore = GraphGenerator.generateTinyGraphStore();
Edge toRemove = graphStore.getEdge("0");
int storeId = toRemove.getStoreId();
graphStore.removeEdge(toRemove);
Assert.assertNull(graphStore.getEdgeByStoreId(storeId));
}

@Test
public void testGetMutualEdge() {
GraphStore graphStore = new GraphStore();
Expand Down
Loading