Skip to content

Commit

Permalink
minor fixes for neo4j and mongodb, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
datomo committed Nov 29, 2023
1 parent a310795 commit 560131a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public Enumerable<Object> 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" );
Expand Down
8 changes: 6 additions & 2 deletions webui/src/main/java/org/polypheny/db/webui/WebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand All @@ -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 ) )
Expand Down
35 changes: 21 additions & 14 deletions webui/src/main/java/org/polypheny/db/webui/crud/LanguageCrud.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand Down Expand Up @@ -216,18 +215,18 @@ public static PolyGraph getGraph( String namespace, TransactionManager manager,
}


public static void attachError( Transaction transaction, List<Result<?, ?>> 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." );
Expand All @@ -241,7 +240,7 @@ public static void attachError( Transaction transaction, List<Result<?, ?>> resu
}
}

results.add( result );
return result;
}


Expand All @@ -250,14 +249,14 @@ public static void attachError( Transaction transaction, List<Result<?, ?>> resu
Catalog catalog = Catalog.getInstance();
ResultIterator iterator = context.getIterator();
List<List<PolyValue>> 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();
Expand Down Expand Up @@ -379,17 +378,25 @@ public static List<String[]> computeResultData( final List<List<PolyValue>> rows
public static ResultBuilder<?, ?, ?, ?> getDocResult( ExecutedContext context, UIRequest request, Statement statement ) {
ResultIterator iterator = context.getIterator();
List<List<PolyValue>> 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 );
Expand Down

0 comments on commit 560131a

Please sign in to comment.