📄 sortjoin.java
字号:
//sort based join
/*Class for sort based join
Methods:-
Open()
GetNext()
Close()
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class SortJoin
{
String FirstRelation;
String SecondRelation;
TableScan R,S;
Heap HR,HS;
TupleNode s,r;
TupleNode[] Buffer1=new TupleNode[50];
TupleNode[] Buffer2=new TupleNode[50];
int Buffer1Pointer=0;
int Buffer2Pointer=0;
int index=0;
//key
int KeyAttributes=0;
int[][] Key=new int[2][50];
public SortJoin()
{
this(null,null,null,null);
}
public SortJoin(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);
HR=new Heap(FirstRelation,fileForR,Key[0][0]);
HS=new Heap(SecondRelation,fileForS,Key[1][0]);
}
//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;
}
void PrintBuffer()
{
System.out.println("\nBuffer 1 : \n");
for(int i=0;i<Buffer1Pointer;i++)
{
R.PrintTuple(Buffer1[i].Value);
}
System.out.println("\nBuffer 2 : \n");
for(int i=0;i<Buffer2Pointer;i++)
{
R.PrintTuple(Buffer2[i].Value);
}
System.out.println("\n");
}
boolean TupleIsGreater(TupleNode a,TupleNode b)
{
int Fora=new Integer(a.Value[(Key[0][0])]).intValue();
int Forb=new Integer(b.Value[(Key[1][0])]).intValue();
if(Fora>Forb)
return true;
else
return false;
}
//Functions
void Open()
{
HR.Open();
HS.Open();
R.Open();
S.Open();
s=HS.GetNext();
r=HR.GetNext();
Buffer1Pointer=0;
Buffer2Pointer=0;
index=0;
}
String[] GetNext()
{
if(Buffer1Pointer==0)
{
Buffer2Pointer=0;
while(r!=null && s!=null && !r.Value[Key[0][0]].equals(s.Value[Key[1][0]]))
{
if(TupleIsGreater(r,s))
{
s=HS.GetNext();
}
else
{
r=HR.GetNext();
}
}
if(r==null || s==null)
return null;
String currentKey=r.Value[Key[0][0]];
while(r.Value[Key[0][0]].equals(currentKey))
{
Buffer1[Buffer1Pointer]=r;
r=HR.GetNext();
Buffer1Pointer++;
}
while(s.Value[Key[1][0]].equals(currentKey))
{
Buffer2[Buffer2Pointer]=s;
s=HS.GetNext();
Buffer2Pointer++;
}
}
String[] JoinResult=Join(Buffer1[Buffer1Pointer-1].Value,Buffer2[index].Value);
index++;
if(index==Buffer2Pointer)
{
index=0;
Buffer1Pointer--;
}
return JoinResult;
}
void Close()
{
R.Close();
S.Close();
HR.Close();
HS.Close();
}
//main function
public static void main(String args[])
{
SortJoin S=new SortJoin("R","S","R.txt","S.txt");
S.R.PrintRelation();
System.out.println("\n");
S.S.PrintRelation();
System.out.println("\n");
//testing
System.out.println("Num of KeyAttributes : "+S.KeyAttributes);
System.out.println("Positions of KeyAttributes : \n");
for(int i=0;i<S.KeyAttributes;i++)
{
System.out.println("In R : "+S.Key[0][i]+" In S : "+S.Key[1][i]);
}
String[] newAttributes=S.JoinedAttributes();
System.out.println();
for(int i=0;i<newAttributes.length;i++)
{
System.out.print(newAttributes[i]+" ");
}
System.out.println();
//Iterator functions
S.Open();
String[] tuple=S.GetNext();
while(tuple!=null)
{
S.R.PrintTuple(tuple);
tuple=S.GetNext();
}
S.Close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -