Skip to content

Commit

Permalink
Fix Neo4j remote deployment (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
gartens authored Dec 20, 2024
1 parent 207978e commit af3d18f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
import org.jetbrains.annotations.NotNull;
import org.polypheny.db.adapter.DeployMode.DeploySetting;
import org.polypheny.db.adapter.annotations.AdapterProperties;
import org.polypheny.db.adapter.java.AdapterTemplate;
import org.polypheny.db.catalog.Catalog;
Expand Down Expand Up @@ -176,6 +177,13 @@ public Adapter<?> addAdapter( String adapterName, String uniqueName, AdapterType

AdapterTemplate adapterTemplate = AdapterTemplate.fromString( adapterName, adapterType );


for ( AbstractAdapterSetting setting : adapterTemplate.settings ) {
if ( setting.appliesTo.stream().noneMatch( s -> s.appliesTo( mode ) ) ) {
settings.remove( setting.name );
}
}

long adapterId = Catalog.getInstance().createAdapter( uniqueName, adapterName, adapterType, settings, mode );
try {
Adapter<?> adapter = adapterTemplate.getDeployer().get( adapterId, uniqueName, settings, mode );
Expand Down
18 changes: 6 additions & 12 deletions core/src/main/java/org/polypheny/db/adapter/DeployMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ public enum DeployMode {
}


public static DeployMode fromString( String mode ) {
if ( mode.equals( "remote" ) ) {
return REMOTE;
} else if ( mode.equals( "docker" ) ) {
return DOCKER;
} else {
return EMBEDDED;
}
}


public enum DeploySetting {
REMOTE( DeployMode.REMOTE ),
DOCKER( DeployMode.DOCKER ),
Expand Down Expand Up @@ -83,5 +72,10 @@ List<DeployMode> getModes( List<DeployMode> availableModes ) {
}
}


boolean appliesTo( DeployMode mode ) {
return usedByAll || this.mode.equals( mode );
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
import org.polypheny.db.adapter.DataStore;
import org.polypheny.db.adapter.DataStore.IndexMethodModel;
import org.polypheny.db.adapter.DeployMode;
import org.polypheny.db.adapter.DeployMode.DeploySetting;
import org.polypheny.db.adapter.GraphModifyDelegate;
import org.polypheny.db.adapter.annotations.AdapterProperties;
import org.polypheny.db.adapter.annotations.AdapterSettingInteger;
import org.polypheny.db.adapter.annotations.AdapterSettingString;
import org.polypheny.db.adapter.neo4j.types.NestedSingleType;
import org.polypheny.db.adapter.neo4j.util.NeoUtil;
import org.polypheny.db.catalog.catalogs.GraphAdapterCatalog;
Expand Down Expand Up @@ -130,12 +133,16 @@ private static String getMappingLabel( long id ) {


@Slf4j
@Extension
@AdapterProperties(
name = "Neo4j",
description = "Neo4j is a graph-model based database system. It stores data in a graph structure which consists of nodes and edges.",
usedModes = { DeployMode.DOCKER, DeployMode.REMOTE },
defaultMode = DeployMode.DOCKER)
@Extension
@AdapterSettingString(name = "host", defaultValue = "localhost", appliesTo = DeploySetting.REMOTE)
@AdapterSettingInteger(name = "port", defaultValue = 7687, appliesTo = DeploySetting.REMOTE)
@AdapterSettingString(name = "user", defaultValue = "neo4j", appliesTo = DeploySetting.REMOTE)
@AdapterSettingString(name = "password", defaultValue = "neo4j", appliesTo = DeploySetting.REMOTE)
public static class Neo4jStore extends DataStore<GraphAdapterCatalog> {

private final String DEFAULT_DATABASE = "public";
Expand All @@ -152,9 +159,6 @@ public static class Neo4jStore extends DataStore<GraphAdapterCatalog> {
@Getter
private NeoNamespace currentNamespace;

@Getter
private NeoGraph currentGraph;

private final TransactionProvider transactionProvider;
private String host;

Expand All @@ -173,7 +177,6 @@ public Neo4jStore( final long adapterId, final String uniqueName, final Map<Stri
this.auth = AuthTokens.basic( this.user, this.pass );

if ( deployMode == DeployMode.DOCKER ) {

if ( settings.getOrDefault( "deploymentId", "" ).isEmpty() ) {
int instanceId = Integer.parseInt( settings.get( "instanceId" ) );
DockerInstance instance = DockerManager.getInstance().getInstanceById( instanceId )
Expand Down Expand Up @@ -206,7 +209,9 @@ public Neo4jStore( final long adapterId, final String uniqueName, final Map<Stri
this.host = settings.get( "host" );
this.port = Integer.parseInt( settings.get( "port" ) );
this.container = null;

if ( !testConnection() ) {
throw new GenericRuntimeException( "Could not connect to neo4j database" );
}
} else {
throw new GenericRuntimeException( "Not supported deploy mode: " + deployMode.name() );
}
Expand All @@ -233,14 +238,12 @@ public Neo4jStore( final long adapterId, final String uniqueName, final Map<Stri
* Test if a connection to the provided Neo4j database is possible.
*/
private boolean testConnection() {
if ( container == null ) {
return false;
if ( container != null ) {
HostAndPort hp = container.connectToContainer( 7687 );
this.host = hp.host();
this.port = hp.port();
}

HostAndPort hp = container.connectToContainer( 7687 );
this.host = hp.host();
this.port = hp.port();

try {
this.db = GraphDatabase.driver( new URI( String.format( "bolt://%s:%s", host, port ) ), auth );
} catch ( URISyntaxException e ) {
Expand Down

0 comments on commit af3d18f

Please sign in to comment.