📄 nestedloopjoin.java
字号:
//tuple based nested loop join
/*Class for tuple based nested loop join
Methods:-
Open()
GetNext()
Close()
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class NestedLoopJoin
{
String FirstRelation;
String SecondRelation;
TableScan R,S;
String[] s,r;
//key
int KeyAttributes=0;
int[][] Key=new int[2][50];
public NestedLoopJoin()
{
this(null,null,null,null);
}
public NestedLoopJoin(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);
}
//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()
{
R.Open();
S.Open();
s=S.GetNext();
}
String[] GetNext()
{
r=R.GetNext();
String[] JoinResult=Join(r,s);
while(JoinResult==null)
{
r = R.GetNext();
if(r==null)
{
R.Close();
s=S.GetNext();
if(s==null)
{
return null;
}
R.Open();
r=R.GetNext();
}
JoinResult=Join(r,s);
}
return JoinResult;
}
void Close()
{
R.Close();
S.Close();
}
//main function
public static void main(String args[])
{
NestedLoopJoin N=new NestedLoopJoin("R","S","R.txt","S.txt");
N.R.PrintRelation();
System.out.println("\n");
N.S.PrintRelation();
System.out.println("\n");
//testing
System.out.println("Num of KeyAttributes : "+N.KeyAttributes);
System.out.println("Positions of KeyAttributes : ");
for(int i=0;i<N.KeyAttributes;i++)
{
System.out.println("In R : "+N.Key[0][i]+" In S : "+N.Key[1][i]);
}
String[] newAttributes=N.JoinedAttributes();
System.out.println();
for(int i=0;i<newAttributes.length;i++)
{
System.out.print(newAttributes[i]+" ");
}
System.out.println();
//Iterator functions
N.Open();
String[] tuple=N.GetNext();
while(tuple!=null)
{
N.R.PrintTuple(tuple);
tuple=N.GetNext();
}
N.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -