001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.buffer;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.PropsValues;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.HashSet;
024    import java.util.Set;
025    
026    /**
027     * @author Jorge D??az
028     */
029    public class IndexerRequestBufferUtil {
030    
031            public static void execute(
032                    IndexerRequestBuffer indexerRequestBuffer, int numRequests) {
033    
034                    Set<String> searchEngineIds = new HashSet<String>();
035    
036                    Collection<IndexerRequest> completedIndexerRequests =
037                                    new ArrayList<IndexerRequest>();
038    
039                    if (_log.isDebugEnabled()) {
040                            Collection<IndexerRequest> indexerRequests =
041                                    indexerRequestBuffer.getIndexerRequests();
042    
043                            _log.debug(
044                                    "Indexer request buffer size " + indexerRequests.size() +
045                                            " to execute " + numRequests + " requests");
046                    }
047    
048                    int i = 0;
049    
050                    for (IndexerRequest indexerRequest :
051                                    indexerRequestBuffer.getIndexerRequests()) {
052    
053                            if (_log.isDebugEnabled()) {
054                                    _log.debug(
055                                            "Executing indexer request " + (i++) + ": " +
056                                                    indexerRequest);
057                            }
058    
059                            executeIndexerRequest(searchEngineIds, indexerRequest);
060    
061                            completedIndexerRequests.add(indexerRequest);
062    
063                            if (completedIndexerRequests.size() == numRequests) {
064                                    break;
065                            }
066                    }
067    
068                    for (IndexerRequest indexerRequest : completedIndexerRequests) {
069                            indexerRequestBuffer.remove(indexerRequest);
070                    }
071            }
072    
073            public static void bufferOverflowed(
074                            IndexerRequestBuffer indexerRequestBuffer, int maxBufferSize) {
075    
076                    int currentBufferSize = indexerRequestBuffer.size();
077    
078                    float minimumAvailabilityPercentage =
079                            PropsValues.INDEX_REQUEST_BUFFER_MINIMUM_AVAILABILITY_PERCENTAGE;
080    
081                    int numRequests = Math.round(
082                            currentBufferSize -
083                                    Math.abs(maxBufferSize * minimumAvailabilityPercentage));
084    
085                    if (numRequests > 0) {
086                            try {
087                                    BufferOverflowThreadLocal.setOverflowMode(true);
088    
089                                    IndexerRequestBufferUtil.execute(
090                                            indexerRequestBuffer, numRequests);
091                            }
092                            finally {
093                                    BufferOverflowThreadLocal.setOverflowMode(false);
094                            }
095                    }
096            }
097    
098            protected static void executeIndexerRequest(
099                    Set<String> searchEngineIds, IndexerRequest indexerRequest) {
100    
101                    try {
102                            indexerRequest.execute();
103    
104                            searchEngineIds.add(indexerRequest.getSearchEngineId());
105                    }
106                    catch (Exception e) {
107                            if (_log.isWarnEnabled()) {
108                                    _log.warn(
109                                            "Unable to execute index request " + indexerRequest, e);
110                            }
111                    }
112            }
113    
114            public static void execute(IndexerRequestBuffer indexerRequestBuffer) {
115                    execute(indexerRequestBuffer, indexerRequestBuffer.size());
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    IndexerRequestBufferUtil.class);
120    
121    }