<commit_before>#include "logutils.h"

#include "common.h"

#include "cache_state.h"

bool initialize_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "initialize_global_cache_state got NULL state");
		return false;
	}

	// TODO: DB connection

	state->cache_state.nonce = 0; // TODO
	state->cache_state.serial_number = 0; // TODO

	int retval = pthread_rwlock_init(&state->lock, NULL);
	if (retval != 0)
	{
		char errorbuf[ERROR_BUF_SIZE];
		log_error(retval, errorbuf, "pthread_rwlock_init() for global cache state");
		return false;
	}

	return true;
}

bool update_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "update_global_cache_state got NULL state");
		return false;
	}

	int retval;
	char errorbuf[ERROR_BUF_SIZE];

	retval = pthread_rwlock_wrlock(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_wrlock() for global cache state");
		return false;
	}

	// TODO: update state->cache_state

	retval = pthread_rwlock_unlock(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_unlock() for global cache state");
		return false;
	}

	return true;
}

void close_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "close_global_cache_state got NULL state");
		return;
	}

	int retval;
	char errorbuf[ERROR_BUF_SIZE];

	retval = pthread_rwlock_destroy(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_destroy() for global cache state");
	}
}<commit_msg>separate out a function for the actual DB queries<commit_after>#include "logutils.h"

#include "common.h"

#include "cache_state.h"

static bool get_cache_state(struct cache_state * state /* TODO: DB connection */)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "get_cache_state() got NULL state");
		return false;
	}

	// TODO: real implementation instead of this stub

	state->nonce = 0;
	state->serial_number = 0;

	return true;
}

bool initialize_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "initialize_global_cache_state got NULL state");
		return false;
	}

	// TODO: DB connection

	bool ret = get_cache_state(&state->cache_state);

	int retval = pthread_rwlock_init(&state->lock, NULL);
	if (retval != 0)
	{
		char errorbuf[ERROR_BUF_SIZE];
		log_error(retval, errorbuf, "pthread_rwlock_init() for global cache state");
		return false;
	}

	return ret;
}

bool update_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "update_global_cache_state got NULL state");
		return false;
	}

	bool ret;
	int retval;
	char errorbuf[ERROR_BUF_SIZE];

	retval = pthread_rwlock_wrlock(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_wrlock() for global cache state");
		return false;
	}

	ret = get_cache_state(&state->cache_state);

	retval = pthread_rwlock_unlock(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_unlock() for global cache state");
		return false;
	}

	return ret;
}

void close_global_cache_state(struct global_cache_state * state)
{
	if (state == NULL)
	{
		log_msg(LOG_ERR, "close_global_cache_state got NULL state");
		return;
	}

	int retval;
	char errorbuf[ERROR_BUF_SIZE];

	retval = pthread_rwlock_destroy(&state->lock);
	if (retval != 0)
	{
		log_error(retval, errorbuf, "pthread_rwlock_destroy() for global cache state");
	}

	// TODO: DB connection
}
{'fraction_non_alphanumeric': 0.12097230073487847, 'fraction_numerical': 0.0033917467495760316, 'mean_word_length': 4.898333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2710521', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1269, 'n_words': 298}