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

com.scudata.expression.mfn.dw.CreateCursor 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.expression.mfn.dw;

import java.util.ArrayList;

import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.Context;
import com.scudata.dm.Env;
import com.scudata.dm.Sequence;
import com.scudata.dm.cursor.ConjxCursor;
import com.scudata.dm.cursor.ICursor;
import com.scudata.dm.cursor.MergeCursor;
import com.scudata.dm.cursor.MergeCursor2;
import com.scudata.dm.cursor.MultipathCursors;
import com.scudata.dm.cursor.UpdateMergeCursor;
import com.scudata.dw.Cursor;
import com.scudata.dw.IDWCursor;
import com.scudata.dw.IPhyTable;
import com.scudata.dw.RvsCursor;
import com.scudata.expression.Expression;
import com.scudata.expression.IParam;
import com.scudata.expression.Node;
import com.scudata.expression.ParamInfo2;
import com.scudata.expression.PhyTableFunction;
import com.scudata.expression.operator.And;
import com.scudata.resources.EngineMessage;

/**
 * ????????α?
 * T.cursor(x:C,??;w;k:n)
 * @author RunQian
 *
 */
public class CreateCursor extends PhyTableFunction {
	public Object calculate(Context ctx) {
		ICursor cs = createCursor(table, param, option, ctx);
		if (option != null && option.indexOf('x') != -1) {
			setOptionX(cs, option);
		}
		if (option != null && option.indexOf('z') != -1) {
			return rvs(cs);
		}
		return cs;
	}
	
	private static void parseFilterParam(IParam param, ArrayList expList, 
			ArrayList fieldList, ArrayList codeList, ArrayList optList, Context ctx) {
		if (param == null) {
		} else if (param.isLeaf()) {
			expList.add(param.getLeafExpression());
		} else if (param.getType() == IParam.Colon) {
			int subSize = param.getSubSize();
			if (subSize > 3) {
				MessageManager mm = EngineMessage.get();
				throw new RQException("cursor" + mm.getMessage("function.invalidParam"));						
			}
			
			IParam sub0 = param.getSub(0);
			IParam sub1 = param.getSub(1);
			if (sub0 == null || sub1 == null) {
				MessageManager mm = EngineMessage.get();
				throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
			}
			
			String fkName = sub0.getLeafExpression().getIdentifierName();
			fieldList.add(fkName);
			Object val = sub1.getLeafExpression().calculate(ctx);
			if (val instanceof Sequence) {
				codeList.add((Sequence)val);
			} else if (val == null) {
				codeList.add(new Sequence());
			} else {
				MessageManager mm = EngineMessage.get();
				throw new RQException("cursor" + mm.getMessage("function.paramTypeError"));
			}
			
			if (subSize > 2) {
				IParam sub2 = param.getSub(2);
				if (sub2 != null) {
					String opt = sub2.getLeafExpression().toString();
					optList.add(opt);
				} else {
					optList.add(null);
				}
			} else {
				optList.add(null);
			}
		}
	}
	
	//???α?????
	private static ICursor rvs(ICursor src) {
		if (src instanceof Cursor) {
			Cursor cs = (Cursor) src;
			if (!cs.hasModify() && cs.getGathers() == null) {
				return new RvsCursor(cs);
			}
		}
		MessageManager mm = EngineMessage.get();
		throw new RQException("cursor" + mm.getMessage("function.invalidParam"));		
	}
	
	public static void setOptionX(ICursor cs, String opt) {
		if (cs instanceof IDWCursor) {
			((IDWCursor) cs).setOption(opt);
		} else if (cs instanceof MultipathCursors) {
			MultipathCursors mcs = (MultipathCursors) cs;
			ICursor[] cursors = mcs.getCursors();
			for (ICursor cursor : cursors) {
				setOptionX(cursor, opt);
			}
		} else if (cs instanceof MergeCursor2) {
			MergeCursor2 mcs = (MergeCursor2) cs;
			setOptionX(mcs.getCursor1(), opt);
			setOptionX(mcs.getCursor2(), opt);
		} else if (cs instanceof MergeCursor) {
			MergeCursor mcs = (MergeCursor) cs;
			ICursor[] cursors = mcs.getCursors();
			for (ICursor cursor : cursors) {
				setOptionX(cursor, opt);
			}
		} else if (cs instanceof ConjxCursor) {
			ConjxCursor mcs = (ConjxCursor) cs;
			ICursor[] cursors = mcs.getCursors();
			for (ICursor cursor : cursors) {
				setOptionX(cursor, opt);
			}
		} else if (cs instanceof UpdateMergeCursor) {
			((UpdateMergeCursor) cs).setOption(opt);
		}
	}
	
