* * 我学习了网格索引结构。到目前为止,我已经创建了名为data_gen.jar的数据生成器
这是data_gen的格式输出数据 每行由ID,x1,y1,x2和y2组成,表示矩形
让我们说这个数据存储到名为input_data_set.txt的文本中 现在我的下一步是,首先:制作另一个程序来做到这一点
./ gri -m page_capacity -l object_list_size -h -f input_data_set
-m:磁盘页面中的点数 -l:点数可以存储在内存中 -h:使用希尔伯特顺序计算订购号。默认为行 排序。
-f:输入文件名。
执行该命令后,必须生成三个文件 gri.conf , gri.idx 和 gri.iof 。
在 grid.conf 中我们存储配置 为简单起见,我们假设 gri.iof 文件的页面大小足以存储溢出点。这意味着你可以放弃一些点 当点数超过 gri.iof 文件的页面容量时。还可以将数据集调整为 防止过多的溢出点。
这是更详细的数字
然后,在构造之后,使用以下命令进入交互模式
./gri -i [-c cache_size] -h
I)nsert
D)elete
Q)uery / Index
U)pdate
CMD>
用户可以键入命令以与gri交互。以下是模拟。
./gri -i -c 4 -h
CMD>我4:8,9,8,9
gri:将点4插入单元格(x,y)。
CMD>问题4:8,9,8,9
gri:点4存储在单元格(x,y),第2页
gri:第2页包含要点:3,4
gri-cache:2,3,4,5 #disk页码
CMD> D 4:8,9,8,9
gri:从第2页的单元格(x,y)删除第4点
gri:第2页包含要点:3
CMD> U 4:8,9,8,9 1,2,1,2
gri:第4点从第2页迁移到第0页
gri-cache:4,5,1,0
万一,我在java做... 有没有人可以帮助我或指导我..任何参考都会很棒 我对下一步感到困惑,我只在数据生成器中取得了成功
这是一些代码
GRI.java
package gri;
import java.io.*;
public class GRI
{
public static void main(String[] args) throws IOException
{
String strin="null";
BufferedReader buf;
int M=4,N=10000;
double Sx=100,Sy=100;
int method=1;
int timestamp=1;
System.out.println("Please enter the number of data:");
buf=new BufferedReader(new InputStreamReader(System.in));
strin=buf.readLine();
N=Integer.parseInt(strin);
System.out.println("Please enter a page capacity (M):");
strin=buf.readLine();
M=Integer.parseInt(strin);
System.out.println("Please enter Sx:");
strin=buf.readLine();
Sx=Double.parseDouble(strin);
System.out.println("Please enter Sy:");
strin=buf.readLine();
Sy=Double.parseDouble(strin);
System.out.println("N: "+N+" "+"M:"+M+" "+"Sx:"+Sx+" "+"Sy:"+Sy);
int count_d=N; //Record number of existing information
// System.out.println("請選擇index方式:");
System.out.println("1.row order");
System.out.println("2.Z-Curve");
// strin=buf.readLine();
// method=Integer.parseInt(strin);
System.out.println("Please enter K:");
strin=buf.readLine();
int k=Integer.parseInt(strin);
System.out.println("Please X% of the point before entering as a query: ");
strin=buf.readLine();
int x=Integer.parseInt(strin);
int query_num=N/100*x;
System.out.println("query number: "+query_num);
String temp=Integer.toString(N);
int N_len=temp.length();
temp="X";
for(int i=0;i<N_len+15-1;i++)
{
temp=temp+"X";
}
double grid=0;
int grid_i=0,cell=0;
cell=(int)Math.ceil((double)N/M); //至少需幾個cells 取大於的最小整數
grid=Math.sqrt(cell); // ? x ? 的?
grid_i=(int)Math.ceil(grid); // ? 取大於的最小整數 grid_i x grid_i
int grid_z=1;
if(method==2)
{
while(grid_z<grid_i)
{
grid_z*=2;
}
grid_i=grid_z;
}
System.out.println("至少需cell:"+cell+" "+"幾乘幾:"+grid_i+" x "+grid_i);
double cellx=Sx/grid_i; // 一格的 x 分量
double celly=Sy/grid_i; // 一格的 y 分量
System.out.println("一格的 X 分量:"+cellx+" "+"一格的 Y 分量:"+celly);
//輸出 設定檔
FileWriter fw3 = new FileWriter("gri.conf");
BufferedWriter bfw3 = new BufferedWriter(fw3);
bfw3.write("size of data: "+N);
bfw3.newLine();
bfw3.write("page capacity: "+M);
bfw3.newLine();
bfw3.write("grid size: "+grid_i+" x "+grid_i);
bfw3.newLine();
bfw3.write("一格的 X 分量: "+cellx+" 一格的 Y 分量: "+celly);
bfw3.newLine();
bfw3.flush();
fw3.close();
String str;
FileReader fr = new FileReader("stdout1.txt");
BufferedReader bfr = new BufferedReader(fr);
//orderfile空間
FileWriter fw = new FileWriter("gri.idx");
BufferedWriter bfw = new BufferedWriter(fw);
//規劃overflow空間
FileWriter fw2 = new FileWriter("gri.iof");
BufferedWriter bfw2 = new BufferedWriter(fw2);
//開始規劃
for(int i=0;i<grid_i*grid_i;i++)
{
String disk_p="#";
for(int j=0;j<N_len-Integer.toString(i).length();j++)
disk_p=disk_p+"0";
disk_p=disk_p+Integer.toString(i);
bfw.write(disk_p);
bfw.newLine();
bfw2.write(disk_p);
bfw2.newLine();
for(int j=0;j<M;j++)
{
bfw.write(temp);
bfw.newLine();
bfw2.write(temp);
bfw2.newLine();
}
}
bfw.flush();
fw.close();
bfw2.flush();
fw2.close();
// System.out.println("orderfile & overlfow create");
File file = new File("gri.idx");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
File file2 = new File("gri.iof");
RandomAccessFile raf2 = new RandomAccessFile(file2, "rw");
//建index
while( ( str=bfr.readLine() )!=null ) //從stdout.txt讀取
{
int ci=0,cj=0; // 以 row ordered 的編號
int disk_p=0;
int overflow=0;
String diskcon; //應放位置
String[] str_arr = str.split(" "); // str_arr[0]=ID, str_arr[1]=Xlow , str_arr[2]=Ylow...
String data=str_arr[0]+" "+str_arr[1]+" "+str_arr[2];
ci=(int) Math.ceil( (Double.parseDouble(str_arr[1])/cellx) );
cj=(int) Math.ceil( (Double.parseDouble(str_arr[2])/celly) );
// System.out.println(ci+" "+cj);
disk_p=(cj-1)*grid_i+ci-1; //DISK中的位置 -1因為從0開始
if(method==2) //以Z_ORDER的方式計算
disk_p=cul_zorder(ci,cj);
//not need
String pos = "#";
for(int i=0;i<N_len-Integer.toString(disk_p).length();i++)
pos=pos+"0";
pos+=Integer.toString(disk_p);
//not need
// System.out.println(disk_p);
raf.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 ); //搜尋區塊
for(int i=0;i<M;i++) //檢查此區塊是否已存M點
{
if((diskcon=raf.readLine()).equals(temp))
{
raf.seek(raf.getFilePointer()-17-N_len);
raf.writeBytes(data);
for(int j=0;j<temp.length()-data.length();j++)
raf.writeBytes(" ");
break;
}
else
overflow++;
}
if(overflow==M) //已經存滿M個點 overflow
{
raf2.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 ); //搜尋區塊
for(int i=0;i<M;i++) //檢查此區塊是否已存M點
{
if((diskcon=raf2.readLine()).equals(temp))
{
raf2.seek(raf2.getFilePointer()-17-N_len);
raf2.writeBytes(data);
for(int j=0;j<temp.length()-data.length();j++)
raf2.writeBytes(" ");
break;
}
}
}
raf.seek(0);
raf2.seek(0);
}
raf.close();
raf2.close();
fr.close();
//成功建立index
while( !(strin.equals("E")) )
{
File file3 = new File("gri.idx");
RandomAccessFile raf3 = new RandomAccessFile(file3, "rw");
File file4 = new File("gri.iof");
RandomAccessFile raf4 = new RandomAccessFile(file4, "rw");
System.out.println("Enter the command: ");
System.out.println("I)nsert ");
System.out.println("D)elete ");
System.out.println("Q)uery");
System.out.println("U)pdate to next time ");
System.out.println("E)xit ");
strin=buf.readLine();
strin=strin.toUpperCase();
String inc;
switch(strin)
{
case "I":
{
System.out.println("Please enter xlow,ylow,xhigh,yhigh");
inc="null,";
inc=inc+buf.readLine();
String result[]=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
count_d=Insert(inc,raf3,raf4,M,result,N_len,temp,count_d);
break;
}
case "D":
{
System.out.println("Please enter ID,xlow,ylow,xhigh,yhigh");
inc=buf.readLine();
String result[]=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
if(result[0].equals("true"))
{
Delete(inc,raf3,raf4,M,result,N_len,temp);
System.out.println("Remove finished");
}
else
System.out.println("It does not exist at this point");
break;
}
case "Q":
{
// System.out.println("please enter ID,xlow,ylow,xhigh,yhigh");
inc=buf.readLine();
String result[]=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
if(result[0].equals("true"))
System.out.println("The presence of this point");
else
System.out.println("It does not exist at this point");
// System.out.println("此點會被歸在cell: ("+result[1]+","+result[2]+") ,會被放在: "+result[3]+" 區");
break;
}
case "U":
{
if(timestamp<100)
{
String source_file="stdout"+Integer.toString(timestamp)+".txt";
timestamp++;
String update_file="stdout"+Integer.toString(timestamp)+".txt";
FileReader fr_s = new FileReader(source_file);
BufferedReader bfr_s = new BufferedReader(fr_s);
FileReader fr_u = new FileReader(update_file);
BufferedReader bfr_u = new BufferedReader(fr_u);
String str_s,str_u;
int temp_ID=1;
int count_fai=0;
while( ( str_s=bfr_s.readLine() )!=null ) //從source讀取 & update
{
String temp2[] = str_s.split(" ");
String everyID=Integer.toString(temp_ID);
temp_ID++;
inc=everyID+","+temp2[1]+","+temp2[2]+","+temp2[3]+","+temp2[4];
String ID[]=inc.split(",");
String result[]=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
// System.out.println(everyID);
if(result[0].equals("true"))
{
Delete(inc,raf3,raf4,M,result,N_len,temp);
str_u=bfr_u.readLine();
String temp3[] = str_u.split(" ");
inc=everyID+","+temp3[1]+","+temp3[2]+","+temp3[3]+","+temp3[4];
result=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
System.out.print("移動結果:");
Insert(inc,raf3,raf4,M,result,N_len,temp,count_d);
}
else
{
str_u=bfr_u.readLine();
count_fai++;
// System.out.pruintln("不存在此點");
}
}
System.out.println("finish");
// System.out.println(count_fai);
}
// System.out.println("請輸入此點的ID,xlow,ylow,xhigh,yhigh");
// inc=buf.readLine();
// String ID[]=inc.split(",");
// String result[]=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
// if(result[0].equals("true"))
// {
// Delete(inc,raf3,raf4,M,result,N_len,temp);
// System.out.println("要移動到? 輸入xlow,ylow,xhigh,yhigh");
// inc=ID[0]+","+buf.readLine();
// result=checkpoint(inc,raf3,raf4,M,grid_i,N_len,temp,cellx,celly,method);
// System.out.print("移動結果:");
// Insert(inc,raf3,raf4,M,result,N_len,temp,count_d);
// }
//
// else
// System.out.println("不存在此點");
break;
}
case "E":
{
System.out.println("exit... thanks .. bye..");
break;
}
}
raf3.close();
raf4.close();
}
}
static int Insert(String P, RandomAccessFile raf, RandomAccessFile raf2,int M, String result[], int N_len, String temp,int count_d) throws IOException
{
int disk_p=Integer.parseInt(result[3]);
String diskcon,data;
String[] P_pos = P.split(",");
raf.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
raf2.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
int overflow=0;
for(int i=0;i<M;i++) //檢查此區塊是否存了M個點
{
if((diskcon=raf.readLine()).equals(temp))
{
if(P_pos[0].equals("null"))
{
count_d++;
data=Integer.toString(count_d)+": "+P_pos[1]+" "+P_pos[2];
}
else
data=P_pos[0]+": "+P_pos[1]+" "+P_pos[2];
raf.seek(raf.getFilePointer()-17-N_len);
raf.writeBytes(data);
for(int j=0;j<temp.length()-data.length();j++)
raf.writeBytes(" ");
System.out.println("成功,"+P_pos[0]+" 在orderfile "+result[3]+"區");
return count_d;
}
else
overflow++;
}
if(overflow==M) //已經存滿M個點 overflow
{
overflow=0;
for(int i=0;i<M;i++)
{
if((diskcon=raf2.readLine()).equals(temp))
{
if(P_pos[0].equals("null"))
{
count_d++;
data=Integer.toString(count_d)+": "+P_pos[1]+" "+P_pos[2];
}
else
data=P_pos[0]+": "+P_pos[1]+" "+P_pos[2];
raf2.seek(raf2.getFilePointer()-17-N_len);
raf2.writeBytes(data);
for(int j=0;j<temp.length()-data.length();j++)
raf2.writeBytes(" ");
System.out.println("成功, 在Overflow "+result[3]+"區");
return count_d;
}
else
overflow++;
}
}
if(overflow==M)
System.out.println("都滿囉,丟掉了");
return count_d;
}
static void Delete(String P, RandomAccessFile raf, RandomAccessFile raf2,int M, String result[], int N_len, String temp) throws IOException
{
int disk_p=Integer.parseInt(result[3]);
String diskcon;
String[] P_pos = P.split(",");
raf.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
raf2.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
P_pos[0]=P_pos[0]+":";
for(int i=0;i<M;i++) //檢查此區塊(orderfile)是否存點
{
diskcon=raf.readLine();
if(!diskcon.equals(temp))
{
String[] pos = diskcon.split(" ");
if(pos[1].equals(P_pos[1]) && pos[2].equals(P_pos[2]) && pos[0].equals(P_pos[0])) //找到了
{
raf.seek(raf.getFilePointer()-17-N_len);
raf.writeBytes(temp);
raf.writeBytes("\r\n");
}
}
}
for(int i=0;i<M;i++) //檢查此區塊(overflow)是否存點
{
diskcon=raf2.readLine();
if(!diskcon.equals(temp))
{
String[] pos = diskcon.split(" ");
if(pos[1].equals(P_pos[1]) && pos[2].equals(P_pos[2]) && pos[0].equals(P_pos[0])) //找到了
{
raf2.seek(raf2.getFilePointer()-17-N_len);
raf2.writeBytes(temp);
raf2.writeBytes("\r\n");
}
}
}
}
static String[] checkpoint(String P, RandomAccessFile raf, RandomAccessFile raf2, int M, int grid_i, int N_len, String temp, double cellx, double celly,int method) throws IOException
{
String result[]={"","","",""};
String diskcon;
String[] P_pos = P.split(",");
int ci=(int) Math.ceil( (Double.parseDouble(P_pos[1])/cellx) );
int cj=(int) Math.ceil( (Double.parseDouble(P_pos[2])/celly) );
int disk_p=(cj-1)*grid_i+ci-1;
if(method==2)
disk_p=cul_zorder(ci,cj);
result[1]=Integer.toString(ci);
result[2]=Integer.toString(cj);
result[3]=Integer.toString(disk_p);
if(P_pos[0].equals("null"))
{
result[0]="null";
return result;
}
P_pos[0]=P_pos[0]+":";
raf.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
raf2.seek( ( N_len+1+2+ (temp.length()+2)*M )*disk_p + N_len+1+2 );
for(int i=0;i<M;i++) //檢查ORDERFILE是否存此點
{
// System.out.println("搜尋orderfile區塊");
diskcon=raf.readLine();
// System.out.println(diskcon);
if(!diskcon.equals(temp))
{
String[] pos = diskcon.split(" ");
if(pos[1].equals(P_pos[1]) && pos[2].equals(P_pos[2]))
{
if(P_pos[0].equals(pos[0]))
{
System.out.println("在orderfile "+result[3]+"區");
result[0]="true"; //return true;
return result;
}
}
}
}
for(int i=0;i<M;i++) //檢查OVERFLOW是否存此點
{
diskcon=raf2.readLine();
// System.out.println(diskcon);
if(!diskcon.equals(temp))
{
String[] pos = diskcon.split(" ");
if(pos[1].equals(P_pos[1]) && pos[2].equals(P_pos[2]))
{
if(P_pos[0].equals(pos[0]))
{
System.out.println("在overflow "+result[3]+"區");
result[0]="true"; //return true;
return result;
}
}
}
}
result[0]="false"; //return false;
return result;
}
static int cul_zorder(int ci,int cj)
{
String b_ci=null,b_cj=null;
int x=ci-1,y=cj-1,disk_p;
if(x==0)
b_ci=b_ci+"0";
if(y==0)
b_cj=b_cj+"0";
while(x>0)
{
b_ci=b_ci+Integer.toString(x%2);
x=x/2;
}
while(y>0)
{
b_cj=b_cj+Integer.toString(y%2);
y=y/2;
}
if(b_ci.length()>b_cj.length())
for(int i=b_ci.length()-b_cj.length();i>0;i--)
b_cj=b_cj+"0";
else if(b_ci.length()<b_cj.length())
for(int i=b_cj.length()-b_ci.length();i>0;i--)
b_ci=b_ci+"0";
String zdisk="nu";
for(int i=b_cj.length()-1;i>=0;i--)
{
if(b_ci.charAt(i)=='0')
zdisk=zdisk+"0";
else if(b_ci.charAt(i)=='1')
zdisk=zdisk+"1";
if(b_cj.charAt(i)=='0')
zdisk=zdisk+"0";
else if(b_cj.charAt(i)=='1')
zdisk=zdisk+"1";
}
int two_d=1,dig=0;
for(int i=zdisk.length()-1;i>=0;i--)
{
if(zdisk.charAt(i)=='1')
dig=dig+two_d;
two_d=two_d*2;
}
disk_p=dig;
return disk_p;
}
}