public static String unhide(String key) { final String prefix = Hidden.hide("task" + "_"); if (key.startsWith(prefix)) { return key.substring(prefix.length()); } return key; } }
@Override public Set<String> keys() { Iterator<Vertex> vertices = this.queryAllVariableVertices(); Set<String> keys = new HashSet<>(); while (vertices.hasNext()) { keys.add(vertices.next().<String>value(Hidden.hide(VARIABLE_KEY))); } return keys; }
@Override public Map<String, Object> asMap() { Map<String, Object> variables = new HashMap<>(); Iterator<Vertex> vertices = this.queryAllVariableVertices(); while (vertices.hasNext()) { Vertex vertex = vertices.next(); String key = vertex.value(Hidden.hide(VARIABLE_KEY)); String type = vertex.value(Hidden.hide(VARIABLE_TYPE)); if (!Arrays.asList(TYPES).contains(Hidden.hide(type))) { throw Graph.Variables.Exceptions .dataTypeOfVariableValueNotSupported(type); } Object value = vertex.value(Hidden.hide(type)); variables.put(key, value); } return Collections.unmodifiableMap(variables); }
@Override public <R> Optional<R> get(String key) { if (key == null) { throw Graph.Variables.Exceptions.variableKeyCanNotBeNull(); } if (key.isEmpty()) { throw Graph.Variables.Exceptions.variableKeyCanNotBeEmpty(); } Vertex vertex = this.queryVariableVertex(key); if (vertex == null) { return Optional.empty(); } String type = vertex.value(Hidden.hide(VARIABLE_TYPE)); if (!Arrays.asList(TYPES).contains(Hidden.hide(type))) { throw Graph.Variables.Exceptions .dataTypeOfVariableValueNotSupported(type); } // The value of key VARIABLE_TYPE is the name of variable value return Optional.of(vertex.value(Hidden.hide(type))); }
private void initialize(final Neo4jGraphAPI baseGraph, final Configuration configuration) { this.configuration.copy(configuration); this.baseGraph = baseGraph; this.neo4jGraphVariables = new Neo4jGraphVariables(this); this.tx().readWrite(); final Optional<Boolean> hasMultiProperties = this.neo4jGraphVariables.get(Graph.Hidden.hide(CONFIG_MULTI_PROPERTIES)); final Optional<Boolean> hasMetaProperties = this.neo4jGraphVariables.get(Graph.Hidden.hide(CONFIG_META_PROPERTIES)); boolean supportsMetaProperties = hasMetaProperties.orElse(this.configuration.getBoolean(CONFIG_META_PROPERTIES, false)); boolean supportsMultiProperties = hasMultiProperties.orElse(this.configuration.getBoolean(CONFIG_MULTI_PROPERTIES, false)); if (supportsMultiProperties != supportsMetaProperties) throw new IllegalArgumentException(this.getClass().getSimpleName() + " currently supports either both meta-properties and multi-properties or neither"); if (!hasMultiProperties.isPresent()) this.neo4jGraphVariables.set(Graph.Hidden.hide(CONFIG_MULTI_PROPERTIES), supportsMultiProperties); if (!hasMetaProperties.isPresent()) this.neo4jGraphVariables.set(Graph.Hidden.hide(CONFIG_META_PROPERTIES), supportsMetaProperties); this.trait = supportsMultiProperties ? MultiMetaNeo4jTrait.instance() : NoMultiNoMetaNeo4jTrait.instance(); if (supportsMultiProperties) LOGGER.warn(this.getClass().getSimpleName() + " multi/meta-properties feature is considered experimental and should not be used in a production setting until this warning is removed"); this.tx().commit(); }
@Override public <V> Property<V> setProperty(final Neo4jVertexProperty vertexProperty, final String key, final V value) { final Neo4jNode vertexPropertyNode = Neo4jHelper.getVertexPropertyNode(vertexProperty); if (null != vertexPropertyNode) { vertexPropertyNode.setProperty(key, value); return new Neo4jProperty<>(vertexProperty, key, value); } else { final Neo4jNode vertexNode = ((Neo4jVertex) vertexProperty.element()).getBaseVertex(); final Neo4jNode newVertexPropertyNode = ((WrappedGraph<Neo4jGraphAPI>) vertexProperty.element().graph()).getBaseGraph().createNode(VERTEX_PROPERTY_LABEL, vertexProperty.label()); newVertexPropertyNode.setProperty(T.key.getAccessor(), vertexProperty.key()); newVertexPropertyNode.setProperty(T.value.getAccessor(), vertexProperty.value()); newVertexPropertyNode.setProperty(vertexProperty.key(), vertexProperty.value()); newVertexPropertyNode.setProperty(key, value); vertexNode.connectTo(newVertexPropertyNode, Graph.Hidden.hide(vertexProperty.key())); vertexNode.setProperty(vertexProperty.key(), VERTEX_PROPERTY_TOKEN); Neo4jHelper.setVertexPropertyNode(vertexProperty, newVertexPropertyNode); return new Neo4jProperty<>(vertexProperty, key, value); } }
@Override public void removeVertexProperty(final Neo4jVertexProperty vertexProperty) { final Neo4jNode vertexPropertyNode = Neo4jHelper.getVertexPropertyNode(vertexProperty); final Neo4jNode vertexNode = ((Neo4jVertex) vertexProperty.element()).getBaseVertex(); if (null == vertexPropertyNode) { if (vertexNode.degree(Neo4jDirection.OUTGOING, Graph.Hidden.hide(vertexProperty.key())) == 0) { if (vertexNode.hasProperty(vertexProperty.key())) vertexNode.removeProperty(vertexProperty.key()); } } else { vertexPropertyNode.relationships(Neo4jDirection.BOTH).forEach(Neo4jRelationship::delete); vertexPropertyNode.delete(); if (vertexNode.degree(Neo4jDirection.OUTGOING, Graph.Hidden.hide(vertexProperty.key())) == 0) { if (vertexNode.hasProperty(vertexProperty.key())) vertexNode.removeProperty(vertexProperty.key()); } } }
@Override public <V> VertexProperty<V> getVertexProperty(final Neo4jVertex vertex, final String key) { final Neo4jNode node = vertex.getBaseVertex(); if (node.hasProperty(key)) { if (node.getProperty(key).equals(VERTEX_PROPERTY_TOKEN)) { if (node.degree(Neo4jDirection.OUTGOING, Graph.Hidden.hide(key)) > 1) throw Vertex.Exceptions.multiplePropertiesExistForProvidedKey(key); else { return (VertexProperty<V>) new Neo4jVertexProperty<>(vertex, node.relationships(Neo4jDirection.OUTGOING, Graph.Hidden.hide(key)).iterator().next().end()); } } else { return new Neo4jVertexProperty<>(vertex, key, (V) node.getProperty(key)); } } else return VertexProperty.<V>empty(); }
private void createVariableVertex(String key, Object value) { VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); GraphTransaction tx = this.graph.graphTransaction(); HugeVertex vertex = new HugeVertex(tx, null, vl); try { this.setProperty(vertex, key, value); } catch (IllegalArgumentException e) { throw Graph.Variables.Exceptions .dataTypeOfVariableValueNotSupported(value); } // AUTOMATIC id vertex.assignId(null); tx.addVertex(vertex); }
@Override public <V> Iterator<VertexProperty<V>> getVertexProperties(final Neo4jVertex vertex, final String... keys) { if (Neo4jHelper.isDeleted(vertex.getBaseVertex())) return Collections.emptyIterator(); // TODO: I believe its because the vertex property is deleted, but then seen again in the iterator. ? return IteratorUtils.stream(vertex.getBaseVertex().getKeys()) .filter(key -> ElementHelper.keyExists(key, keys)) .flatMap(key -> { if (vertex.getBaseVertex().getProperty(key).equals(VERTEX_PROPERTY_TOKEN)) return IteratorUtils.stream(vertex.getBaseVertex().relationships(Neo4jDirection.OUTGOING, Graph.Hidden.hide(key))) .map(relationship -> (VertexProperty<V>) new Neo4jVertexProperty<>(vertex, relationship.end())); else return Stream.of(new Neo4jVertexProperty<>(vertex, key, (V) vertex.getBaseVertex().getProperty(key))); }).iterator(); }
public static String hide(String key) { return Hidden.hide("task" + "_" + key); }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldHaveExceptionConsistencyWhenUsingSystemVertexLabel() { final String label = Graph.Hidden.hide("systemLabel"); final Vertex v = graph.addVertex(); try { v.addEdge(label, v); fail("Call to Vertex.addEdge() should throw an exception when label is a system key"); } catch (Exception ex) { validateException(Element.Exceptions.labelCanNotBeAHiddenKey(label), ex); } }
private HugeVertex queryVariableVertex(String key) { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); PropertyKey pkey = this.graph.propertyKey(Hidden.hide(VARIABLE_KEY)); query.query(Condition.eq(pkey.id(), key)); query.showHidden(true); Iterator<Vertex> vertices = this.graph.vertices(query); if (!vertices.hasNext()) { return null; } return (HugeVertex) vertices.next(); }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldHaveExceptionConsistencyWhenUsingSystemVertexLabel() { final String label = Graph.Hidden.hide("systemLabel"); try { graph.addVertex(T.label, label); fail("Call to Graph.addVertex() should throw an exception when label is a system key"); } catch (Exception ex) { validateException(Element.Exceptions.labelCanNotBeAHiddenKey(label), ex); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldHaveExceptionConsistencyWhenUsingSystemVertexLabelOnOverload() { final String label = Graph.Hidden.hide("systemLabel"); try { graph.addVertex(label); fail("Call to Graph.addVertex() should throw an exception when label is a system key"); } catch (Exception ex) { validateException(Element.Exceptions.labelCanNotBeAHiddenKey(label), ex); } }
private IndexLabel createIndex(VertexLabel label, String field) { HugeGraph graph = this.graph(); SchemaManager schema = graph.schema(); String name = Hidden.hide("task-index-by-" + field); IndexLabel indexLabel = schema.indexLabel(name) .on(HugeType.VERTEX_LABEL, TASK) .by(field) .build(); graph.schemaTransaction().addIndexLabel(label, indexLabel); return indexLabel; } }
private Iterator<Vertex> queryAllVariableVertices() { ConditionQuery query = new ConditionQuery(HugeType.VERTEX); VertexLabel vl = this.graph.vertexLabel(Hidden.hide(VARIABLES)); query.eq(HugeKeys.LABEL, vl.id()); query.showHidden(true); return this.graph.vertices(query); }
@Parameterized.Parameters(name = "expect({0})") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][]{ {"propertyValueCanNotBeNull", "k", null, Property.Exceptions.propertyValueCanNotBeNull()}, {"propertyKeyCanNotBeNull", null, "v", Property.Exceptions.propertyKeyCanNotBeNull()}, {"propertyKeyCanNotBeEmpty", "", "v", Property.Exceptions.propertyKeyCanNotBeEmpty()}, {"propertyKeyCanNotBeAHiddenKey", Graph.Hidden.hide("systemKey"), "value", Property.Exceptions.propertyKeyCanNotBeAHiddenKey(Graph.Hidden.hide("systemKey"))}}); }
public static String makeSystemName(String name) { return Graph.Hidden.hide(name); }
public static String makeSystemName(String name) { return Graph.Hidden.hide(name); }