/** * Constructs a by wrapping the provided {@link uk.gov.gchq.gaffer.data.element.Edge} * and using the {@link uk.gov.gchq.gaffer.data.element.ElementValueLoader} to lazily load the element's identifiers and * properties when requested. * * @param edge the edge to wrap. * @param valueLoader the element value loader to use to lazily load the element's identifiers and properties */ public LazyEdge(final Edge edge, final ElementValueLoader valueLoader) { this(edge, valueLoader, new LazyProperties(edge.getProperties(), valueLoader)); }
@Test public void shouldDelegateSetIdentifiersToEdge() { // Given final Edge edge = mock(Edge.class); given(edge.getProperties()).willReturn(mock(Properties.class)); final ElementValueLoader edgeLoader = mock(ElementValueLoader.class); final LazyEdge lazyEdge = new LazyEdge(edge, edgeLoader); // When lazyEdge.setIdentifiers("src", "dest", DirectedType.UNDIRECTED); // Then verify(edgeLoader, never()).loadIdentifiers(edge); verify(edge).setIdentifiers("src", "dest", DirectedType.UNDIRECTED); }
@Test public void shouldGetLazyProperties() { // Given final Edge edge = new Edge.Builder().build(); final ElementValueLoader edgeLoader = mock(ElementValueLoader.class); final LazyEdge lazyEdge = new LazyEdge(edge, edgeLoader); // When final LazyProperties result = lazyEdge.getProperties(); // Then assertNotNull(result); assertNotSame(edge.getProperties(), result); }
@Test public void shouldLoadIdentifierWhenNotLoaded() { // Given final Edge edge = mock(Edge.class); given(edge.getProperties()).willReturn(mock(Properties.class)); final ElementValueLoader edgeLoader = mock(ElementValueLoader.class); final LazyEdge lazyEdge = new LazyEdge(edge, edgeLoader); final IdentifierType identifierType = IdentifierType.DESTINATION; final String exceptedIdentifierValue = "identifier value"; given(edge.getDestination()).willReturn(exceptedIdentifierValue); // When Object identifierValue = lazyEdge.getIdentifier(identifierType); // Then assertEquals(exceptedIdentifierValue, identifierValue); assertEquals(identifierValue, edge.getDestination()); verify(edgeLoader).loadIdentifiers(edge); }
@Test public void shouldNotRemovePropertiesWhenNoRelevantExcludeProperties() { //Given final View view = new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition.Builder() .excludeProperties(TestPropertyNames.COUNT) .build()) .entity(TestGroups.ENTITY, new ViewElementDefinition.Builder() .properties(TestPropertyNames.PROP_2) .build()) .build(); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "1") .property(TestPropertyNames.PROP_2, "2") .property(TestPropertyNames.PROP_3, "3") .build(); //When ViewUtil.removeProperties(view, edge); //Then assertEquals(3, edge.getProperties().size()); assertEquals("1", edge.getProperties().get(TestPropertyNames.PROP_1)); assertEquals("2", edge.getProperties().get(TestPropertyNames.PROP_2)); assertEquals("3", edge.getProperties().get(TestPropertyNames.PROP_3)); }
@Test public void shouldNotRemovePropertiesWhenNotSet() { //Given final View view = new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition.Builder() .allProperties() .build()) .entity(TestGroups.ENTITY, new ViewElementDefinition.Builder() .properties(TestPropertyNames.PROP_2) .build()) .build(); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "1") .property(TestPropertyNames.PROP_2, "2") .property(TestPropertyNames.PROP_3, "3") .build(); //When ViewUtil.removeProperties(view, edge); //Then assertEquals(3, edge.getProperties().size()); assertEquals("1", edge.getProperties().get(TestPropertyNames.PROP_1)); assertEquals("2", edge.getProperties().get(TestPropertyNames.PROP_2)); assertEquals("3", edge.getProperties().get(TestPropertyNames.PROP_3)); }
@Test public void shouldRemoveExcludedProperties() { //Given final View view = new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition.Builder() .excludeProperties(TestPropertyNames.PROP_1) .build()) .entity(TestGroups.ENTITY, new ViewElementDefinition.Builder() .excludeProperties(TestPropertyNames.PROP_2) .build()) .build(); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "1") .property(TestPropertyNames.PROP_2, "2") .property(TestPropertyNames.PROP_3, "3") .build(); //When ViewUtil.removeProperties(view, edge); //Then assertEquals(2, edge.getProperties().size()); assertEquals("2", edge.getProperties().get(TestPropertyNames.PROP_2)); assertEquals("3", edge.getProperties().get(TestPropertyNames.PROP_3)); }
@Test public void shouldKeepOnlyProvidedProperties() { //Given final View view = new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition.Builder() .properties(TestPropertyNames.PROP_1) .build()) .entity(TestGroups.ENTITY, new ViewElementDefinition.Builder() .properties(TestPropertyNames.PROP_2) .build()) .build(); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "1") .property(TestPropertyNames.PROP_2, "2") .property(TestPropertyNames.PROP_3, "3") .build(); //When ViewUtil.removeProperties(view, edge); //Then assertEquals(1, edge.getProperties().size()); assertEquals("1", edge.getProperties().get(TestPropertyNames.PROP_1)); }
@Test public void shouldNotRemoveAllPropertiesWhenNoRelevantProperties() { //Given final View view = new View.Builder() .edge(TestGroups.EDGE, new ViewElementDefinition.Builder() .properties(TestPropertyNames.COUNT) .build()) .entity(TestGroups.ENTITY, new ViewElementDefinition.Builder() .properties(TestPropertyNames.PROP_2) .build()) .build(); final Edge edge = new Edge.Builder() .group(TestGroups.EDGE) .property(TestPropertyNames.PROP_1, "1") .property(TestPropertyNames.PROP_2, "2") .property(TestPropertyNames.PROP_3, "3") .build(); //When ViewUtil.removeProperties(view, edge); //Then assertEquals(0, edge.getProperties().size()); }
/** * Constructs a by wrapping the provided {@link uk.gov.gchq.gaffer.data.element.Edge} * and using the {@link uk.gov.gchq.gaffer.data.element.ElementValueLoader} to lazily load the element's identifiers and * properties when requested. * * @param edge the edge to wrap. * @param valueLoader the element value loader to use to lazily load the element's identifiers and properties */ public LazyEdge(final Edge edge, final ElementValueLoader valueLoader) { this(edge, valueLoader, new LazyProperties(edge.getProperties(), valueLoader)); }
public void write(final Edge edge) throws SerialisationException { recordConsumer.startMessage(); final int indexPos = writeEdge(edge, schema); writeProperties(edge.getProperties(), schema, indexPos); recordConsumer.endMessage(); }
@Override public Pair<Key, Key> getKeysFromEdge(final Edge edge) { // Get pair of row keys final Pair<byte[], byte[]> rowKeys = getRowKeysFromEdge(edge); final byte[] columnFamily = buildColumnFamily(edge.getGroup()); final byte[] columnQualifier = buildColumnQualifier(edge.getGroup(), edge.getProperties()); final byte[] columnVisibility = buildColumnVisibility(edge.getGroup(), edge.getProperties()); final long timeStamp = buildTimestamp(edge.getGroup(), edge.getProperties()); // Create Accumulo keys - note that second row key may be null (if it's // a self-edge) and // in that case we should return null second key final Key key1 = new Key(rowKeys.getFirst(), columnFamily, columnQualifier, columnVisibility, timeStamp); final Key key2 = null != rowKeys.getSecond() ? new Key(rowKeys.getSecond(), columnFamily, columnQualifier, columnVisibility, timeStamp) : null; // Return pair of keys return new Pair<>(key1, key2); }
@Override public void write(final Kryo kryo, final Output output, final Edge edge) { output.writeString(edge.getGroup()); kryo.writeClass(output, edge.getSource().getClass()); kryo.writeObject(output, edge.getSource()); kryo.writeClass(output, edge.getDestination().getClass()); kryo.writeObject(output, edge.getDestination()); output.writeBoolean(edge.isDirected()); kryo.writeObjectOrNull(output, edge.getProperties(), Properties.class); }
private void updateElementIndex(final Element element, final MapImpl mapImpl) { if (element instanceof Entity) { final Entity entity = (Entity) element; final EntityId entityId = new EntitySeed(entity.getVertex()); mapImpl.addIndex(entityId, element); } else { final Edge edge = (Edge) element; edge.setIdentifiers(edge.getSource(), edge.getDestination(), edge.isDirected(), EdgeId.MatchedVertex.SOURCE); final EntityId sourceEntityId = new EntitySeed(edge.getSource()); mapImpl.addIndex(sourceEntityId, edge); final Edge destMatchedEdge = new Edge(edge.getGroup(), edge.getSource(), edge.getDestination(), edge.isDirected(), EdgeId.MatchedVertex.DESTINATION, edge.getProperties()); final EntityId destinationEntityId = new EntitySeed(edge.getDestination()); mapImpl.addIndex(destinationEntityId, destMatchedEdge); final EdgeId edgeId = new EdgeSeed(edge.getSource(), edge.getDestination(), edge.isDirected()); mapImpl.addIndex(edgeId, edge); } } }