Skip to content

Commit

Permalink
adding adapterTemplateModel, removing frontend logic from backend
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Aug 31, 2023
1 parent 18d786b commit c7c3dd9
Show file tree
Hide file tree
Showing 42 changed files with 217 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.Value;
import lombok.experimental.Accessors;
import lombok.experimental.NonFinal;
import org.polypheny.db.adapter.DeployMode.DeploySetting;
import org.polypheny.db.adapter.annotations.AdapterProperties;
import org.polypheny.db.adapter.annotations.AdapterSettingBoolean;
Expand All @@ -37,23 +36,28 @@
import org.polypheny.db.adapter.annotations.AdapterSettingString;

@Accessors(chain = true)
@Value
@NonFinal
public abstract class AbstractAdapterSetting {

public final String name;
public final boolean canBeNull;
public final String subOf;
public final boolean required;
public final boolean modifiable;
public final String defaultValue;
private final int position;
public String name;
public boolean canBeNull;
public String subOf;
public boolean required;
public boolean modifiable;
public String defaultValue;
public int position;
@Setter
@NonFinal
public String description;
public AdapterSettingType type;

@Getter
public final List<DeploySetting> appliesTo;
public List<DeploySetting> appliesTo;


public AbstractAdapterSetting( final String name, final boolean canBeNull, final String subOf, final boolean required, final boolean modifiable, List<DeploySetting> appliesTo, String defaultValue, int position ) {
public AbstractAdapterSetting( final AdapterSettingType type, final String name, final boolean canBeNull, final String subOf, final boolean required, final boolean modifiable, List<DeploySetting> appliesTo, String defaultValue, int position ) {
this.type = type;
this.name = name;
this.canBeNull = canBeNull;
this.subOf = Objects.equals( subOf, "" ) ? null : subOf;
Expand All @@ -77,63 +81,37 @@ public AbstractAdapterSetting( final String name, final boolean canBeNull, final
* @param properties which are defined by the corresponding Adapter
* @return a map containing the available modes and the corresponding collections of AdapterSettings
*/
public static Map<String, List<AbstractAdapterSetting>> fromAnnotations( Annotation[] annotations, AdapterProperties properties ) {
Map<String, List<AbstractAdapterSetting>> settings = new HashMap<>();
public static List<AbstractAdapterSetting> fromAnnotations( Annotation[] annotations, AdapterProperties properties ) {
List<AbstractAdapterSetting> settings = new ArrayList<>();

for ( Annotation annotation : annotations ) {
if ( annotation instanceof AdapterSettingString ) {
mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingString.fromAnnotation( (AdapterSettingString) annotation ) );
settings.add( AbstractAdapterSettingString.fromAnnotation( (AdapterSettingString) annotation ) );
} else if ( annotation instanceof AdapterSettingString.List ) {
Arrays.stream( ((AdapterSettingString.List) annotation).value() ).forEach( el -> mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingString.fromAnnotation( el ) ) );
Arrays.stream( ((AdapterSettingString.List) annotation).value() ).forEach( el -> settings.add( AbstractAdapterSettingString.fromAnnotation( el ) ) );
} else if ( annotation instanceof AdapterSettingBoolean ) {
mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingBoolean.fromAnnotation( (AdapterSettingBoolean) annotation ) );
settings.add( AbstractAdapterSettingBoolean.fromAnnotation( (AdapterSettingBoolean) annotation ) );
} else if ( annotation instanceof AdapterSettingBoolean.List ) {
Arrays.stream( ((AdapterSettingBoolean.List) annotation).value() ).forEach( el -> mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingBoolean.fromAnnotation( el ) ) );
Arrays.stream( ((AdapterSettingBoolean.List) annotation).value() ).forEach( el -> settings.add( AbstractAdapterSettingBoolean.fromAnnotation( el ) ) );
} else if ( annotation instanceof AdapterSettingInteger ) {
mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingInteger.fromAnnotation( (AdapterSettingInteger) annotation ) );
settings.add( AbstractAdapterSettingInteger.fromAnnotation( (AdapterSettingInteger) annotation ) );
} else if ( annotation instanceof AdapterSettingInteger.List ) {
Arrays.stream( ((AdapterSettingInteger.List) annotation).value() ).forEach( el -> mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingInteger.fromAnnotation( el ) ) );
Arrays.stream( ((AdapterSettingInteger.List) annotation).value() ).forEach( el -> settings.add( AbstractAdapterSettingInteger.fromAnnotation( el ) ) );
} else if ( annotation instanceof AdapterSettingList ) {
mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingList.fromAnnotation( (AdapterSettingList) annotation ) );
settings.add( AbstractAdapterSettingList.fromAnnotation( (AdapterSettingList) annotation ) );
} else if ( annotation instanceof AdapterSettingList.List ) {
Arrays.stream( ((AdapterSettingList.List) annotation).value() ).forEach( el -> mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingList.fromAnnotation( el ) ) );
Arrays.stream( ((AdapterSettingList.List) annotation).value() ).forEach( el -> settings.add( AbstractAdapterSettingList.fromAnnotation( el ) ) );
} else if ( annotation instanceof AdapterSettingDirectory ) {
mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingDirectory.fromAnnotation( (AdapterSettingDirectory) annotation ) );
settings.add( AbstractAdapterSettingDirectory.fromAnnotation( (AdapterSettingDirectory) annotation ) );
} else if ( annotation instanceof AdapterSettingDirectory.List ) {
Arrays.stream( ((AdapterSettingDirectory.List) annotation).value() ).forEach( el -> mergeSettings( settings, properties.usedModes(), AbstractAdapterSettingDirectory.fromAnnotation( el ) ) );
Arrays.stream( ((AdapterSettingDirectory.List) annotation).value() ).forEach( el -> settings.add( AbstractAdapterSettingDirectory.fromAnnotation( el ) ) );
}
}