	public static ICursor createCursor(IPhyTable table, IParam param, String opt, Context ctx) {
		// ?Ƿ??????·?α?
		boolean isMultiThread = opt != null && opt.indexOf('m') != -1;
		
		if (param == null && !isMultiThread) {
			return table.cursor(null, null, null, null, null, null, opt, ctx);
		}
		
		IParam fieldParam = null; // ѡ???ֶβ???
		Expression filter = null; // ????????
		
		// ?????????ֶκ͹?????ά??
		String []fkNames = null;
		Sequence []codes = null;
		String []opts = null;
		
		MultipathCursors mcs = null; // ͬ???ֶ??α?
		int segSeq = 0;
		int segCount = 0;
		if (isMultiThread) {
			segCount = Env.getCursorParallelNum();
		}
		
		if (param != null && param.getType() == IParam.Semicolon) {
			int size = param.getSubSize();
			if (size > 3) {
				MessageManager mm = EngineMessage.get();
				throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
			}
			
			fieldParam = param.getSub(0);
			IParam expParam = param.getSub(1);
			if (expParam == null) {
			} else if (expParam.isLeaf()) {
				filter = expParam.getLeafExpression();
			} else {
				ArrayList expList = new ArrayList();
				ArrayList fieldList = new ArrayList();
				ArrayList codeList = new ArrayList();
				ArrayList optList = new ArrayList();
				
				if (expParam.getType() == IParam.Colon) {
					parseFilterParam(expParam, expList, fieldList, codeList, optList, ctx);
				} else {
					for (int p = 0, psize = expParam.getSubSize(); p < psize; ++p) {
						IParam sub = expParam.getSub(p);
						parseFilterParam(sub, expList, fieldList, codeList, optList, ctx);
					}
				}
				
				int fieldCount = fieldList.size();
				if (fieldCount > 0) {
					fkNames = new String[fieldCount];
					codes = new Sequence[fieldCount];
					opts = new String[fieldCount];
					fieldList.toArray(fkNames);
					codeList.toArray(codes);
					optList.toArray(opts);
				}
				
				int expCount = expList.size();
				if (expCount == 1) {
					filter = expList.get(0);
				} else if (expCount > 1) {
					Expression exp = expList.get(0);
					Node home = exp.getHome();
					for (int i = 1; i < expCount; ++i) {
						exp = expList.get(i);
						And and = new And();
						and.setLeft(home);
						and.setRight(exp.getHome());
						home = and;
					}
					
					filter = new Expression(home);
				}
			}
			
			if (size > 2) {
				IParam segParam = param.getSub(2);
				if (segParam == null) {
				} else if (segParam.isLeaf()) {
					Object obj = segParam.getLeafExpression().calculate(ctx);
					if (obj instanceof MultipathCursors) {
						mcs = (MultipathCursors)obj;
					} else if (obj instanceof ICursor) {
						// cursor@m????????Ϊ?ջ????????????Ŀ?С??2???ܲ??᷵?ض?·?α?
						isMultiThread = false;
					} else {
						if (!isMultiThread) {
							MessageManager mm = EngineMessage.get();
							throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
						}
						
						if (!(obj instanceof Number)) {
							MessageManager mm = EngineMessage.get();
							throw new RQException("cursor" + mm.getMessage("function.paramTypeError"));
						}

						segCount = ((Number)obj).intValue();
					}
				} else {
					if (segParam.getSubSize() != 2) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
					}
					
					IParam sub0 = segParam.getSub(0);
					IParam sub1 = segParam.getSub(1);
					if (sub0 == null || sub1 == null) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
					}
					
					Object obj = sub0.getLeafExpression().calculate(ctx);
					if (!(obj instanceof Number)) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("cursor" + mm.getMessage("function.paramTypeError"));
					}

					segSeq = ((Number)obj).intValue();
					obj = sub1.getLeafExpression().calculate(ctx);
					if (!(obj instanceof Number)) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("cursor" + mm.getMessage("function.paramTypeError"));
					}

					segCount = ((Number)obj).intValue();
					if (segSeq < 1 || segSeq > segCount) {
						MessageManager mm = EngineMessage.get();
						throw new RQException("cursor" + mm.getMessage("function.invalidParam"));
					}
					
					isMultiThread = false;
				}
			}
		} else {
			fieldParam = param;
		}
		
		Expression []exps = null;
		String []names = null;
		if (fieldParam != null) {
			ParamInfo2 pi = ParamInfo2.parse(fieldParam, "cursor", false, false);
			exps = pi.getExpressions1();
			names = pi.getExpressionStrs2();
			
			int len = exps.length;
			for (int i = 0; i < len; i++) {
				if (names[i] == null && exps[i] != null) {
					names[i] = exps[i].getIdentifierName();
				}
			}
		}
		
		if (mcs != null) {
			return table.cursor(exps, names, filter, fkNames, codes, opts, mcs, opt, ctx);
		} else if (isMultiThread && segCount > 1) {
			return table.cursor(exps, names, filter, fkNames, codes, opts, segCount, opt, ctx);
		} else {
			if (segSeq < 1) {
				return table.cursor(exps, names, filter, fkNames, codes, opts, opt, ctx);
			} else {
				return table.cursor(exps, names, filter, fkNames, codes, opts, segSeq, segCount, opt, ctx);
			}
		}
	}
	
	public boolean isLeftTypeMatch(Object obj) {
		if (obj instanceof IPhyTable) {
			if (option != null && option.indexOf('z') != -1)
				return true;
			if (option != null && option.indexOf('v') != -1)
				return false;
			return true;
		}
		
		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy