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

com.scudata.expression.mfn.file.Reset 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.file;

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.FileGroup;
import com.scudata.dm.FileObject;
import com.scudata.dm.Sequence;
import com.scudata.dm.cursor.ICursor;
import com.scudata.dm.cursor.MemoryCursor;
import com.scudata.dw.ComTable;
import com.scudata.expression.FileFunction;
import com.scudata.expression.IParam;
import com.scudata.resources.EngineMessage;

/**
 * ??????????ݻ??߸?????????ݵ??????
 * f.reset(f??) f.reset(f??,x;cs)
 * @author RunQian
 *
 */
public class Reset extends FileFunction {
	public Object calculate(Context ctx) {
		Object obj = null;
		ICursor cs = null;
		IParam param = this.param;
		if (param != null && param.getType() == IParam.Semicolon && param.getSubSize() == 2) {
			IParam csParam = param.getSub(1);
			if (csParam != null) {
				obj = csParam.getLeafExpression().calculate(ctx);
				if (obj == null) {
					//do nothing
				} else if (obj instanceof ICursor) {
					cs = (ICursor) obj;
				} else if (obj instanceof Sequence) {
					cs = new MemoryCursor((Sequence)obj);
				} else {
					MessageManager mm = EngineMessage.get();
					throw new RQException("reset" + mm.getMessage("function.paramTypeError"));
				}
			}
			param = param.getSub(0);
		}
		
		File f = null;
		FileGroup fg = null;
		String distribute = null;
		Integer blockSize = null;
		
		if (param == null) {
			FileObject fo = (FileObject) this.file;
			
			ComTable gt;
			try {
				gt = ComTable.open(fo, ctx);
			} catch (IOException e) {
				throw new RQException(e.getMessage(), e);
			}
			
			boolean result = gt.reset(null, option, ctx, null, null, cs);
			gt.close();
			return result;
		} else if (param.isLeaf()) {
			obj = param.getLeafExpression().calculate(ctx);
			f = null;
			if (obj instanceof FileObject) {
				f = ((FileObject) obj).getLocalFile().file();
			} else {
				MessageManager mm = EngineMessage.get();
				throw new RQException("reset" + mm.getMessage("function.paramTypeError"));
			}
			
			FileObject fo = (FileObject) this.file;
			if (f != null) {
				String opt = option;
				if ((opt == null || opt.indexOf('y') == -1) && f.exists()) {
					MessageManager mm = EngineMessage.get();
					throw new RQException(mm.getMessage("file.fileAlreadyExist", f.getName()));
				} else if (opt != null && opt.indexOf('y') != -1 && f.exists()) {
					try {
						ComTable table = ComTable.open(f, ctx);
						table.delete();
					} catch (IOException e) {
						throw new RQException(e.getMessage(), e);
					}
				}
			}
			
			try {
				ComTable gt = ComTable.open(fo, ctx);
				boolean result =  gt.reset(f, option, ctx, null, null, cs);
				gt.close();
				return result;
			} catch (IOException e) {
				throw new RQException(e.getMessage(), e);
			}
		} 
		
		if (param.getType() == IParam.Colon) {
			//f:b
			IParam sub0 = param.getSub(0);
			if (sub0 != null) {
				obj = sub0.getLeafExpression().calculate(ctx);
				if (obj instanceof FileObject) {
					f = ((FileObject) obj).getLocalFile().file();
				} else if (obj instanceof FileGroup) {
					fg= (FileGroup) obj;
				} else {
					MessageManager mm = EngineMessage.get();
					throw new RQException("reset" + mm.getMessage("function.paramTypeError"));
				}
			}
			IParam blockSizeParam = param.getSub(1);
			if (blockSizeParam != null) {
				String b = blockSizeParam.getLeafExpression().calculate(ctx).toString();
				try {
					blockSize = Integer.parseInt(b);
				} catch (NumberFormatException e) {
				}
			}
		} else if (param.getType() == IParam.Comma) {
			//x
			IParam expParam = param.getSub(1);
			if (expParam != null) {
				distribute = expParam.getLeafExpression().toString();
			}
			
			param = param.getSub(0);
			if (param.isLeaf()) {
				//f
				obj = param.getLeafExpression().calculate(ctx);
				if (obj instanceof FileObject) {
					f = ((FileObject) obj).getLocalFile().file();
				} else if (obj instanceof FileGroup) {
					fg= (FileGroup) obj;
				} else {
					MessageManager mm = EngineMessage.get();
					throw new RQException("reset" + mm.getMessage("function.paramTypeError"));
				}
			} else if (param.getType() == IParam.Colon) {
				//f:b
				IParam sub0 = param.getSub(0);
				if (sub0 != null) {
					obj = sub0.getLeafExpression().calculate(ctx);
					if (obj instanceof FileObject) {
						f = ((FileObject) obj).getLocalFile().file();
					} else if (obj instanceof FileGroup) {
						fg= (FileGroup) obj;
					} else {
						MessageManager mm = EngineMessage.get();
						throw new RQException("reset" + mm.getMessage("function.paramTypeError"));
					}
				}
				IParam blockSizeParam = param.getSub(1);
				if (blockSizeParam != null) {
					String b = blockSizeParam.getLeafExpression().calculate(ctx).toString();
					try {
						blockSize = Integer.parseInt(b);
					} catch (NumberFormatException e) {
					}
				}
			}
		}
		
		FileObject fo = (FileObject) this.file;
		try {
			ComTable gt = ComTable.open(fo, ctx);
			if (f != null) {
				String opt = option;
				if ((opt == null || opt.indexOf('y') == -1) && f.exists()) {
					MessageManager mm = EngineMessage.get();
					throw new RQException(mm.getMessage("file.fileAlreadyExist", f.getName()));
				} else if (opt != null && opt.indexOf('y') != -1 && f.exists()) {
					try {
						ComTable table = ComTable.open(f, ctx);
						table.delete();
					} catch (IOException e) {
						throw new RQException(e.getMessage(), e);
					}
				}
				
				boolean result =  gt.reset(f, option, ctx, distribute, blockSize, cs);
				gt.close();
				return result;
			} else {
				if (distribute == null) {
					MessageManager mm = EngineMessage.get();
					throw new RQException("reset" + mm.getMessage("function.invalidParam"));
				}
				boolean result =  gt.resetFileGroup(fg, option, ctx, distribute, blockSize, cs);
				gt.close();
				return result;
			}
		} catch (IOException e) {
			throw new RQException(e.getMessage(), e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy