Skip to content

Commit

Permalink
added default namespace to adapters and removed hardcoded namespace f…
Browse files Browse the repository at this point in the history
…rom ddlManager and catalog methods
  • Loading branch information
datomo committed Apr 2, 2024
1 parent 79d84cf commit 55153a3
Show file tree
Hide file tree
Showing 24 changed files with 79 additions and 67 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/org/polypheny/db/adapter/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public abstract class Adapter<ACatalog extends AdapterCatalog> implements Scanna
private final AdapterProperties properties;
protected final DeployMode deployMode;
protected String deploymentId;
@Getter
private final String adapterName;
public final String adapterName;
public final ACatalog adapterCatalog;


Expand Down
15 changes: 10 additions & 5 deletions core/src/main/java/org/polypheny/db/adapter/AdapterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,7 @@ public ImmutableMap<String, DataSource<?>> getSources() {
}





public Adapter<?> addAdapter( String adapterName, String uniqueName, AdapterType adapterType, DeployMode mode, Map<String, String> settings ) {
public Adapter<?> addAdapter( String adapterName, String uniqueName, long defaultNamespace, AdapterType adapterType, DeployMode mode, Map<String, String> settings ) {
uniqueName = uniqueName.toLowerCase();
if ( getAdapters().containsKey( uniqueName ) ) {
throw new GenericRuntimeException( "There is already an adapter with this unique name" );
Expand All @@ -184,7 +181,7 @@ public Adapter<?> addAdapter( String adapterName, String uniqueName, AdapterType

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

long adapterId = Catalog.getInstance().createAdapter( uniqueName, adapterName, adapterType, settings, mode );
long adapterId = Catalog.getInstance().createAdapter( uniqueName, adapterName, defaultNamespace, adapterType, settings, mode );
try {
Adapter<?> adapter = adapterTemplate.getDeployer().get( adapterId, uniqueName, settings );
adapterByName.put( adapter.getUniqueName(), adapter );
Expand Down Expand Up @@ -266,4 +263,12 @@ public interface Function4<P1, P2, P3, R> {

}


@FunctionalInterface
public interface Function5<P1, P2, P3, P4, R> {

R get( P1 p1, P2 p2, P3 p3, P4 p4 );

}

}
5 changes: 3 additions & 2 deletions core/src/main/java/org/polypheny/db/catalog/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,13 @@ public void removeObserver( PropertyChangeListener listener ) {
*
* @param uniqueName The unique name of the adapter
* @param clazz The class name of the adapter
* @param defaultNamespace The default namespace of the adapter
* @param type The type of adapter
* @param settings The configuration of the adapter
* @param mode
* @param mode The deploy mode of the adapter
* @return The id of the newly added adapter
*/
public abstract long createAdapter( String uniqueName, String clazz, AdapterType type, Map<String, String> settings, DeployMode mode );
public abstract long createAdapter( String uniqueName, String clazz, long defaultNamespace, AdapterType type, Map<String, String> settings, DeployMode mode );

/**
* Update settings of an adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import io.activej.serializer.annotations.Deserialize;
import io.activej.serializer.annotations.Serialize;
import java.io.Serial;
import java.util.HashMap;
import java.util.Map;
import lombok.EqualsAndHashCode;
Expand All @@ -35,6 +36,7 @@
@SuperBuilder(toBuilder = true)
public class LogicalAdapter implements PolyObject {

@Serial
private static final long serialVersionUID = -6140489767408917639L;

@Serialize
Expand All @@ -51,6 +53,8 @@ public class LogicalAdapter implements PolyObject {
public String adapterTypeName;
@Serialize
public DeployMode mode;
@Serialize
public long defaultNamespace;


public enum AdapterType {STORE, SOURCE}
Expand All @@ -62,14 +66,16 @@ public LogicalAdapter(
@Deserialize("adapterName") @NonNull final String adapterName,
@Deserialize("type") @NonNull final AdapterType adapterType,
@Deserialize("mode") @NotNull final DeployMode mode,
@Deserialize("settings") @NonNull final Map<String, String> settings ) {
@Deserialize("settings") @NonNull final Map<String, String> settings,
@Deserialize("defaultNamespace") final long defaultNamespace ) {
this.id = id;
this.uniqueName = uniqueName;
this.adapterName = adapterName;
this.type = adapterType;
this.settings = new HashMap<>( settings );
this.adapterTypeName = getAdapterName();
this.mode = mode;
this.defaultNamespace = defaultNamespace;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,9 @@ public void dropNamespace( long id ) {


@Override
public long createAdapter( String uniqueName, String clazz, AdapterType type, Map<String, String> settings, DeployMode mode ) {
public long createAdapter( String uniqueName, String clazz, long defaultNamespace, AdapterType type, Map<String, String> settings, DeployMode mode ) {
long id = idBuilder.getNewAdapterId();
adapters.put( id, new LogicalAdapter( id, uniqueName, clazz, type, mode, settings ) );
adapters.put( id, new LogicalAdapter( id, uniqueName, clazz, type, mode, settings, defaultNamespace ) );
change();
return id;
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/polypheny/db/ddl/DdlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ public static DdlManager getInstance() {
*
* @param uniqueName unique name of the newly created adapter
* @param adapterName name of adapter, which is used to create the adapter
* @param defaultNamespace the default namespace for the adapter
* @param adapterType the specific {@link AdapterType} for the adapter to create
* @param config configuration for the adapter
* @param mode the deploy mode
*/
public abstract void createAdapter( String uniqueName, String adapterName, AdapterType adapterType, Map<String, String> config, DeployMode mode );
public abstract void createAdapter( String uniqueName, String adapterName, long defaultNamespace, AdapterType adapterType, Map<String, String> config, DeployMode mode );

/**
* Drop an adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void dropNamespace( long id ) {


@Override
public long createAdapter( String uniqueName, String clazz, AdapterType type, Map<String, String> settings, DeployMode mode ) {
public long createAdapter( String uniqueName, String clazz, long defaultNamespace, AdapterType type, Map<String, String> settings, DeployMode mode ) {
throw new NotImplementedException();
}

Expand Down
35 changes: 17 additions & 18 deletions dbms/src/main/java/org/polypheny/db/ddl/DdlManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,17 @@ public long createNamespace( String initialName, DataModel type, boolean ifNotEx


@Override
public void createAdapter( String uniqueName, String adapterName, AdapterType adapterType, Map<String, String> config, DeployMode mode ) {
public void createAdapter( String uniqueName, String adapterName, long defaultNamespace, AdapterType adapterType, Map<String, String> config, DeployMode mode ) {
uniqueName = uniqueName.toLowerCase();
Adapter<?> adapter = AdapterManager.getInstance().addAdapter( adapterName, uniqueName, adapterType, mode, config );
Adapter<?> adapter = AdapterManager.getInstance().addAdapter( adapterName, uniqueName, defaultNamespace, adapterType, mode, config );

if ( adapter instanceof DataSource<?> ) {
handleSource( (DataSource<?>) adapter );
handleSource( (DataSource<?>) adapter, defaultNamespace );
}
}


private void handleSource( DataSource<?> adapter ) {
private void handleSource( DataSource<?> adapter, long defaultNamespace ) {
Map<String, List<ExportedColumn>> exportedColumns;
try {
exportedColumns = adapter.getExportedColumns();
Expand All @@ -228,26 +228,26 @@ private void handleSource( DataSource<?> adapter ) {
for ( Map.Entry<String, List<ExportedColumn>> entry : exportedColumns.entrySet() ) {
// Make sure the table name is unique
String tableName = entry.getKey();
if ( catalog.getSnapshot().rel().getTable( Catalog.defaultNamespaceId, tableName ).isPresent() ) { // apparently we put them all into 1?
if ( catalog.getSnapshot().rel().getTable( defaultNamespace, tableName ).isPresent() ) { // apparently we put them all into 1?
int i = 0;
while ( catalog.getSnapshot().rel().getTable( Catalog.defaultNamespaceId, tableName + i ).isPresent() ) {
while ( catalog.getSnapshot().rel().getTable( defaultNamespace, tableName + i ).isPresent() ) {
i++;
}
tableName += i;
}

LogicalTable logical = catalog.getLogicalRel( Catalog.defaultNamespaceId ).addTable( tableName, EntityType.SOURCE, !(adapter).isDataReadOnly() );
LogicalTable logical = catalog.getLogicalRel( defaultNamespace ).addTable( tableName, EntityType.SOURCE, !(adapter).isDataReadOnly() );
List<LogicalColumn> columns = new ArrayList<>();

Pair<AllocationPartition, PartitionProperty> partitionProperty = createSinglePartition( logical.namespaceId, logical );

AllocationPlacement placement = catalog.getAllocRel( Catalog.defaultNamespaceId ).addPlacement( logical.id, Catalog.defaultNamespaceId, adapter.adapterId );
AllocationEntity allocation = catalog.getAllocRel( Catalog.defaultNamespaceId ).addAllocation( adapter.getAdapterId(), placement.id, partitionProperty.left.id, logical.id );
AllocationPlacement placement = catalog.getAllocRel( defaultNamespace ).addPlacement( logical.id, defaultNamespace, adapter.adapterId );
AllocationEntity allocation = catalog.getAllocRel( defaultNamespace ).addAllocation( adapter.getAdapterId(), placement.id, partitionProperty.left.id, logical.id );
List<AllocationColumn> aColumns = new ArrayList<>();
int colPos = 1;

for ( ExportedColumn exportedColumn : entry.getValue() ) {
LogicalColumn column = catalog.getLogicalRel( Catalog.defaultNamespaceId ).addColumn(
LogicalColumn column = catalog.getLogicalRel( defaultNamespace ).addColumn(
exportedColumn.name,
logical.id,
colPos++,
Expand All @@ -260,7 +260,7 @@ private void handleSource( DataSource<?> adapter ) {
exportedColumn.nullable,
Collation.getDefaultCollation() );

AllocationColumn allocationColumn = catalog.getAllocRel( Catalog.defaultNamespaceId ).addColumn(
AllocationColumn allocationColumn = catalog.getAllocRel( defaultNamespace ).addColumn(
placement.id,
logical.id,
column.id,
Expand All @@ -284,7 +284,6 @@ private void handleSource( DataSource<?> adapter ) {

@Override
public void dropAdapter( String name, Statement statement ) {
long defaultNamespaceId = Catalog.defaultNamespaceId;
name = name.replace( "'", "" );

LogicalAdapter adapter = catalog.getSnapshot().getAdapter( name ).orElseThrow();
Expand All @@ -300,7 +299,7 @@ public void dropAdapter( String name, Statement statement ) {
} else if ( allocation.unwrap( AllocationTable.class ).isPresent() ) {

for ( LogicalForeignKey fk : catalog.getSnapshot().rel().getForeignKeys( allocation.logicalId ) ) {
catalog.getLogicalRel( defaultNamespaceId ).deleteForeignKey( fk.id );
catalog.getLogicalRel( allocation.namespaceId ).deleteForeignKey( fk.id );
}

LogicalTable table = catalog.getSnapshot().rel().getTable( allocation.logicalId ).orElseThrow();
Expand All @@ -316,22 +315,22 @@ public void dropAdapter( String name, Statement statement ) {
}
// Delete column placement in catalog
for ( AllocationColumn column : allocation.unwrap( AllocationTable.class ).get().getColumns() ) {
catalog.getAllocRel( defaultNamespaceId ).deleteColumn( allocation.id, column.columnId );
catalog.getAllocRel( allocation.namespaceId ).deleteColumn( allocation.id, column.columnId );
}

// delete allocation
catalog.getAllocRel( defaultNamespaceId ).deleteAllocation( allocation.id );
catalog.getAllocRel( allocation.namespaceId ).deleteAllocation( allocation.id );

// Remove primary keys
catalog.getLogicalRel( defaultNamespaceId ).deletePrimaryKey( table.id );
catalog.getLogicalRel( allocation.namespaceId ).deletePrimaryKey( table.id );

// Delete columns
for ( LogicalColumn column : catalog.getSnapshot().rel().getColumns( allocation.logicalId ) ) {
catalog.getLogicalRel( defaultNamespaceId ).deleteColumn( column.id );
catalog.getLogicalRel( allocation.namespaceId ).deleteColumn( column.id );
}

// Delete the table
catalog.getLogicalRel( defaultNamespaceId ).deleteTable( table.id );
catalog.getLogicalRel( allocation.namespaceId ).deleteTable( table.id );
// Reset plan cache implementation cache & routing cache
statement.getQueryProcessor().resetCaches();
}
Expand Down
4 changes: 2 additions & 2 deletions dbms/src/main/java/org/polypheny/db/ddl/DefaultInserter.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ private static void restoreAdapters( DdlManager ddlManager, Catalog catalog, Run

// Deploy default storeId
Map<String, String> defaultStore = Catalog.snapshot().getAdapterTemplate( Catalog.defaultStore.getAdapterName(), AdapterType.STORE ).orElseThrow().getDefaultSettings();
ddlManager.createAdapter( "hsqldb", Catalog.defaultStore.getAdapterName(), AdapterType.STORE, defaultStore, DeployMode.EMBEDDED );
ddlManager.createAdapter( "hsqldb", Catalog.defaultStore.getAdapterName(), Catalog.defaultNamespaceId, AdapterType.STORE, defaultStore, DeployMode.EMBEDDED );

if ( mode == RunMode.TEST ) {
return; // source adapters create schema structure, which we do not want for testing
}

// Deploy default CSV view
Map<String, String> defaultSource = Catalog.snapshot().getAdapterTemplate( Catalog.defaultSource.getAdapterName(), AdapterType.SOURCE ).orElseThrow().getDefaultSettings();
ddlManager.createAdapter( "hr", Catalog.defaultSource.getAdapterName(), AdapterType.SOURCE, defaultSource, DeployMode.REMOTE );
ddlManager.createAdapter( "hr", Catalog.defaultSource.getAdapterName(), Catalog.defaultNamespaceId, AdapterType.SOURCE, defaultSource, DeployMode.REMOTE );


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,6 @@ public void renameLogicalColumn( long id, String newColumnName ) {
}






@SuppressWarnings("unused")
private interface Excludes {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class ExcelSource extends DataSource<RelAdapterCatalog> {
public String sheetName;


public ExcelSource( long storeId, String uniqueName, Map<String, String> settings ) {
public ExcelSource( final long storeId, final String uniqueName, final Map<String, String> settings ) {
super( storeId, uniqueName, settings, true, new RelAdapterCatalog( storeId ) );

this.connectionMethod = settings.containsKey( "method" ) ? ConnectionMethod.from( settings.get( "method" ) ) : ConnectionMethod.UPLOAD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ public abstract class AbstractJdbcSource extends DataSource<RelAdapterCatalog> i


public AbstractJdbcSource(
long storeId,
String uniqueName,
Map<String, String> settings,
String diverClass,
SqlDialect dialect,
boolean readOnly ) {
final long storeId,
final String uniqueName,
final Map<String, String> settings,
final String diverClass,
final SqlDialect dialect,
final boolean readOnly ) {
super( storeId, uniqueName, settings, readOnly, new RelAdapterCatalog( storeId ) );
this.connectionFactory = createConnectionFactory( settings, dialect, diverClass );
this.dialect = dialect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public abstract class AbstractJdbcStore extends DataStore<RelAdapterCatalog> imp


public AbstractJdbcStore(
long storeId,
String uniqueName,
Map<String, String> settings,
SqlDialect dialect,
boolean persistent ) {
final long storeId,
final String uniqueName,
final Map<String, String> settings,
final SqlDialect dialect,
final boolean persistent ) {
super( storeId, uniqueName, settings, persistent, new RelAdapterCatalog( storeId ) );
this.dialect = dialect;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
@AdapterSettingString(name = "table", defaultValue = "public.foo,public.bar", description = "Maximum number of concurrent JDBC connections.")
public class MonetdbSource extends AbstractJdbcSource {

public MonetdbSource( long storeId, String uniqueName, final Map<String, String> settings ) {
public MonetdbSource( final long storeId, final String uniqueName, final Map<String, String> settings ) {
super( storeId, uniqueName, settings, "nl.cwi.monetdb.jdbc.MonetDriver", MonetdbSqlDialect.DEFAULT, false );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class MonetdbStore extends AbstractJdbcStore {
private DockerContainer container;


public MonetdbStore( long storeId, String uniqueName, final Map<String, String> settings ) {
public MonetdbStore( final long storeId, final String uniqueName, final Map<String, String> settings ) {
super( storeId, uniqueName, settings, MonetdbSqlDialect.DEFAULT, true );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static class MongoStore extends DataStore<DocAdapterCatalog> {
private final List<PolyType> unsupportedTypes = ImmutableList.of();


public MongoStore( long adapterId, String uniqueName, Map<String, String> settings ) {
public MongoStore( final long adapterId, final String uniqueName, final Map<String, String> settings ) {
super( adapterId, uniqueName, settings, true, new DocAdapterCatalog( adapterId ) );

if ( deployMode == DeployMode.DOCKER ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.polypheny.db.prepare.Context;
import org.polypheny.db.sql.language.dialect.MysqlSqlDialect;

@SuppressWarnings("unused")
public class MysqlSourcePlugin extends PolyPlugin {


Expand Down Expand Up @@ -90,7 +91,7 @@ public void stop() {
description = "List of tables which should be imported. The names must to be separated by a comma.")
public static class MysqlSource extends AbstractJdbcSource {

public MysqlSource( long storeId, String uniqueName, final Map<String, String> settings ) {
public MysqlSource( final long storeId, final String uniqueName, final Map<String, String> settings ) {
super( storeId, uniqueName, settings, "org.mariadb.jdbc.Driver", MysqlSqlDialect.DEFAULT, false );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static class Neo4jStore extends DataStore<GraphAdapterCatalog> {
private String host;


public Neo4jStore( long adapterId, String uniqueName, Map<String, String> adapterSettings ) {
public Neo4jStore( final long adapterId, final String uniqueName, final Map<String, String> adapterSettings ) {
super( adapterId, uniqueName, adapterSettings, true, new GraphAdapterCatalog( adapterId ) );

this.user = "neo4j";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
description = "List of tables which should be imported. The names must to be separated by a comma.")
public class PostgresqlSource extends AbstractJdbcSource {

public PostgresqlSource( long storeId, String uniqueName, final Map<String, String> settings ) {
public PostgresqlSource( final long storeId, final String uniqueName, final Map<String, String> settings ) {
super(
storeId,
uniqueName,
Expand Down
Loading

0 comments on commit 55153a3

Please sign in to comment.