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

com.scudata.dw.pseudo.PseudoDefination 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.dw.pseudo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.scudata.common.ICloneable;
import com.scudata.common.MessageManager;
import com.scudata.common.RQException;
import com.scudata.dm.BFileReader;
import com.scudata.dm.BaseRecord;
import com.scudata.dm.Context;
import com.scudata.dm.DataStruct;
import com.scudata.dm.Env;
import com.scudata.dm.FileObject;
import com.scudata.dm.Record;
import com.scudata.dm.Sequence;
import com.scudata.dm.cursor.MemoryCursor;
import com.scudata.dw.ComTable;
import com.scudata.dw.IPhyTable;
import com.scudata.dw.PhyTable;
import com.scudata.dw.PhyTableGroup;
import com.scudata.expression.Expression;
import com.scudata.resources.EngineMessage;

//???ڶ????????????
public class PseudoDefination implements Cloneable, ICloneable {
	public static final String PD_FILE = "file";
	public static final String PD_ZONE = "zone";
	public static final String PD_DATE = "date";
	public static final String PD_USER = "user";
	public static final String PD_COLUMN = "column";
	public static final String PD_VAR = "var";
	public static final String PD_UPDATE = "update";
	
	private Object file;//?ļ????????ļ?????????
	private Sequence zone;//??????????б?
	private String date;//?????ֶ?
	private String user;//?ʻ??ֶ?
	private String var;//???/?ڱ?/??Ⱥ?ڱ???????
	private List columns;//?????????ֶζ???
	
	private List tables;//???????ļ???table????
	private PhyTableGroup tableGroup;//???ļ???????
	private List maxValues;//??ÿ??table?????ֵ
	private List minValues;//??ÿ??table????Сֵ
	private Sequence memoryTable;//?ڴ?????????????
	
	private FileObject fileObject;//???ļ?????
	private DataStruct ds;//???ļ??ṹ
	private boolean isBFile = false;
	private String[] sortedFields;//?????ֶ?
	private Expression updateExp;
	
	public PseudoDefination() {
		
	}
	
	public PseudoDefination(BaseRecord pd, Context ctx) {
		file = getFieldValue(pd, PD_FILE);
		zone = (Sequence) getFieldValue(pd, PD_ZONE);
		date = (String) getFieldValue(pd, PD_DATE);
		user = (String) getFieldValue(pd, PD_USER);
		var = (String) getFieldValue(pd, PD_VAR);
		updateExp = (Expression) getFieldValue(pd, PD_UPDATE);
		Sequence seq = (Sequence) getFieldValue(pd, PD_COLUMN);
		if (seq != null) {
			columns = new ArrayList();
			int size = seq.length();
			for (int i = 1; i <= size; i++) {
				Record rec = (Record) seq.get(i);
				columns.add(new PseudoColumn(rec));
			}
		}
		if (file == null && var == null) {
			MessageManager mm = EngineMessage.get();
			throw new RQException(mm.getMessage("file.fileNotExist", "NULL"));
		}
		
		if (file != null) {
			if (!checkBFile(ctx)) {
				parseFileToTable(ctx);
			}
			sortedFields = getAllSortedColNames();
		}
		
		//???????date???????max??min
		if (date != null) {
			
		}
		
		if (var != null) {
			memoryTable = (Sequence) new Expression(var).calculate(ctx);
		}
		
		if (tables != null && tables.size() > 0) {
			doUpdate(ctx);
		}
	}
	
	public PseudoDefination(PseudoDefination pd) {
		this.file = pd.file;
		this.zone = pd.zone;
		this.date = pd.date;
		this.user = pd.user;
		this.var = pd.var;
		this.tables = pd.tables;
		this.maxValues = pd.maxValues;
		this.minValues = pd.minValues;
		this.memoryTable = pd.memoryTable;
		this.fileObject = pd.fileObject;
		this.isBFile = pd.isBFile;
		this.sortedFields = pd.sortedFields;
		this.ds = pd.ds;
		this.updateExp = pd.updateExp;
		this.tableGroup = pd.tableGroup;
		
		if (pd.columns != null) {
			this.columns = new ArrayList();
			this.columns.addAll(pd.columns);
		}
	}
	
	public Object getFile() {
		return file;
	}

	public void setFile(Object file) {
		this.file = file;
	}

	public Sequence getZone() {
		return zone;
	}

