NITCbase/nitcbase.github.io

Issues with Rename Attribute in Block Access Layer

Closed this issue · 1 comments

Few corrections in BlockAccess::renameAttribute()

  • A confusing IF block inside the loop.

if (attrcatRecId.block == -1 && attrcatRecId.slot == -1) {
// Continue to search for more attributes
continue;
}

This doesn't seem right.

  • Using a while loop would be simpler.
  • The algorithm doesn't check for E_ATTREXIST case

The way it is right now, this algorithm is quite confusing. Attaching my implementation in case it helps.

  RecId defaultId{-1, -1};
  Attribute relationName;
  strcpy(relationName.strval, relName);
  RelCacheTable::setSearchIndex(0, &defaultId);
  RecId recId = ba_linearSearch(0, "RelName", relationName, EQ);

  if (recId.block == -1) return E_RELNOTEXIST;

  RelCacheTable::setSearchIndex(1, &defaultId);
  recId = ba_linearSearch(1, "RelName", relationName, EQ);
  RecId attrId{-1, -1};
  Attribute record[6];

  while (recId.block != -1) {
    RecBuffer attrBlk{recId.block};
    attrBlk.getRecord(record, recId.slot);

    if (strcmp(record[1].strval, oldName) == 0) attrId = recId;

    if (strcmp(record[1].strval, newName) == 0) return E_ATTREXIST;

    recId = ba_linearSearch(1, "RelName", relationName, EQ);
  }

  if (attrId.block == -1) return E_ATTRNOTEXIST;

  RecBuffer attrBlk{attrId.block};
  attrBlk.getRecord(record, attrId.slot);
  strcpy(record[1].strval, newName);
  attrBlk.setRecord(record, attrId.slot);

  return SUCCESS;

Fixed, The algorithm in documentation is updated to the following:

int BlockAccess::renameAttribute(char *relName, char *oldName, char *newName) {
	// Search for the relation with name relName in relation catalog using Linear Search
	Attribute attrValueRelName;
	strcpy(attrValueRelName.sVal, relName);
        RecId searchIndex = {-1, -1};
        RelCacheTable::setSearchIndex(RELCAT_RELID, &searchIndex);
	RecId relcatRecId = linearSearch(RELCAT_RELID, "RelName", attrValueRelName, EQ);

	// If relation with name relName does not exits (relcatRecId == {-1,-1})
	//    return E_RELNOTEXIST;

	/*** 
		Iterating over all Attribute Catalog Entry record corresponding to relation to find the required attribute 
	***/
	// Reset the Search Index by using RelCacheTable and setting value to {-1, -1}
	searchIndex = {-1, -1};
	RelCacheTable::setSearchIndex(ATTRCAT_RELID, &searchIndex);
	
	// Define RecId attrId {-1, -1} - used to check if an attribute of oldName exists or not.
 	RecId attrId{-1, -1};
	Attribute attrcatEntryRecord[ATTRCAT_NO_ATTRS];
  	while (true) {
		RecId attrcatEntryRecId = linearSearch(ATTRCAT_RELID, "RelName", relationName, EQ);

		if (attrcatEntryRecId.block == -1) {
			break;
		}

		// Get the attribute catalog record from the attribute catalog
		// Hint: instantiate RecBuffer Class with appropriate block number and use getRecord method to store the record into attrcatEntryRecord.

    	        // if (strcmp(attrcatEntryRecord[1].sVal, oldName) == 0) attrId = attrcatEntryRecId;

    	        // if (strcmp(attrcatEntryRecord[1].sVal, newName) == 0) return E_ATTREXIST;

    	
  	}
	
	// if (attrId.block == -1) return E_ATTRNOTEXIST;

	// Update the entry corresponding to the attribute in the Attribute Catalog Relation.
	/* Hint: Instantiate RecBuffer class by passing appropriate block number = attrId.block
		Get the record corresponding to the entry by using getRecord method by passing attrId.slot
		as argument.
		Use setRecord method to set the record back after updating the entry appropriately.
	*/


	// return SUCCESS
}