Search in sources :

Example 1 with Executor

use of com.aerospike.client.command.Executor in project aerospike-client-java by aerospike.

the class AerospikeClient method get.

//-------------------------------------------------------
// Batch Read Operations
//-------------------------------------------------------
/**
	 * Read multiple records for specified batch keys in one batch call.
	 * This method allows different namespaces/bins to be requested for each key in the batch.
	 * The returned records are located in the same list.
	 * If the BatchRead key field is not found, the corresponding record field will be null.
	 * The policy can be used to specify timeouts and maximum concurrent threads.
	 * This method requires Aerospike Server version >= 3.6.0.
	 * 
	 * @param policy				batch configuration parameters, pass in null for defaults
	 * @param records				list of unique record identifiers and the bins to retrieve.
	 *                              The returned records are located in the same list.
	 * @throws AerospikeException	if read fails
	 */
public final void get(BatchPolicy policy, List<BatchRead> records) throws AerospikeException {
    if (records.size() == 0) {
        return;
    }
    if (policy == null) {
        policy = batchPolicyDefault;
    }
    List<BatchNode> batchNodes = BatchNode.generateList(cluster, policy, records);
    if (policy.maxConcurrentThreads == 1 || batchNodes.size() <= 1) {
        // Run batch requests sequentially in same thread.
        for (BatchNode batchNode : batchNodes) {
            if (!batchNode.node.hasBatchIndex()) {
                throw new AerospikeException(ResultCode.PARAMETER_ERROR, "Requested command requires a server that supports new batch index protocol.");
            }
            MultiCommand command = new Batch.ReadListCommand(batchNode, policy, records);
            command.execute(cluster, policy, null, batchNode.node, true);
        }
    } else {
        // Run batch requests in parallel in separate threads.
        //			
        // Multiple threads write to the record list, so one might think that
        // volatile or memory barriers are needed on the write threads and this read thread.
        // This should not be necessary here because it happens in Executor which does a 
        // volatile write (completedCount.incrementAndGet()) at the end of write threads
        // and a synchronized waitTillComplete() in this thread.
        Executor executor = new Executor(cluster, policy, batchNodes.size());
        for (BatchNode batchNode : batchNodes) {
            if (!batchNode.node.hasBatchIndex()) {
                throw new AerospikeException(ResultCode.PARAMETER_ERROR, "Requested command requires a server that supports new batch index protocol.");
            }
            MultiCommand command = new Batch.ReadListCommand(batchNode, policy, records);
            executor.addCommand(batchNode.node, command);
        }
        executor.execute(policy.maxConcurrentThreads);
    }
}
Also used : AsyncQueryExecutor(com.aerospike.client.async.AsyncQueryExecutor) BatchExecutor(com.aerospike.client.command.BatchExecutor) QueryRecordExecutor(com.aerospike.client.query.QueryRecordExecutor) QueryAggregateExecutor(com.aerospike.client.query.QueryAggregateExecutor) AsyncScanExecutor(com.aerospike.client.async.AsyncScanExecutor) Executor(com.aerospike.client.command.Executor) BatchNode(com.aerospike.client.command.BatchNode) MultiCommand(com.aerospike.client.command.MultiCommand)

Example 2 with Executor

use of com.aerospike.client.command.Executor in project aerospike-client-java by aerospike.

the class AerospikeClient method scanAll.

//-------------------------------------------------------
// Scan Operations
//-------------------------------------------------------
/**
	 * Read all records in specified namespace and set.  If the policy's
	 * <code>concurrentNodes</code> is specified, each server node will be read in
	 * parallel.  Otherwise, server nodes are read in series.
	 * <p>
	 * This call will block until the scan is complete - callbacks are made
	 * within the scope of this call.
	 * 
	 * @param policy				scan configuration parameters, pass in null for defaults
	 * @param namespace				namespace - equivalent to database name
	 * @param setName				optional set name - equivalent to database table
	 * @param callback				read callback method - called with record data
	 * @param binNames				optional bin to retrieve. All bins will be returned if not specified.
	 * 								Aerospike 2 servers ignore this parameter.
	 * @throws AerospikeException	if scan fails
	 */