settings.forEach( ( key, values ) -> values.sort( Comparator.comparingInt( value -> value.position ) ) );
return settings;
}


/**
* Merges the provided setting into the provided map of AdapterSettings
*
* @param settings already correctly sorted settings
* @param deployModes the deployment modes which are supported by this specific adapter
* @param setting the setting which is merged into the map
*/
private static void mergeSettings( Map<String, List<AbstractAdapterSetting>> settings, DeployMode[] deployModes, AbstractAdapterSetting setting ) {
// we need to unpack the underlying DeployModes
for ( DeployMode mode : setting.appliesTo
.stream()
.flatMap( mode -> mode.getModes( Arrays.asList( deployModes ) ).stream() )
.collect( Collectors.toList() ) ) {

if ( settings.containsKey( mode.getName() ) ) {
settings.get( mode.getName() ).add( setting );
} else {
List<AbstractAdapterSetting> temp = new ArrayList<>();
temp.add( setting );
settings.put( mode.getName(), temp );
}
}
}


/**
* In most subclasses, this method returns the defaultValue, because the UI overrides the defaultValue when a new value is set.
*/
Expand All @@ -153,4 +131,9 @@ public static List<AbstractAdapterSetting> serializeSettings( List<AbstractAdapt
}


public enum AdapterSettingType {
DIRECTORY, INTEGER, LIST, STRING, BOOLEAN

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@

public class AbstractAdapterSettingBoolean extends AbstractAdapterSetting {

private final String type = "Boolean";


public AbstractAdapterSettingBoolean( String name, boolean canBeNull, String subOf, boolean required, boolean modifiable, boolean defaultValue, List<DeploySetting> modes, int position ) {
super( name, canBeNull, subOf, required, modifiable, modes, String.valueOf( defaultValue ), position );
super( AdapterSettingType.BOOLEAN, name, canBeNull, subOf, required, modifiable, modes, String.valueOf( defaultValue ), position );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@

@Accessors(chain = true)
public class AbstractAdapterSettingDirectory extends AbstractAdapterSetting {

private final String type = "Directory";
@Setter
public String directory;
//This field is necessary for the UI and needs to be initialized to be serialized to JSON.
Expand All @@ -39,7 +37,7 @@ public class AbstractAdapterSettingDirectory extends AbstractAdapterSetting {


public AbstractAdapterSettingDirectory( String name, boolean canBeNull, String subOf, boolean required, boolean modifiable, List<DeploySetting> modes, int position ) {
super( name, canBeNull, subOf, required, modifiable, modes, null, position );
super( AdapterSettingType.DIRECTORY, name, canBeNull, subOf, required, modifiable, modes, null, position );
//so it will be serialized
this.directory = "";
this.inputStreams = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@

public class AbstractAdapterSettingInteger extends AbstractAdapterSetting {

private final String type = "Integer";


public AbstractAdapterSettingInteger( String name, boolean canBeNull, String subOf, boolean required, boolean modifiable, Integer defaultValue, List<DeploySetting> modes, int position ) {
super( name, canBeNull, subOf, required, modifiable, modes, defaultValue.toString(), position );
super( AdapterSettingType.INTEGER, name, canBeNull, subOf, required, modifiable, modes, defaultValue.toString(), position );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@

@Accessors(chain = true)
public class AbstractAdapterSettingList extends AbstractAdapterSetting {

private final String type = "List";
public List<String> options;
public boolean dynamic = false;


public AbstractAdapterSettingList( String name, boolean canBeNull, final String subOf, boolean required, boolean modifiable, List<String> options, List<DeploySetting> modes, String defaultValue, int position ) {
super( name, canBeNull, subOf, required, modifiable, modes, defaultValue, position );
super( AdapterSettingType.LIST, name, canBeNull, subOf, required, modifiable, modes, defaultValue, position );
this.options = options;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@

public class AbstractAdapterSettingString extends AbstractAdapterSetting {

private final String type = "String";


public AbstractAdapterSettingString( String name, boolean canBeNull, String subOf, boolean required, boolean modifiable, String defaultValue, List<DeploySetting> modes, int position ) {
super( name, canBeNull, subOf, required, modifiable, modes, defaultValue, position );
super( AdapterSettingType.STRING, name, canBeNull, subOf, required, modifiable, modes, defaultValue, position );
}


Expand Down
9 changes: 1 addition & 8 deletions core/src/main/java/org/polypheny/db/adapter/Adapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -145,19 +144,13 @@ public Expression getNamespaceAsExpression( long id ) {


public List<AbstractAdapterSetting> getAvailableSettings( Class<?> clazz ) {
return AbstractAdapterSetting.fromAnnotations( clazz.getAnnotations(), properties )
.values()
.stream()
.flatMap( Collection::stream )
.collect( Collectors.toList() );
return AbstractAdapterSetting.fromAnnotations( clazz.getAnnotations(), properties );
}


public static Map<String, String> getDefaultSettings( Class<DataStore<?>> clazz ) {
return AbstractAdapterSetting.fromAnnotations( clazz.getAnnotations(), clazz.getAnnotation( AdapterProperties.class ) )
.values()
.stream()
.flatMap( Collection::stream )
.collect( Collectors.toMap( e -> e.name, e -> e.defaultValue ) );
}

Expand Down
65 changes: 12 additions & 53 deletions core/src/main/java/org/polypheny/db/adapter/AdapterManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -35,23 +32,20 @@
import lombok.AllArgsConstructor;
import org.apache.calcite.linq4j.tree.Expression;
import org.apache.calcite.linq4j.tree.Expressions;
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;
import org.polypheny.db.catalog.entity.CatalogAdapter;
import org.polypheny.db.catalog.entity.CatalogAdapter.AdapterType;
import org.polypheny.db.catalog.entity.allocation.AllocationEntity;
import org.polypheny.db.catalog.exceptions.GenericRuntimeException;
import org.polypheny.db.config.ConfigDocker;
import org.polypheny.db.config.RuntimeConfig;
import org.polypheny.db.util.Pair;

public class AdapterManager {

public static Expression ADAPTER_MANAGER_EXPRESSION = Expressions.call( AdapterManager.class, "getInstance" );

private static final Map<Pair<String, AdapterType>, AdapterTemplate> REGISTER = new ConcurrentHashMap<>();
public static final Map<Pair<String, AdapterType>, AdapterTemplate> REGISTER = new ConcurrentHashMap<>();

private final Map<Long, Adapter<?>> adapterById = new HashMap<>();
private final Map<String, Adapter<?>> adapterByName = new HashMap<>();
Expand Down Expand Up @@ -170,60 +164,18 @@ public ImmutableMap<String, DataSource<?>> getSources() {
public List<AdapterInformation> getAvailableAdapters( AdapterType adapterType ) {
List<AdapterTemplate> adapterTemplates = getAdapters( adapterType );

List<AdapterInformation> result = new LinkedList<>();
List<AdapterInformation> result = new ArrayList<>();

for ( AdapterTemplate adapterTemplate : adapterTemplates ) {
// Exclude abstract classes
if ( !Modifier.isAbstract( adapterTemplate.getClazz().getModifiers() ) ) {
Map<String, List<AbstractAdapterSetting>> settings = new HashMap<>();

AdapterProperties properties = adapterTemplate.getClazz().getAnnotation( AdapterProperties.class );
if ( properties == null ) {
throw new RuntimeException( adapterTemplate.getClazz().getSimpleName() + " does not annotate the adapter correctly" );
throw new GenericRuntimeException( adapterTemplate.getClazz().getSimpleName() + " does not annotate the adapter correctly" );
}

// Used to evaluate which mode is used when deploying the adapter
settings.put(
"mode",
Collections.singletonList(
new AbstractAdapterSettingList(
"mode",
false,
null,
true,
true,
Collections.singletonList( "default" ),
Collections.singletonList( DeploySetting.DEFAULT ),
"default",
0 ) ) );

// Add empty list for each available mode
Arrays.stream( properties.usedModes() ).forEach( mode -> settings.put( mode.getName(), new ArrayList<>() ) );

// Add default which is used by all available modes
settings.put( "default", new ArrayList<>() );

// Merge annotated AdapterSettings into settings
Map<String, List<AbstractAdapterSetting>> annotatedSettings = AbstractAdapterSetting.fromAnnotations( adapterTemplate.getClazz().getAnnotations(), adapterTemplate.getClazz().getAnnotation( AdapterProperties.class ) );
settings.putAll( annotatedSettings );

// If the adapter uses docker add the dynamic docker setting
if ( settings.containsKey( "docker" ) ) {
settings
.get( "docker" )
.add( new BindableAbstractAdapterSettingsList<>(
"instanceId",
"DockerInstance",
false,
null,
true,
false,
RuntimeConfig.DOCKER_INSTANCES.getList( ConfigDocker.class ).stream().filter( ConfigDocker::isDockerRunning ).collect( Collectors.toList() ),
ConfigDocker::getAlias,
ConfigDocker.class )
.bind( RuntimeConfig.DOCKER_INSTANCES )
.setDescription( "To configure additional Docker instances, use the Docker Config in the Config Manager." ) );
}
List<AbstractAdapterSetting> settings = AbstractAdapterSetting.fromAnnotations( adapterTemplate.getClazz().getAnnotations(), adapterTemplate.getClazz().getAnnotation( AdapterProperties.class ) );

result.add( new AdapterInformation( properties.name(), properties.description(), adapterType, settings ) );
}
Expand Down Expand Up @@ -301,13 +253,20 @@ public void restoreAdapters() {
}


public List<AdapterInformation> getAvailableAdapters() {
List<AdapterInformation> adapters = new ArrayList<>( getAvailableAdapters( AdapterType.STORE ) );
adapters.addAll( getAvailableAdapters( AdapterType.SOURCE ) );
return adapters;
}


@AllArgsConstructor
public static class AdapterInformation {

public final String name;
public final String description;
public final AdapterType type;
public final Map<String, List<AbstractAdapterSetting>> settings;
public final List<AbstractAdapterSetting> settings;


public static JsonSerializer<AdapterInformation> getSerializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.polypheny.db.exploreByExample.requests;


import org.polypheny.db.webui.models.UiColumnDefinition;
import org.polypheny.db.webui.models.catalog.UiColumnDefinition;
import org.polypheny.db.webui.models.requests.UIRequest;


Expand Down
Loading

0 comments on commit c7c3dd9

Please sign in to comment.