diff --git a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/endpoint/impl/PostgresEndpoint.java b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/endpoint/impl/PostgresEndpoint.java index 6b1e3c960..82d1fa89c 100644 --- a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/endpoint/impl/PostgresEndpoint.java +++ b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/endpoint/impl/PostgresEndpoint.java @@ -60,6 +60,9 @@ public class PostgresEndpoint extends Endpoint { private PostgresLanguage postgresLanguage; + //Join Table + private List idList; + public PostgresEndpoint() {} public PostgresEndpoint(RetryPolicy retryPolicy) { @@ -221,6 +224,10 @@ public PostgresLanguage getPostgresLanguage() { return postgresLanguage; } + public List getIdList() { + return idList; + } + public StringResponse upsert( WordEmbeddings wordEmbeddings, String filename, @@ -273,6 +280,13 @@ public StringResponse insertIntoJoinTable( this.metadataTableNames = List.of(metadataTableName); return this.postgresService.insertIntoJoinTable(this).blockingGet(); } + public StringResponse batchInsertIntoJoinTable( + String metadataTableName, List idList, String metadataId) { + this.idList = idList; + this.metadataId = metadataId; + this.metadataTableNames = List.of(metadataTableName); + return this.postgresService.batchInsertIntoJoinTable(this).blockingGet(); + } public Observable> query( WordEmbeddings wordEmbeddings, PostgresDistanceMetric metric, int topK) { diff --git a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/client/impl/PostgresClient.java b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/client/impl/PostgresClient.java index 6d714a38d..7a8395a1d 100644 --- a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/client/impl/PostgresClient.java +++ b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/client/impl/PostgresClient.java @@ -198,6 +198,28 @@ public EdgeChain insertIntoJoinTable(PostgresEndpoint postgresEn postgresEndpoint); } + public EdgeChain batchInsertIntoJoinTable(PostgresEndpoint postgresEndpoint) { + return new EdgeChain<>( + Observable.create( + emitter -> { + try { + this.metadataRepository.batchInsertIntoJoinTable( + postgresEndpoint.getTableName(), + postgresEndpoint.getMetadataTableNames().get(0), + postgresEndpoint.getIdList(), + postgresEndpoint.getMetadataId() + ); + + emitter.onNext(new StringResponse("Inserted")); + emitter.onComplete(); + + } catch (final Exception e) { + emitter.onError(e); + } + }), + postgresEndpoint); + } + public EdgeChain> query(PostgresEndpoint postgresEndpoint) { return new EdgeChain<>( diff --git a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/repositories/PostgresClientMetadataRepository.java b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/repositories/PostgresClientMetadataRepository.java index 215715923..c3dd37f80 100644 --- a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/repositories/PostgresClientMetadataRepository.java +++ b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/index/repositories/PostgresClientMetadataRepository.java @@ -104,6 +104,26 @@ public void insertIntoJoinTable(PostgresEndpoint postgresEndpoint) { UUID.fromString(postgresEndpoint.getMetadataId()))); } + @Transactional + public void batchInsertIntoJoinTable( + String tableName, String metadataTableName, List idList, String metadataId) { + String joinTableName = tableName + "_join_" + metadataTableName; + List sqlStatements = new ArrayList<>(); + for(String id: idList) { + sqlStatements.add( + String.format( + "INSERT INTO %s (id, metadata_id) VALUES ('%s', '%s') ON CONFLICT (id) DO UPDATE SET" + + " metadata_id = EXCLUDED.metadata_id;", + joinTableName, + UUID.fromString(id), + UUID.fromString(metadataId) + ) + ); + } + jdbcTemplate.batchUpdate(sqlStatements.toArray(new String[0])); + } + + @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public List> queryWithMetadata( String tableName, diff --git a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/retrofit/PostgresService.java b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/retrofit/PostgresService.java index e548e7803..d46a07f67 100644 --- a/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/retrofit/PostgresService.java +++ b/FlySpring/edgechain-app/src/main/java/com/edgechain/lib/retrofit/PostgresService.java @@ -33,6 +33,9 @@ public interface PostgresService { @POST(value = "index/postgres/join/insert") Single insertIntoJoinTable(@Body PostgresEndpoint postgresEndpoint); + @POST(value = "index/postgres/join/batch-insert") + Single batchInsertIntoJoinTable(@Body PostgresEndpoint postgresEndpoint); + @POST(value = "index/postgres/query") Single> query(@Body PostgresEndpoint postgresEndpoint); diff --git a/FlySpring/edgechain-app/src/main/java/com/edgechain/service/controllers/index/PostgresController.java b/FlySpring/edgechain-app/src/main/java/com/edgechain/service/controllers/index/PostgresController.java index fdab6cef1..6d2f5bb71 100644 --- a/FlySpring/edgechain-app/src/main/java/com/edgechain/service/controllers/index/PostgresController.java +++ b/FlySpring/edgechain-app/src/main/java/com/edgechain/service/controllers/index/PostgresController.java @@ -59,6 +59,13 @@ public Single insertIntoJoinTable( return edgeChain.toSingle(); } + @PostMapping("/join/batch-insert") + public Single batchInsertIntoJoinTable( + @RequestBody PostgresEndpoint postgresEndpoint) { + EdgeChain edgeChain = this.postgresClient.batchInsertIntoJoinTable(postgresEndpoint); + return edgeChain.toSingle(); + } + @PostMapping("/query") public Single> query( @RequestBody PostgresEndpoint postgresEndpoint) {