Skip to content

Commit

Permalink
* Partial implementation of merging.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcalero committed Feb 26, 2013
1 parent afda480 commit a2ed524
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion attica.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ attica.directory=/afs/inf.ed.ac.uk/user/s09/s0948339/Courses/ADBS/attica/dist/da
# The attica temporary directory
attica.temp.directory=/afs/inf.ed.ac.uk/user/s09/s0948339/Courses/ADBS/attica/dist/tmpDir
# The number of pages in the buffer pool
attica.buffersize=50
attica.buffersize=10
70 changes: 58 additions & 12 deletions src/org/dejave/attica/engine/operators/ExternalSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@
package org.dejave.attica.engine.operators;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

import org.dejave.attica.model.Relation;
import org.dejave.attica.storage.Tuple;

import org.dejave.attica.storage.FileUtil;
import org.dejave.attica.storage.Page;
import org.dejave.attica.storage.RelationIOManager;
import org.dejave.attica.storage.StorageManager;
import org.dejave.attica.storage.StorageManagerException;

import org.dejave.attica.storage.FileUtil;
import org.dejave.attica.storage.Tuple;

/**
* ExternalSort: Your implementation of sorting.
Expand Down Expand Up @@ -155,7 +153,7 @@ public void setup() throws EngineException {
if (! done) inputIOMan.insertTuple(tuple);
}
}
System.out.println(">>Number of pages: " +
System.out.println(">> Number of pages: " +
FileUtil.getNumberOfPages(inputFile));
//////
// The input is now in inputIOMan and can be
Expand Down Expand Up @@ -186,13 +184,13 @@ public void setup() throws EngineException {
bufferedPages.clear();
}

System.out.println("Number of temporary files: " +
System.out.println(">> Number of temporary files: " +
tempFiles.size());
System.out.println(">> Ceiling(" +
FileUtil.getNumberOfPages(inputFile) +
"/" +
"3)"+
" = " +
buffers +
") = " +
tempFiles.size());

////////////////////////////////////////////
Expand All @@ -214,7 +212,7 @@ public void setup() throws EngineException {
/////
initMergeRun();

System.out.println(">>Sorting took: " +
System.out.println(">> Sorting took: " +
(float)(System.currentTimeMillis() - time)*0.001 +
"s");

Expand Down Expand Up @@ -261,7 +259,7 @@ private void sortPages(ArrayList<Page> pages) {
}
}

private void initMergeRun() throws IOException, StorageManagerException {
private void initMergeRun() throws IOException, StorageManagerException, EngineException {
/////////////////////////////////////////
// Temporary merge placeholder.
// Simply copies the input to the output
Expand All @@ -273,9 +271,57 @@ private void initMergeRun() throws IOException, StorageManagerException {
}
}
}
tempIOManagers.clear();
/////////////////////////////////////////


/////
//
/////
while (tempIOManagers.size() > 0) {
if (tempIOManagers.size() < buffers - 1) {
mergeRun(tempIOManagers.size());
} else {
mergeRun(buffers - 1);
}
}
}

@SuppressWarnings("unchecked")
private void mergeRun(int count) throws IOException, StorageManagerException, EngineException {
// The current index being compared on each
// of the lists being merged
int[] indices = new int[count];

Tuple min = null;
boolean done = false;

// Create temporary file to be used as output
String tempFile = FileUtil.createTempFileName();
sm.createFile(tempFile);
tempFiles.add(tempFile);
RelationIOManager outMan = new RelationIOManager(sm, getOutputRelation(), tempFile);


while (!done) {
// Find the minimum value of the next tuples in
// each file being checked.
for (int i = 0; i < count; i++) {
RelationIOManager man = tempIOManagers.get(i);
Tuple newTuple = man.tuples().iterator().next();
// man.tuples().iterator().

if (min == null) {
min = newTuple;
} else if (newTuple.getValue(slots[0])
.compareTo(min.getValue(slots[0])) < 0) {
min = newTuple;
}
}

// Add the minumum value to the output manager.
outMan.insertTuple(min);
min = null;
}
}

private void quickSort(Page p) {
Expand Down

0 comments on commit a2ed524

Please sign in to comment.