From 72ca1874e049be978c3bfc6c1e7021962da3e15a Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Thu, 28 Nov 2024 13:02:56 +0530 Subject: [PATCH] Remove unnecessary metadata objects for same strand calls --- .../persist/sql/datastore/SQLProcessor.java | 42 +++---------------- 1 file changed, 6 insertions(+), 36 deletions(-) diff --git a/native/src/main/java/io/ballerina/stdlib/persist/sql/datastore/SQLProcessor.java b/native/src/main/java/io/ballerina/stdlib/persist/sql/datastore/SQLProcessor.java index 9662b99..1362793 100644 --- a/native/src/main/java/io/ballerina/stdlib/persist/sql/datastore/SQLProcessor.java +++ b/native/src/main/java/io/ballerina/stdlib/persist/sql/datastore/SQLProcessor.java @@ -19,8 +19,6 @@ package io.ballerina.stdlib.persist.sql.datastore; import io.ballerina.runtime.api.Environment; -import io.ballerina.runtime.api.concurrent.StrandMetadata; -import io.ballerina.runtime.api.constants.RuntimeConstants; import io.ballerina.runtime.api.creators.TypeCreator; import io.ballerina.runtime.api.creators.ValueCreator; import io.ballerina.runtime.api.types.PredefinedTypes; @@ -33,13 +31,9 @@ import io.ballerina.runtime.api.values.BStream; import io.ballerina.runtime.api.values.BString; import io.ballerina.runtime.api.values.BTypedesc; -import io.ballerina.runtime.transactions.TransactionLocalContext; -import io.ballerina.runtime.transactions.TransactionResourceManager; import io.ballerina.stdlib.persist.Constants; import io.ballerina.stdlib.persist.sql.Utils; -import java.util.Map; - import static io.ballerina.stdlib.persist.Constants.KEY_FIELDS; import static io.ballerina.stdlib.persist.ErrorGenerator.wrapError; import static io.ballerina.stdlib.persist.Utils.getEntity; @@ -47,7 +41,6 @@ import static io.ballerina.stdlib.persist.Utils.getMetadata; import static io.ballerina.stdlib.persist.Utils.getPersistClient; import static io.ballerina.stdlib.persist.Utils.getRecordTypeWithKeyFields; -import static io.ballerina.stdlib.persist.Utils.getTransactionContextProperties; import static io.ballerina.stdlib.persist.sql.Constants.DB_CLIENT; import static io.ballerina.stdlib.persist.sql.Constants.PERSIST_EXECUTION_RESULT; import static io.ballerina.stdlib.persist.sql.Constants.SQL_EXECUTE_METHOD; @@ -69,7 +62,6 @@ private SQLProcessor() { static BStream query(Environment env, BObject client, BTypedesc targetType, BObject whereClause, BObject orderByClause, BObject limitClause, BObject groupByClause) { // This method will return `stream` - BString entity = getEntity(env); BObject persistClient = getPersistClient(client, entity); BArray keyFields = (BArray) persistClient.get(KEY_FIELDS); @@ -77,8 +69,6 @@ static BStream query(Environment env, BObject client, BTypedesc targetType, BObj RecordType recordTypeWithIdFields = getRecordTypeWithKeyFields(keyFields, recordType); BTypedesc targetTypeWithIdFields = ValueCreator.createTypedescValue(recordTypeWithIdFields); - - Map trxContextProperties = getTransactionContextProperties(); BArray[] metadata = getMetadata(recordType); BArray fields = metadata[0]; BArray includes = metadata[1]; @@ -90,9 +80,8 @@ static BStream query(Environment env, BObject client, BTypedesc targetType, BObj // typedesc rowType, string[] fields = [], string[] include = [] // )` // which returns `stream|persist:Error` - persistClient, Constants.RUN_READ_QUERY_METHOD, new StrandMetadata(false, trxContextProperties), - targetTypeWithIdFields, fields, includes, whereClause, - orderByClause, limitClause, groupByClause); + persistClient, Constants.RUN_READ_QUERY_METHOD, null, targetTypeWithIdFields, fields, includes, + whereClause, orderByClause, limitClause, groupByClause); if (result instanceof BStream bStream) { // stream return Utils.createPersistSQLStreamValue(bStream, targetType, fields, includes, typeDescriptions, persistClient, null); @@ -109,14 +98,12 @@ persistClient, Constants.RUN_READ_QUERY_METHOD, new StrandMetadata(false, trxCon static Object queryOne(Environment env, BObject client, BArray path, BTypedesc targetType) { // This method will return `targetType|persist:Error` - BString entity = getEntity(env); BObject persistClient = getPersistClient(client, entity); BArray keyFields = (BArray) persistClient.get(KEY_FIELDS); RecordType recordType = (RecordType) targetType.getDescribingType(); - Map trxContextProperties = getTransactionContextProperties(); RecordType recordTypeWithIdFields = getRecordTypeWithKeyFields(keyFields, recordType); BTypedesc targetTypeWithIdFields = ValueCreator.createTypedescValue(recordTypeWithIdFields); @@ -134,9 +121,8 @@ static Object queryOne(Environment env, BObject client, BArray path, BTypedesc t // string[] fields = [], string[] include = [], typedesc[] typeDescriptions = [] // )` // which returns `record {}|persist:Error` - getPersistClient(client, entity), Constants.RUN_READ_BY_KEY_QUERY_METHOD, - new StrandMetadata(false, trxContextProperties), targetType, targetTypeWithIdFields, key, - fields, includes, typeDescriptions); + getPersistClient(client, entity), Constants.RUN_READ_BY_KEY_QUERY_METHOD, null, targetType, + targetTypeWithIdFields, key, fields, includes, typeDescriptions); } catch (BError bError) { return wrapError(bError); } @@ -157,22 +143,13 @@ static Object executeNativeSQL(Environment env, BObject client, BObject paramSQL private static BStream queryNativeSQLBal(Environment env, BObject client, BObject paramSQLString, BTypedesc targetType) { // This method will return `stream` - BObject dbClient = (BObject) client.get(DB_CLIENT); - TransactionResourceManager trxResourceManager = TransactionResourceManager.getInstance(); - TransactionLocalContext currentTrxContext = trxResourceManager.getCurrentTransactionContext(); - return (BStream) env.yieldAndRun(() -> { try { - Map properties = null; - if (currentTrxContext != null) { - properties = Map.of(RuntimeConstants.CURRENT_TRANSACTION_CONTEXT_PROPERTY, currentTrxContext); - } Object result = env.getRuntime().callMethod( // Call `sqlClient.query(paramSQLString, targetType)` which returns // `stream` - dbClient, SQL_QUERY_METHOD, new StrandMetadata(false, properties), paramSQLString, - targetType); + dbClient, SQL_QUERY_METHOD, null, paramSQLString, targetType); // returned type is `stream` BStream sqlStream = (BStream) result; BObject persistNativeStream = createPersistNativeSQLStream(sqlStream, null); @@ -188,18 +165,11 @@ dbClient, SQL_QUERY_METHOD, new StrandMetadata(false, properties), paramSQLStrin private static Object executeNativeSQLBal(Environment env, BObject client, BObject paramSQLString) { BObject dbClient = (BObject) client.get(DB_CLIENT); - TransactionResourceManager trxResourceManager = TransactionResourceManager.getInstance(); - TransactionLocalContext currentTrxContext = trxResourceManager.getCurrentTransactionContext(); - return env.yieldAndRun(() -> { - Map properties = null; - if (currentTrxContext != null) { - properties = Map.of(RuntimeConstants.CURRENT_TRANSACTION_CONTEXT_PROPERTY, currentTrxContext); - } try { Object result = env.getRuntime().callMethod( // Call `sqlClient.execute(paramSQLString)` which returns `sql:ExecutionResult|sql:Error` - dbClient, SQL_EXECUTE_METHOD, new StrandMetadata(false, properties), paramSQLString); + dbClient, SQL_EXECUTE_METHOD, null, paramSQLString); if (result instanceof BMap map) { // returned type is `sql:ExecutionResult` return ValueCreator.createRecordValue(getModule(), PERSIST_EXECUTION_RESULT, (BMap) map);