<commit_before>/*
 * file_linked_list.c
 *
 *  Created on: Jun 8, 2015
 *	  Author: workstation
 */

#include "file_linked_list.h"

err_t
fll_reset(
	ll_file_t *linked_list_file
) {
	ll_file_node_t *ll_probe;	/** allocate ll_probe node to access head */

	ll_probe							= (ll_file_node_t *) malloc(linked_list_file->node_size);
	linked_list_file->current			= HEAD_NODE;						/** point to head node in list and clear rest of pointers*/
	linked_list_file->previous			= INVALID_NODE;
	linked_list_file->iterator_state	= initialized;						/** the iterator is initialized and ready to consume nodes */
	fll_get(linked_list_file, ll_probe);/** Read in the head node and determine what to do*/
	linked_list_file->next				= ll_probe->next;					/** Point to the first node in the list */
	free(ll_probe);
	return err_ok;	/** @TODO cleanup return codes */
}

err_t
fll_open(
	ll_file_t *linked_list_file,
	int (*compare)(ll_file_t *, ll_file_node_t *, ll_file_node_t *),
	key_type_t key_type,
	ion_key_size_t key_size,
	ion_value_size_t value_size,
	int bucket,
	int id
) {
	fe_filename_t filename;

	filename.instance_id	= id;					/** This is the parent id */
	filename.child.child_id = bucket;
	fe_encode_child_id(&filename);
	/* allocation space for file name */
	/* io_printf("%i",FILENAME_SIZE); */
	/* io_printf("%p\n",linked_list_file->file_name); */
	/* if(linked_list_file->file_name == NULL) {io_printf("NULL\n");} */
	/* free(linked_list_file->file_name); */
	linked_list_file->file_name = malloc(FILENAME_SIZE);/* malloc(strlen(filename.child.child_filename)+1 * sizeof(char));//(char*)malloc(FILENAME_SIZE); */
	strcpy(linked_list_file->file_name, filename.child.child_filename);
	filename.destroy(&filename);

	/* attempt to open the file */
	linked_list_file->file = fopen(linked_list_file->file_name, "r+b");

	/** NOTE: On windows machines file !!MUST!! be opened with +b to allow for binary mode,*/
	if (linked_list_file->file == NULL) {
		/** then the file did not exist, so create it!*/
#if DEBUG
		io_printf("file does note exist!\n");
#endif
		free(linked_list_file->file_name);	/** free up before you call again*/
		linked_list_file->file_name = NULL;
		return err_item_not_found;
	}
	else {
#if DEBUG
		io_printf("file already exists!\n");
#endif
		linked_list_file->node_size = sizeof(((ll_file_node_t *) 0)->next) + key_size + value_size;
		linked_list_file->compare	= compare;
		return fll_reset(linked_list_file);	/** and reset */
	}
}

err_t
fll_create(
	ll_file_t *linked_list_file,
	int (*compare)(ll_file_t *, ll_file_node_t *, ll_file_node_t *),
	key_type_t key_type,
	ion_key_size_t key_size,
	ion_value_size_t value_size,
	int bucket,
	int id
) {
	/** This assumes that the the file may or may not exist and
	 * if it does, it will delete and create a new file based linked
	 * list
	 */

	fe_filename_t filename;

	filename.instance_id		= id;				/** This is the parent id */
	filename.child.child_id		= bucket;
	fe_encode_child_id(&filename);
	/* allocation space for file name */
	linked_list_file->file_name = (char *) malloc(FILENAME_SIZE);
	strcpy(linked_list_file->file_name, filename.child.child_filename);	/* VALGRIND HATES THIS --Heath */
	filename.destroy(&filename);

	linked_list_file->file = fopen(linked_list_file->file_name, "w+b");

	/** NOTE: On windows machines file !!MUST!! be opened with +b to allow for binary mode,
									otherwise when 0x0A is encountered 0x0D will be included (\r\n)*/
	/* create head node and update */
	ll_file_node_t *head_node = malloc(sizeof(head_node->next) + key_size + value_size);

	head_node->next						= END_OF_LIST;
	linked_list_file->node_size			= sizeof(head_node->next) + key_size + value_size;
	/* data is garbage so just leave it */
	linked_list_file->current			= HEAD_NODE;
	linked_list_file->next				= INVALID_NODE;
	linked_list_file->next				= head_node->next;
	linked_list_file->iterator_state	= uninitialized;

	/* write out head node */
	fll_write_node(linked_list_file, head_node);

	/* compute node size */
	linked_list_file->node_size = key_size + value_size + sizeof(head_node->next);
	/** clean up to handle general data with a pointer */

	/* bind comparator */
	linked_list_file->compare = compare;

	free(head_node);
	/** @todo fix return value */
	return 0;
}

