Skip to content

Stage 6 (Buffer Management and Write Back policy)

Pasted image 20250127182825.png

Implementing ALTER TABLE RENAME and ALTER TABLE RENAME COLUMN

To kick out a disk block for the buffer, we use the largest timestamp block

TABLE RENAME and TABLE RENAME COLUMN alters the schema, so Schema layer

int Frontend::alter_table_rename(oldName, newName) {
    return Schema::renameRel(oldName, newName);
}

int Frontend::alter_table_rename_column(oldName, newName) {
    return Schema::renameAttr(relname, oldAttrName, newAttrName);
}

int Schema::renameRel() {
    check if oldName matches RELATIONCAT or ATTRIBUTECAT
    check if open -> not allowed
    return BlockAccess::renameRelation(oldRename, newRelName);
}

int BlockAccess::renameRelation() {
    Reset search index of Relation Catalog
    search for the new relation name
    if(exists) -> return E_RELEXIST;

    search the RELCAT for oldRelName
    strcpy(record[RELCAT_RELNAME_INDEX].sVal, relationName);

    do the same with the attributes
    Dont forget to SetBuffer
}

int BlockAccess::renameAttribute() {
    reset the searchIndex for relationCatalog
    search for the relation in relation Catalog

    while(true) {
        search for the records in ATTRIBUTECAT with the same relation Name

        if(attribute rename = oldname) {
            fix it to a variable
            break
        }
        if(attribute name = newName) {
            return E_ATTREXISTS 
        }
    }
}

StaticBuffer

int StaticBuffer::setDirtyBit(int blockNum) {
    int bufferNum = getFreeBuffer(blockNum);
}

changes to
int StaticBuffer::getFreeBuffer(int blockNum) {
    // implemented LRU Algorithm
    // Writing back the disk at the same time
}

int BlockBuffer::loadBlockAndGetBufferPtr(unsigned char **bufferPtr)

{

    int bufferNum = StaticBuffer::getBufferNum(this->blockNum);



    if (bufferNum == E_BLOCKNOTINBUFFER)

    {

        bufferNum = StaticBuffer::getFreeBuffer(this->blockNum);



        if (bufferNum == E_OUTOFBOUND)

        {

            return E_OUTOFBOUND;

        }

        for(int i = 0; i < MAX_OPEN; i++) {

           StaticBuffer::metainfo[i].timeStamp = 0;

        }

        StaticBuffer::metainfo[bufferNum].timeStamp += 1;

        Disk::readBlock(StaticBuffer::blocks[bufferNum], this->blockNum);

    }



    *bufferPtr = StaticBuffer::blocks[bufferNum];



    return SUCCESS;

}