Skip to content

Applied Refactoring techniques to improve code quality #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/main/java/simpledb/algorithm/Join/HashJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,4 @@ public TupleIterator doJoin() {
return null;
}

@Override
public void close() {

}
}
1 change: 0 additions & 1 deletion src/main/java/simpledb/algorithm/Join/JoinStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ protected int fetchTuples(final OpIterator child, final Tuple[] tuples) throws E
// Join child1 and child2, return a tuple iterator result
public abstract TupleIterator doJoin();

public abstract void close();
}
4 changes: 0 additions & 4 deletions src/main/java/simpledb/algorithm/Join/NestedLoopJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@ public TupleIterator doJoin() {
return new TupleIterator(this.td, tuples);
}

@Override
public void close() {

}
}
105 changes: 58 additions & 47 deletions src/main/java/simpledb/algorithm/Join/SortMergeJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
// Phase4: do join on block1 and block2

public class SortMergeJoin extends JoinStrategy {
private final int blockCacheSize = 131072 * 5;
private Tuple[] block1;
private Tuple[] block2;
private final int blockCacheSize = 131072 * 5;
private Tuple[] block1;
private Tuple[] block2;

private JoinPredicate lt;
private JoinPredicate eq;
private JoinPredicate lt;
private JoinPredicate eq;

private static final String LESS_THAN_OR_EQ_OPN = "LESS_THAN_OR_EQ";
private static final String GREATER_THAN_OR_EQ_OPN = "GREATER_THAN_OR_EQ";

public SortMergeJoin(final OpIterator child1, final OpIterator child2, final TupleDesc td,
final JoinPredicate joinPredicate) {
Expand Down Expand Up @@ -78,57 +81,66 @@ private void mergeJoin(final List<Tuple> tupleList, int end1, int end2) {
final Predicate.Op op = this.joinPredicate.getOperator();
switch (op) {
case EQUALS: {
while (index1 < end1 && index2 < end2) {
final Tuple lTuple = this.block1[index1];
final Tuple rTuple = this.block2[index2];
if (eq.filter(lTuple, rTuple)) {
// If equal , we should find the right boundary that equal to lTuple in block1 and rTuple in block2
final JoinPredicate eq1 = new JoinPredicate(field1, Predicate.Op.EQUALS, field1);
final JoinPredicate eq2 = new JoinPredicate(field2, Predicate.Op.EQUALS, field2);
int begin1 = index1 + 1, begin2 = index2 + 1;
while (begin1 < end1 && eq1.filter(lTuple, this.block1[begin1]))
begin1++;
while (begin2 < end2 && eq2.filter(rTuple, this.block2[begin2]))
begin2++;
for (int i = index1; i < begin1; i++) {
for (int j = index2; j < begin2; j++) {
tupleList.add(mergeTuple(this.block1[i], this.block2[j], this.td));
}
}
index1 = begin1;
index2 = begin2;
} else if (lt.filter(lTuple, rTuple)) {
index1++;
} else {
index2++;
}
}
equalsPredicate(tupleList, end1, end2, field1, field2, index1, index2);
return;
}
case LESS_THAN:
case LESS_THAN_OR_EQ: {
while (index1 < end1) {
final Tuple lTuple = this.block1[index1++];
while (index2 < end2 && !this.joinPredicate.filter(lTuple, this.block2[index2]))
index2++;
while (index2 < end2) {
final Tuple rTuple = this.block2[index2++];
tupleList.add(mergeTuple(lTuple, rTuple, this.td));
}
}
joinTuples(tupleList, end1, end2, index1, index2, LESS_THAN_OR_EQ_OPN);
return;
}
case GREATER_THAN:
case GREATER_THAN_OR_EQ: {
while (index1 < end1) {
final Tuple lTuple = this.block1[index1++];
while (index2 < end2 && this.joinPredicate.filter(lTuple, this.block2[index2]))
index2++;
for (int i = 0; i < index2; i++) {
final Tuple rTuple = this.block2[i];
tupleList.add(mergeTuple(lTuple, rTuple, this.td));
joinTuples(tupleList, end1, end2, index1, index2, GREATER_THAN_OR_EQ_OPN);

}
}
}

private void joinTuples(List<Tuple> tupleList, int end1, int end2, int index1, int index2, String opertaion) {
while (index1 < end1) {
final Tuple lTuple = this.block1[index1++];
while (index2 < end2 && !this.joinPredicate.filter(lTuple, this.block2[index2]))
index2++;
if (LESS_THAN_OR_EQ_OPN.equals(opertaion)) {
while (index2 < end2) {
final Tuple rTuple = this.block2[index2++];
tupleList.add(mergeTuple(lTuple, rTuple, this.td));
}
} else {
for (int i = 0; i < index2; i++) {
final Tuple rTuple = this.block2[i];
tupleList.add(mergeTuple(lTuple, rTuple, this.td));
}
}
}
}

private void equalsPredicate(List<Tuple> tupleList, int end1, int end2, int field1, int field2, int index1,
int index2) {
while (index1 < end1 && index2 < end2) {
final Tuple lTuple = this.block1[index1];
final Tuple rTuple = this.block2[index2];
if (eq.filter(lTuple, rTuple)) {
// If equal , we should find the right boundary that equal to lTuple in block1 and rTuple in block2
final JoinPredicate eq1 = new JoinPredicate(field1, Predicate.Op.EQUALS, field1);
final JoinPredicate eq2 = new JoinPredicate(field2, Predicate.Op.EQUALS, field2);
int begin1 = index1 + 1, begin2 = index2 + 1;
while (begin1 < end1 && eq1.filter(lTuple, this.block1[begin1]))
begin1++;
while (begin2 < end2 && eq2.filter(rTuple, this.block2[begin2]))
begin2++;
for (int i = index1; i < begin1; i++) {
for (int j = index2; j < begin2; j++) {
tupleList.add(mergeTuple(this.block1[i], this.block2[j], this.td));
}
}
index1 = begin1;
index2 = begin2;
} else if (lt.filter(lTuple, rTuple)) {
index1++;
} else {
index2++;
}
}
}
Expand All @@ -147,7 +159,6 @@ private void sortTuples(final Tuple[] tuples, int field, int len) {
});
}

@Override
public void close() {
this.block1 = null;
this.block2 = null;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/simpledb/execution/Aggregate.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class Aggregate extends Operator {

private Aggregator.Op op;
private Aggregator aggregator;
private TupleDesc td;

private TupleIterator iterator;

Expand Down
1 change: 0 additions & 1 deletion src/main/java/simpledb/execution/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Delete extends Operator {
private TransactionId tid;
private OpIterator child;
private int tableId;
private TupleDesc td;
private boolean isFetched;

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/simpledb/execution/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class Filter extends Operator {

private Predicate predicate;
private OpIterator child;
private TupleDesc tupleDesc;

/**
* Constructor accepts a predicate to apply and a child operator to read
Expand All @@ -31,7 +30,7 @@ public Filter(Predicate p, OpIterator child) {
// some code goes here
this.child = child;
this.predicate = p;
this.tupleDesc = child.getTupleDesc();
this.td = child.getTupleDesc();
}

public Predicate getPredicate() {
Expand All @@ -41,7 +40,7 @@ public Predicate getPredicate() {

public TupleDesc getTupleDesc() {
// some code goes here
return this.tupleDesc;
return this.td;
}

public void open() throws DbException, NoSuchElementException, TransactionAbortedException {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/simpledb/execution/HashEquiJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class HashEquiJoin extends Operator {
private static final long serialVersionUID = 1L;
private final JoinPredicate pred;
private OpIterator child1, child2;
private final TupleDesc comboTD;
transient private Tuple t1 = null;
transient private Tuple t2 = null;

Expand All @@ -34,15 +33,15 @@ public HashEquiJoin(JoinPredicate p, OpIterator child1, OpIterator child2) {
this.pred = p;
this.child1 = child1;
this.child2 = child2;
comboTD = TupleDesc.merge(child1.getTupleDesc(), child2.getTupleDesc());
this.td = TupleDesc.merge(child1.getTupleDesc(), child2.getTupleDesc());
}

public JoinPredicate getJoinPredicate() {
return pred;
}

public TupleDesc getTupleDesc() {
return comboTD;
return this.td;
}

public String getJoinField1Name() {
Expand Down Expand Up @@ -119,7 +118,7 @@ private Tuple processList() {
int td2n = t2.getTupleDesc().numFields();

// set fields in combined tuple
Tuple t = new Tuple(comboTD);
Tuple t = new Tuple(this.td);
for (int i = 0; i < td1n; i++)
t.setField(i, t1.getField(i));
for (int i = 0; i < td2n; i++)
Expand Down
1 change: 0 additions & 1 deletion src/main/java/simpledb/execution/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class Insert extends Operator {
private TransactionId tid;
private OpIterator child;
private int tableId;
private TupleDesc td;
private boolean isFetched;

/**
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/simpledb/execution/Join.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class Join extends Operator {
private JoinPredicate joinPredicate;
private OpIterator child1;
private OpIterator child2;
private TupleDesc td;

private JoinStrategy joinStrategy;
private TupleIterator iterator;
Expand Down Expand Up @@ -99,7 +98,9 @@ public void open() throws DbException, NoSuchElementException, TransactionAborte

public void close() {
// some code goes here
this.joinStrategy.close();
if (this.joinStrategy instanceof SortMergeJoin) {
((SortMergeJoin) this.joinStrategy).close();
}
this.iterator.close();
this.child1.close();
this.child2.close();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/simpledb/execution/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
public abstract class Operator implements OpIterator {

private static final long serialVersionUID = 1L;
public TupleDesc td;

public boolean hasNext() throws DbException, TransactionAbortedException {
if (!this.open)
Expand Down
1 change: 0 additions & 1 deletion src/main/java/simpledb/execution/OrderBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class OrderBy extends Operator {

private static final long serialVersionUID = 1L;
private OpIterator child;
private final TupleDesc td;
private final List<Tuple> childTups = new ArrayList<>();
private final int orderByField;
private final String orderByFieldName;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/simpledb/execution/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class Project extends Operator {

private static final long serialVersionUID = 1L;
private OpIterator child;
private final TupleDesc td;
private final List<Integer> outFieldIds;

/**
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/simpledb/index/BTreeFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
public class BTreeFile implements DbFile {

private final File f;
private final File file;
private final TupleDesc td;
private final int tableid;
private final int keyField;
Expand All @@ -42,7 +42,7 @@ public class BTreeFile implements DbFile {
* @param td - the tuple descriptor of tuples in the file
*/
public BTreeFile(File f, int key, TupleDesc td) {
this.f = f;
this.file = f;
this.tableid = f.getAbsoluteFile().hashCode();
this.keyField = key;
this.td = td;
Expand All @@ -52,7 +52,7 @@ public BTreeFile(File f, int key, TupleDesc td) {
* Returns the File backing this BTreeFile on disk.
*/
public File getFile() {
return f;
return file;
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ public TupleDesc getTupleDesc() {
public Page readPage(PageId pid) {
BTreePageId id = (BTreePageId) pid;

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
if (id.pgcateg() == BTreePageId.ROOT_PTR) {
byte[] pageBuf = new byte[BTreeRootPtrPage.getPageSize()];
int retval = bis.read(pageBuf, 0, BTreeRootPtrPage.getPageSize());
Expand Down Expand Up @@ -142,7 +142,7 @@ public void writePage(Page page) throws IOException {
BTreePageId id = (BTreePageId) page.getId();

byte[] data = page.getPageData();
RandomAccessFile rf = new RandomAccessFile(f, "rw");
RandomAccessFile rf = new RandomAccessFile(file, "rw");
if (id.pgcateg() == BTreePageId.ROOT_PTR) {
rf.write(data);
rf.close();
Expand All @@ -159,7 +159,7 @@ public void writePage(Page page) throws IOException {
*/
public int numPages() {
// we only ever write full pages
return (int) ((f.length() - BTreeRootPtrPage.getPageSize()) / BufferPool.getPageSize());
return (int) ((file.length() - BTreeRootPtrPage.getPageSize()) / BufferPool.getPageSize());
}

/**
Expand Down Expand Up @@ -1049,9 +1049,9 @@ public List<Page> deleteTuple(TransactionId tid, Tuple t) throws DbException, IO
BTreeRootPtrPage getRootPtrPage(TransactionId tid, Map<PageId, Page> dirtypages) throws DbException, IOException,
TransactionAbortedException {
synchronized (this) {
if (f.length() == 0) {
if (file.length() == 0) {
// create the root pointer page and the root page
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(f, true));
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(file, true));
byte[] emptyRootPtrData = BTreeRootPtrPage.createEmptyPageData();
byte[] emptyLeafData = BTreeLeafPage.createEmptyPageData();
bw.write(emptyRootPtrData);
Expand Down Expand Up @@ -1111,7 +1111,7 @@ public int getEmptyPageNo(TransactionId tid, Map<PageId, Page> dirtypages) throw
if (headerId == null) {
synchronized (this) {
// create the new page
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(f, true));
BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream(file, true));
byte[] emptyData = BTreeInternalPage.createEmptyPageData();
bw.write(emptyData);
bw.close();
Expand Down Expand Up @@ -1146,7 +1146,7 @@ private Page getEmptyPage(TransactionId tid, Map<PageId, Page> dirtypages, int p
BTreePageId newPageId = new BTreePageId(tableid, emptyPageNo, pgcateg);

// write empty page to disk
RandomAccessFile rf = new RandomAccessFile(f, "rw");
RandomAccessFile rf = new RandomAccessFile(file, "rw");
rf.seek(BTreeRootPtrPage.getPageSize() + (long) (emptyPageNo - 1) * BufferPool.getPageSize());
rf.write(BTreePage.createEmptyPageData());
rf.close();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/simpledb/optimizer/CostCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

/** Class returned by {@link JoinOptimizer#computeCostAndCardOfSubplan} specifying the
/** Class returned by {@linkJoinOptimizer#computeCostAndCardOfSubplan} specifying the
cost and cardinality of the optimal plan represented by plan.
*/
public class CostCard {
Expand Down
Loading