err_t
fll_next(
	/* HEATH //When this method is called, something seems to overflow; cannot see what though. */
	ll_file_t				*linked_list_file,
	volatile ll_file_node_t *ll_node
) {
	/** @todo Make sure to check status of next!!! */
	if (linked_list_file->iterator_state == uninitialized) {
		/** check the state of the iterator before proceeding */
		return err_uninitialized;
	}
	else {
		if (linked_list_file->iterator_state != removed) {
			/** if the current item has been removed, previous stays as previous */
			linked_list_file->previous = linked_list_file->current;	/** grab position of last node */
		}

		linked_list_file->iterator_state = active;	/** and set back to active */

		if ((linked_list_file->current = linked_list_file->next) == END_OF_LIST) {<commit_msg>Fix undefined size calculation for node in FLL<commit_after>/*
 * file_linked_list.c
 *
 *  Created on: Jun 8, 2015
 *	  Author: workstation
 */

#include "file_linked_list.h"

err_t
fll_reset(
	ll_file_t *linked_list_file
) {
	ll_file_node_t *ll_probe;	/** allocate ll_probe node to access head */

	ll_probe							= (ll_file_node_t *) malloc(linked_list_file->node_size);
	linked_list_file->current			= HEAD_NODE;						/** point to head node in list and clear rest of pointers*/
	linked_list_file->previous			= INVALID_NODE;
	linked_list_file->iterator_state	= initialized;						/** the iterator is initialized and ready to consume nodes */
	fll_get(linked_list_file, ll_probe);/** Read in the head node and determine what to do*/
	linked_list_file->next				= ll_probe->next;					/** Point to the first node in the list */
	free(ll_probe);
	return err_ok;	/** @TODO cleanup return codes */
}

err_t
fll_open(
	ll_file_t *linked_list_file,
	int (*compare)(ll_file_t *, ll_file_node_t *, ll_file_node_t *),
	key_type_t key_type,
	ion_key_size_t key_size,
	ion_value_size_t value_size,
	int bucket,
	int id
) {
	fe_filename_t filename;

	filename.instance_id	= id;					/** This is the parent id */
	filename.child.child_id = bucket;
	fe_encode_child_id(&filename);
	/* allocation space for file name */
	/* io_printf("%i",FILENAME_SIZE); */
	/* io_printf("%p\n",linked_list_file->file_name); */
	/* if(linked_list_file->file_name == NULL) {io_printf("NULL\n");} */
	/* free(linked_list_file->file_name); */
	linked_list_file->file_name = malloc(FILENAME_SIZE);/* malloc(strlen(filename.child.child_filename)+1 * sizeof(char));//(char*)malloc(FILENAME_SIZE); */
	strcpy(linked_list_file->file_name, filename.child.child_filename);
	filename.destroy(&filename);

	/* attempt to open the file */
	linked_list_file->file = fopen(linked_list_file->file_name, "r+b");

	/** NOTE: On windows machines file !!MUST!! be opened with +b to allow for binary mode,*/
	if (linked_list_file->file == NULL) {
		/** then the file did not exist, so create it!*/
#if DEBUG
		io_printf("file does note exist!\n");
#endif
		free(linked_list_file->file_name);	/** free up before you call again*/
		linked_list_file->file_name = NULL;
		return err_item_not_found;
	}
	else {
#if DEBUG
		io_printf("file already exists!\n");
#endif
		linked_list_file->node_size = sizeof(((ll_file_node_t *) 0)->next) + key_size + value_size;
		linked_list_file->compare	= compare;
		return fll_reset(linked_list_file);	/** and reset */
	}
}

err_t
fll_create(
	ll_file_t *linked_list_file,
	int (*compare)(ll_file_t *, ll_file_node_t *, ll_file_node_t *),
	key_type_t key_type,
	ion_key_size_t key_size,
	ion_value_size_t value_size,
	int bucket,
	int id
) {
	/** This assumes that the the file may or may not exist and
	 * if it does, it will delete and create a new file based linked
	 * list
	 */

	fe_filename_t filename;

	filename.instance_id		= id;				/** This is the parent id */
	filename.child.child_id		= bucket;
	fe_encode_child_id(&filename);
	/* allocation space for file name */
	linked_list_file->file_name = (char *) malloc(FILENAME_SIZE);
	strcpy(linked_list_file->file_name, filename.child.child_filename);	/* VALGRIND HATES THIS --Heath */
	filename.destroy(&filename);

	linked_list_file->file = fopen(linked_list_file->file_name, "w+b");

	/** NOTE: On windows machines file !!MUST!! be opened with +b to allow for binary mode,
									otherwise when 0x0A is encountered 0x0D will be included (\r\n)*/
	/* create head node and update */
	ll_file_node_t *head_node = malloc(sizeof(*head_node) + key_size + value_size);
//	ll_file_node_t *head_node = malloc(sizeof(head_node->next) + key_size + value_size);

	head_node->next						= END_OF_LIST;
	linked_list_file->node_size			= sizeof(*head_node) + key_size + value_size;
//	linked_list_file->node_size			= sizeof(head_node->next) + key_size + value_size;
	/* data is garbage so just leave it */
	linked_list_file->current			= HEAD_NODE;
	linked_list_file->next				= INVALID_NODE;
	linked_list_file->next				= head_node->next;
	linked_list_file->iterator_state	= uninitialized;

	/* write out head node */
	fll_write_node(linked_list_file, head_node);

	/* compute node size */
	linked_list_file->node_size = sizeof(*head_node) + key_size + value_size;
//	linked_list_file->node_size = key_size + value_size + sizeof(head_node->next);
	/** clean up to handle general data with a pointer */

	/* bind comparator */
	linked_list_file->compare = compare;

	free(head_node);
	/** @todo fix return value */
	return 0;
}

err_t
fll_next(
	/* HEATH //When this method is called, something seems to overflow; cannot see what though. */
	ll_file_t				*linked_list_file,
	volatile ll_file_node_t *ll_node
) {
	/** @todo Make sure to check status of next!!! */
	if (linked_list_file->iterator_state == uninitialized) {
		/** check the state of the iterator before proceeding */
		return err_uninitialized;
	}
	else {
		if (linked_list_file->iterator_state != removed) {
			/** if the current item has been removed, previous stays as previous */
			linked_list_file->previous = linked_list_file->current;	/** grab position of last node */
		}

		linked_list_file->iterator_state = active;	/** and set back to active */

		if ((linked_list_file->current = linked_list_file->next) == END_OF_LIST) {
{'fraction_non_alphanumeric': 0.1229967175130334, 'fraction_numerical': 0.0023170496234794363, 'mean_word_length': 4.3981240229286085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, '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': '3824247', 'n_tokens_mistral': 3813, 'n_tokens_neox': 3602, 'n_words': 898}