RC setAttr (Record *record, Schema *schema, int attrNum, Value *value){ int offset=0; int offset_attr=0; char *slot=record->data; *((bool *)(slot+offset))=true; //set the first bool to be true which means this slot is not empty offset=sizeof(bool)+attrNum*sizeof(bool); *((bool *)(slot+offset))=true; //set the its according attribute's bool to be true.(not empty) if (attrOffset(schema, attrNum,&offset_attr)!=0) return EXIT_FAILURE; //get the offset of the attributes in the tuple. offset_attr+=sizeof(bool)+schema->numAttr*sizeof(bool); switch (value->dt) { //according to its different type to cpy the content to the slot case DT_BOOL: memcpy(slot+offset_attr, &(value->v.boolV), sizeof(bool)); break; case DT_FLOAT: memcpy(slot+offset_attr, &(value->v.floatV), sizeof(float)); break; case DT_STRING: strcpy(slot+offset_attr,value->v.stringV); break; case DT_INT: memcpy(slot+offset_attr, &(value->v.intV), sizeof(int)); break; } return RC_OK; }
RC getAttr (Record *record, Schema *schema, int attrNum, Value **value){ int offset = 0; char * slot =record->data; attrOffset(schema, attrNum, &offset); //calculate the offset of the attribute in the tuple. offset+=sizeof(bool)+sizeof(bool)*schema->numAttr; //skip the header of the slot. Value* a_value= (Value *)malloc(sizeof(Value)); a_value->dt=schema->dataTypes[attrNum]; switch (a_value->dt) { case DT_BOOL: memcpy(&(a_value->v.boolV),slot+offset,sizeof(bool)); break; case DT_FLOAT: memcpy( &(a_value->v.floatV),slot+offset,sizeof(float)); break; case DT_STRING: a_value->v.stringV=calloc(schema->typeLength[attrNum]+1, sizeof(char)); memcpy( a_value->v.stringV,slot+offset,schema->typeLength[attrNum]); break; case DT_INT: memcpy(&(a_value->v.intV),slot+offset,sizeof(int)); break; } *value=a_value; return RC_OK; }
// dealing with records and attribute values void calSlotSize(Schema *schema) { int length=0; attrOffset(schema,schema->numAttr, &length); length+=sizeof(bool)+sizeof(bool)*schema->numAttr; //a serial of bites to record whether the tuple and attributes are empty or not slotSize=length+sizeof(RID); }
int setPosDifference(Schema *schema, int attrNum, int diff) { int i = 0; while (i <= 0) { int positionalDifference; if(attrNum!=-100) attrOffset(schema, attrNum, &positionalDifference); return positionalDifference; } }
char * serializeAttr(Record *record, Schema *schema, int attrNum) { int offset; char *attrData; VarString *result; MAKE_VARSTRING(result); attrOffset(schema, attrNum, &offset); attrData = record->data + offset; switch(schema->dataTypes[attrNum]) { case DT_INT: { int val = 0; memcpy(&val,attrData, sizeof(int)); APPEND(result, "%s:%i", schema->attrNames[attrNum], val); } break; case DT_STRING: { char *buf; int len = schema->typeLength[attrNum]; buf = (char *) malloc(len + 1); strncpy(buf, attrData, len); buf[len] = '\0'; APPEND(result, "%s:%s", schema->attrNames[attrNum], buf); free(buf); } break; case DT_FLOAT: { float val; memcpy(&val,attrData, sizeof(float)); APPEND(result, "%s:%f", schema->attrNames[attrNum], val); } break; case DT_BOOL: { bool val; memcpy(&val,attrData, sizeof(bool)); APPEND(result, "%s:%s", schema->attrNames[attrNum], val ? "TRUE" : "FALSE"); } break; default: return "NO SERIALIZER FOR DATATYPE"; } RETURN_STRING(result); }
/* * This function is used to get the attribute values of a record */ RC getAttr (Record *record, Schema *schema, int attrNum, Value **value) { //variables for offset int offset; char *attrData; *value = (Value*)malloc(sizeof(Value)); //calculate the offset, to get the attribute value from attrOffset(schema, attrNum, &offset); attrData = record->data + offset; (*value)->dt =schema->dataTypes[attrNum]; //switch on the attribute data types switch(schema->dataTypes[attrNum]) { case DT_INT: { memcpy(&((*value)->v.intV) ,attrData,sizeof(int)); //get the attribute into value } break; case DT_STRING: { char *buf; int len = schema->typeLength[attrNum]; buf = (char *) malloc(len + 1); strncpy(buf, attrData, len); buf[len] = '\0'; (*value)->v.stringV = buf; } break; case DT_FLOAT: { memcpy(&((*value)->v.floatV),attrData, sizeof(float)); } break; case DT_BOOL: { memcpy(&((*value)->v.boolV),attrData ,sizeof(bool)); } break; default: //if different data encountered other than INT, FLOAT, BOOL, STRING return (EC 402) return RC_RM_NO_DESERIALIZER_FOR_THIS_DATATYPE; } return RC_OK; }
/* * This function is used to set the attributes in a record */ RC setAttr (Record *record, Schema *schema, int attrNum, Value *value) { //Modifying rm_serializer serializeAttr int offset; char *attrData; //calculate the offset values attrOffset(schema, attrNum, &offset); attrData = record->data + offset; //switch on attributes datatype value switch(schema->dataTypes[attrNum]) { case DT_INT: { memcpy(attrData,&(value->v.intV) ,sizeof(int)); //copy the newly set attribute value } break; case DT_STRING: { char *buf; int len = schema->typeLength[attrNum]; buf = (char *) malloc(len); buf = value->v.stringV; buf[len] = '\0'; //end the string with '\0' memcpy(attrData,buf,len); } break; case DT_FLOAT: { memcpy(attrData,&(value->v.floatV), sizeof(float)); } break; case DT_BOOL: { memcpy(attrData,&(value->v.boolV) ,sizeof(bool)); } break; default: return RC_RM_NO_DESERIALIZER_FOR_THIS_DATATYPE; } return RC_OK; }