Skip to content

Stage 3 (Shit gets real ngl)

Disk Buffer

Due to principle of Locality, NITCbase buffers all the disk i/o operations. We will be pre-allocating memory for holding 32 disk blocks. All operations will be done on that buffer until the disk block is swapped with more recently required disk block (LRU Algorithm).

We will be using loadBlockandGetBufferPointer() instead of read Block

We use unsigned char block[BUFFER_CAPACITY][BLOCK_SIZE] in Static Buffer

StaticBuffer::loadBufferAndGetPtr(unsigned char **bufferPtr) {
    check if this->blockNum is valid
    if(free) {
        StaticBuffer::getFreeBlock(this->blockNum);
        Disk::readBlock(StaticBuffer::blocks[blockNum]);
    }
    *bufferPtr = StaticBuffer::blocks[blockNum];
}

Cache

All operations require relation catalog and attribute catalog data, due to which we need cache

Relation Catalog and Attribute Catalog are arrays of size 12 (MAX_OPEN)

Each entry stores the catalog entries for a relation.

Relation Cache

  • Purpose: Stores information about a database relation that is actively being used (open relation).
  • Contents:
    • Relation Catalog Entry: Metadata describing the relation, such as its name, schema, and other properties.
    • Record Identifier (Rec-ID): Indicates where this relation's metadata is stored on the disk. The Rec-ID is typically split into:
      • Block Number: Identifies the block on the disk where the entry resides.
      • Slot Number: Specifies the position within the block for the entry.
    • Runtime Data: Additional information needed during the program's execution, such as references or temporary states.

In summary, the relation cache acts as a temporary storage area for metadata about a specific relation, helping the system access it efficiently without repeatedly querying the disk.

Attribute Cache

  • Purpose: Stores information about the attributes (fields/columns) of an open relation.
  • Structure: Implemented as a linked list, where each node contains:
    • Attribute Catalog Entry: Metadata for a specific attribute, such as its name, type, size, and constraints.
    • Rec-IDs: Similar to the relation cache, this specifies where the attribute's metadata is stored on the disk.
    • Runtime Metadata: Information needed during execution, such as temporary data related to the attribute or its current state.

Pasted image 20241226152710.png Pasted image 20250103232458.png

int RelCacheTable::getRelCatEntry(int relId, RelCatEntry* relCatBuf) {
    check if relId >= MAX_OPEN return E_OUTOFBOUND;
    if(relCache[relId] -> nullptr)
        return E_NOTOPENSOMETHINGGODKNOWS;
    *relCatBuf = relCache[recId]->relCatEntry;
}
void RelCacheTable::recordToRelCatEntry(
union Attribute record[ATTRS_SIZE],
RelCatEntry* relCatEntry
) {
    copy every THENGAKOLA of record to relCatEntry;
}