⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hashjoin.java

📁 this a program for for database implement in this we are implementing several join algorithms
💻 JAVA
字号:
//Hash join
/*Class for hash join
    Methods:-
	Open()
	GetNext()
	Close()
	*/

import java.io.*;
import java.util.*;
import java.lang.*;

public class HashJoin
	{
		String FirstRelation;
		String SecondRelation;
		TableScan R,S;
		String[] s,r;
		int CurrentPos=0;
		//key
		int KeyAttributes=0;
		int[][] Key=new int[2][50];
		
		TupleNode[] HashTable=new TupleNode[50];
		
		public HashJoin()
		{
			this(null,null,null,null);
		}
		
		public HashJoin(String Rname,String Sname,String fileForR,String fileForS)
		{
			FirstRelation = Rname;
			SecondRelation= Sname;
			R=new TableScan(Rname,fileForR);
			S=new TableScan(Sname,fileForS);
			UpdateKey(R,S);
		}
		public void FillHashTable(TableScan R1)
		{
			R1.Open();
			String[] t=R1.GetNext();
			while(t!=null)
			{
				InsertInHash(t);
				t=R1.GetNext();
			}
			R1.Close();
			
		}
		void InsertInHash(String[] t)
		{
			int KeyForR=Key[0][0];
			int index=GetKey(t,KeyForR);
			TupleNode temp=new TupleNode(t);
			if(HashTable[index]==null)
			{
				HashTable[index]=temp;
			}
			else
			{
				TupleNode old=HashTable[index];
				while(old.next!=null)
				{
					old=old.next;
				}
				old.next=temp;
			}
		}
		
		void PrintHashTable()
		{
			for(int i=0;i<50;i++)
			{
				if(HashTable[i]!=null)
				{
					System.out.println("Index : "+i);
					TupleNode Temp=HashTable[i];
					R.PrintTuple(Temp.Value);
					while(Temp.next!=null)
					{
						Temp=Temp.next;
						R.PrintTuple(Temp.Value);
					}
					System.out.println("------");
				}
			}
		}
		int GetKey(String[] s,int index)
		{
			String temp=s[index];
			int h = 0;
			for (int j = 0; j < temp.length(); j++) 
			{
				h = 31*h + temp.charAt(j);
			}
			h=h%50;
			return h;
		}
		//Getting key
		void UpdateKey(TableScan t,TableScan u)
		{
			for(int i=0;i<t.NumOfAttributes;i++)
			{
				for(int j=0;j<u.NumOfAttributes;j++)
				{
					if(t.AllAttributes[i].equals(u.AllAttributes[j]))
					{
						Key[0][KeyAttributes]=i;
						Key[1][KeyAttributes]=j;
						KeyAttributes++;
						break;
					}
				}
			}
		}
		boolean KeyMatched(String[] t,String[] u)
		{
			boolean result=true;
			
			for(int i=0;i<KeyAttributes;i++)
			{
				if(!t[Key[0][i]].equals(u[Key[1][i]]))
				{
					result=false;
					break;
				}
			}
			return result;
		}
		String[] Join(String[] t,String[] u)
		{
			if(t==null || u==null)
				return null;
				
			int ResultLength=t.length+u.length-KeyAttributes;
			String[] Result=new String[ResultLength];
			if(KeyMatched(t,u))
			{
				int i=0;
				String[] CopyOfU=new String[u.length];
				for(int k=0;k<u.length;k++)
					{
						CopyOfU[k]=u[k];
					}
					
				for(;i<t.length;i++)
				{
					Result[i]=t[i];
				}
				for(int j=0;j<KeyAttributes;j++)
				{
					CopyOfU[Key[1][j]]=null;
				}
				for(int j=0;j<CopyOfU.length;j++)
				{
					if(CopyOfU[j]!=null)
					{
						Result[i]=CopyOfU[j];
						i++;
					}
				}
			
			return Result;
			}
			else
			{
			return null;
			}
		}
		String[] JoinedAttributes()
		{
			int ResultLength=R.NumOfAttributes+S.NumOfAttributes-KeyAttributes;
			String[] Result=new String[ResultLength];
			
				int i=0;
				String[] CopyOfS=new String[S.NumOfAttributes];
				for(int k=0;k<S.NumOfAttributes;k++)
					{
						CopyOfS[k]=S.AllAttributes[k];
					}
				for(;i<R.NumOfAttributes;i++)
				{
					Result[i]=R.AllAttributes[i];
				}
				for(int j=0;j<KeyAttributes;j++)
				{
					CopyOfS[Key[1][j]]=null;
				}
				for(int j=0;j<CopyOfS.length;j++)
				{
					if(CopyOfS[j]!=null)
					{
						Result[i]=CopyOfS[j];
						i++;
					}
				}
			
			return Result;
			
		}
		//Functions
		void Open()
		{
			FillHashTable(R);
			PrintHashTable();
			S.Open();
			s=S.GetNext();
		}
		
		String[] GetNext()
		{	
			if(s==null)
				return null;
				
			int KeyForS= GetKey(s,Key[1][0]);
			int tempIndex=CurrentPos;
			//System.out.println("CurrentPos : "+CurrentPos);
			TupleNode tupleFromHash=HashTable[KeyForS];
			//System.out.println("tuple found");
			while(tempIndex!=0)
			{
				tupleFromHash=tupleFromHash.next;
				tempIndex--;
				
			}
			if(tupleFromHash==null)
			{
				CurrentPos=0;
				s=S.GetNext();
				return GetNext();
			}
			String[] JoinResult=Join(tupleFromHash.Value,s);
			if(JoinResult!=null)
			{
				CurrentPos++;
				if(tupleFromHash.next==null)
				{
					CurrentPos=0;
					s=S.GetNext();
					//return GetNext();
				}
			}
			
			while(JoinResult==null)
			{
				if(s==null)
					return null;
				if(tupleFromHash!=null)
				{
				tupleFromHash = tupleFromHash.next;
				CurrentPos++;
				if(tupleFromHash==null)
				{
					CurrentPos=0;
					s=S.GetNext();
						if(s==null)
							return null;
					KeyForS= GetKey(s,Key[1][0]);
					tupleFromHash=HashTable[KeyForS];
					
				}
				JoinResult=Join(tupleFromHash.next.Value,s);
				}
				else
				{
				s=S.GetNext();
				KeyForS= GetKey(s,Key[1][0]);
			
			tupleFromHash=HashTable[KeyForS];
				JoinResult=Join(tupleFromHash.Value,s);
					if(JoinResult!=null)
					{
						CurrentPos++;
						if(tupleFromHash.next==null)
						{
							CurrentPos=0;
							s=S.GetNext();
						}
					}
				}
			}
			
			return JoinResult;
		}
		
		void Close()
		{
			S.Close();
		}
		
		//main function
		public static void main(String args[])
		{
			HashJoin H=new HashJoin("R","S","R.txt","S.txt");
			H.R.PrintRelation();
			System.out.println("\n");
		
			H.S.PrintRelation();
			System.out.println("\n");
			
			//testing
			System.out.println("Num of KeyAttributes : "+H.KeyAttributes);
			System.out.println("Positions of KeyAttributes : ");
			for(int i=0;i<H.KeyAttributes;i++)
			{
				System.out.println("In R : "+H.Key[0][i]+" In S : "+H.Key[1][i]);
			}
			String[] newAttributes=H.JoinedAttributes();
			System.out.println();
			for(int i=0;i<newAttributes.length;i++)
			{
				System.out.print(newAttributes[i]+" ");
			}
			System.out.println();
			//Iterator functions
			H.Open();
			
			String[] tuple=H.GetNext();
					
			while(tuple!=null)
			{
				H.R.PrintTuple(tuple);
				tuple=H.GetNext();
			}
			
			H.Close();
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -