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

com.scudata.expression.mfn.dw.Append Maven / Gradle / Ivy

Go to download

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

There is a newer version: 20241126
Show newest version
package com.scudata.expression.mfn.dw;

import java.io.File;
import java.io.IOException;

import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.dm.FileObject;
import com.scudata.dm.Sequence;
import com.scudata.dm.cursor.ICursor;
import com.scudata.dm.cursor.MultipathCursors;
import com.scudata.dw.ColPhyTable;
import com.scudata.dw.ComTable;
import com.scudata.dw.IPhyTable;
import com.scudata.dw.PhyTableGroup;
import com.scudata.expression.PhyTableFunction;
import com.scudata.resources.EngineMessage;

/**
 * ׷???α????ݵ??????
 * T.append(cs)
 * @author RunQian
 *
 */
public class Append extends PhyTableFunction {
	public Object calculate(Context ctx) {
		if (param == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("append" + mm.getMessage("function.missingParam"));
		} else if (!param.isLeaf()) {
			MessageManager mm = EngineMessage.get();
			throw new RQException("append" + mm.getMessage("function.invalidParam"));
		}
		
		if (table instanceof ColPhyTable) {
			ColPhyTable colTable = (ColPhyTable) table;
			if (!colTable.getGroupTable().isPureFormat()) {
				MessageManager mm = EngineMessage.get();
				throw new RQException(mm.getMessage("dw.oldVersion2"));
			}
		}
		
		Object obj = param.getLeafExpression().calculate(ctx);
		ICursor cursor;
		if (obj instanceof ICursor) {
			cursor = (ICursor)obj;
		} else if (obj instanceof Sequence) {
			cursor = ((Sequence)obj).cursor();
		} else if (obj == null) {
			return table;
		} else {
			MessageManager mm = EngineMessage.get();
			throw new RQException("append" + mm.getMessage("function.paramTypeError"));
		}
		
		try {
			synchronized(table) {
				if (option != null && option.indexOf('y') != -1) {
					PhyTableGroup tg = new PhyTableGroup(null, new IPhyTable[] {table}, null, null, ctx);
					Sequence seq = cursor.fetch();
					tg.setMemoryTable(seq);
					return tg;
				}
				
				if (option != null && option.indexOf('p') != -1 && cursor instanceof MultipathCursors) {
					parallelAppend((MultipathCursors) cursor, ctx);
				} else {
					table.append(cursor, option);
				}
			}
		} catch (IOException e) {
			throw new RQException(e.getMessage(), e);
		}
		
		return table;
	}
	
	private void parallelAppend(MultipathCursors mcs, Context ctx) throws IOException {
		int num = mcs.getPathCount();
		ColPhyTable[] tables = new ColPhyTable[num];
		File[] files = new File[num];
		Thread[] threads = new Thread[num];
		
		tables[0] = (ColPhyTable) table;
		for (int i = 1; i < num; i++) {
			FileObject tmp = FileObject.createTempFileObject();
			files[i] = tmp.getLocalFile().file();
			((ColPhyTable)table).getGroupTable().reset(files[i], "S", null, null);
			tables[i] = (ColPhyTable) ComTable.openBaseTable(files[i], ctx);
		}
		
		for (int i = 0; i < num; i++) {
			threads[i] = newAppendThread(tables[i], mcs.getPathCursor(i), option);
			threads[i].start();
		}
		for (int i = 0; i < num; i++) {
			try {
				threads[i].join();
			} catch (InterruptedException e) {
				throw new RQException(e.getMessage(), e);
			}
		}
		
		for (int i = 1; i < num; i++) {
			table.append(tables[i]);
			tables[i].getGroupTable().delete();
		}
	}
	
	public static Thread newAppendThread(final ColPhyTable table, final ICursor cs, final String option) {
		return new Thread() {
			public void run() {
				try {
					table.append(cs, option);
				} catch (IOException e) {
					throw new RQException(e.getMessage(), e);
				}
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy