<commit_before>/*
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stddef.h>

#include <unistd.h>
#include <stdlib.h>
#include <new>
#include <string>
#include <string.h>
#include <locale>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include "novoht.h"
#define DEBUG	TRUE

NoVoHT::NoVoHT(){
   initialize("", 1000, -1, 0);
}

NoVoHT::NoVoHT(const string& f) {
   initialize(f, 1000, -1, 0);
}

NoVoHT::NoVoHT(const string& f, const int& s, const int& m) {
   initialize(f, s, m, 0);
}

// NoVoHT( Filename, size, magic number, resize threashold )
NoVoHT::NoVoHT(const string& f, const int& s, const int& m, const float& r) {
   initialize(f, s, m, r);
}

void NoVoHT::initialize(const string& fname, const int& initSize,
        const int& gcnum, const float& resizeRatio){
	kvpairs = new kvpair*[initSize];
	for (int x = 0; x < initSize; x++) {
		kvpairs[x] = NULL;
	}
        sem_init(&map_lock, 0, 1);
        sem_init(&write_lock, 0, 1);
	resizing = false;
   rewriting = false;
	nRem = 0;
	resizeNum = resizeRatio;
	size = initSize;
	numEl = 0;
	filename = fname;
	oldpairs = NULL;
	readFile();
	magicNumber = gcnum;
}

//0 success, -1 no insert, -2 no write
int NoVoHT::put(string k, string v) {
   sem_wait(&map_lock);
	if (numEl >= size * resizeNum) {
		if (resizeNum != 0) {
			resize(size * 2);
		}
	}
	int slot;
	slot = hash(k) % size;
	kvpair *cur = kvpairs[slot];
	kvpair *add = new kvpair;
	add->key = k;
	add->val = v;
	add->next = NULL;
   add->positions = NULL;
	if (cur == NULL) {
		int rc = write(add);
		if (rc){
			delete add;
			sem_post(&map_lock);
			return -1;
		}
		kvpairs[slot] = add;
		numEl++;
      sem_post(&map_lock);
		return 0;
	}
	while (cur->next != NULL) {
		if (k.compare(cur->key) == 0) {
			delete add;
			string oldv = cur->val;
			cur->val = v;
			int rc = write(cur);
			if (rc) {
				cur->val = oldv;
				sem_post(&map_lock);
				return -1;
			}
			mark(cur->positions);
         sem_post(&map_lock);
			return 0;
		}
		cur = cur->next;
	}
	if (k.compare(cur->key) == 0) {
		delete add;
		string oldv = cur->val;
		cur->val = v;
		int rc = write(cur);
		if (rc) {
			cur->val = oldv;
			sem_post(&map_lock);
			return -1;
		}
		mark(cur->positions);
      sem_post(&map_lock);
		return 0;
	}
	int rc = write(add);
	if (rc){
		delete add;
		sem_post(&map_lock);
		return -1;
	}
	cur->next = add;
	numEl++;
   sem_post(&map_lock);
	return 0;
}

NoVoHT::~NoVoHT(){
   if (dbfile){
      writeFile();
      if (writeThread)
         pthread_join(writeThread, NULL);
      fclose(dbfile);
   }
   for (int i = 0; i < size; i++){
      fsu(kvpairs[i]);
   }
   delete [] kvpairs;
}

string* NoVoHT::get(string k) {
   sem_wait(&map_lock);
   sem_post(&map_lock);
	int loc = hash(k) % size;
	kvpair *cur = kvpairs[loc];
	while (cur != NULL && !k.empty()) {
		if (k.compare(cur->key) == 0)
			return &(cur->val);
		cur = cur->next;
	}
	return NULL;
}

//return 0 for success, -1 fail to remove, -2 not found
int NoVoHT::remove(string k){
   sem_wait(&map_lock);
   int loc = hash(k)%size;
   kvpair *cur = kvpairs[loc];
   if (cur == NULL) {
      sem_post(&map_lock);
      return -2;       //not found
   }
   if (k.compare(cur->key) ==0) {
		int ret;
      fpos_list * toRem = kvpairs[loc]->positions;
      kvpairs[loc] = cur->next;
      numEl--;
      ret= rewriting ? logrm(k, toRem) : mark(toRem);
		if (ret) {
			sem_post(&map_lock);
			return -1;
		}<commit_msg>Bug fix: overwrite get string<commit_after>/*
 * Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stddef.h>

#include <unistd.h>
#include <stdlib.h>
#include <new>
#include <string>
#include <string.h>
#include <locale>
#include <stdio.h>
#include <iostream>
#include <pthread.h>
#include "novoht.h"
#define DEBUG	TRUE

NoVoHT::NoVoHT(){
   initialize("", 1000, -1, 0);
}

NoVoHT::NoVoHT(const string& f) {
   initialize(f, 1000, -1, 0);
}

NoVoHT::NoVoHT(const string& f, const int& s, const int& m) {
   initialize(f, s, m, 0);
}

// NoVoHT( Filename, size, magic number, resize threashold )
NoVoHT::NoVoHT(const string& f, const int& s, const int& m, const float& r) {
   initialize(f, s, m, r);
}

void NoVoHT::initialize(const string& fname, const int& initSize,
        const int& gcnum, const float& resizeRatio){
	kvpairs = new kvpair*[initSize];
	for (int x = 0; x < initSize; x++) {
		kvpairs[x] = NULL;
	}
        sem_init(&map_lock, 0, 1);
        sem_init(&write_lock, 0, 1);
	resizing = false;
   rewriting = false;
	nRem = 0;
	resizeNum = resizeRatio;
	size = initSize;
	numEl = 0;
	filename = fname;
	oldpairs = NULL;
	readFile();
	magicNumber = gcnum;
}

//0 success, -1 no insert, -2 no write
int NoVoHT::put(string k, string v) {
   sem_wait(&map_lock);
	if (numEl >= size * resizeNum) {
		if (resizeNum != 0) {
			resize(size * 2);
		}
	}
	int slot;
	slot = hash(k) % size;
	kvpair *cur = kvpairs[slot];
	kvpair *add = new kvpair;
	add->key = k;
	add->val = v;
	add->next = NULL;
   add->positions = NULL;
	if (cur == NULL) {
		int rc = write(add);
		if (rc){
			delete add;
			sem_post(&map_lock);
			return -1;
		}
		kvpairs[slot] = add;
		numEl++;
      sem_post(&map_lock);
		return 0;
	}
	while (cur->next != NULL) {
		if (k.compare(cur->key) == 0) {
			delete add;
			string oldv = cur->val;
			cur->val = v;
			int rc = write(cur);
			if (rc) {
				cur->val = oldv;
				sem_post(&map_lock);
				return -1;
			}
			mark(cur->positions);
         sem_post(&map_lock);
			return 0;
		}
		cur = cur->next;
	}
	if (k.compare(cur->key) == 0) {
		delete add;
		string oldv = cur->val;
		cur->val = v;
		int rc = write(cur);
		if (rc) {
			cur->val = oldv;
			sem_post(&map_lock);
			return -1;
		}
		mark(cur->positions);
      sem_post(&map_lock);
		return 0;
	}
	int rc = write(add);
	if (rc){
		delete add;
		sem_post(&map_lock);
		return -1;
	}
	cur->next = add;
	numEl++;
   sem_post(&map_lock);
	return 0;
}

NoVoHT::~NoVoHT(){
   if (dbfile){
      writeFile();
      if (writeThread)
         pthread_join(writeThread, NULL);
      fclose(dbfile);
   }
   for (int i = 0; i < size; i++){
      fsu(kvpairs[i]);
   }
   delete [] kvpairs;
}

string* NoVoHT::get(string k) {
   sem_wait(&map_lock);
   sem_post(&map_lock);
	int loc = hash(k) % size;
	kvpair *cur = kvpairs[loc];
	while (cur != NULL && !k.empty()) {
		if (k.compare(cur->key) == 0)
			return new string((cur->val).c_str());
		cur = cur->next;
	}
	return NULL;
}

//return 0 for success, -1 fail to remove, -2 not found
int NoVoHT::remove(string k){
   sem_wait(&map_lock);
   int loc = hash(k)%size;
   kvpair *cur = kvpairs[loc];
   if (cur == NULL) {
      sem_post(&map_lock);
      return -2;       //not found
   }
   if (k.compare(cur->key) ==0) {
		int ret;
      fpos_list * toRem = kvpairs[loc]->positions;
      kvpairs[loc] = cur->next;
      numEl--;
      ret= rewriting ? logrm(k, toRem) : mark(toRem);
		if (ret) {
			sem_post(&map_lock);
			return -1;
		}
{'fraction_non_alphanumeric': 0.1607567973938103, 'fraction_numerical': 0.012780353339180555, 'mean_word_length': 3.0211586901763225, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1301861', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3077, 'n_words': 865}