	public void setZone(Sequence zone) {
		this.zone = zone;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}
	
	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}
	
	public List getColumns() {
		return columns;
	}
	
	public void setColumns(List columns) {
		this.columns = columns;
	}


	public List getTables() {
		return tables;
	}

	public void setTables(List tables) {
		this.tables = tables;
	}

	public Sequence getMemoryTable() {
		return memoryTable;
	}

	public void setMemoryTable(Sequence memoryTable) {
		this.memoryTable = memoryTable;
	}

	public static void setFieldValue(BaseRecord pd, String name, Object value) {
		int index = pd.getFieldIndex(name);
		if (index != -1) {
			pd.setNormalFieldValue(index, value);
		}
	}
	
	public static Object getFieldValue(BaseRecord pd, String name) {
		int index = pd.getFieldIndex(name);
		if (index != -1) {
			return pd.getFieldValue(index);
		} else {
			return null;
		}
	}
	
	/**
	 * ?????ֶ???????α??
	 * @param pname ?ֶ???
	 * @return
	 */
	public PseudoColumn findColumnByName(String name) {
		if (name == null || columns == null || columns.size() == 0) {
			return null;
		} else {
			for (PseudoColumn col : columns) {
				if (col.getName() != null && name.equals(col.getName())) {
					return col;
				}
			}
		}
		return null;
	}
	
	/**
	 * ????α?ֶ???????α??
	 * @param pname α?ֶ?????Ҳ?????Ƕ?ֵbits??????ֶ???
	 * @return
	 */
	public PseudoColumn findColumnByPseudoName(String pname) {
		if (pname == null || columns == null || columns.size() == 0) {
			return null;
		} else {
			for (PseudoColumn col : columns) {
				if (col.getName() != null && col.getExp() != null && pname.equals(col.getName())) {
					return col;
				}
				if (col.getName() != null && col.getDim() != null && col.getFkey() != null && pname.equals(col.getName())) {
					return col;
				}
				if (col.getPseudo() != null && pname.equals(col.getPseudo())) {
					return col;
				}
				if (col.getBits() != null && col.getBits().firstIndexOf(pname) != 0) {
					return col;
				}
			}
		}
		return null;
	}

	/**
	 * ?õ??ļ?fn?????????
	 * @param fn
	 * @param partitions
	 * @param ctx
	 */
	private void parseFileToTable(String fn, int partitions[], Context ctx) {
		if (partitions == null) {
			FileObject fo = new FileObject(fn, null, null, ctx);
			File f = fo.getLocalFile().file();
			PhyTable t = ComTable.openBaseTable(f, ctx);
			tables.add(t);
			ctx.removeResource(t.getGroupTable());
		} else {
			int pcount = partitions.length;
			for (int i = 0; i < pcount; ++i) {
				File file = Env.getPartitionFile(partitions[i], fn);
				PhyTable table = ComTable.openBaseTable(file, ctx);
				table.getGroupTable().setPartition(partitions[i]);
				tables.add(table);
			}
			
			if (pcount > 1) {
				IPhyTable []tbls = new IPhyTable[pcount];
				for (int i = 0; i < pcount; ++i) {
					tbls[i] = tables.get(i);
				}
				tableGroup = new PhyTableGroup(fn, tbls, partitions, null, ctx);
			}
		}
	}
	
	/**
	 * ?õ??ļ????ļ???????????
	 * @param ctx
	 */
	private void parseFileToTable(Context ctx) {
		Object file = this.file;
		int partitions[] = null;

		Sequence zone = getZone();
		if (zone != null) {
			partitions = zone.toIntArray();
		}
		
		tables = new ArrayList();
		
		if (file instanceof String) {
			parseFileToTable((String) file, partitions, ctx);
		} else {
			MessageManager mm = EngineMessage.get();
			throw new RQException(mm.getMessage("function.invalidParam"));
		}
		
		if (date != null) {
			String dateName = date;
			//????????????α?ֶ?
//			PseudoColumn dateCol = findColumnByPseudoName(date);
//			if (dateCol != null && dateCol.getExp() != null) {
//				dateName = dateCol.getName();
//			}
			maxValues = new ArrayList();
			minValues = new ArrayList();
			for (IPhyTable t : tables) {
				try {
					Object[] values = ((PhyTable)t).getMaxMinValue(dateName);
					if (values != null) {
						maxValues.add(values[0]);
						minValues.add(values[1]);
					}
				} catch (IOException e) {
					throw new RQException(e.getMessage());
				}
			}
		}
	}

	/**
	 * ???????ļ??????󡣲????ھͷ???null
	 * @return
	 */
	public PhyTableGroup getTableMetaDataGroup() {
		return tableGroup;
	}
	
	public String getUgrp() {
		String[] names = getAllColNames();
		if (names == null) return null;
		return names[0];
	}
	
	public String[] getAllColNames() {
		if (isBFile) {
			return ds.getFieldNames();
		} else if (var == null) {
			return tables.get(0).getAllColNames();
		} else {
			return memoryTable.dataStruct().getFieldNames();
		}
	}

	public String[] getAllSortedColNames() {
		if (isBFile) {
			return ds.getPrimary();
		} else if (var == null) {
			return tables.get(0).getAllKeyColNames();
		} else {
			return memoryTable.dataStruct().getPrimary();
		}
	}
	
	public void addPseudoColumn(PseudoColumn column) {
		if (columns == null) {
			columns = new ArrayList();
		}
		columns.add(column);
	}
	
	public String getDistribute() {
		if (isBFile) {
			return null;
		} else if (var == null) {
			return tables.get(0).getDistribute();
		} else {
			return null;
		}
	}
	
	public Integer getPartition() {
		if (isBFile) {
			return null;
		} else if (var == null && tables.get(0) instanceof PhyTable) {
			return ((PhyTable) tables.get(0)).getGroupTable().getPartition();
		} else {
			return null;
		}
	}
	
	/**
	 * ?ж?fields?Ƿ?????????????ֶ?
	 * @param fields
	 * @return
	 */
	public boolean isSortedFields(String[] fields) {
		if (sortedFields == null || fields == null) {
			return false;
		}
		
		int len = fields.length;
		if (len > sortedFields.length || len == 0) {
			return false;
		}
		
		for (int i = 0; i < len; i++) {
			if (fields[i] == null) {
				return false;
			}
			if (!fields[i].equals(sortedFields[i])) {
				return false;
			}
		}
		return true;
	}
	
	/**
	 * ????Ƿ??Ǽ??ļ?
	 * @return
	 */
	private boolean checkBFile(Context ctx) {
		if (!(file instanceof String)) {
			return false;
		}
		FileObject fo = new FileObject((String)file, null, null, ctx);;
		BFileReader reader = new BFileReader(fo);
		try {
			reader.open();
			ds = reader.getFileDataStruct();
			reader.close();
			
			setFileObject(fo);
			isBFile = true;
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean isBFile() {
		return isBFile;
	}

	public FileObject getFileObject() {
		return fileObject;
	}

	public void setFileObject(FileObject fileObject) {
		this.fileObject = fileObject;
	}
	
	public List getMaxValues() {
		return maxValues;
	}

	public List getMinValues() {
		return minValues;
	}

	public Object deepClone() {
		return new PseudoDefination(this);
	}
	
	private void doUpdate(Context ctx) {
		Sequence append = null;
		Sequence update = null;
		Sequence delete = null;
		Expression exp = updateExp;
		if (exp == null) return;
		
		Object obj = exp.calculate(ctx);
		if (obj instanceof Sequence) {
			Sequence seq = (Sequence) obj;
			if (seq.length() == 3) {
				obj = seq.get(1);
				if (obj != null && obj instanceof Sequence) {
					append = (Sequence) obj;
				}
				obj = seq.get(2);
				if (obj != null && obj instanceof Sequence) {
					update = (Sequence) obj;
				}
				obj = seq.get(3);
				if (obj != null && obj instanceof Sequence) {
					delete = (Sequence) obj;
				}
				
				try {
					if (append != null) {
						tables.get(tables.size() - 1).append(new MemoryCursor(append), "y");
					}
					if (update != null) {
						for (IPhyTable table : tables) {
							table.update(update, "y");
						}
					}
					if (delete != null) {
						for (IPhyTable table : tables) {
							table.delete(delete, "y");
						}
					}
				} catch (IOException e) {
					throw new RQException(e);
				}
				return;
			}
		}
		MessageManager mm = EngineMessage.get();
		throw new RQException(mm.getMessage("function.invalidParam"));
	}
}