cache.c
//Reserves a space block if the block isn't in the cache already -> what if the block doesn't exist, is written to
Reserves a space block if the block isn't in the cache already -> what if the block doesn't exist, is written to
Reserves a space block if the block isn ' t in the cache already - > what if the block doesn ' t exist is written to
bool WriteToBlockAndMarkDirty(cache_t* cache, global_block_id_t targetBlock, const byte* toCopy, uint32_t startOffset, uint32_t endPosition, int server_id, int* present) {
    block_t* toWriteInto = GetBlockFromCache(cache, targetBlock);
    if(!toWriteInto) {
        if(!FetchBlockFromServer(cache, targetBlock, server_id)) {
            return false;
        }
        else{
            *present = 0;
            return WriteToBlockAndMarkDirty(cache, targetBlock, toCopy, startOffset, endPosition, server_id, present);
        }
    }
    else {
        if(startOffset > cache->BlockSize || endPosition > cache->BlockSize || startOffset > endPosition) {
            fprintf(stderr, "Write Offsets are illogical.\n");
            return false;
        }
        pthread_mutex_lock(toWriteInto->lock);
        memcpy( ((void*)toWriteInto->data)+startOffset, (void*)toCopy, (endPosition - startOffset) );
        if(toWriteInto->dirty) {
            pthread_mutex_unlock(toWriteInto->lock);
            return true;
        }
        toWriteInto->dirty = true;
        pthread_mutex_unlock(toWriteInto->lock);
        pthread_mutex_lock(cache->DirtyListLock);
        if( !addIDToIDList(&(cache->DirtyList), targetBlock) ) {
            pthread_mutex_unlock(cache->DirtyListLock);
            return false;
        }
        else{
            pthread_mutex_unlock(cache->DirtyListLock);
        }
    }
    return true;
}
bool WriteToBlockAndMarkDirty ( cache_t * cache , global_block_id_t targetBlock , const byte * toCopy , uint32_t startOffset , uint32_t endPosition , int server_id , int * present ) { block_t * toWriteInto = GetBlockFromCache ( cache , targetBlock ) ; if ( ! toWriteInto ) { if ( ! FetchBlockFromServer ( cache , targetBlock , server_id ) ) { return false ; } else { * present = 0 ; return WriteToBlockAndMarkDirty ( cache , targetBlock , toCopy , startOffset , endPosition , server_id , present ) ; } } else { if ( startOffset > cache -> BlockSize || endPosition > cache -> BlockSize || startOffset > endPosition ) { fprintf ( stderr , " \n " ) ; return false ; } pthread_mutex_lock ( toWriteInto -> lock ) ; memcpy ( ( ( void * ) toWriteInto -> data ) + startOffset , ( void * ) toCopy , ( endPosition - startOffset ) ) ; if ( toWriteInto -> dirty ) { pthread_mutex_unlock ( toWriteInto -> lock ) ; return true ; } toWriteInto -> dirty = true ; pthread_mutex_unlock ( toWriteInto -> lock ) ; pthread_mutex_lock ( cache -> DirtyListLock ) ; if ( ! addIDToIDList ( & ( cache -> DirtyList ) , targetBlock ) ) { pthread_mutex_unlock ( cache -> DirtyListLock ) ; return false ; } else { pthread_mutex_unlock ( cache -> DirtyListLock ) ; } } return true ; }
Reserves a space block if the block isn't in the cache already -> what if the block doesn't exist, is written to
Reserves a space block if the block isn ' t in the cache already - > what if the block doesn ' t exist is written to
//Must get the block from the server previous to writing to it //fprintf(stderr, "WARN: block was already dirty\n"); //toWriteInto->host = server_id; //Add to dirty list