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

com.aerospike.client.command.Batch Maven / Gradle / Ivy

/*
 * Copyright 2012-2019 Aerospike, Inc.
 *
 * Portions may be licensed to Aerospike, Inc. under one or more contributor
 * license agreements WHICH ARE COMPATIBLE WITH THE APACHE LICENSE, VERSION 2.0.
 *
 * 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 com.aerospike.client.command;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.BatchRead;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.cluster.Cluster;
import com.aerospike.client.policy.BatchPolicy;
import com.aerospike.client.policy.Replica;

public final class Batch {
	//-------------------------------------------------------
	// ReadList
	//-------------------------------------------------------

	public static final class ReadListCommand extends MultiCommand {
		private final Executor parent;
		private final BatchNode batch;
		private final BatchPolicy policy;
		private final List records;

		public ReadListCommand(Executor parent, BatchNode batch, BatchPolicy policy, List records) {
			super(false);
			this.parent = parent;
			this.batch = batch;
			this.policy = policy;
			this.records = records;
		}

		@Override
		protected void writeBuffer() {
			setBatchRead(policy, records, batch);
		}

		@Override
		protected void parseRow(Key key) throws IOException {
			BatchRead record = records.get(batchIndex);

			if (Arrays.equals(key.digest, record.key.digest)) {
				if (resultCode == 0) {
					record.record = parseRecord();
				}
			}
			else {
				throw new AerospikeException.Parse("Unexpected batch key returned: " + key.namespace + ',' + Buffer.bytesToHexString(key.digest) + ',' + batchIndex);
			}
		}

		@Override
		protected boolean shouldRetryBatch() {
			return (policy.replica == Replica.SEQUENCE || policy.replica == Replica.PREFER_RACK) && (parent == null || ! parent.isDone());
		}

		@Override
		protected boolean retryBatch(Cluster cluster, int socketTimeout, int totalTimeout, long deadline, int iteration, int commandSentCounter) {
			// Retry requires keys for this node to be split among other nodes.
			// This is both recursive and exponential.
			List batchNodes = BatchNode.generateList(cluster, policy, records, sequence, batch);

			if (batchNodes.size() == 1 && batchNodes.get(0).node == batch.node) {
				// Batch node is the same.  Go through normal retry.
				return false;
			}

			// Run batch requests sequentially in same thread.
			for (BatchNode batchNode : batchNodes) {
				MultiCommand command = new ReadListCommand(parent, batchNode, policy, records);
				command.sequence = sequence;
				command.execute(cluster, policy, null, batchNode.node, true, socketTimeout, totalTimeout, deadline, iteration, commandSentCounter);
			}
			return true;
		}
	}

	//-------------------------------------------------------
	// GetArray
	//-------------------------------------------------------

	public static final class GetArrayCommand extends MultiCommand {
		private final Executor parent;
		private final BatchNode batch;
		private final BatchPolicy policy;
		private final Key[] keys;
		private final String[] binNames;
		private final Record[] records;
		private final int readAttr;

		public GetArrayCommand(
			Executor parent,
			BatchNode batch,
			BatchPolicy policy,
			Key[] keys,
			String[] binNames,
			Record[] records,
			int readAttr
		) {
			super(false);
			this.parent = parent;
			this.batch = batch;
			this.policy = policy;
			this.keys = keys;
			this.binNames = binNames;
			this.records = records;
			this.readAttr = readAttr;
		}

		@Override
		protected void writeBuffer() {
			setBatchRead(policy, keys, batch, binNames, readAttr);
		}

		@Override
		protected void parseRow(Key key) throws IOException {
			if (Arrays.equals(key.digest, keys[batchIndex].digest)) {
				if (resultCode == 0) {
					records[batchIndex] = parseRecord();
				}
			}
			else {
				throw new AerospikeException.Parse("Unexpected batch key returned: " + key.namespace + ',' + Buffer.bytesToHexString(key.digest) + ',' + batchIndex);
			}
		}

		@Override
		protected boolean shouldRetryBatch() {
			return (policy.replica == Replica.SEQUENCE || policy.replica == Replica.PREFER_RACK) && (parent == null || ! parent.isDone());
		}

		@Override
		protected boolean retryBatch(Cluster cluster, int socketTimeout, int totalTimeout, long deadline, int iteration, int commandSentCounter) {
			// Retry requires keys for this node to be split among other nodes.
			// This is both recursive and exponential.
			List batchNodes = BatchNode.generateList(cluster, policy, keys, sequence, batch);

			if (batchNodes.size() == 1 && batchNodes.get(0).node == batch.node) {
				// Batch node is the same.  Go through normal retry.
				return false;
			}

			// Run batch requests sequentially in same thread.
			for (BatchNode batchNode : batchNodes) {
				MultiCommand command = new GetArrayCommand(parent, batchNode, policy, keys, binNames, records, readAttr);
				command.sequence = sequence;
				command.execute(cluster, policy, null, batchNode.node, true, socketTimeout, totalTimeout, deadline, iteration, commandSentCounter);
			}
			return true;
		}
	}

	//-------------------------------------------------------
	// ExistsArray
	//-------------------------------------------------------

	public static final class ExistsArrayCommand extends MultiCommand {
		private final Executor parent;
		private final BatchNode batch;
		private final BatchPolicy policy;
		private final Key[] keys;
		private final boolean[] existsArray;

		public ExistsArrayCommand(
			Executor parent,
			BatchNode batch,
			BatchPolicy policy,
			Key[] keys,
			boolean[] existsArray
		) {
			super(false);
			this.parent = parent;
			this.batch = batch;
			this.policy = policy;
			this.keys = keys;
			this.existsArray = existsArray;
		}

		@Override
		protected void writeBuffer() {
			setBatchRead(policy, keys, batch, null, Command.INFO1_READ | Command.INFO1_NOBINDATA);
		}

		@Override
		protected void parseRow(Key key) throws IOException {
			if (opCount > 0) {
				throw new AerospikeException.Parse("Received bins that were not requested!");
			}

			if (Arrays.equals(key.digest, keys[batchIndex].digest)) {
				existsArray[batchIndex] = resultCode == 0;
			}
			else {
				throw new AerospikeException.Parse("Unexpected batch key returned: " + key.namespace + ',' + Buffer.bytesToHexString(key.digest) + ',' + batchIndex);
			}
		}

		@Override
		protected boolean shouldRetryBatch() {
			return (policy.replica == Replica.SEQUENCE || policy.replica == Replica.PREFER_RACK) && (parent == null || ! parent.isDone());
		}

		@Override
		protected boolean retryBatch(Cluster cluster, int socketTimeout, int totalTimeout, long deadline, int iteration, int commandSentCounter) {
			// Retry requires keys for this node to be split among other nodes.
			// This is both recursive and exponential.
			List batchNodes = BatchNode.generateList(cluster, policy, keys, sequence, batch);

			if (batchNodes.size() == 1 && batchNodes.get(0).node == batch.node) {
				// Batch node is the same.  Go through normal retry.
				return false;
			}

			// Run batch requests sequentially in same thread.
			for (BatchNode batchNode : batchNodes) {
				MultiCommand command = new ExistsArrayCommand(parent, batchNode, policy, keys, existsArray);
				command.sequence = sequence;
				command.execute(cluster, policy, null, batchNode.node, true, socketTimeout, totalTimeout, deadline, iteration, commandSentCounter);
			}
			return true;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy