Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary metadata objects for same strand calls #96

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,21 +31,16 @@
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;
import static io.ballerina.stdlib.persist.Utils.getKey;
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;
Expand All @@ -69,16 +62,13 @@ private SQLProcessor() {
static BStream query(Environment env, BObject client, BTypedesc targetType, BObject whereClause,
BObject orderByClause, BObject limitClause, BObject groupByClause) {
// This method will return `stream<targetType, persist:Error?>`

BString entity = getEntity(env);
BObject persistClient = getPersistClient(client, entity);
BArray keyFields = (BArray) persistClient.get(KEY_FIELDS);
RecordType recordType = (RecordType) targetType.getDescribingType();

RecordType recordTypeWithIdFields = getRecordTypeWithKeyFields(keyFields, recordType);
BTypedesc targetTypeWithIdFields = ValueCreator.createTypedescValue(recordTypeWithIdFields);

Map<String, Object> trxContextProperties = getTransactionContextProperties();
BArray[] metadata = getMetadata(recordType);
BArray fields = metadata[0];
BArray includes = metadata[1];
Expand All @@ -90,9 +80,8 @@ static BStream query(Environment env, BObject client, BTypedesc targetType, BObj
// typedesc<record {}> rowType, string[] fields = [], string[] include = []
// )`
// which returns `stream<record {}, sql:Error?>|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<record {}, sql:Error?>
return Utils.createPersistSQLStreamValue(bStream, targetType, fields, includes, typeDescriptions,
persistClient, null);
Expand All @@ -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<String, Object> trxContextProperties = getTransactionContextProperties();
RecordType recordTypeWithIdFields = getRecordTypeWithKeyFields(keyFields, recordType);
BTypedesc targetTypeWithIdFields = ValueCreator.createTypedescValue(recordTypeWithIdFields);

Expand All @@ -134,9 +121,8 @@ static Object queryOne(Environment env, BObject client, BArray path, BTypedesc t
// string[] fields = [], string[] include = [], typedesc<record {}>[] 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);
}
Expand All @@ -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<targetType, persist:Error?>`

BObject dbClient = (BObject) client.get(DB_CLIENT);
TransactionResourceManager trxResourceManager = TransactionResourceManager.getInstance();
TransactionLocalContext currentTrxContext = trxResourceManager.getCurrentTransactionContext();

return (BStream) env.yieldAndRun(() -> {
try {
Map<String, Object> 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<targetType, sql:Error?>`
dbClient, SQL_QUERY_METHOD, new StrandMetadata(false, properties), paramSQLString,
targetType);
dbClient, SQL_QUERY_METHOD, null, paramSQLString, targetType);
// returned type is `stream<record {}, sql:Error?>`
BStream sqlStream = (BStream) result;
BObject persistNativeStream = createPersistNativeSQLStream(sqlStream, null);
Expand All @@ -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<String, Object> 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<BString,
Object>) map);
Expand Down
Loading