public final void scanAll(ScanPolicy policy, String namespace, String setName, ScanCallback callback, String... binNames) throws AerospikeException {
    if (policy == null) {
        policy = scanPolicyDefault;
    }
    if (policy.scanPercent <= 0 || policy.scanPercent > 100) {
        throw new AerospikeException(ResultCode.PARAMETER_ERROR, "Invalid scan percent: " + policy.scanPercent);
    }
    Node[] nodes = cluster.getNodes();
    if (nodes.length == 0) {
        throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Scan failed because cluster is empty.");
    }
    if (policy.concurrentNodes) {
        Executor executor = new Executor(cluster, policy, nodes.length);
        long taskId = RandomShift.instance().nextLong();
        for (Node node : nodes) {
            ScanCommand command = new ScanCommand(policy, namespace, setName, callback, binNames, taskId);
            executor.addCommand(node, command);
        }
        executor.execute(policy.maxConcurrentNodes);
    } else {
        for (Node node : nodes) {
            scanNode(policy, node, namespace, setName, callback, binNames);
        }
    }
}
Also used : AsyncQueryExecutor(com.aerospike.client.async.AsyncQueryExecutor) BatchExecutor(com.aerospike.client.command.BatchExecutor) QueryRecordExecutor(com.aerospike.client.query.QueryRecordExecutor) QueryAggregateExecutor(com.aerospike.client.query.QueryAggregateExecutor) AsyncScanExecutor(com.aerospike.client.async.AsyncScanExecutor) Executor(com.aerospike.client.command.Executor) ScanCommand(com.aerospike.client.command.ScanCommand) Node(com.aerospike.client.cluster.Node) BatchNode(com.aerospike.client.command.BatchNode)

Example 3 with Executor

use of com.aerospike.client.command.Executor in project aerospike-client-java by aerospike.

the class AerospikeClient method execute.

//----------------------------------------------------------
// Query/Execute UDF
//----------------------------------------------------------
/**
	 * Apply user defined function on records that match the statement filter.
	 * Records are not returned to the client.
	 * This asynchronous server call will return before command is complete.
	 * The user can optionally wait for command completion by using the returned
	 * ExecuteTask instance.
	 * 
	 * @param policy				write configuration parameters, pass in null for defaults
	 * @param statement				record filter
	 * @param packageName			server package where user defined function resides
	 * @param functionName			function name
	 * @param functionArgs			to pass to function name, if any
	 * @throws AerospikeException	if command fails
	 */
public final ExecuteTask execute(WritePolicy policy, Statement statement, String packageName, String functionName, Value... functionArgs) throws AerospikeException {
    if (policy == null) {
        policy = writePolicyDefault;
    }
    statement.setAggregateFunction(packageName, functionName, functionArgs);
    statement.prepare(false);
    Node[] nodes = cluster.getNodes();
    if (nodes.length == 0) {
        throw new AerospikeException(ResultCode.SERVER_NOT_AVAILABLE, "Command failed because cluster is empty.");
    }
    Executor executor = new Executor(cluster, policy, nodes.length);
    for (Node node : nodes) {
        ServerCommand command = new ServerCommand(policy, statement);
        executor.addCommand(node, command);
    }
    executor.execute(nodes.length);
    return new ExecuteTask(cluster, policy, statement);
}
Also used : AsyncQueryExecutor(com.aerospike.client.async.AsyncQueryExecutor) BatchExecutor(com.aerospike.client.command.BatchExecutor) QueryRecordExecutor(com.aerospike.client.query.QueryRecordExecutor) QueryAggregateExecutor(com.aerospike.client.query.QueryAggregateExecutor) AsyncScanExecutor(com.aerospike.client.async.AsyncScanExecutor) Executor(com.aerospike.client.command.Executor) Node(com.aerospike.client.cluster.Node) BatchNode(com.aerospike.client.command.BatchNode) ExecuteTask(com.aerospike.client.task.ExecuteTask) ServerCommand(com.aerospike.client.query.ServerCommand)

Aggregations

AsyncQueryExecutor (com.aerospike.client.async.AsyncQueryExecutor)3 AsyncScanExecutor (com.aerospike.client.async.AsyncScanExecutor)3 BatchExecutor (com.aerospike.client.command.BatchExecutor)3 BatchNode (com.aerospike.client.command.BatchNode)3 Executor (com.aerospike.client.command.Executor)3 QueryAggregateExecutor (com.aerospike.client.query.QueryAggregateExecutor)3 QueryRecordExecutor (com.aerospike.client.query.QueryRecordExecutor)3 Node (com.aerospike.client.cluster.Node)2 MultiCommand (com.aerospike.client.command.MultiCommand)1 ScanCommand (com.aerospike.client.command.ScanCommand)1 ServerCommand (com.aerospike.client.query.ServerCommand)1 ExecuteTask (com.aerospike.client.task.ExecuteTask)1