Skip to content

Commit

Permalink
Implement #156
Browse files Browse the repository at this point in the history
  • Loading branch information
mbastian committed Nov 6, 2022
1 parent aca607f commit cd0c2a8
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/gephi/graph/api/GraphModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static class Factory {
*
* @return new instance
*/
public static GraphModel newInstance() {
public static GraphModelImpl newInstance() {
return new GraphModelImpl();
}

Expand All @@ -111,7 +111,7 @@ public static GraphModel newInstance() {
* @param config configuration
* @return new instance
*/
public static GraphModel newInstance(Configuration config) {
public static GraphModelImpl newInstance(Configuration config) {
return new GraphModelImpl(config);
}
}
Expand All @@ -137,6 +137,23 @@ public static GraphModel read(DataInput input) throws IOException {
}
}

/**
* Read the <code>input</code> into the given graph model. The provided graph
* model should be empty.
*
* @param input data input to read from
* @return the graphmodel passed as parameter
* @throws IOException if an io error occurs
*/
public static GraphModel read(DataInput input, GraphModel graphModel) throws IOException {
try {
org.gephi.graph.impl.Serialization s = new org.gephi.graph.impl.Serialization();
return s.deserializeGraphModel(input, graphModel);
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
}

/**
* Read the <code>input</code> and return the read graph model without an
* explicit version header in the input. To be used with old graphstore
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/gephi/graph/impl/Serialization.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.util.Map;
import java.util.Set;
import org.gephi.graph.api.Configuration;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Origin;
import org.gephi.graph.api.Estimator;
import org.gephi.graph.api.TimeFormat;
Expand Down Expand Up @@ -247,6 +248,16 @@ public GraphModelImpl deserializeGraphModel(DataInput is) throws IOException, Cl
return model;
}

public GraphModelImpl deserializeGraphModel(DataInput is, GraphModel graphModel) throws IOException, ClassNotFoundException {
model = (GraphModelImpl) graphModel;
readVersion = (Float) deserialize(is);
Configuration config = (Configuration) deserialize(is);
model.setConfiguration(config);
deserialize(is);
model.store.defaultColumns.resetConfiguration();
return model;
}

public GraphModelImpl deserializeGraphModelWithoutVersionPrefix(DataInput is, float version) throws IOException, ClassNotFoundException {
readVersion = version;
Configuration config = (Configuration) deserialize(is);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/gephi/graph/impl/SerializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.Map;
import java.util.Set;
import org.gephi.graph.api.Configuration;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Origin;
import org.gephi.graph.api.Estimator;
import org.gephi.graph.api.TimeFormat;
Expand Down Expand Up @@ -1224,6 +1225,20 @@ public void testDefaultColumns() throws Exception {
Assert.assertSame(read.defaultColumns().edgeId(), read.getEdgeTable().getColumn("id"));
}

@Test
public void testDeserializeWithGraphModel() throws Exception {
GraphModelImpl gm = GraphGenerator.generateSmallUndirectedGraphStore().graphModel;
Serialization ser = new Serialization(gm);

DataInputOutput dio = new DataInputOutput();
ser.serializeGraphModel(dio, gm);
byte[] bytes = dio.toByteArray();

GraphModelImpl read = GraphModel.Factory.newInstance();
read = ser.deserializeGraphModel(dio.reset(bytes), read);
Assert.assertTrue(read.deepEquals(gm));
}

@Test
public void testDeserializeWithoutVersion() throws Exception {
GraphModelImpl gm = GraphGenerator.generateSmallGraphStore().graphModel;
Expand Down

0 comments on commit cd0c2a8

Please sign in to comment.