Search in sources :

Example 41 with Label

use of org.neo4j.graphdb.Label in project neo4j by neo4j.

the class Set method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException {
    boolean forProperty = parser.options().containsKey("p");
    boolean forLabel = parser.options().containsKey("l");
    if (forProperty || !forLabel) {
        // Property
        if (parser.arguments().size() < 2) {
            throw new ShellException("Must supply key and value, " + "like: set title \"This is a my title\"");
        }
        String key = parser.arguments().get(0);
        ValueType valueType = getValueType(parser);
        Object value = parseValue(parser.arguments().get(1), valueType);
        NodeOrRelationship thing = getCurrent(session);
        thing.setProperty(key, value);
    } else {
        // Label
        Node node = getCurrent(session).asNode();
        for (Label label : parseLabels(parser)) {
            node.addLabel(label);
        }
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : OptionValueType(org.neo4j.shell.OptionValueType) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ShellException(org.neo4j.shell.ShellException)

Example 42 with Label

use of org.neo4j.graphdb.Label in project neo4j by neo4j.

the class ClusterLocksIT method aPendingMemberShouldBeAbleToServeReads.

@Test
public void aPendingMemberShouldBeAbleToServeReads() throws Throwable {
    // given
    createNodeOnMaster(testLabel, cluster.getMaster());
    cluster.sync();
    HighlyAvailableGraphDatabase slave = cluster.getAnySlave();
    cluster.fail(slave, ClusterManager.NetworkFlag.values());
    cluster.await(instanceEvicted(slave));
    assertEquals(PENDING, slave.getInstanceState());
    // when
    for (int i = 0; i < 10; i++) {
        try (Transaction tx = slave.beginTx()) {
            Node single = Iterables.single(slave.getAllNodes());
            Label label = Iterables.single(single.getLabels());
            assertEquals(testLabel, label);
            tx.success();
            break;
        } catch (TransactionTerminatedException e) {
            // Race between going to pending and reading, try again in a little while
            Thread.sleep(1_000);
        }
    }
// then no exceptions thrown
}
Also used : TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) Transaction(org.neo4j.graphdb.Transaction) HighlyAvailableGraphDatabase(org.neo4j.kernel.ha.HighlyAvailableGraphDatabase) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) Test(org.junit.Test)

Example 43 with Label

use of org.neo4j.graphdb.Label in project neo4j by neo4j.

the class StoreUpgradeIntegrationTest method checkLabelCounts.

private static void checkLabelCounts(GraphDatabaseAPI db) {
    try (Transaction ignored = db.beginTx()) {
        HashMap<Label, Long> counts = new HashMap<>();
        for (Node node : db.getAllNodes()) {
            for (Label label : node.getLabels()) {
                Long count = counts.get(label);
                if (count != null) {
                    counts.put(label, count + 1);
                } else {
                    counts.put(label, 1L);
                }
            }
        }
        ThreadToStatementContextBridge bridge = db.getDependencyResolver().resolveDependency(ThreadToStatementContextBridge.class);
        Statement statement = bridge.get();
        for (Map.Entry<Label, Long> entry : counts.entrySet()) {
            assertEquals(entry.getValue().longValue(), statement.readOperations().countsForNode(statement.readOperations().labelGetForName(entry.getKey().name())));
        }
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) HashMap(java.util.HashMap) Statement(org.neo4j.kernel.api.Statement) Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) Map(java.util.Map) HashMap(java.util.HashMap) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap)

Example 44 with Label

use of org.neo4j.graphdb.Label in project neo4j by neo4j.

the class SchemaProcedure method buildSchemaGraph.

