// Fix void NoVoHT::merge() { //while(write_lock){} //write_lock=true; sem_wait(&map_lock); sem_wait(&write_lock); char buf[300]; char sec[300]; rewind(dbfile); while (readTabString(dbfile, buf) != NULL) { if (buf[0] == '~') { readTabString(dbfile, sec); char * pos; pos = strtok(sec, ","); while (pos != NULL) { fseek(swapFile, (off_t) atoi(pos), SEEK_SET); char test[300]; readTabString(swapFile, test); if (strcmp(test, (buf + 1)) == 0) { fseek(swapFile, (off_t) atoi(pos), SEEK_SET); fputc('~', swapFile); } pos = strtok(NULL, ","); } } else { //while (map_lock) {} //map_lock = true; //sem_wait(&map_lock); fseek(swapFile, 0, SEEK_END); string s(buf); kvpair* p = kvpairs[hash(s) % size]; while (p != NULL) { if (p->key.compare(s) == 0) { destroyFposList(p->positions); p->positions = new fpos_list; p->positions->next = NULL; fgetpos(swapFile, &(p->positions->pos)); fprintf(swapFile, "%s\t%s\t", p->key.c_str(), p->val.c_str()); p->diff = false; break; } else p = p->next; } //map_lock = false; //sem_post(&map_lock); } } fclose(dbfile); dbfile = swapFile; rewriting = false; sem_post(&map_lock); sem_post(&write_lock); }
void NoVoHT::readFile(){ if(!dbfile) return; char s[300]; char v[300]; while(readTabString(dbfile, s) != NULL){ string key(s); if (readTabString(dbfile, v) == NULL) break; string val(v); if (key[0] != '~'){ put(key,val); } } writeFile(); }
// rewrite this shit void NoVoHT::readFile() { //if ((dbfile = fopen(".nddovoht.mrg", "r")) == 0){ if (filename.empty()) return; rename(filename.c_str(), ".novoht.swp"); dbfile = fopen(filename.c_str(), "w+"); setvbuf(dbfile,NULL, _IONBF, 0); if ((swapFile = fopen(".novoht.swp", "r")) == NULL) return; rewriting = false; char s[300]; char v[300]; while (readTabString(swapFile, s) != NULL) { string key(s); if (readTabString(swapFile, v) == NULL) break; string val(v); if (key[0] != '~') { put(key, val); } } fclose(swapFile); if (unlink(".novoht.swp")) perror("Error deleting swap file"); //FILE * mergeFile; if ((swapFile = fopen(".novoht.mrg", "r")) != NULL){ // Corrupted close, merging changes char buf[300]; char sec[300]; while (readTabString(swapFile,buf) != NULL){ if(buf[0] == '~'){ remove(buf+1); readTabString(swapFile, sec); } else{ readTabString(swapFile,sec); put(string(buf), string(sec)); } } int rmrc = unlink(".novoht.mrg"); if (rmrc) { perror("Error deleting merge file"); } } }