Skip to content

Commit ccc675e

Browse files
authored
implementation processModifySubscriptionEvent (#757)
* implementation processModifySubscriptionEvent Signed-off-by: qGYdXbY2 <47661341+qGYdXbY2@users.noreply.github.com>
1 parent 232b4b2 commit ccc675e

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

xyz-psql-connector/src/main/java/com/here/xyz/psql/DatabaseHandler.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.here.xyz.events.IterateHistoryEvent;
4141
import com.here.xyz.events.ModifyFeaturesEvent;
4242
import com.here.xyz.events.ModifySpaceEvent;
43+
import com.here.xyz.events.ModifySubscriptionEvent;
4344
import com.here.xyz.events.SearchForFeaturesEvent;
4445
import com.here.xyz.models.geojson.coordinates.BBox;
4546
import com.here.xyz.models.geojson.implementation.Feature;
@@ -450,6 +451,31 @@ else if(event.getOperation() == UPDATE)
450451
return new SuccessResponse().withStatus("OK");
451452
}
452453

454+
455+
protected XyzResponse executeModifySubscription(ModifySubscriptionEvent event) throws SQLException {
456+
457+
String space = event.getSpace(),
458+
tableName = config.readTableFromEvent(event),
459+
schemaName = config.getDatabaseSettings().getSchema();
460+
461+
boolean bLastSubscriptionToDelete = event.getHasNoActiveSubscriptions();
462+
463+
switch(event.getOperation())
464+
{ case CREATE :
465+
case UPDATE : return new FeatureCollection().withCount((long) executeUpdateWithRetry( SQLQueryBuilder.buildAddSubscriptionQuery(space, schemaName, tableName ) ));
466+
467+
case DELETE :
468+
if( !bLastSubscriptionToDelete )
469+
return new FeatureCollection().withCount( 1l );
470+
else
471+
return new FeatureCollection().withCount((long) executeUpdateWithRetry(SQLQueryBuilder.buildRemoveSubscriptionQuery(space, schemaName)));
472+
473+
default: break;
474+
}
475+
476+
return null;
477+
}
478+
453479
protected XyzResponse executeIterateHistory(IterateHistoryEvent event) throws SQLException {
454480
if(event.isCompact())
455481
return executeQueryWithRetry(SQLQueryBuilder.buildSquashHistoryQuery(event), this::compactHistoryResultSetHandler, true);

xyz-psql-connector/src/main/java/com/here/xyz/psql/PSQLXyzConnector.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.here.xyz.events.GetFeaturesByIdEvent;
3333
import com.here.xyz.events.GetFeaturesByTileEvent;
3434
import com.here.xyz.events.GetFeaturesByTileEvent.ResponseType;
35+
import com.here.xyz.events.ModifySubscriptionEvent.Operation;
3536
import com.here.xyz.events.GetHistoryStatisticsEvent;
3637
import com.here.xyz.events.GetStatisticsEvent;
3738
import com.here.xyz.events.GetStorageStatisticsEvent;
@@ -553,10 +554,28 @@ protected XyzResponse processModifySpaceEvent(ModifySpaceEvent event) throws Exc
553554

554555
@Override
555556
protected XyzResponse processModifySubscriptionEvent(ModifySubscriptionEvent event) throws Exception {
556-
// Needs further implementation
557-
return new SuccessResponse();
557+
try{
558+
logger.info("{} Received ModifySpaceEvent", traceItem);
559+
560+
this.validateModifySubscriptionEvent(event);
561+
562+
return executeModifySubscription(event);
563+
}catch (SQLException e){
564+
return checkSQLException(e, config.readTableFromEvent(event));
565+
}finally {
566+
logger.info("{} Finished ModifySpaceEvent", traceItem);
567+
}
558568
}
559569

570+
private void validateModifySubscriptionEvent(ModifySubscriptionEvent event) throws Exception {
571+
572+
switch(event.getOperation())
573+
{ case CREATE : case UPDATE : case DELETE : break;
574+
default:
575+
throw new ErrorResponseException(streamId, XyzError.ILLEGAL_ARGUMENT, "Modify Subscription - Operation (" + event.getOperation() + ") not supported" );
576+
}
577+
578+
}
560579

561580
@Override
562581
protected XyzResponse processIterateHistoryEvent(IterateHistoryEvent event) {
@@ -642,7 +661,6 @@ private void validateModifySpaceEvent(ModifySpaceEvent event) throws Exception{
642661
}
643662
}
644663

645-
646664
private static final Pattern ERRVALUE_22P02 = Pattern.compile("invalid input syntax for type numeric:\\s+\"([^\"]*)\"\\s+Query:"),
647665
ERRVALUE_22P05 = Pattern.compile("ERROR:\\s+(.*)\\s+Detail:\\s+(.*)\\s+Where:");
648666

xyz-psql-connector/src/main/java/com/here/xyz/psql/SQLQueryBuilder.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,22 @@ protected static String addHistoryTriggerSQL(final String schema, final String t
890890
public static String getForceMode(boolean isForce2D) {
891891
return isForce2D ? "ST_Force2D" : "ST_Force3D";
892892
}
893+
894+
public static SQLQuery buildAddSubscriptionQuery(String space, String schemaName, String tableName) {
895+
String theSql =
896+
"insert into xyz_config.space_meta ( id, schem, h_id, meta ) values(?,?,?,'{\"subscriptions\":true}' )"
897+
+" on conflict (id,schem) do "
898+
+" update set meta = xyz_config.space_meta.meta || excluded.meta ";
899+
900+
return new SQLQuery(theSql, space,schemaName,tableName);
901+
}
902+
903+
public static SQLQuery buildRemoveSubscriptionQuery(String space, String schemaName) {
904+
String theSql =
905+
"update xyz_config.space_meta "
906+
+" set meta = meta - 'subscriptions' "
907+
+"where ( id, schem ) = ( ?, ? ) ";
908+
909+
return new SQLQuery(theSql, space,schemaName );
910+
}
893911
}

0 commit comments

Comments
 (0)