java和数据库高手来看这个有挑战性的项目,是我一在英国计算机名校读研的朋友的作业,让我帮忙,现在看的我直头晕!谁能帮我分析一下这个

Achilles_KG 2004-11-11 01:36:08
作业要求:http://www.inf.ed.ac.uk/teaching/courses/adbs/assignment1.html
attica DBMS说明:http://www.inf.ed.ac.uk/teaching/courses/adbs/attica/
从网页上可以下载这个系统的源代码。
我今天分析了一天,有这么几个问题没弄明白:
1。这个ExternalSort.java的功能到底是是完成对特定表的内容排序,然后输入一张新的表,还是完成sql语句中的order by解析?我分析的结果是后者。
2。如何在要完成的这个ExernalSort.java中的setup()方法进行order by后面的key word的传递?
3。PlanBuilder.java中如可以让ExernalSort.java的功能实现?
以下是我仅仅能写出的部分代码:
protected void initTempFiles () throws StorageManagerException {
////////////////////////////////////////////
//
// initialise the temporary files here
// make sure you throw the right exception
//
////////////////////////////////////////////
try{
inputFile = FileUtil.createTempFileName();
sm.createFile(inputFile);
outputFile = FileUtil.createTempFileName();
sm.createFile(outputFile);
}
catch (Exception e) {
StorageManagerException sme = new StorageManagerException("Could not initialise temporary files");
sme.setStackTrace(e.getStackTrace());
throw sme;
}

} // initTempFiles()

/**
* Set up this external sort operator
*
* @throws EngineException thrown whenever there is something wrong with
* setting this operator up
*/
public void setup () throws EngineException {
try {
////////////////////////////////////////////
//
// this is a blocking operator -- store the input
// in a temporary file and sort the file
//
////////////////////////////////////////////


Relation rel = getInputOperator().getOutputRelation();
inputMan = new RelationIOManager(sm, rel, inputFile);

boolean done = false;
while (! done) {
Tuple[] tuples = getInputOperator().getNext();
if (tuples != null) {
done = (tuples[0] instanceof EndOfStreamTuple);
if (! done) {
for (int i = 0; i < tuples.length; i++) {
inputMan.insertTuple(tuples[i]);
}
}
}
}

////////////////////////////////////////////
//
// the output should reside in the output file
//
////////////////////////////////////////////

outputMan = new RelationIOManager(sm, getOutputRelation(),
outputFile);
outputMan.openTupleCursor();
}
catch (StorageManagerException sme) {
EngineException ee = new EngineException("Could not store " +
"intermediate relations " +
"to files.");
ee.setStackTrace(sme.getStackTrace());
throw ee;
}
}

最关键的部分,如何来用传递进来的参数进行外部排序。。。。。晕死我。
想听听高手们的分析和提示。
...全文
648 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Achilles_KG 2004-11-22
  • 打赏
  • 举报
回复
sortTuples()我随便写了个最简单的bubble sort,比较字段的优先级有slot[]数组来传递,planbuilder中可以传递进这个slots[],首先比较优先级0的字段,如果相同再比较1,2...这部分我用了函数递归调用,如果相同,不断传入下一个字段进行比较
public void sortTuples(Tuple[] tuples,int[] slots){
for(int i = 1; i < tuples.length; i++)
for(int j = 0; j < tuples.length - i; j++){
compareAndSwap(tuples[j], tuples[j+1], slots, 0);
}
}
//compare the values of 2 tuples
public int compareTuples(Tuple left, Tuple right, int key){
Comparable leftComp = (Comparable) left.getJavaObject(key);
Comparable rightComp = (Comparable) right.getJavaObject(key);
int val = leftComp.compareTo(rightComp);
return val;
}
//swap the values of 2 tuples
public void swapTuples(Tuple left, Tuple right){
Tuple temp;
temp = left;
left = right;
right = temp;
}
//compare 2 tuples, make the value of tuple on the left less than the value of tuple on the right
public void compareAndSwap(Tuple left, Tuple right, int[] slots, int count){
int key = count;
if(compareTuples(left, right, slots[key]) > 0){
swapTuples(left,right);
}
else if((compareTuples(left, right, slots[key])==0) && (++key < slots.length)){
compareAndSwap(left, right, slots, key);
}
}
最近忙其他的事情,研究不够深刻,请高手继续指点,并期待n路归并排序的java程序
Achilles_KG 2004-11-22
  • 打赏
  • 举报
