Skip to content

Commit

Permalink
minor adjustment to communication between Polypheny and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Sep 28, 2023
1 parent 1f02f8a commit a03c086
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void updateSnapshot() {

// update with newly generated physical entities
this.snapshot = SnapshotBuilder.createSnapshot( idBuilder.getNewSnapshotId(), this, logicalCatalogs, allocationCatalogs );
this.listeners.firePropertyChange( "snapshot", null, null );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public String toString() {
public Enumerable<PolyValue[]> scan( DataContext dataContext ) {
dataContext.getStatement().getTransaction().registerInvolvedAdapter( excelSource );
final AtomicBoolean cancelFlag = DataContext.Variable.CANCEL_FLAG.get( dataContext );
return new AbstractEnumerable<PolyValue[]>() {
return new AbstractEnumerable<>() {
@Override
public Enumerator<PolyValue[]> enumerator() {
return new ExcelEnumerator( source, cancelFlag, false, null, new ExcelEnumerator.ArrayRowConverter( fieldTypes, fields ), sheet );
Expand Down
19 changes: 18 additions & 1 deletion webui/src/main/java/org/polypheny/db/webui/Crud.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.Part;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -169,6 +171,7 @@
import org.polypheny.db.util.FileInputHandle;
import org.polypheny.db.util.Pair;
import org.polypheny.db.util.PolyphenyHomeDirManager;
import org.polypheny.db.webui.auth.AuthCrud;
import org.polypheny.db.webui.crud.CatalogCrud;
import org.polypheny.db.webui.crud.LanguageCrud;
import org.polypheny.db.webui.crud.StatisticCrud;
Expand All @@ -194,6 +197,7 @@
import org.polypheny.db.webui.models.catalog.AdapterModel;
import org.polypheny.db.webui.models.catalog.AdapterModel.AdapterSettingValueModel;
import org.polypheny.db.webui.models.catalog.PolyTypeModel;
import org.polypheny.db.webui.models.catalog.SnapshotModel;
import org.polypheny.db.webui.models.catalog.UiColumnDefinition;
import org.polypheny.db.webui.models.catalog.UiColumnDefinition.UiColumnDefinitionBuilder;
import org.polypheny.db.webui.models.requests.BatchUpdateRequest;
Expand All @@ -213,7 +217,7 @@

@Getter
@Slf4j
public class Crud implements InformationObserver {
public class Crud implements InformationObserver, PropertyChangeListener {

private static final Gson gson = new Gson();
private final TransactionManager transactionManager;
Expand All @@ -226,6 +230,9 @@ public class Crud implements InformationObserver {
public final StatisticCrud statisticCrud;

public final CatalogCrud catalogCrud;
public final AuthCrud authCrud;


private final Catalog catalog = Catalog.getInstance();


Expand All @@ -241,6 +248,9 @@ public class Crud implements InformationObserver {
this.languageCrud = new LanguageCrud( this );
this.statisticCrud = new StatisticCrud( this );
this.catalogCrud = new CatalogCrud( this );
this.authCrud = new AuthCrud( this );

Catalog.getInstance().addObserver( this );
}


Expand Down Expand Up @@ -3672,6 +3682,13 @@ public void getAvailablePlugins( Context ctx ) {
}


@Override
public void propertyChange( PropertyChangeEvent evt ) {
log.warn( "changed" );
authCrud.broadcast( SnapshotModel.from( Catalog.snapshot() ) );
}


public static class QueryExecutionException extends Exception {

QueryExecutionException( String message ) {
Expand Down
14 changes: 12 additions & 2 deletions webui/src/main/java/org/polypheny/db/webui/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.polypheny.db.StatusService;
import org.polypheny.db.catalog.Catalog;
Expand Down Expand Up @@ -107,6 +108,8 @@ public Throwable read( JsonReader in ) throws IOException {


private static HttpServer INSTANCE = null;
@Getter
private WebSocket webSocketHandler;


public static HttpServer getInstance() {
Expand Down Expand Up @@ -149,8 +152,8 @@ public void run() {
Catalog.defaultUserId,
Catalog.defaultNamespaceId );

WebSocket webSocketHandler = new WebSocket( crud );
webSockets( server, webSocketHandler );
this.webSocketHandler = new WebSocket( crud );
webSockets( server, this.webSocketHandler );

// Get modified index.html
server.get( "/", ctx -> {
Expand Down Expand Up @@ -195,6 +198,8 @@ private void defaultException( Class<? extends Exception> exceptionClass, Javali
private void crudRoutes( Javalin webuiServer, Crud crud ) {
attachCatalogMetaRoutes( webuiServer, crud );

attachPartnerRoutes( webuiServer, crud );

webuiServer.post( "/anyQuery", LanguageCrud::anyQuery );

webuiServer.post( "/insertTuple", crud::insertTuple );
Expand Down Expand Up @@ -358,6 +363,11 @@ private void crudRoutes( Javalin webuiServer, Crud crud ) {
}


private void attachPartnerRoutes( Javalin webuiServer, Crud crud ) {
webuiServer.get( "/auth/deregister", crud.authCrud::deregister );
}


private static void attachCatalogMetaRoutes( Javalin webuiServer, Crud crud ) {
webuiServer.post( "/getSchemaTree", crud.catalogCrud::getSchemaTree );

Expand Down
14 changes: 9 additions & 5 deletions webui/src/main/java/org/polypheny/db/webui/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.polypheny.db.webui.models.requests.GraphRequest;
import org.polypheny.db.webui.models.requests.QueryRequest;
import org.polypheny.db.webui.models.requests.RelAlgRequest;
import org.polypheny.db.webui.models.requests.RequestModel;
import org.polypheny.db.webui.models.requests.UIRequest;
import org.polypheny.db.webui.models.results.RelationalResult;
import org.polypheny.db.webui.models.results.Result;
Expand Down Expand Up @@ -100,9 +101,9 @@ public void onMessage( final WsMessageContext ctx ) {
//close analyzers of a previous query that was sent over the same socket.
Crud.cleanupOldSession( queryAnalyzers, ctx.getSessionId() );

UIRequest request = ctx.messageAsClass( UIRequest.class );
RequestModel request = ctx.messageAsClass( RequestModel.class );
Set<String> xIds = new HashSet<>();
switch ( request.requestType ) {
switch ( request.type ) {
case "GraphRequest":
GraphRequest graphRequest = ctx.messageAsClass( GraphRequest.class );
PolyGraph graph = LanguageCrud.getGraph( graphRequest.namespaceId, crud.getTransactionManager() );
Expand Down Expand Up @@ -133,11 +134,14 @@ public void onMessage( final WsMessageContext ctx ) {
}
ctx.send( results );
break;

case "RegisterRequest":
RelAlgRequest registerRequest = ctx.messageAsClass( RelAlgRequest.class );
crud.authCrud.register( registerRequest, ctx );
break;
case "RelAlgRequest":
case "EntityRequest":
Result<?, ?> result = null;
if ( request.requestType.equals( "RelAlgRequest" ) ) {
if ( request.type.equals( "RelAlgRequest" ) ) {
RelAlgRequest relAlgRequest = ctx.messageAsClass( RelAlgRequest.class );
try {
result = crud.executeAlg( relAlgRequest, ctx.session );
Expand Down Expand Up @@ -189,7 +193,7 @@ public void onMessage( final WsMessageContext ctx ) {
ctx.send( result );
break;
default:
throw new GenericRuntimeException( "Unexpected WebSocket request: " + request.requestType );
throw new GenericRuntimeException( "Unexpected WebSocket request: " + request.type );
}
queryAnalyzers.put( ctx.getSessionId(), xIds );
}
Expand Down
69 changes: 69 additions & 0 deletions webui/src/main/java/org/polypheny/db/webui/auth/AuthCrud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.webui.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.javalin.http.Context;
import io.javalin.websocket.WsMessageContext;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.polypheny.db.webui.Crud;
import org.polypheny.db.webui.models.requests.RegisterRequest;
import org.polypheny.db.webui.models.requests.RelAlgRequest;

@Slf4j
public class AuthCrud {

private final ObjectMapper mapper = new ObjectMapper();
private final Crud crud;
private final ConcurrentHashMap<UUID, PartnerStatus> partners = new ConcurrentHashMap<>();


public AuthCrud( Crud crud ) {
this.crud = crud;
}


public void register( RelAlgRequest registerRequest, WsMessageContext context ) {
String id = context.queryParam( "source" );
if ( id != null ) {
log.warn( "Partner " + id + " already registered" );
return;
}
PartnerStatus status = new PartnerStatus( context.session );
log.warn( "New partner with id " + status.id + " registered" );
partners.put( status.id, status );
context.send( new RegisterRequest( status.id.toString() ) );
}


public void deregister( Context context ) {
String id = context.queryParam( "id" );
if ( id == null ) {
log.warn( "Partner with empty id is not registered" );
return;
}
partners.remove( UUID.fromString( id ) );
}


public <E> void broadcast( E msg ) {
//partners.values().forEach( p -> HttpServer.getInstance().getWebSocketHandler(). );
}

}
40 changes: 40 additions & 0 deletions webui/src/main/java/org/polypheny/db/webui/auth/PartnerStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.webui.auth;

import java.util.UUID;
import lombok.Getter;
import org.eclipse.jetty.websocket.api.Session;

@Getter
public class PartnerStatus {

public final UUID id;
private final Session session;


public PartnerStatus( UUID id, Session session ) {
this.id = id;
this.session = session;
}


public PartnerStatus( Session session ) {
this( UUID.randomUUID(), session );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.webui.models.requests;

import lombok.EqualsAndHashCode;
import lombok.Value;

@EqualsAndHashCode(callSuper = true)
@Value
public class RegisterRequest extends RequestModel {

public String source;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2019-2023 The Polypheny Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.polypheny.db.webui.models.requests;

public class RequestModel {

/**
* ExpressionType of a request, e.g. QueryRequest or RelAlgRequest
*/
public String type;

public String payload;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@
/**
* Required to parse a request coming from the UI using Gson
*/
public class UIRequest {
public class UIRequest extends RequestModel {


/**
* ExpressionType of a request, e.g. QueryRequest or RelAlgRequest
*/
public String requestType;

/**
* The name of the table the data should be fetched from
*/
Expand Down Expand Up @@ -89,7 +84,7 @@ private UIRequest( JsonReader in ) throws IOException {
while ( in.peek() != JsonToken.NULL ) {
switch ( in.nextName() ) {
case "requestType":
requestType = in.nextString();
type = in.nextString();
break;
case "entityId":
entityId = in.nextLong();
Expand Down Expand Up @@ -119,7 +114,6 @@ private UIRequest( JsonReader in ) throws IOException {
}



static BiConsumer<JsonWriter, String> stringSerializer = ( out, val ) -> {
try {
out.value( val );
Expand Down Expand Up @@ -162,7 +156,7 @@ public void write( JsonWriter out, UIRequest value ) throws IOException {
}
out.beginObject();
out.name( "requestType" );
out.value( value.requestType );
out.value( value.type );
out.name( "tableId" );
out.value( value.entityId );
out.name( "currentPage" );
Expand Down

0 comments on commit a03c086

Please sign in to comment.