Skip to main content

Schema Layer Stub Code

Schema.cpp

int createRel(char relName[],int nAttrs, char attrs[][ATTR_SIZE],int attrtype[]){

// declare variable relNameAsAttribute of type Attribute
// copy the relName into relNameAsAttribute.sVal

// declare a variable targetRelId of type RecId

/*
Reset the searchIndex using RelCacheTable::resetSearhIndex()
Search the relation catalog (relId given by the constant RELCAT_RELID)
for attribute value attribute "RelName" = relNameAsAttribute using
BlockAccess::linearSearch() with OP = EQ
*/

// if a relation with name `relName` already exists ( linearSearch() does
// not return {-1,-1} )
// return E_RELEXIST;

// compare every pair of attributes of attrNames[] array
// if any attribute names have same string value,
// return E_DUPLICATEATTR (i.e 2 attributes have same value)

/* declare relCatRecord of type Attribute which will be used to store the
record corresponding to the new relation which will be inserted
into relation catalog */
Attribute relCatRecord[RELCAT_NO_ATTRS];
// fill relCatRecord fields as given below
// offset RELCAT_REL_NAME_INDEX: relName
// offset RELCAT_NO_ATTRIBUTES_INDEX: numOfAttributes
// offset RELCAT_NO_RECORDS_INDEX: 0
// offset RELCAT_FIRST_BLOCK_INDEX: -1
// offset RELCAT_LAST_BLOCK_INDEX: -1
// offset RELCAT_NO_SLOTS_PER_BLOCK_INDEX: floor((2016 / (16 * nAttrs + 1)))
// (number of slots is calculated as specified in the physical layer docs)

// retVal = BlockAccess::insert(RELCAT_RELID(=0), relCatRecord);
// if BlockAccess::insert fails return retVal
// (this call could fail if there is no more space in the relation catalog)

// iterate through 0 to numOfAttributes - 1 :
{
/* declare Attribute attrCatRecord[6] to store the attribute catalog
record corresponding to i'th attribute of the argument passed*/
// (where i is the iterator of the loop)
// fill attrCatRecord fields as given below
// offset ATTRCAT_REL_NAME_INDEX: relName
// offset ATTRCAT_ATTR_NAME_INDEX: attrNames[i]
// offset ATTRCAT_ATTR_TYPE_INDEX: attrTypes[i]
// offset ATTRCAT_PRIMARY_FLAG_INDEX: -1
// offset ATTRCAT_ROOT_BLOCK_INDEX: -1
// offset ATTRCAT_OFFSET_INDEX: i

// retVal = BlockAccess::insert(ATTRCAT_RELID(=1), attrCatRecord);
/* if attribute catalog insert fails:
delete the relation by calling deleteRel(targetrel) of schema layer
return E_DISKFULL
// (this is necessary because we had already created the
// relation catalog entry which needs to be removed)
*/
}

// return SUCCESS
}


int Schema::deleteRel(char *relName) {
// if the relation to delete is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// get the rel-id using appropriate method of OpenRelTable class by
// passing relation name as argument

// if relation is opened in open relation table, return E_RELOPEN

// Call BlockAccess::deleteRelation() with appropriate argument.

// return the value returned by the above deleteRelation() call

/* the only that should be returned from deleteRelation() is E_RELNOTEXIST.
The deleteRelation call may return E_OUTOFBOUND from the call to
loadBlockAndGetBufferPtr, but if your implementation so far has been
correct, it should not reach that point. That error could only occur
if the BlockBuffer was initialized with an invalid block number.
*/
}


int createIndex(char relName[ATTR_SIZE],char attrName[ATTR_SIZE]){
// if the relName is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// get the relation's rel-id using OpenRelTable::getRelId() method

// if relation is not open in open relation table, return E_RELNOTOPEN
// (check if the value returned from getRelId function call = E_RELNOTOPEN)

// create a bplus tree using BPlusTree::bPlusCreate() and return the value
return BPlusTree::bPlusCreate(relId, attrName);
}


int Schema::dropIndex(char *relName, char *attrName) {
// if the relName is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// get the rel-id using OpenRelTable::getRelId()

// if relation is not open in open relation table, return E_RELNOTOPEN
// (check if the value returned from getRelId function call = E_RELNOTOPEN)

// get the attribute catalog entry corresponding to the attribute
// using AttrCacheTable::getAttrCatEntry()

// if getAttrCatEntry() fails, return E_ATTRNOTEXIST

int rootBlock = /* get the root block from attrcat entry */;

if (/* attribute does not have an index (rootBlock = -1) */) {
return E_NOINDEX;
}

// destroy the bplus tree rooted at rootBlock using BPlusTree::bPlusDestroy()
BPlusTree::bPlusDestroy(rootBlock);

// set rootBlock = -1 in the attribute cache entry of the attribute using
// AttrCacheTable::setAttrCatEntry()

return SUCCESS;
}


int renameRel(char oldRelName[ATTR_SIZE], char newRelName[ATTR_SIZE]) {
// if the oldRelName or newRelName is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// if the relation is open
// (check if OpenRelTable::getRelId() returns E_RELNOTOPEN)
// return E_RELOPEN

// retVal = BlockAccess::renameRelation(oldRelName, newRelName);
// return retVal
}


int Schema::renameAttr(char *relName, char *oldAttrName, char *newAttrName) {
// if the relName is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// if the relation is open
// (check if OpenRelTable::getRelId() returns E_RELNOTOPEN)
// return E_RELOPEN

// Call BlockAccess::renameAttribute with appropriate arguments.

// return the value returned by the above renameAttribute() call
}


int Schema::openRel(char *relName) {
// Call openRelation method of OpenRelTable by passing appropriate arguments
int relId = OpenRelTable::openRel(relName);

// if relId is valid (>= 0) return SUCCESS
// else return the error
}


int closeRel(char relName[ATTR_SIZE]) {
// if the relName is either Relation Catalog or Attribute Catalog,
// return E_NOTPERMITTED
// (check if the relation names are either "RELATIONCAT" and "ATTRIBUTECAT".
// you may use the following constants: RELCAT_NAME and ATTRCAT_NAME)

// get the relation's rel-id using OpenRelTable::getRelationId() method

// if relation is not open in open relation table, return E_RELNOTOPEN
// (check if the value returned from getRelId function call = E_RELNOTOPEN)

// close the relId'th relation using OpenRelTable::closeRelation()
// let the return value be retVal
// return retVal;
}