回复
这个实际上是完成attica这个数据库系统的order by子句的功能,我现在已经实现了每个page中的turples进行排序(可以是快速,选择等方法),但是最终要把n个page中的turples进行外部排序,不断写入到文件中,这里我想应该使用的是n路归并排序的算法,这个算法谁能提供一下java的代码?我只会写2路归并排序
这是setup()方法中的部分代码

Relation rel = getInputOperator().getOutputRelation();
inputMan = new RelationIOManager(sm, rel, inputFile);
outputMan = new RelationIOManager(sm, rel, outputFile);

boolean done = false;
while (! done) {
Tuple[] tuples = getInputOperator().getNext();
if (tuples != null) {
done = (tuples[0] instanceof EndOfStreamTuple);
if (! done) {
for (int i = 0; i < tuples.length; i++) {
inputMan.insertTuple(tuples[i]);
}
}
}
}
//sort the turples in each page and write the sorted results in page to disk;
int maxTuples = 0;
int pageNumbers = 0;
inputMan.openPageCursor();
while(inputMan.hasMorePages()){
Page page = inputMan.nextPage();
maxTuples = page.getNumberOfTuples();
int number = page.getNumberOfTuples();
Tuple temp[] = new Tuple[number];
for(int i = 0; i < number; i++){
temp[i] = page.retrieveTuple(i);
}
sortTuples(temp,slots);//可以是任意一种内排序算法,根据具体情况而定了
for(int i = 0; i < number; i++){
page.setTuple(i,temp[i]);
}
sm.writePage(page);
pageNumbers++;
}
hocus 2004-11-18
  • 打赏
  • 举报
回复
没兴趣
我写代码公司付钱
zyp123 2004-11-18
  • 打赏
  • 举报
回复
帮顶
blooney 2004-11-18
  • 打赏
  • 举报
回复
哇?出的作业原来比考试还难 啊??!!

我感兴趣啊,不过没学这么多呢啊
showerXP 2004-11-11
  • 打赏
  • 举报
回复
dbms=data base manage system你真厉害


搂主因该把你的研究成果说一下。还有是什么东西说具体一点。最好是有一个uml类图
vongood 2004-11-11
  • 打赏
  • 举报
回复
gz
Achilles_KG 2004-11-11
  • 打赏
  • 举报
回复
我知道了,在PlanBuilder中有可以写调用ExternalSort的地方,系统最后自然就支持order by,我现在正在研究中,完成后把我的结果发上来
Javaor2004 2004-11-11
  • 打赏
  • 举报
回复
偶是菜鸟刚学习编程没有一个月~~~

请问什么是dbms?
Achilles_KG 2004-11-11
  • 打赏
  • 举报
回复
楼上的,如果采用order by来,必然不是对一张已经生成的特定表的内容进行排序了,那么order by后面的参数必然是一个tuple instance的attribute[i],这样在ExternalSort.java中必然有一个方法可以接受tuple和attibute参数,这个方法的定义如何写比较恰当?在作业要求的setup方法中,没有留下可传递参数的地方
Achilles_KG 2004-11-11
  • 打赏
  • 举报
回复
我的目的是想问问大家有没有兴趣去下载这个dbms系统亲自动手做做,有问题再一起来讨论
jeffzhu 2004-11-11
  • 打赏
  • 举报
回复
这样问东西很少有人回答的
honbo 2004-11-11
  • 打赏
  • 举报
回复
简单地看了前面的一小部分,
这个ExternalSort.java的功能,应该是进行外排序,当然,要根据order by
的内容进行排序了,
PlanBuilder.java应该是一个优化器,采用适当的策略调用前者。
laughter75 2004-11-11
  • 打赏
  • 举报
回复
可以看看,作作,试试 ; 就是把源码看一遍,也能学到不少东西吧!!呵呵!!

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