/**
 * Copyright (c) 2000-2017 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * pSubscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.search.buffer;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.util.PropsValues;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @author Jorge Díaz
 */
public class IndexerRequestBufferUtil {

	public static void execute(
		IndexerRequestBuffer indexerRequestBuffer, int numRequests) {

		Set<String> searchEngineIds = new HashSet<String>();

		Collection<IndexerRequest> completedIndexerRequests =
				new ArrayList<IndexerRequest>();

		if (_log.isDebugEnabled()) {
			Collection<IndexerRequest> indexerRequests =
				indexerRequestBuffer.getIndexerRequests();

			_log.debug(
				"Indexer request buffer size " + indexerRequests.size() +
					" to execute " + numRequests + " requests");
		}

		int i = 0;

		for (IndexerRequest indexerRequest :
				indexerRequestBuffer.getIndexerRequests()) {

			if (_log.isDebugEnabled()) {
				_log.debug(
					"Executing indexer request " + (i++) + ": " +
						indexerRequest);
			}

			executeIndexerRequest(searchEngineIds, indexerRequest);

			completedIndexerRequests.add(indexerRequest);

			if (completedIndexerRequests.size() == numRequests) {
				break;
			}
		}

		for (IndexerRequest indexerRequest : completedIndexerRequests) {
			indexerRequestBuffer.remove(indexerRequest);
		}
	}

	public static void bufferOverflowed(
			IndexerRequestBuffer indexerRequestBuffer, int maxBufferSize) {

		int currentBufferSize = indexerRequestBuffer.size();

		float minimumAvailabilityPercentage =
			PropsValues.INDEX_REQUEST_BUFFER_MINIMUM_AVAILABILITY_PERCENTAGE;

		int numRequests = Math.round(
			currentBufferSize -
				Math.abs(maxBufferSize * minimumAvailabilityPercentage));

		if (numRequests > 0) {
			try {
				BufferOverflowThreadLocal.setOverflowMode(true);

				IndexerRequestBufferUtil.execute(
					indexerRequestBuffer, numRequests);
			}
			finally {
				BufferOverflowThreadLocal.setOverflowMode(false);
			}
		}
	}

	protected static void executeIndexerRequest(
		Set<String> searchEngineIds, IndexerRequest indexerRequest) {

		try {
			indexerRequest.execute();

			searchEngineIds.add(indexerRequest.getSearchEngineId());
		}
		catch (Exception e) {
			if (_log.isWarnEnabled()) {
				_log.warn(
					"Unable to execute index request " + indexerRequest, e);
			}
		}
	}

	public static void execute(IndexerRequestBuffer indexerRequestBuffer) {
		execute(indexerRequestBuffer, indexerRequestBuffer.size());
	}

	private static Log _log = LogFactoryUtil.getLog(
		IndexerRequestBufferUtil.class);

}