void BufferManager::save(){
    for (int i = 0; i < NUMOFBUFFER; i++) {
        if (blocks[i].getInTableBit() && blocks[i].getDirtyBit()) {
            bufferToFile(i);
        }
    }
}
//private
int BufferManager::ReplaceBlockIndex(){
    static int tempIndex = 0;
    while (1) {
        if(tempIndex == NUMOFBUFFER){
            tempIndex -= NUMOFBUFFER;
        }
        if (!blocks[tempIndex].getPinBit()) {
            if (blocks[tempIndex].getReferenceBit()) {
                blocks[tempIndex].setReferenceBit(0);
            }
            else{
                if (blocks[tempIndex].getDirtyBit()) {
                    if(bufferToFile(tempIndex)){
                        goto CONTINUELOOP;
                    }
                }
                //erase the hash key
                if (blocks[tempIndex].getInTableBit()) {
                    IndexTable.erase(hashKey(blocks[tempIndex].getFileName(), blocks[tempIndex].getPartOfFile()));
                    blocks[tempIndex].setInTableBit(0);
                }
                return tempIndex++;
            }
        }
    CONTINUELOOP:
        tempIndex++;
    
    }
}
void testCase(){
   char buf[100];
   char uid[100];
   strcpy(buf,"12345678911234561234567890123456");
   strcat(buf,"21345678911234561234567890123456");
   strcpy(uid,"abc");
   bufferToFile(buf,strlen(buf),uid,1);
}
int BufferManager::bufferToFile(string fileName, int part){
    if (IndexTable.find(hashKey(fileName, part)) == IndexTable.end()) {
        fprintf(stderr, "buffer not exists, in BufferManager::bufferToFile.\n");
        return -1;
    }
    int index = IndexTable[hashKey(fileName, part)];
    
    return bufferToFile(index);
}