📄 tablescan.java
字号:
/*Class for table scan Iterator
Methods:-
Open()
GetNext()
Close()
We are assuming block size is 2
*/
import java.io.*;
import java.util.*;
import java.lang.*;
public class TableScan
{
int BlockSize=2;
String[][] mainMemory=new String[50][50];
String[] AllAttributes=new String[50];
//For Processing
int BlockPointer;
int TuplePointer;
String[][] Buffer=new String[20][20];
//Some Constants
int NumOfAttributes=0;
int NumOfTuples=0;
String Relation;
String Filename;
public TableScan()
{
this(null,null);
}
public TableScan(String R,String file)
{
Relation=R;
Filename=file;
System.out.println("File : "+Filename);
LoadFile(Filename);
}
//Reading Files
String[] ReadFile(String file)
{
String[] file_data=new String[100];
try{
BufferedReader is = new BufferedReader(new FileReader(file));
String inputLine;
int i=0;
while ((inputLine = is.readLine()) != null)
{
file_data[i]=inputLine.toString();
i++;
}
is.close();
} catch (IOException e)
{
System.out.println("IOException: " + e);
}
return file_data;
}
//tokenize
String[] Tokenize(String S)
{
String[] result = S.split("\\s");
return result;
}
//Loading Data
void LoadFile(String file)
{
String[] Table=ReadFile(file);
String[] Attributes=Tokenize(Table[0]);
NumOfAttributes=Attributes.length;
for(int i=0;i<NumOfAttributes;i++)
{
AllAttributes[i]=Attributes[i];
}
int pointer=0;
while(pointer<50 && Table[pointer+1]!=null)
{
String[] temp=Tokenize(Table[pointer+1]);
for(int i=0;i<NumOfAttributes;i++)
{
mainMemory[pointer][i]=temp[i];
}
pointer++;
}
NumOfTuples=pointer;
}
//Printing Data
void PrintRelation()
{
System.out.println("Relation name : "+Relation);
System.out.println("No of Attributes : "+NumOfAttributes);
System.out.println("No of Tuples : "+NumOfTuples);
System.out.println("Table : ");
for(int i=0;i<NumOfAttributes;i++)
{
System.out.print(AllAttributes[i]+" ");
}
System.out.println();
for(int i=0;i<NumOfTuples;i++)
{
for(int j=0;j<NumOfAttributes;j++)
{
System.out.print(mainMemory[i][j]+" ");
}
System.out.println();
}
}
void PrintTuple(String[] t)
{
for(int i=0;i<t.length;i++)
{
System.out.print(t[i]+" ");
}
System.out.println();
}
//Iterator Functions
void Open()
{
BlockPointer=0;
TuplePointer=0;
}
String[] GetNext()
{
String[] tuple=new String[NumOfAttributes];
if(TuplePointer==BlockSize)
{
BlockPointer=BlockPointer+BlockSize;
if(BlockPointer>NumOfTuples)
{
return null;
}
else
{
TuplePointer=0;
}
}
for(int i=0;i<NumOfAttributes;i++)
{
tuple[i]=mainMemory[BlockPointer+TuplePointer][i];
}
TuplePointer++;
if(tuple[0]==null)
return null;
else
return tuple;
}
void Close()
{
//System.out.println("Iterator TableScan for "+Relation+" is Closed !");
}
//main function
/*
public static void main(String args[])
{
TableScan R=new TableScan("R","R.txt");
R.PrintRelation();
System.out.println("\n");
R.Open();
String[] tuple=R.GetNext();
while(tuple!=null)
{
R.PrintTuple(tuple);
tuple=R.GetNext();
}
R.Close();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -