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

com.scudata.parallel.RemoteCursor Maven / Gradle / Ivy

Go to download

SPL(Structured Process Language) A programming language specially for structured data computing.

There is a newer version: 20240823
Show newest version
package com.scudata.parallel;

import java.io.*;

import com.scudata.common.*;
import com.scudata.dm.*;
import com.scudata.dm.cursor.ICursor;

/**
 * Զ???α?
 * 
 * @author Joancy
 *
 */
public class RemoteCursor extends ICursor implements Serializable {
	private static final long serialVersionUID = 1L;
	String host;
	int port, taskId, proxyId;

	UnitClient unitClient = null;
	boolean isClosed = false;

	/**
	 * ????һ??Զ???α?
	 * @param host ????IP
	 * @param port ?˿ں?
	 * @param taskId ?????
	 * @param proxyId ?α??????
	 */
	public RemoteCursor(String host, int port, int taskId, int proxyId){
		this.host = host;
		this.port = port;
		this.taskId = taskId;
		this.proxyId = proxyId;
	}
	
	/**
	 * ????Զ???α?
	 * @param host ????IP
	 * @param port ?˿ں?
	 * @param proxyId ?α??????
	 */
	public RemoteCursor(String host, int port, int proxyId){
		this(host,port,-1,proxyId);
	}

	UnitClient getUnitClient() throws Exception {
		if (unitClient == null) {
			unitClient = new UnitClient(host, port);
			unitClient.connect();
		}
		return unitClient;
	}

	private Object executeMethod(String methodName, Object[] argValues) {
		try {
			UnitClient uc = getUnitClient();
			Request req = new Request(Request.CURSOR_METHOD);
			req.setAttr(Request.METHOD_TaskId, new Integer(taskId));
			req.setAttr(Request.METHOD_ProxyId, new Integer(proxyId));
			req.setAttr(Request.METHOD_MethodName, methodName);
			req.setAttr(Request.METHOD_ArgValues, argValues);

			Response res = uc.send(req);
			if (res.getError() != null) {
				throw res.getError();
			}
			if (res.getException() != null) {
				throw res.getException();
			}
			return res.getResult();
		} catch (Exception x) {
			throw new RQException("Execute 'RemoteCursor' method " + methodName
					+ " failed on " + unitClient + " for " + x.getMessage(), x);
		}
	}

	protected long skipOver(long n) {
		if( isClosed ) return 0;
		Long I = null;
		try {
			I = (Long) executeMethod("skip", new Object[] { new Long(n) });
			return I.longValue();
		} finally {
			if (I != null && I.longValue() < n) {
				close();
			}
		}
	}

	public synchronized void close() {
		if( isClosed ) return;
		try {
			executeMethod("close", null);
		} catch (Exception x) {
		} finally {
			unitClient.close();
			isClosed = true;
		}
	}

	protected Sequence get(int n) {
		if( isClosed ) return null;
		Sequence t = null;
		try {
			t = (Sequence) executeMethod("fetch", new Object[] { new Integer(n) });
			return t;
		} finally {
			if (t == null || t.length() < n) {
				close();
			}
		}
	}
	
	/**
	 * ȡ?α?????ݽṹ??Ϣ
	 * @return ???ݽṹ
	 */
	public DataStruct getDataStruct() {
		if(dataStruct!=null){
			return dataStruct;
		}
		if( isClosed ) return dataStruct;
		
		dataStruct = (DataStruct) executeMethod("getDataStruct", null);
		return dataStruct;
	}
	

	/**
	 * ʵ??toString???ı?????
	 */
	public String toString() {
		return "RemoteCursor@" + host + ":" + port + " cursorId:" + proxyId;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy