From 560131af44f6286470368c0d73d51fe3bc57f3ff Mon Sep 17 00:00:00 2001 From: datomo Date: Wed, 29 Nov 2023 23:50:48 +0100 Subject: [PATCH] minor fixes for neo4j and mongodb, error handling --- .../db/adapter/mongodb/MongoEntity.java | 2 +- .../org/polypheny/db/webui/WebSocket.java | 8 +++-- .../polypheny/db/webui/crud/LanguageCrud.java | 35 +++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/plugins/mongodb-adapter/src/main/java/org/polypheny/db/adapter/mongodb/MongoEntity.java b/plugins/mongodb-adapter/src/main/java/org/polypheny/db/adapter/mongodb/MongoEntity.java index 80f87aa1f2..32213aa64b 100644 --- a/plugins/mongodb-adapter/src/main/java/org/polypheny/db/adapter/mongodb/MongoEntity.java +++ b/plugins/mongodb-adapter/src/main/java/org/polypheny/db/adapter/mongodb/MongoEntity.java @@ -497,7 +497,7 @@ public Enumerable handleDirectDML( Operation operation, String filter, L try { final long changes = doDML( operation, filter, operations, onlyOne, needsDocument, xid, bucket ); - return Linq4j.asEnumerable( Collections.singletonList( PolyLong.of( changes ) ) ); + return Linq4j.asEnumerable( Collections.singletonList( new PolyValue[]{ PolyLong.of( changes ) } ) ); } catch ( MongoException e ) { entity.getTransactionProvider().rollback( xid ); log.warn( "Failed" ); diff --git a/webui/src/main/java/org/polypheny/db/webui/WebSocket.java b/webui/src/main/java/org/polypheny/db/webui/WebSocket.java index 13bc95641b..2f84dc38e7 100644 --- a/webui/src/main/java/org/polypheny/db/webui/WebSocket.java +++ b/webui/src/main/java/org/polypheny/db/webui/WebSocket.java @@ -35,6 +35,7 @@ import org.polypheny.db.catalog.Catalog; import org.polypheny.db.catalog.entity.logical.LogicalNamespace; import org.polypheny.db.catalog.exceptions.GenericRuntimeException; +import org.polypheny.db.catalog.logistic.DataModel; import org.polypheny.db.languages.QueryLanguage; import org.polypheny.db.processing.QueryContext; import org.polypheny.db.type.entity.graph.PolyGraph; @@ -157,16 +158,18 @@ public void onMessage( final WsMessageContext ctx ) { UIRequest uiRequest = ctx.messageAsClass( UIRequest.class ); try { LogicalNamespace namespace = Catalog.getInstance().getSnapshot().getNamespace( uiRequest.namespace ).orElse( null ); - switch ( namespace.dataModel ) { + switch ( namespace == null ? DataModel.RELATIONAL : namespace.dataModel ) { case RELATIONAL: result = crud.getTable( uiRequest ); break; case DOCUMENT: + String entity = Catalog.snapshot().doc().getCollection( uiRequest.entityId ).map( c -> c.name ).orElse( "" ); result = LanguageCrud.anyQueryResult( QueryContext.builder() - .query( "db.%s.find({})" ) + .query( String.format( "db.%s.find({})", entity ) ) .language( QueryLanguage.from( "mongo" ) ) .origin( POLYPHENY_UI ) + .batch( uiRequest.noLimit ? -1 : crud.getPageSize() ) .transactionManager( crud.getTransactionManager() ) .informationTarget( i -> i.setSession( ctx.session ) ) .namespaceId( namespace == null ? Catalog.defaultNamespaceId : namespace.id ) @@ -178,6 +181,7 @@ public void onMessage( final WsMessageContext ctx ) { .query( "MATCH (n) RETURN n" ) .language( QueryLanguage.from( "cypher" ) ) .origin( POLYPHENY_UI ) + .batch( uiRequest.noLimit ? -1 : crud.getPageSize() ) .namespaceId( namespace == null ? Catalog.defaultNamespaceId : namespace.id ) .transactionManager( crud.getTransactionManager() ) .informationTarget( i -> i.setSession( ctx.session ) ) diff --git a/webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java b/webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java index 313f1767f2..b76963402a 100644 --- a/webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java +++ b/webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java @@ -141,8 +141,7 @@ public static void anyQuery( Context ctx ) { for ( ExecutedContext executedContext : executedContexts ) { if ( executedContext.getError().isPresent() ) { - attachError( transaction, results, executedContext, executedContext.getError().get() ); - return results; + return List.of( buildErrorResult( transaction, executedContext, executedContext.getError().get() ).build() ); } results.add( builder.apply( executedContext, request, executedContext.getStatement() ).build() ); @@ -216,18 +215,18 @@ public static PolyGraph getGraph( String namespace, TransactionManager manager, } - public static void attachError( Transaction transaction, List> results, ExecutedContext context, Throwable t ) { + public static ResultBuilder buildErrorResult( Transaction transaction, ExecutedContext context, Throwable t ) { //String msg = t.getMessage() == null ? "" : t.getMessage(); - Result result; + ResultBuilder result; switch ( context.getQuery().getLanguage().getDataModel() ) { case RELATIONAL: - result = RelationalResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build(); + result = RelationalResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ); break; case DOCUMENT: - result = DocResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build(); + result = DocResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ); break; case GRAPH: - result = GraphResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ).build(); + result = GraphResult.builder().error( t == null ? null : t.getMessage() ).query( context.getQuery().getQuery() ).xid( transaction.getXid().toString() ); break; default: throw new GenericRuntimeException( "Unknown data model." ); @@ -241,7 +240,7 @@ public static void attachError( Transaction transaction, List> resu } } - results.add( result ); + return result; } @@ -250,14 +249,14 @@ public static void attachError( Transaction transaction, List> resu Catalog catalog = Catalog.getInstance(); ResultIterator iterator = context.getIterator(); List> rows = new ArrayList<>(); - for ( int i = 0; i < request.currentPage; i++ ) { - rows = iterator.getNextBatch(); - } - try { + for ( int i = 0; i < request.currentPage; i++ ) { + rows = iterator.getNextBatch(); + } + iterator.close(); } catch ( Exception e ) { - throw new GenericRuntimeException( e ); + return buildErrorResult( statement.getTransaction(), context, e ); } boolean hasMoreRows = context.getIterator().hasMoreRows(); @@ -379,17 +378,25 @@ public static List computeResultData( final List> rows public static ResultBuilder getDocResult( ExecutedContext context, UIRequest request, Statement statement ) { ResultIterator iterator = context.getIterator(); List> data = iterator.getNextBatch(); + try { + for ( int i = 0; i < request.currentPage; i++ ) { + data = iterator.getNextBatch(); + } + iterator.close(); } catch ( Exception e ) { - throw new GenericRuntimeException( e ); + return buildErrorResult( statement.getTransaction(), context, e ); } + boolean hasMoreRows = context.getIterator().hasMoreRows(); + return DocResult.builder() .header( context.getIterator().getImplementation().rowType.getFields().stream().map( FieldDefinition::of ).toArray( FieldDefinition[]::new ) ) .data( data.stream().map( d -> d.get( 0 ).toJson() ).toArray( String[]::new ) ) .query( context.getQuery().getQuery() ) .language( context.getQuery().getLanguage() ) + .hasMore( hasMoreRows ) .xid( statement.getTransaction().getXid().toString() ) .dataModel( context.getIterator().getImplementation().getDataModel() ) .namespace( request.namespace );