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

com.aerospike.client.proxy.ExecuteCommandProxy Maven / Gradle / Ivy

There is a newer version: 8.1.4
Show newest version
/*
 * Copyright 2012-2023 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.proxy;

import java.util.Map;

import com.aerospike.client.AerospikeException;
import com.aerospike.client.Key;
import com.aerospike.client.Record;
import com.aerospike.client.Value;
import com.aerospike.client.command.Command;
import com.aerospike.client.listener.ExecuteListener;
import com.aerospike.client.policy.WritePolicy;
import com.aerospike.client.proxy.grpc.GrpcCallExecutor;
import com.aerospike.proxy.client.KVSGrpc;

public final class ExecuteCommandProxy extends ReadCommandProxy {
	private final ExecuteListener executeListener;
	private final WritePolicy writePolicy;
	private final Key key;
	private final String packageName;
	private final String functionName;
	private final Value[] args;

	public ExecuteCommandProxy(
		GrpcCallExecutor executor,
		ExecuteListener executeListener,
		WritePolicy writePolicy,
		Key key,
		String packageName,
		String functionName,
		Value[] args
	) {
		super(KVSGrpc.getExecuteStreamingMethod(), executor, null, writePolicy, key, false);
		this.executeListener = executeListener;
		this.writePolicy = writePolicy;
		this.key = key;
		this.packageName = packageName;
		this.functionName = functionName;
		this.args = args;
	}

	@Override
	void writeCommand(Command command) {
		command.setUdf(writePolicy, key, packageName, functionName, args);
	}

	@Override
	void parseResult(Parser parser) {
		Record record = parseRecordResult(parser);
		Object obj = parseEndResult(record);

		try {
			executeListener.onSuccess(key, obj);
		}
		catch (Throwable t) {
			logOnSuccessError(t);
		}
	}

	private static Object parseEndResult(Record record) {
		if (record == null || record.bins == null) {
			return null;
		}

		Map map = record.bins;

		Object obj = map.get("SUCCESS");

		if (obj != null) {
			return obj;
		}

		// User defined functions don't have to return a value.
		if (map.containsKey("SUCCESS")) {
			return null;
		}

		obj = map.get("FAILURE");

		if (obj != null) {
			throw new AerospikeException(obj.toString());
		}
		throw new AerospikeException("Invalid UDF return value");
	}

	@Override
	protected void handleNotFound(int resultCode) {
		throw new AerospikeException(resultCode);
	}

	@Override
	void onFailure(AerospikeException ae) {
		executeListener.onFailure(ae);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy