<commit_before>/*
 * Hibernate OGM, Domain model persistence for NoSQL datastores
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.ogm.dialect.batch.spi;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import org.hibernate.ogm.model.key.spi.EntityKey;
import org.hibernate.ogm.util.impl.Log;
import org.hibernate.ogm.util.impl.LoggerFactory;

/**
 * A queue for {@link Operation}s.
 * <p>
 * It keeps track of the elements that are going to be affected by an {@link InsertOrUpdateTupleOperation}.
 * The queue can be closed, in that case it will throw an exception when trying to add or poll an operation.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME>
 */
public class OperationsQueue {

	/**
	 * A queue that is always closed
	 */
	public static final OperationsQueue CLOSED_QUEUE = new OperationsQueue() {
		@Override
		public boolean isClosed() {
			return true;
		}
	};

	private static final Log log = LoggerFactory.make();

	private final Queue<Operation> operations = new LinkedList<Operation>();

	private final Map<EntityKey, GroupedChangesToEntityOperation> groupedOperations = new HashMap<>();

	private final Set<EntityKey> insertionQueue = new HashSet<>();

	private boolean closed = false;

	public OperationsQueue() {
	}

	public void add(InsertOrUpdateTupleOperation operation) {
		add( (GroupableEntityOperation) operation );
		insertionQueue.add( operation.getEntityKey() );
	}

	public void add(GroupableEntityOperation operation) {
		validate();
		GroupedChangesToEntityOperation groupedOperation = getOrCreateGroupedChangesOnEntityOperation( operation.getEntityKey() );
		groupedOperation.addOperation( operation );
	}

	private GroupedChangesToEntityOperation getOrCreateGroupedChangesOnEntityOperation(EntityKey entityKey) {
		GroupedChangesToEntityOperation groupedOperation = groupedOperations.get( entityKey );
		if ( groupedOperation == null ) {
			groupedOperation = new GroupedChangesToEntityOperation( entityKey );
			groupedOperations.put( entityKey, groupedOperation );
			addOperation( groupedOperation );
		}
		return groupedOperations.get( entityKey );
	}

	public void add(Operation operation) {
		validate();
		addOperation( operation );
	}

	private void validate() {
		if ( isClosed() ) {
			throw log.closedOperationQueue();
		}
	}

	private void addOperation(Operation operation) {
		log.debugf( "Add batched operation %1$s", operation );
		operations.add( operation );
	}

	public Operation poll() {
		validate();
		return operations.poll();
	}

	public void clear() {
		groupedOperations.clear();
		operations.clear();
		insertionQueue.clear();
	}

	public void close() {
		clear();
		closed = true;
	}
<commit_msg>OGM-1064 Change the way operations are added to the queue for clarity<commit_after>/*
 * Hibernate OGM, Domain model persistence for NoSQL datastores
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.ogm.dialect.batch.spi;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import org.hibernate.ogm.model.key.spi.EntityKey;
import org.hibernate.ogm.util.impl.Log;
import org.hibernate.ogm.util.impl.LoggerFactory;

/**
 * A queue for {@link Operation}s.
 * <p>
 * It keeps track of the elements that are going to be affected by an {@link InsertOrUpdateTupleOperation}.
 * The queue can be closed, in that case it will throw an exception when trying to add or poll an operation.
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME> &lt;<EMAIL>&gt;
 * @author <NAME>
 */
public class OperationsQueue {

	/**
	 * A queue that is always closed
	 */
	public static final OperationsQueue CLOSED_QUEUE = new OperationsQueue() {
		@Override
		public boolean isClosed() {
			return true;
		}
	};

	private static final Log log = LoggerFactory.make();

	private final Queue<Operation> operations = new LinkedList<Operation>();

	private final Map<EntityKey, GroupedChangesToEntityOperation> groupedOperations = new HashMap<>();

	private final Set<EntityKey> insertionQueue = new HashSet<>();

	private boolean closed = false;

	public OperationsQueue() {
	}

	public void add(Operation operation) {
		validate();

		log.debugf( "Add batched operation %1$s", operation );

		if ( operation instanceof InsertOrUpdateTupleOperation ) {
			addInsertOrUpdateTupleOperation( (InsertOrUpdateTupleOperation) operation );
		}
		else if ( operation instanceof GroupableEntityOperation ) {
			addGroupableEntityOperation( (GroupableEntityOperation) operation );
		}
		else {
			addOperation( operation );
		}
	}

	private void addInsertOrUpdateTupleOperation(InsertOrUpdateTupleOperation operation) {
		addGroupableEntityOperation( operation );
		insertionQueue.add( operation.getEntityKey() );
	}

	private void addGroupableEntityOperation(GroupableEntityOperation operation) {
		validate();
		GroupedChangesToEntityOperation groupedOperation = getOrCreateGroupedChangesOnEntityOperation( operation.getEntityKey() );
		groupedOperation.addOperation( operation );
	}

	private void addOperation(Operation operation) {
		operations.add( operation );
	}

	private GroupedChangesToEntityOperation getOrCreateGroupedChangesOnEntityOperation(EntityKey entityKey) {
		GroupedChangesToEntityOperation groupedOperation = groupedOperations.get( entityKey );
		if ( groupedOperation == null ) {
			groupedOperation = new GroupedChangesToEntityOperation( entityKey );
			groupedOperations.put( entityKey, groupedOperation );
			addOperation( groupedOperation );
		}
		return groupedOperations.get( entityKey );
	}

	private void validate() {
		if ( isClosed() ) {
			throw log.closedOperationQueue();
		}
	}

	public Operation poll() {
		validate();
		return operations.poll();
	}

	public void clear() {
		groupedOperations.clear();
		operations.clear();
		insertionQueue.clear();
	}

	public void close() {
		clear();
		closed = true;
	}

{'fraction_non_alphanumeric': 0.10342096927462781, 'fraction_numerical': 0.0022172949002217295, 'mean_word_length': 4.798898071625344, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '2372795', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1785, 'n_words': 508}