use of org.vertexium.property.StreamingPropertyValue in project vertexium by visallo.
the class BackupRestoreTest method testSaveAndLoad.
@Test
public void testSaveAndLoad() throws IOException, ClassNotFoundException {
Graph graph = createGraph();
Metadata prop1Metadata = new Metadata();
prop1Metadata.add("metadata1", "metadata1Value", GraphTestBase.VISIBILITY_A);
int largePropertyValueSize = 1000;
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(largePropertyValueSize));
StreamingPropertyValue largeDataValue = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
Vertex v1 = graph.prepareVertex("v1", GraphTestBase.VISIBILITY_A).addPropertyValue("id1a", "prop1", "value1a", prop1Metadata, GraphTestBase.VISIBILITY_A).addPropertyValue("id1b", "prop1", "value1b", GraphTestBase.VISIBILITY_A).addPropertyValue("id2", "prop2", "value2", GraphTestBase.VISIBILITY_B).setProperty("largeData", largeDataValue, GraphTestBase.VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
Vertex v2 = graph.addVertex("v2", GraphTestBase.VISIBILITY_A, AUTHORIZATIONS_A);
Vertex v3 = graph.addVertex("v3", GraphTestBase.VISIBILITY_B, AUTHORIZATIONS_B);
graph.addEdge("e1to2", v1, v2, "label1", GraphTestBase.VISIBILITY_A, AUTHORIZATIONS_A);
graph.addEdge("e1to3", v1, v3, "label1", GraphTestBase.VISIBILITY_B, AUTHORIZATIONS_B);
File tmp = File.createTempFile(getClass().getName(), ".json");
try (FileOutputStream out = new FileOutputStream(tmp)) {
System.out.println("saving graph to: " + tmp);
GraphBackup graphBackup = new GraphBackup();
graphBackup.save(graph, out, AUTHORIZATIONS_A_AND_B);
}
try (FileInputStream in = new FileInputStream(tmp)) {
Graph loadedGraph = createGraph();
GraphRestore graphRestore = new GraphRestore();
graphRestore.restore(loadedGraph, in, AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(3, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_A_AND_B)));
Assert.assertEquals(2, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_A)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_B)));
Assert.assertEquals(2, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_A_AND_B)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_A)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_B)));
v1 = loadedGraph.getVertex("v1", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(2, IterableUtils.count(v1.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
Iterable<Property> properties = v1.getProperties();
boolean prop1_id1a_found = false;
boolean prop1_id1b_found = false;
for (Property property : properties) {
if (property.getName().equals("prop1")) {
if (property.getKey().equals("id1a")) {
prop1_id1a_found = true;
assertEquals("value1a", property.getValue());
}
if (property.getKey().equals("id1b")) {
prop1_id1b_found = true;
assertEquals("value1b", property.getValue());
}
}
}
assertTrue("prop1[id1a] not found", prop1_id1a_found);
assertTrue("prop1[id1b] not found", prop1_id1b_found);
assertEquals("value2", v1.getPropertyValue("prop2", 0));
StreamingPropertyValue spv = (StreamingPropertyValue) v1.getPropertyValue("largeData", 0);
assertNotNull("largeData property not found", spv);
assertEquals(String.class, spv.getValueType());
assertEquals(expectedLargeValue, IOUtils.toString(spv.getInputStream()));
v2 = loadedGraph.getVertex("v2", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(1, IterableUtils.count(v2.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
v3 = loadedGraph.getVertex("v3", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(1, IterableUtils.count(v3.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
}
tmp.delete();
}
use of org.vertexium.property.StreamingPropertyValue in project vertexium by visallo.
the class OverflowIntoHdfsStreamingPropertyValueStorageStrategy method saveStreamingPropertyValueSmall.
private StreamingPropertyValueRef saveStreamingPropertyValueSmall(ElementMutationBuilder elementMutationBuilder, String rowKey, Property property, byte[] data, StreamingPropertyValue propertyValue) {
String dataTableRowKey = new DataTableRowKey(rowKey, property).getRowKey();
Mutation dataMutation = new Mutation(dataTableRowKey);
dataMutation.put(EMPTY_TEXT, EMPTY_TEXT, property.getTimestamp(), new Value(data));
elementMutationBuilder.saveDataMutation(dataMutation);
return new StreamingPropertyValueTableRef(dataTableRowKey, propertyValue, data);
}
use of org.vertexium.property.StreamingPropertyValue in project vertexium by visallo.
the class GraphTestBase method testStreamingPropertyValueResetMutlipleLargeReadsUntilEnd.
@Test
public void testStreamingPropertyValueResetMutlipleLargeReadsUntilEnd() throws IOException {
assumeTrue("InputStream mark/reset is not supported", isInputStreamMarkResetSupported());
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
byte[] expectedLargeValueBytes = expectedLargeValue.getBytes();
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
graph.prepareVertex("v1", VISIBILITY_A).setProperty("propLarge", propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A);
StreamingPropertyValue prop = (StreamingPropertyValue) v1.getPropertyValue("propLarge");
InputStream in = prop.getInputStream();
in.mark(2);
for (int i = 0; i < 3; i++) {
int totalBytesRead = 0;
while (in.read() >= 0) {
totalBytesRead++;
assertTrue("Read past end of input stream", totalBytesRead <= expectedLargeValueBytes.length);
}
assertEquals("Read unexpected number of bytes on loop: " + i, expectedLargeValueBytes.length, totalBytesRead);
assertEquals(-1, in.read());
in.reset();
}
}
use of org.vertexium.property.StreamingPropertyValue in project vertexium by visallo.
the class GraphTestBase method testStreamingPropertyDecreasingSize.
@Test
public void testStreamingPropertyDecreasingSize() throws IOException {
Metadata metadata = new Metadata();
Long timestamp = System.currentTimeMillis();
String expectedValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedValue.getBytes()), String.class, (long) expectedValue.length());
graph.prepareVertex("v1", VISIBILITY_A).addPropertyValue("key1", "largeProp", propLarge, metadata, timestamp, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B);
StreamingPropertyValue spv = (StreamingPropertyValue) v1.getPropertyValue("key1", "largeProp");
assertEquals(expectedValue, spv.readToString());
// now save a smaller value, making sure it gets truncated
expectedValue = "small";
propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedValue.getBytes()), String.class, (long) expectedValue.length());
graph.prepareVertex("v1", VISIBILITY_A).addPropertyValue("key1", "largeProp", propLarge, metadata, timestamp + 1, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B);
spv = (StreamingPropertyValue) v1.getPropertyValue("key1", "largeProp");
assertEquals(expectedValue, spv.readToString());
}
use of org.vertexium.property.StreamingPropertyValue in project vertexium by visallo.
the class GraphTestBase method testStreamingPropertyValueMarkReset.
@Test
public void testStreamingPropertyValueMarkReset() throws IOException {
assumeTrue("InputStream mark/reset is not supported", isInputStreamMarkResetSupported());
String expectedLargeValue = "abcdefghijk";
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
graph.prepareVertex("v1", VISIBILITY_A).setProperty("propLarge", propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A);
StreamingPropertyValue prop = (StreamingPropertyValue) v1.getPropertyValue("propLarge");
InputStream in = prop.getInputStream();
byte[] buffer = new byte[15];
int sizeRead = in.read(buffer);
assertEquals(11, sizeRead);
assertEquals("abcdefghijk", new String(buffer, 0, sizeRead));
in.reset();
buffer = new byte[3];
sizeRead = in.read(buffer);
assertEquals(3, sizeRead);
assertEquals("abc", new String(buffer, 0, sizeRead));
assertEquals('d', (char) in.read());
assertEquals('e', (char) in.read());
in.mark(32);
buffer = new byte[5];
sizeRead = in.read(buffer);
assertEquals(5, sizeRead);
assertEquals("fghij", new String(buffer, 0, sizeRead));
in.reset();
buffer = new byte[10];
sizeRead = in.read(buffer);
assertEquals(6, sizeRead);
assertEquals("fghijk", new String(buffer, 0, sizeRead));
assertEquals(-1, in.read(buffer));
in.reset();
buffer = new byte[10];
sizeRead = in.read(buffer);
assertEquals(6, sizeRead);
assertEquals("fghijk", new String(buffer, 0, sizeRead));
}
Aggregations