public GraphResult buildSchemaGraph() {
    final Map<String, NodeImpl> nodes = new HashMap<>();
    final Map<String, Set<RelationshipImpl>> relationships = new HashMap<>();
    try (Statement statement = kernelTransaction.acquireStatement()) {
        ReadOperations readOperations = statement.readOperations();
        StatementTokenNameLookup statementTokenNameLookup = new StatementTokenNameLookup(readOperations);
        try (Transaction transaction = graphDatabaseAPI.beginTx()) {
            // add all labelsInDatabase
            ResourceIterator<Label> labelsInDatabase = graphDatabaseAPI.getAllLabelsInUse().iterator();
            while (labelsInDatabase.hasNext()) {
                Label label = labelsInDatabase.next();
                Map<String, Object> properties = new HashMap<>();
                Iterator<NewIndexDescriptor> indexDescriptorIterator = readOperations.indexesGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> indexes = new ArrayList<>();
                while (indexDescriptorIterator.hasNext()) {
                    NewIndexDescriptor index = indexDescriptorIterator.next();
                    String[] propertyNames = PropertyNameUtils.getPropertyKeys(statementTokenNameLookup, index.schema().getPropertyIds());
                    indexes.add(String.join(",", propertyNames));
                }
                properties.put("indexes", indexes);
                Iterator<ConstraintDescriptor> nodePropertyConstraintIterator = readOperations.constraintsGetForLabel(readOperations.labelGetForName(label.name()));
                ArrayList<String> constraints = new ArrayList<>();
                while (nodePropertyConstraintIterator.hasNext()) {
                    PropertyConstraint constraint = ConstraintBoundary.map(nodePropertyConstraintIterator.next());
                    constraints.add(constraint.userDescription(statementTokenNameLookup));
                }
                properties.put("constraints", constraints);
                getOrCreateLabel(label.name(), properties, nodes);
            }
            //add all relationships
            Iterator<RelationshipType> relationshipTypeIterator = graphDatabaseAPI.getAllRelationshipTypesInUse().iterator();
            while (relationshipTypeIterator.hasNext()) {
                RelationshipType relationshipType = relationshipTypeIterator.next();
                String relationshipTypeGetName = relationshipType.name();
                int relId = readOperations.relationshipTypeGetForName(relationshipTypeGetName);
                ResourceIterator<Label> labelsInUse = graphDatabaseAPI.getAllLabelsInUse().iterator();
                List<NodeImpl> startNodes = new LinkedList<>();
                List<NodeImpl> endNodes = new LinkedList<>();
                while (labelsInUse.hasNext()) {
                    Label labelToken = labelsInUse.next();
                    String labelName = labelToken.name();
                    Map<String, Object> properties = new HashMap<>();
                    NodeImpl node = getOrCreateLabel(labelName, properties, nodes);
                    int labelId = readOperations.labelGetForName(labelName);
                    if (readOperations.countsForRelationship(labelId, relId, ReadOperations.ANY_LABEL) > 0) {
                        startNodes.add(node);
                    }
                    if (readOperations.countsForRelationship(ReadOperations.ANY_LABEL, relId, labelId) > 0) {
                        endNodes.add(node);
                    }
                }
                for (NodeImpl startNode : startNodes) {
                    for (NodeImpl endNode : endNodes) {
                        RelationshipImpl relationship = addRelationship(startNode, endNode, relationshipTypeGetName, relationships);
                    }
                }
            }
            transaction.success();
            return getGraphResult(nodes, relationships);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) Label(org.neo4j.graphdb.Label) ArrayList(java.util.ArrayList) RelationshipType(org.neo4j.graphdb.RelationshipType) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) StatementTokenNameLookup(org.neo4j.kernel.api.StatementTokenNameLookup) Statement(org.neo4j.kernel.api.Statement) PropertyConstraint(org.neo4j.kernel.api.constraints.PropertyConstraint) LinkedList(java.util.LinkedList) ReadOperations(org.neo4j.kernel.api.ReadOperations) Transaction(org.neo4j.graphdb.Transaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ConstraintDescriptor(org.neo4j.kernel.api.schema_new.constaints.ConstraintDescriptor)

Example 45 with Label

use of org.neo4j.graphdb.Label in project neo4j by neo4j.

the class GraphDatabaseFacade method allNodesWithLabel.

private ResourceIterator<Node> allNodesWithLabel(final Label myLabel) {
    Statement statement = spi.currentStatement();
    int labelId = statement.readOperations().labelGetForName(myLabel.name());
    if (labelId == KeyReadOperations.NO_SUCH_LABEL) {
        statement.close();
        return emptyIterator();
    }
    final PrimitiveLongIterator nodeIds = statement.readOperations().nodesGetForLabel(labelId);
    return ResourceClosingIterator.newResourceIterator(statement, map(nodeId -> new NodeProxy(nodeActions, nodeId), nodeIds));
}
Also used : PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) InternalIndexState(org.neo4j.kernel.api.index.InternalIndexState) StoreId(org.neo4j.kernel.impl.store.StoreId) RELATIONSHIP_AUTO_INDEX(org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.RELATIONSHIP_AUTO_INDEX) ResourceIterator(org.neo4j.graphdb.ResourceIterator) Suppliers(org.neo4j.function.Suppliers) ResourceClosingIterator(org.neo4j.helpers.collection.ResourceClosingIterator) URL(java.net.URL) ResourceIterable(org.neo4j.graphdb.ResourceIterable) Status(org.neo4j.kernel.api.exceptions.Status) TransactionEventHandler(org.neo4j.graphdb.event.TransactionEventHandler) TopLevelTransaction(org.neo4j.kernel.impl.coreapi.TopLevelTransaction) Statement(org.neo4j.kernel.api.Statement) Property(org.neo4j.kernel.api.properties.Property) RelationshipProxy(org.neo4j.kernel.impl.core.RelationshipProxy) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) RelationshipAutoIndexerFacade(org.neo4j.kernel.impl.coreapi.RelationshipAutoIndexerFacade) IndexNotFoundKernelException(org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) NODE_AUTO_INDEX(org.neo4j.kernel.impl.api.legacyindex.InternalAutoIndexing.NODE_AUTO_INDEX) AutoIndexerFacade(org.neo4j.kernel.impl.coreapi.AutoIndexerFacade) Map(java.util.Map) NodePropertyDescriptor(org.neo4j.kernel.api.schema.NodePropertyDescriptor) Transaction(org.neo4j.graphdb.Transaction) TransactionalContextFactory(org.neo4j.kernel.impl.query.TransactionalContextFactory) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) PrefetchingResourceIterator(org.neo4j.helpers.collection.PrefetchingResourceIterator) KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) BidirectionalTraversalDescriptionImpl(org.neo4j.kernel.impl.traversal.BidirectionalTraversalDescriptionImpl) Result(org.neo4j.graphdb.Result) URLAccessValidationError(org.neo4j.graphdb.security.URLAccessValidationError) ThreadToStatementContextBridge(org.neo4j.kernel.impl.core.ThreadToStatementContextBridge) IndexProviderImpl(org.neo4j.kernel.impl.coreapi.IndexProviderImpl) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) InvalidTransactionTypeKernelException(org.neo4j.kernel.api.exceptions.InvalidTransactionTypeKernelException) KernelException(org.neo4j.kernel.api.exceptions.KernelException) StandardRelationshipActions(org.neo4j.kernel.impl.coreapi.StandardRelationshipActions) String.format(java.lang.String.format) Schema(org.neo4j.graphdb.schema.Schema) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) NO_SUCH_LABEL(org.neo4j.kernel.impl.api.operations.KeyReadOperations.NO_SUCH_LABEL) TransactionTerminatedException(org.neo4j.graphdb.TransactionTerminatedException) IndexManager(org.neo4j.graphdb.index.IndexManager) Neo4jTransactionalContextFactory(org.neo4j.kernel.impl.query.Neo4jTransactionalContextFactory) Optional(java.util.Optional) DependencyResolver(org.neo4j.graphdb.DependencyResolver) RelationshipType(org.neo4j.graphdb.RelationshipType) GraphDatabaseSettings(org.neo4j.graphdb.factory.GraphDatabaseSettings) Guard(org.neo4j.kernel.guard.Guard) Label(org.neo4j.graphdb.Label) PlaceboTransaction(org.neo4j.kernel.impl.coreapi.PlaceboTransaction) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) SchemaImpl(org.neo4j.kernel.impl.coreapi.schema.SchemaImpl) EntityType(org.neo4j.storageengine.api.EntityType) StandardNodeActions(org.neo4j.kernel.impl.coreapi.StandardNodeActions) SchemaKernelException(org.neo4j.kernel.api.exceptions.schema.SchemaKernelException) MonoDirectionalTraversalDescription(org.neo4j.kernel.impl.traversal.MonoDirectionalTraversalDescription) NotFoundException(org.neo4j.graphdb.NotFoundException) Supplier(java.util.function.Supplier) SchemaRuleNotFoundException(org.neo4j.kernel.api.exceptions.schema.SchemaRuleNotFoundException) Node(org.neo4j.graphdb.Node) GraphDatabaseQueryService(org.neo4j.kernel.GraphDatabaseQueryService) IndexManagerImpl(org.neo4j.kernel.impl.coreapi.IndexManagerImpl) ConstraintViolationException(org.neo4j.graphdb.ConstraintViolationException) EntityNotFoundException(org.neo4j.kernel.api.exceptions.EntityNotFoundException) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) ReadOnlyRelationshipIndexFacade(org.neo4j.kernel.impl.coreapi.ReadOnlyRelationshipIndexFacade) ThrowingAction(org.neo4j.function.ThrowingAction) NO_SUCH_PROPERTY_KEY(org.neo4j.kernel.impl.api.operations.KeyReadOperations.NO_SUCH_PROPERTY_KEY) TokenAccess(org.neo4j.kernel.impl.api.TokenAccess) AutoIndexing(org.neo4j.kernel.api.legacyindex.AutoIndexing) Config(org.neo4j.kernel.configuration.Config) ReadOnlyIndexFacade(org.neo4j.kernel.impl.coreapi.ReadOnlyIndexFacade) PrimitiveLongCollections(org.neo4j.collection.primitive.PrimitiveLongCollections) ReadOperations(org.neo4j.kernel.api.ReadOperations) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) PrimitiveLongCollections.map(org.neo4j.collection.primitive.PrimitiveLongCollections.map) Iterators.emptyIterator(org.neo4j.helpers.collection.Iterators.emptyIterator) NodeProxy(org.neo4j.kernel.impl.core.NodeProxy) NewIndexDescriptor(org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor) File(java.io.File) PropertyContainerLocker(org.neo4j.kernel.impl.coreapi.PropertyContainerLocker) TimeUnit(java.util.concurrent.TimeUnit) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) Relationship(org.neo4j.graphdb.Relationship) AUTH_DISABLED(org.neo4j.kernel.api.security.SecurityContext.AUTH_DISABLED) KeyReadOperations(org.neo4j.kernel.impl.api.operations.KeyReadOperations) MultipleFoundException(org.neo4j.graphdb.MultipleFoundException) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) ClientConnectionInfo(org.neo4j.kernel.impl.query.clientconnection.ClientConnectionInfo) ConstraintValidationException(org.neo4j.kernel.api.exceptions.schema.ConstraintValidationException) Collections(java.util.Collections) Statement(org.neo4j.kernel.api.Statement) NodeProxy(org.neo4j.kernel.impl.core.NodeProxy)

Aggregations

Label (org.neo4j.graphdb.Label)82 Test (org.junit.Test)55 Node (org.neo4j.graphdb.Node)48 Transaction (org.neo4j.graphdb.Transaction)44 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)12 RelationshipType (org.neo4j.graphdb.RelationshipType)8 DependencyResolver (org.neo4j.graphdb.DependencyResolver)6 DynamicLabel (org.neo4j.graphdb.DynamicLabel)6 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)6 Statement (org.neo4j.kernel.api.Statement)6 ArrayList (java.util.ArrayList)5 ConstraintViolationException (org.neo4j.graphdb.ConstraintViolationException)5 Relationship (org.neo4j.graphdb.Relationship)5 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)5 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)5 File (java.io.File)4 HashSet (java.util.HashSet)4 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4