private static String readStreamingPropertyValueStringForHistory(StreamingPropertyValue spv) { try (InputStream in = spv.getInputStream()) { byte[] buffer = new byte[HISTORICAL_PROPERTY_MAX_SPV_SIZE]; int bytesRead = in.read(buffer, 0, HISTORICAL_PROPERTY_MAX_SPV_SIZE); if (bytesRead < 0) { return ""; } return new String(buffer, 0, bytesRead); } catch (IOException ex) { throw new VisalloException("Could not read StreamingPropertyValue", ex); } }
protected StreamingPropertyValueRef(StreamingPropertyValue propertyValue) { this.valueType = propertyValue.getValueType().getName(); this.searchIndex = propertyValue.isSearchIndex(); }
protected void setIconProperty( Concept concept, File inDir, String glyphIconFileName, String propertyKey, User user, Authorizations authorizations ) throws IOException { if (glyphIconFileName != null) { File iconFile = new File(inDir, glyphIconFileName); if (!iconFile.exists()) { throw new RuntimeException("Could not find icon file: " + iconFile.toString()); } try (InputStream iconFileIn = new FileInputStream(iconFile)) { StreamingPropertyValue value = new StreamingPropertyValue(iconFileIn, byte[].class); value.searchIndex(false); value.store(true); concept.setProperty(propertyKey, value, user, authorizations); } } }
@Override public String toString() { return "StreamingPropertyValue{" + "valueType=" + getValueType() + ", length=" + getLength() + '}'; } }
@Override public StreamingPropertyValue wrap(VideoTranscript value) { InputStream in = new ByteArrayInputStream(value.toJson().toString().getBytes()); StreamingPropertyValue result = new StreamingPropertyValue(in, byte[].class); result.searchIndex(false); return result; }
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("propLarge", propLarge, VISIBILITY_A) StreamingPropertyValue prop = (StreamingPropertyValue) v1.getPropertyValue("propLarge"); InputStream in = prop.getInputStream(); int amountToRead = expectedLargeValueBytes.length - 8; byte[] buffer = null;
public void testAddStreamingPropertyValue() throws IOException, InterruptedException { String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE)); PropertyValue propSmall = StreamingPropertyValue.create(new ByteArrayInputStream("value1".getBytes()), String.class, 6L); PropertyValue propLarge = StreamingPropertyValue.create( new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class, assertTrue("propSmallValue was " + propSmallValue.getClass().getName(), propSmallValue instanceof StreamingPropertyValue); StreamingPropertyValue value = (StreamingPropertyValue) propSmallValue; assertEquals(String.class, value.getValueType()); assertEquals("value1".getBytes().length, (long) value.getLength()); assertEquals("value1", IOUtils.toString(value.getInputStream())); assertEquals("value1", IOUtils.toString(value.getInputStream())); assertTrue(largePropertyName + " was " + propLargeValue.getClass().getName(), propLargeValue instanceof StreamingPropertyValue); value = (StreamingPropertyValue) propLargeValue; assertEquals(String.class, value.getValueType()); assertEquals(expectedLargeValue.getBytes().length, (long) value.getLength()); assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream())); assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream())); graph.flush(); assertTrue("propSmallValue was " + propSmallValue.getClass().getName(), propSmallValue instanceof StreamingPropertyValue); value = (StreamingPropertyValue) propSmallValue; assertEquals(String.class, value.getValueType()); assertEquals("value1".getBytes().length, (long) value.getLength()); assertEquals("value1", IOUtils.toString(value.getInputStream())); assertEquals("value1", IOUtils.toString(value.getInputStream()));
private static Object convertStreamingPropertyValue(Object value) { StreamingPropertyValue spv = (StreamingPropertyValue) value; try { if (spv.getValueType() == String.class) { try (InputStream in = spv.getInputStream()) { value = IOUtils.toString(in, Charset.defaultCharset()); } } else if (spv.getValueType() == byte[].class) { try (InputStream in = spv.getInputStream()) { byte[] buffer = new byte[100000]; int read; read = in.read(buffer, 0, buffer.length); value = Arrays.copyOfRange(buffer, 0, read); } } else { return "Could not convert StreamingPropertyValue of type " + spv.getValueType().getName(); } } catch (IOException e) { throw new SecurityException("Could not get StreamingPropertyValue input stream", e); } return value; }
StreamingPropertyValue value ) { StreamAndLength streamAndLength = streamAndLength(value.getInputStream(), value.getLength()); String id = makeId(elementId, key, name, visibility, timestamp); try (Handle handle = dbi.open()) { VALUE_LENGTH_COLUMN_NAME ), id, streamAndLength.inputStream, value.getValueType().getName(), streamAndLength.length ); } else {
} else if (propertyValue instanceof StreamingPropertyValue) { StreamingPropertyValue streamingPropertyValue = (StreamingPropertyValue) propertyValue; if (!streamingPropertyValue.isSearchIndex()) { continue; Class valueType = streamingPropertyValue.getValueType(); if (valueType == String.class) { InputStream in = streamingPropertyValue.getInputStream(); propertyValue = StreamUtils.toString(in); } else {
private byte[] getAndSaveImageData(Vertex vertex, String url, Authorizations authorizations) throws IOException { String propertyKey = getPropertyKey(url, null, null, null); StreamingPropertyValue originalImage = VisalloProperties.CACHED_IMAGE.getPropertyValue(vertex, propertyKey); if (originalImage != null) { return IOUtils.toByteArray(originalImage.getInputStream()); } byte[] imageData = httpRepository.get(url); StreamingPropertyValue value = new StreamingPropertyValue(new ByteArrayInputStream(imageData), byte[].class); value.store(true).searchIndex(false); ExistingElementMutation<Vertex> m = vertex.prepareMutation(); VisalloProperties.CACHED_IMAGE.addPropertyValue(m, propertyKey, value, vertex.getVisibility()); m.save(authorizations); return imageData; }
@Test public void testStreamingPropertyValue() { StreamingPropertyValue spv = new DefaultStreamingPropertyValue(new ByteArrayInputStream("test".getBytes()), byte[].class, 4L) .searchIndex(true); testValue( spv, getStreamingPropertyValueBytes(), (givenValue, deserializedValue) -> { assertNotNull(deserializedValue); assertTrue(deserializedValue.isSearchIndex()); assertEquals(byte[].class, deserializedValue.getValueType()); assertEquals(4L, (long) deserializedValue.getLength()); } ); }
totalLength = mediaPropertyValue.getLength(); in = mediaPropertyValue.getInputStream();
public StreamingPropertyValueTableDataRef(String dataRowKey, StreamingPropertyValue propertyValue, long length) { super(propertyValue); this.dataRowKey = dataRowKey; this.valueType = propertyValue.getValueType(); this.length = length; }
@Test public void testDifferentKey() throws UnsupportedEncodingException { VertexBuilder v = getGraph().prepareVertex("v1", visibility); StreamingPropertyValue textValue = new StreamingPropertyValue(new ByteArrayInputStream("<html><body>Text1</body></html>".getBytes("UTF-8")), byte[].class); textValue.searchIndex(false); Metadata metadata = new Metadata(); metadata.add(VisalloProperties.MIME_TYPE.getPropertyName(), "text/html", getVisibilityTranslator().getDefaultVisibility()); v.addPropertyValue("key1", "http://visallo.org/test#raw1", textValue, metadata, visibility); v.save(getGraphAuthorizations()); Vertex vertex = getGraph().getVertex("v1", getGraphAuthorizations()); run(gpw, createWorkerPrepareData(), vertex); Property text1Property = vertex.getProperty("key1", "http://visallo.org/test#text1"); assertTrue(text1Property != null); assertThat(((StreamingPropertyValue) text1Property.getValue()).readToString(), equalTo("Text1")); }
private static int compare(Object first, Object second) { if (first instanceof StreamingPropertyValue && ((StreamingPropertyValue) first).getValueType() == String.class) { first = ((StreamingPropertyValue) first).readToString(); if (second instanceof StreamingPropertyValue && ((StreamingPropertyValue) second).getValueType() == String.class) { second = ((StreamingPropertyValue) second).readToString();
private byte[] createAndSaveCachedImage(Vertex vertex, String propertyKey, String url, int maxWidth, int maxHeight, int jpegQuality, Authorizations authorizations) throws IOException { byte[] imageData = getAndSaveImageData(vertex, url, authorizations); imageData = ImageUtils.resize(imageData, maxWidth, maxHeight, jpegQuality); StreamingPropertyValue value = new StreamingPropertyValue(new ByteArrayInputStream(imageData), byte[].class); value.store(true).searchIndex(false); ExistingElementMutation<Vertex> m = vertex.prepareMutation(); VisalloProperties.CACHED_IMAGE.addPropertyValue(m, propertyKey, value, vertex.getVisibility()); m.save(authorizations); return imageData; }
protected void addVideoTranscriptAsTextPropertiesToMutation(ExistingElementMutation<Vertex> mutation, String propertyKey, VideoTranscript videoTranscript, Metadata metadata, Visibility visibility) { VisalloProperties.MIME_TYPE_METADATA.setMetadata(metadata, "text/plain", getVisibilityTranslator().getDefaultVisibility()); for (VideoTranscript.TimedText entry : videoTranscript.getEntries()) { String textPropertyKey = getVideoTranscriptTimedTextPropertyKey(propertyKey, entry); StreamingPropertyValue value = new StreamingPropertyValue(new ByteArrayInputStream(entry.getText().getBytes()), String.class); VisalloProperties.TEXT.addPropertyValue(mutation, textPropertyKey, value, metadata, visibility); } }
@Test public void testGetStreamingPropertyValueInputStreams() throws Exception { graph.defineProperty("a").dataType(String.class).textIndexHint(TextIndexHint.FULL_TEXT).define(); graph.defineProperty("b").dataType(String.class).textIndexHint(TextIndexHint.FULL_TEXT).define(); graph.defineProperty("c").dataType(String.class).textIndexHint(TextIndexHint.FULL_TEXT).define(); graph.prepareVertex("v1", VISIBILITY_A) .setProperty("a", StreamingPropertyValue.create("Test Value A"), VISIBILITY_A) .setProperty("b", StreamingPropertyValue.create("Test Value B"), VISIBILITY_A) .setProperty("c", StreamingPropertyValue.create("Test Value C"), VISIBILITY_A) .save(AUTHORIZATIONS_A_AND_B); graph.flush(); Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A_AND_B); StreamingPropertyValue spvA = (StreamingPropertyValue) v1.getPropertyValue("a"); assertEquals(12L, (long) spvA.getLength()); StreamingPropertyValue spvB = (StreamingPropertyValue) v1.getPropertyValue("b"); assertEquals(12L, (long) spvA.getLength()); StreamingPropertyValue spvC = (StreamingPropertyValue) v1.getPropertyValue("c"); assertEquals(12L, (long) spvA.getLength()); ArrayList<StreamingPropertyValue> spvs = Lists.newArrayList(spvA, spvB, spvC); List<InputStream> streams = graph.getStreamingPropertyValueInputStreams(spvs); assertEquals("Test Value A", IOUtils.toString(streams.get(0))); assertEquals("Test Value B", IOUtils.toString(streams.get(1))); assertEquals("Test Value C", IOUtils.toString(streams.get(2))); }
@Override public StreamingPropertyValue toStreamingPropertyValue(SqlGraph graph, long timestamp) { StreamingPropertyValue spv = graph.getStreamingPropertyTable() .get(elementId, key, name, visibility, timestamp); if (spv == null) { LOGGER.warn("Could not find SQL SPV with the property timestamp %d, using ref timestamp instead", timestamp, this.timestamp); spv = graph.getStreamingPropertyTable() .get(elementId, key, name, visibility, this.timestamp); } return spv .store(isStore()) .searchIndex(isSearchIndex()); } }