Skip to content

Commit

Permalink
Added NLIJ and NBJ. Changed QL_M to use those two.
Browse files Browse the repository at this point in the history
  • Loading branch information
junkumar committed May 23, 2011
1 parent 5005444 commit ca69431
Show file tree
Hide file tree
Showing 21 changed files with 928 additions and 112 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ src/qlfile
src/gtestfile
build/
lib/
src/contest
nested_scratch
6 changes: 4 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ SM_SOURCES = statistics.cc sm_error.cc sm_manager.cc printer.cc \
QL_SOURCES = statistics.cc ql_manager.cc ql_error.cc file_scan.cc \
file_scan_gtest.cc index_scan.cc index_scan_gtest.cc \
nested_loop_join.cc nested_loop_join_gtest.cc \
ql_manager_gtest.cc projection.cc
ql_manager_gtest.cc projection.cc nested_block_join_gtest.cc \

UTILS_SOURCES = dbcreate.cc dbdestroy.cc redbase.cc
PARSER_SOURCES = scan.c parse.c nodes.c interp.c
TESTER_SOURCES = pf_test1.cc pf_test2.cc pf_test3.cc rm_test.cc \
ix_test.cc parser_test.cc ix_testkpg_2.cc
ix_test.cc parser_test.cc ix_testkpg_2.cc nested_scratch.cc \

GTEST_SOURCES = $(GTEST_ROOT)/src/gtest-all.cc

PF_OBJECTS = $(addprefix $(BUILD_DIR), $(PF_SOURCES:.cc=.o))
Expand Down
5 changes: 5 additions & 0 deletions src/file_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "redbase.h"
#include "iterator.h"
#include "rm.h"
#include "sm.h"

using namespace std;

