org.neo4j.dbms.systemgraph.CommunityTopologyGraphDbmsModelUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of neo4j-kernel Show documentation
Show all versions of neo4j-kernel Show documentation
Neo4j kernel is a lightweight, embedded Java database designed to
store data structured as graphs rather than tables. For more
information, see http://neo4j.org.
/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [https://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.dbms.systemgraph;
import static org.neo4j.dbms.systemgraph.DriverSettings.Keys.CONNECTION_POOL_ACQUISITION_TIMEOUT;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.neo4j.configuration.connectors.BoltConnector;
import org.neo4j.configuration.helpers.RemoteUri;
import org.neo4j.configuration.helpers.SocketAddress;
import org.neo4j.configuration.helpers.SocketAddressParser;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.Entity;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.database.DatabaseIdFactory;
import org.neo4j.kernel.database.DatabaseReference;
import org.neo4j.kernel.database.DatabaseReferenceImpl;
import org.neo4j.kernel.database.DatabaseReferenceImpl.Internal;
import org.neo4j.kernel.database.NamedDatabaseId;
import org.neo4j.kernel.database.NormalizedDatabaseName;
import org.neo4j.logging.Level;
import org.neo4j.values.storable.DurationValue;
public final class CommunityTopologyGraphDbmsModelUtil {
private CommunityTopologyGraphDbmsModelUtil() {}
static Stream getAllPrimaryStandardDatabaseReferencesInRoot(Transaction tx) {
return tx.findNodes(TopologyGraphDbmsModel.DATABASE_LABEL).stream()
.filter(node -> !node.hasProperty(TopologyGraphDbmsModel.DATABASE_VIRTUAL_PROPERTY))
.map(CommunityTopologyGraphDbmsModelUtil::getDatabaseId)
.map(CommunityTopologyGraphDbmsModelUtil::primaryRefFromDatabaseId);
}
private static Internal primaryRefFromDatabaseId(NamedDatabaseId databaseId) {
var alias = new NormalizedDatabaseName(databaseId.name());
return new Internal(alias, databaseId, true);
}
static NormalizedDatabaseName getNameProperty(String labelName, Node node) {
return new NormalizedDatabaseName(
getPropertyOnNode(labelName, node, TopologyGraphDbmsModel.NAME_PROPERTY, String.class));
}
static TopologyGraphDbmsModel.DatabaseAccess getDatabaseAccess(Node databaseNode) {
var accessString = (String) databaseNode.getProperty(
TopologyGraphDbmsModel.DATABASE_ACCESS_PROPERTY,
TopologyGraphDbmsModel.DatabaseAccess.READ_WRITE.toString());
return Enum.valueOf(TopologyGraphDbmsModel.DatabaseAccess.class, accessString);
}
public static Optional createInternalReference(Node alias, NamedDatabaseId targetedDatabase) {
return ignoreConcurrentDeletes(() -> {
var aliasName = new NormalizedDatabaseName(getPropertyOnNode(
TopologyGraphDbmsModel.DATABASE_NAME, alias, TopologyGraphDbmsModel.NAME_PROPERTY, String.class));
var namespace = new NormalizedDatabaseName(getPropertyOnNode(
TopologyGraphDbmsModel.DATABASE_NAME,
alias,
TopologyGraphDbmsModel.NAMESPACE_PROPERTY,
String.class));
var primary = getPropertyOnNode(
TopologyGraphDbmsModel.DATABASE_NAME,
alias,
TopologyGraphDbmsModel.PRIMARY_PROPERTY,
Boolean.class);
return Optional.of(new Internal(aliasName, namespace, targetedDatabase, primary));
});
}
public static Optional createExternalReference(Node ref) {
return ignoreConcurrentDeletes(() -> {
var uriString = getPropertyOnNode(
TopologyGraphDbmsModel.REMOTE_DATABASE_LABEL_DESCRIPTION,
ref,
TopologyGraphDbmsModel.URL_PROPERTY,
String.class);
var targetName = new NormalizedDatabaseName(getPropertyOnNode(
TopologyGraphDbmsModel.REMOTE_DATABASE_LABEL_DESCRIPTION,
ref,
TopologyGraphDbmsModel.TARGET_NAME_PROPERTY,
String.class));
var aliasName = new NormalizedDatabaseName(getPropertyOnNode(
TopologyGraphDbmsModel.REMOTE_DATABASE_LABEL_DESCRIPTION,
ref,
TopologyGraphDbmsModel.NAME_PROPERTY,
String.class));
var namespace = new NormalizedDatabaseName(getPropertyOnNode(
TopologyGraphDbmsModel.REMOTE_DATABASE_LABEL_DESCRIPTION,
ref,
TopologyGraphDbmsModel.NAMESPACE_PROPERTY,
String.class));
var uri = URI.create(uriString);
var host = SocketAddressParser.socketAddress(uri, BoltConnector.DEFAULT_PORT, SocketAddress::new);
var remoteUri = new RemoteUri(uri.getScheme(), List.of(host), uri.getQuery());
var uuid = getPropertyOnNode(
TopologyGraphDbmsModel.REMOTE_DATABASE_LABEL_DESCRIPTION,
ref,
TopologyGraphDbmsModel.VERSION_PROPERTY,
String.class);
return Optional.of(new DatabaseReferenceImpl.External(
targetName, aliasName, namespace, remoteUri, UUID.fromString(uuid)));
});
}
public static Optional getDriverSettings(Node aliasNode) {
return ignoreConcurrentDeletes(() -> {
var connectsWith = StreamSupport.stream(
aliasNode
.getRelationships(
Direction.OUTGOING, TopologyGraphDbmsModel.CONNECTS_WITH_RELATIONSHIP)
.spliterator(),
false)
.toList(); // Must be collected to exhaust the underlying iterator
return connectsWith.stream()
.findFirst()
.map(Relationship::getEndNode)
.map(CommunityTopologyGraphDbmsModelUtil::createDriverSettings);
});
}
public static Optional