<commit_before>/* Copyright (C) 2003-2004 <NAME> */

/* Inside transaction we keep messages stored in sequences in uid fields.
   Before they're written to transaction log the sequences are changed to
   UIDs. This is because we're able to compress sequence ranges better. */

#include "lib.h"
#include "buffer.h"
#include "mail-index-view-private.h"
#include "mail-transaction-log.h"
#include "mail-cache-private.h"
#include "mail-index-transaction-private.h"

#include <stddef.h>

static void mail_index_transaction_add_last(struct mail_index_transaction *t);

struct mail_index_transaction *
mail_index_transaction_begin(struct mail_index_view *view, int hide)
{
	struct mail_index_transaction *t;

	/* don't allow syncing view while there's ongoing transactions */
	mail_index_view_transaction_ref(view);

	t = i_new(struct mail_index_transaction, 1);
	t->refcount = 1;
	t->view = view;
	t->hide_transaction = hide;
	t->first_new_seq = mail_index_view_get_message_count(t->view)+1;
	return t;
}

static void mail_index_transaction_free(struct mail_index_transaction *t)
{
	buffer_t **recs;
	unsigned int i, size;

	mail_index_view_transaction_unref(t->view);

	if (t->extra_rec_updates != NULL) {
		recs = buffer_get_modifyable_data(t->extra_rec_updates, &size);
		size /= sizeof(*recs);

		for (i = 0; i < size; i++) {
			if (recs[i] != NULL)
				buffer_free(recs[i]);
		}
	}

	if (t->appends != NULL)
		buffer_free(t->appends);
	if (t->expunges != NULL)
		buffer_free(t->expunges);
	if (t->updates != NULL)
		buffer_free(t->updates);
	if (t->cache_updates != NULL)
		buffer_free(t->cache_updates);
	if (t->extra_intros != NULL)
		buffer_free(t->extra_intros);
	i_free(t);
}

void mail_index_transaction_ref(struct mail_index_transaction *t)
{
	t->refcount++;
}

void mail_index_transaction_unref(struct mail_index_transaction *t)
{
	if (--t->refcount == 0)
		mail_index_transaction_free(t);
}

static void
mail_index_buffer_convert_to_uids(struct mail_index_transaction *t,
				  buffer_t *buf, size_t record_size, int range)
{
        struct mail_index_view *view = t->view;
	const struct mail_index_record *rec;
	unsigned char *data;
	size_t size, i;
	uint32_t *seq;
	int j;

	if (buf == NULL)
		return;

	/* @UNSAFE */
	data = buffer_get_modifyable_data(buf, &size);
	for (i = 0; i < size; i += record_size) {
		seq = (uint32_t *)&data[i];

		for (j = 0; j <= range; j++, seq++) {
			if (*seq >= t->first_new_seq) {
				rec = mail_index_transaction_lookup(t, *seq);
				*seq = rec->uid;
			} else {
				*seq = MAIL_INDEX_MAP_IDX(view->map,
							  *seq - 1)->uid;
			}
			i_assert(*seq != 0);
		}
	}
}

static int<commit_msg>added assert<commit_after>/* Copyright (C) 2003-2004 <NAME> */

/* Inside transaction we keep messages stored in sequences in uid fields.
   Before they're written to transaction log the sequences are changed to
   UIDs. This is because we're able to compress sequence ranges better. */

#include "lib.h"
#include "buffer.h"
#include "mail-index-view-private.h"
#include "mail-transaction-log.h"
#include "mail-cache-private.h"
#include "mail-index-transaction-private.h"

#include <stddef.h>

static void mail_index_transaction_add_last(struct mail_index_transaction *t);

struct mail_index_transaction *
mail_index_transaction_begin(struct mail_index_view *view, int hide)
{
	struct mail_index_transaction *t;

	/* don't allow syncing view while there's ongoing transactions */
	mail_index_view_transaction_ref(view);

	t = i_new(struct mail_index_transaction, 1);
	t->refcount = 1;
	t->view = view;
	t->hide_transaction = hide;
	t->first_new_seq = mail_index_view_get_message_count(t->view)+1;
	return t;
}

static void mail_index_transaction_free(struct mail_index_transaction *t)
{
	buffer_t **recs;
	unsigned int i, size;

	mail_index_view_transaction_unref(t->view);

	if (t->extra_rec_updates != NULL) {
		recs = buffer_get_modifyable_data(t->extra_rec_updates, &size);
		size /= sizeof(*recs);

		for (i = 0; i < size; i++) {
			if (recs[i] != NULL)
				buffer_free(recs[i]);
		}
	}

	if (t->appends != NULL)
		buffer_free(t->appends);
	if (t->expunges != NULL)
		buffer_free(t->expunges);
	if (t->updates != NULL)
		buffer_free(t->updates);
	if (t->cache_updates != NULL)
		buffer_free(t->cache_updates);
	if (t->extra_intros != NULL)
		buffer_free(t->extra_intros);
	i_free(t);
}

void mail_index_transaction_ref(struct mail_index_transaction *t)
{
	t->refcount++;
}

void mail_index_transaction_unref(struct mail_index_transaction *t)
{
	if (--t->refcount == 0)
		mail_index_transaction_free(t);
}

static void
mail_index_buffer_convert_to_uids(struct mail_index_transaction *t,
				  buffer_t *buf, size_t record_size, int range)
{
        struct mail_index_view *view = t->view;
	const struct mail_index_record *rec;
	unsigned char *data;
	size_t size, i;
	uint32_t *seq;
	int j;

	if (buf == NULL)
		return;

	/* @UNSAFE */
	data = buffer_get_modifyable_data(buf, &size);
	for (i = 0; i < size; i += record_size) {
		seq = (uint32_t *)&data[i];

		for (j = 0; j <= range; j++, seq++) {
			if (*seq >= t->first_new_seq) {
				rec = mail_index_transaction_lookup(t, *seq);
				*seq = rec->uid;
			} else {
				i_assert(*seq <= view->map->records_count);
				*seq = MAIL_INDEX_MAP_IDX(view->map,
							  *seq - 1)->uid;
			}
			i_assert(*seq != 0);
		}
	}
}

static int
{'fraction_non_alphanumeric': 0.13767161933421102, 'fraction_numerical': 0.007899191273274403, 'mean_word_length': 4.443193449334698, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 61, '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': '4027008', 'n_tokens_mistral': 2129, 'n_tokens_neox': 1971, 'n_words': 452}