Expand All @@ -30,6 +31,10 @@ class FileScan: public Iterator {

RC IsValid();
virtual RC Eof() const { return RM_EOF; }
virtual int GetNumPages() const { return psmm->GetNumPages(relName); }
virtual int GetNumSlotsPerPage() const { return rfs.GetNumSlotsPerPage(); }
virtual int GetNumRecords() const { return psmm->GetNumRecords(relName); }
virtual RC GotoPage(PageNum p) { return rfs.GotoPage(p); }

private:
RM_FileScan rfs;
Expand Down
52 changes: 36 additions & 16 deletions src/index_scan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ IndexScan::IndexScan(SM_Manager& smm,
bool desc)
:ifs(IX_IndexScan()), prmm(&rmm), pixm(&ixm), psmm(&smm),
rmh(RM_FileHandle()), ixh(IX_IndexHandle()), relName(relName_),
nOFilters(nOutFilters), oFilters(NULL)
nOFilters(nOutFilters), oFilters(NULL), attrName(indexAttrName)
{
if(relName == NULL || indexAttrName == NULL) {
if(relName_ == NULL || indexAttrName == NULL) {
status = SM_NOSUCHTABLE;
return;
}

attrCount = -1;
attrs = NULL;
RC rc = smm.GetFromTable(relName, attrCount, attrs);
RC rc = smm.GetFromTable(relName.c_str(), attrCount, attrs);
if (rc != 0) {
status = rc;
return;
Expand All @@ -47,26 +47,31 @@ IndexScan::IndexScan(SM_Manager& smm,
assert(cond.rhsValue.data == NULL || cond.bRhsIsAttr == FALSE);
// only conditions
// on index key can be pushed down.
assert(strcmp(cond.lhsAttr.attrName, indexAttrName) == 0);
assert(strcmp(cond.lhsAttr.relName, relName) == 0);
assert(strcmp(cond.lhsAttr.attrName, indexAttrName) == 0 ||
strcmp(cond.rhsAttr.attrName, indexAttrName) == 0);
assert(strcmp(cond.lhsAttr.relName, relName.c_str()) == 0 ||
strcmp(cond.rhsAttr.relName, relName.c_str()) == 0);

rc = prmm->OpenFile(relName, rmh);
rc = prmm->OpenFile(relName.c_str(), rmh);
if (rc != 0) {
status = rc;
return;
}

rc = pixm->OpenIndex(relName, indexNo, ixh);
rc = pixm->OpenIndex(relName.c_str(), indexNo, ixh);
if (rc != 0) {
status = rc;
return;
}

rc = ifs.OpenScan(ixh,
cond.op,
cond.rhsValue.data,
NO_HINT,
desc);
// rc = ifs.OpenScan(ixh,
// cond.op,
// cond.rhsValue.data,
// NO_HINT,
// desc);
this->desc = desc;
this->c = cond.op;
rc = ReOpenScan(cond.rhsValue.data);
if (rc != 0) {
status = rc;
return;
Expand All @@ -78,7 +83,7 @@ IndexScan::IndexScan(SM_Manager& smm,
}

explain << "IndexScan\n";
explain << " relName = " << relName << "\n";
explain << " relName = " << relName.c_str() << "\n";
explain << " attrName = " << indexAttrName << "\n";
if(cond.rhsValue.data != NULL)
explain << " ScanCond = " << cond << "\n";
Expand All @@ -91,6 +96,21 @@ IndexScan::IndexScan(SM_Manager& smm,
status = 0;
}

// will close if already open
// made available for NLIJ to use
// only value is new, rest of the index attr condition is the same
RC IndexScan::ReOpenScan(void* newData)
{
if(ifs.IsOpen())
ifs.CloseScan();

return ifs.OpenScan(ixh,
c,
newData,
NO_HINT,
desc);
}

string IndexScan::Explain()
{
return indent + explain.str();
Expand Down Expand Up @@ -172,7 +192,7 @@ RC IndexScan::GetNext(Tuple &t)
Condition cond = oFilters[i];
DataAttrInfo condAttr;
RID r;
rc = psmm->GetAttrFromCat(relName, cond.lhsAttr.attrName, condAttr, r);
rc = psmm->GetAttrFromCat(relName.c_str(), cond.lhsAttr.attrName, condAttr, r);
if (rc != 0) return rc;

Predicate p(condAttr.attrType,
Expand All @@ -186,10 +206,10 @@ RC IndexScan::GetNext(Tuple &t)
if(cond.bRhsIsAttr == TRUE) {
DataAttrInfo rhsAttr;
RID r;
rc = psmm->GetAttrFromCat(relName, cond.lhsAttr.attrName, rhsAttr, r);
rc = psmm->GetAttrFromCat(relName.c_str(), cond.lhsAttr.attrName, rhsAttr, r);
if (rc != 0) return rc;
rhs = (buf + rhsAttr.offset);
}
}

if(!p.eval(buf, rhs, cond.op)) {
recordIn = false;
Expand Down
13 changes: 12 additions & 1 deletion src/index_scan.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,27 @@ class IndexScan: public Iterator {
RC IsValid();
virtual RC Eof() const { return IX_EOF; }

// will close if already open
// made available for NLIJ to use
// only value is new, rest of the index attr condition is the same
virtual RC ReOpenScan(void* newData);
virtual string GetIndexAttr() const { return attrName; }
virtual string GetIndexRel() const { return relName; }

private:
IX_IndexScan ifs;
IX_Manager* pixm;
RM_Manager* prmm;
SM_Manager* psmm;
const char * relName;
string relName;
string attrName;
RM_FileHandle rmh;
IX_IndexHandle ixh;
int nOFilters;
Condition* oFilters;
// options used to open scan
bool desc;
CompOp c;
};

#endif // INDEXSCAN_H
9 changes: 8 additions & 1 deletion src/ix_indexscan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ RC IX_IndexScan::OpenScan(const IX_IndexHandle &fileHandle,
value = value_; // TODO deep copy ?
OpOptimize();
}

// cerr << "IX_IndexScan::OpenScan with value ";
// if(value == NULL)
// cerr << "NULL" << endl;
// else
// cerr << *(int*)value << endl;
// pixh->Print(cerr);
return 0;
}
Expand Down Expand Up @@ -262,7 +268,7 @@ RC IX_IndexScan::CloseScan()
currNode = NULL;
currPos = -1;
if(currKey != NULL) {
delete [] currKey;
delete [] ((char*) currKey);
currKey = NULL;
}
currRid = RID(-1, -1);
Expand All @@ -278,6 +284,7 @@ RC IX_IndexScan::ResetState()
currPos = -1;
lastNode = NULL;
eof = false;
foundOne = false;

return this->OpOptimize();
}
Expand Down
140 changes: 140 additions & 0 deletions src/nested_block_join.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//
// File: nested_block_join.h
//

#ifndef NESTEDBLOCKJOIN_H
#define NESTEDBLOCKJOIN_H

#include "nested_loop_join.h"
#include "file_scan.h"
#include <vector>
#include <cmath>

using namespace std;

class NestedBlockJoin: public NestedLoopJoin {
public:
NestedBlockJoin(
FileScan* lhsIt, // access for left i/p to join -R
Iterator* rhsIt, // access for right i/p to join -S
RC& status,
int nOutFilters = 0,
const Condition outFilters[] = NULL,
int blockPages = 40
)
:NestedLoopJoin(lhsIt, rhsIt, status, nOutFilters, outFilters),
blockSize(blockPages-3) // leave room for 3 other pages
{
justOpen = true;
// outer params
nPages = lhsIt->GetNumPages();
pageSize = lhsIt->GetNumSlotsPerPage();

cerr << "nPages, pageSize " << nPages << ", " << pageSize << endl;

int nBlocks = ceil(float(nPages)/blockSize);
for(int j = 0; j < nBlocks; j++) {
// RM page 0 is header - so things start at 1
blocks.push_back(1 + j*blockSize);
cerr << j << "th block - start page " << blocks[j] << endl;
}
blockIt = blocks.begin();
left_rc = 0;
right_rc = 0;

explain.str("");
explain << "NestedBlockJoin\n";
if(nOFilters > 0) {
explain << " nJoinConds = " << nOutFilters << "\n";
for (int i = 0; i < nOutFilters; i++)
explain << " joinConds[" << i << "]:" << outFilters[i] << "\n";
}
}

virtual RC Open() { justOpen = true; return this->NestedLoopJoin::Open(); }


virtual RC GetNext(Tuple &t) {
RC invalid = IsValid(); if(invalid) return invalid;

// cout << "GetNext [[" << left << " - " << right << endl;

if(!bIterOpen)
return QL_FNOTOPEN;

bool joined = false;

if(justOpen) {
right_rc = rhsIt->GetNext(right);
// cout << "justOpen " << left << " - " << right << endl;
}
justOpen = false;

while(blockIt != blocks.end()) {
while(right_rc != rhsIt->Eof()) {
vector<int>::iterator next = blockIt;
next++;
int nextp = -1;
if(next != blocks.end())
nextp = *next;
while(left_rc != lhsIt->Eof() && left.GetRid().Page() != nextp) {
// nrecs++;
// lhsIt++;
// cout << left << " - " << right << endl;
EvalJoin(t, joined);

left_rc = lhsIt->GetNext(left);

if(joined)
return 0;

if(left_rc == lhsIt->Eof())
break;

// cout << "[" << *lhsIt << ", " << *rhsIt << "]" << endl;
}
// nrecs = 0;
// rhsIt++;
right_rc = rhsIt->GetNext(right);
// cerr << "new right is " << right << endl;
// advance to right block start
lhsIt->Close();
lhsIt->Open();
FileScan* lfs = dynamic_cast<FileScan*>(lhsIt);
assert(lfs != NULL);
lfs->GotoPage(*blockIt);
left_rc = lhsIt->GetNext(left);
}
blockIt++;
if(blockIt == blocks.end())
break;
// cout << "block " << *blockIt << endl;

// advance to right block start
lhsIt->Close();
lhsIt->Open();
FileScan* lfs = dynamic_cast<FileScan*>(lhsIt);
assert(lfs != NULL);
lfs->GotoPage(*blockIt);
left_rc = lhsIt->GetNext(left);

rhsIt->Close();
rhsIt->Open();
right_rc = rhsIt->GetNext(right);
}
// eof
return QL_EOF;
}

protected:
RC left_rc;
RC right_rc;
bool justOpen;
int blockSize;
int nPages; // num pages used by outer
int pageSize; // num slots per page for outer
vector<int> blocks;
vector<int>::iterator blockIt;
};

#endif // NESTEDBLOCKJOIN_H
Loading

0 comments on commit ca69431

Please sign in to comment.