void commandContainerCleanup(CommandContainer commandContainer){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; lockLock(internal->lock); time_t currentTime; // Stores seconds elapsed since 01-01-1970 time(¤tTime); // Cleanup atomic commands if(linkedListSize(internal->atomicCommands)>0){ int length=linkedListSize(internal->atomicCommands); Command atomicCommand; while(length>0){ linkedListGetFirst(internal->atomicCommands,(Object*)&atomicCommand); int commandTime=commandGetInt(atomicCommand,SERVER_HANDLING_TIME); int distributed=commandGetInt(atomicCommand,DISTRIBUTED); if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){ commandFree(atomicCommand); linkedListRemoveFirst(internal->atomicCommands); }else{ break; } length=linkedListSize(internal->atomicCommands); } } // Cleanup commands int length=linkedListSize(internal->versions); int i; int* intBuffer; while(length>0){ linkedListGetFirst(internal->versions,(Object*)&intBuffer); Command temp; hashmapGet(internal->versionMap,*intBuffer,(Object*)&temp); int commandTime=commandGetInt(temp,SERVER_HANDLING_TIME); int persisted=commandGetInt(temp,PERSISTED); int distributed=commandGetInt(temp,DISTRIBUTED); if((currentTime-commandTime>MAX_TIME_TO_HOLD_COMMAND&&persisted==TRUE&&distributed==TRUE)||(currentTime-commandTime>MAX_TIME_TO_HOLD_UNCLEANED_COMMAND)){ linkedListRemoveFirst(internal->versions); hashmapRemove(internal->versionMap,*intBuffer); memoryFree(intBuffer); commandFree(temp); }else{ break; } length=linkedListSize(internal->versions); } lockUnlock(internal->lock); }
Properties propertiesCreate(char *filePath) { PropertiesInternal *m = (PropertiesInternal *) memoryAlloc(sizeof(PropertiesInternal)); m->map = hashmapCreate(SMALL_CONTAINER_SIZE); FILE *fp; long len; char *buf; fp = fopen(filePath, "rt"); if (fp == NULL) { printf("couldn't open the properties file"); }; fseek(fp, 0, SEEK_END); //go to end len = ftell(fp); //get position at end (length) fseek(fp, 0, SEEK_SET); //go to beg. buf = (char *) memoryAlloc(len + 1); //malloc buffer long result = fread(buf, (size_t) (len + 1), 1, fp); //read into buffer if (result < 0) { printf("Fail to read properties file ."); } fclose(fp); buf[len] = 0; char *key; char *value; const char newLineDelimiter[] = "\n"; const char spaseDelimiter[] = "="; char *token; token = strtok(buf, newLineDelimiter); LinkedList linkedList = linkedListCreate(); while (token != NULL) { linkedListAddFirst(linkedList, token); token = strtok(NULL, newLineDelimiter); } while (linkedListSize(linkedList) > 0) { char *token = (char *) linkedListRemoveFirst(linkedList); key = strtok(token, spaseDelimiter); value = strtok(NULL, spaseDelimiter); unsigned int keyId = stringTransformToInt(key, strlen(key)); hashmapPut(m->map, keyId, value); } linkedListFree(linkedList); return m; }