All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.datarouter.client.mysql.op.read.MysqlGetOpExecutor Maven / Gradle / Ivy

There is a newer version: 0.0.125
Show newest version
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.client.mysql.op.read;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

import io.datarouter.client.mysql.MysqlClientType;
import io.datarouter.client.mysql.sql.MysqlSqlFactory;
import io.datarouter.model.databean.Databean;
import io.datarouter.model.field.Field;
import io.datarouter.model.field.FieldSet;
import io.datarouter.model.key.primary.PrimaryKey;
import io.datarouter.model.serialize.fielder.DatabeanFielder;
import io.datarouter.scanner.Scanner;
import io.datarouter.storage.config.Config;
import io.datarouter.storage.serialize.fieldcache.PhysicalDatabeanFieldInfo;
import io.datarouter.storage.util.DatarouterCounters;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class MysqlGetOpExecutor{

	private static final int BATCH_SIZE = 100;

	@Inject
	private MysqlSqlFactory mysqlSqlFactory;
	@Inject
	private MysqlClientType mysqlClientType;

	public ,
			D extends Databean,
			F extends DatabeanFielder,
			T extends Comparable>
	List execute(
			PhysicalDatabeanFieldInfo databeanFieldInfo,
			String opName,
			Collection> keys,
			Config config,
			List> selectFields,
			Function> select,
			Connection connection,
			String indexName){
		Stream> dedupedSortedKeys = keys.stream()
				.distinct()
				.sorted();
		String tableName = databeanFieldInfo.getTableName();
		String clientName = databeanFieldInfo.getClientId().getName();
		String nodeName = databeanFieldInfo.getNodeName() + "." + indexName;
		boolean disableIntroducer = databeanFieldInfo.getDisableIntroducer();
		List result = new ArrayList<>(keys.size());
		for(List> keyBatch : Scanner.of(dedupedSortedKeys)
				.batch(config.findRequestBatchSize().orElse(BATCH_SIZE))
				.iterable()){
			PreparedStatement ps = mysqlSqlFactory
					.createSql(databeanFieldInfo.getClientId(), tableName, disableIntroducer)
					.getMulti(tableName, config, selectFields, keyBatch, indexName)
					.prepare(connection);
			DatarouterCounters.incClientNodeCustom(mysqlClientType, opName + " selects", clientName, nodeName, 1L);
			List resultBatch = select.apply(ps);
			DatarouterCounters.incClientNodeCustom(mysqlClientType, opName + " rows", clientName, nodeName, result
					.size());
			result.addAll(resultBatch);
		}
		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy