001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.search.solr;
017
018import java.io.IOException;
019
020import org.apache.solr.client.solrj.SolrClient;
021import org.apache.solr.client.solrj.SolrRequest;
022import org.apache.solr.client.solrj.SolrServerException;
023import org.apache.solr.client.solrj.request.UpdateRequest;
024import org.apache.solr.common.params.UpdateParams;
025import org.apache.solr.common.util.NamedList;
026import org.slf4j.Logger;
027
028/**
029 * Update {@link SolrClient} which uses the Solr `noAutoCommit` Update Request Processor chain, so as to not trigger the autoCommit mechanism.
030 */
031public class NoAutoCommitUpdateClient extends AbstractAmetysConcurrentUpdateClient
032{
033    private static final String __UPDATE_CHAIN_NAME = "noAutoCommit";
034    
035    /**
036     * Constructor
037     * @param solrServerUrl The Solr server URL
038     * @param collectionName The name of the collection, on which operations will be allowed. Operations on other collections will be forbidden.
039     * @param queueSize The buffer size before the documents are sent to the server
040     * @param threadCount The number of background threads used to empty the queue
041     * @param logger internal logger
042     */
043    public NoAutoCommitUpdateClient(String solrServerUrl, String collectionName, int queueSize, int threadCount, Logger logger)
044    {
045        super(solrServerUrl, collectionName, queueSize, threadCount, logger);
046    }
047    
048    @Override
049    public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException
050    {
051        _setUpdateRequestProcessorChain(request);
052        return super.request(request, collection);
053    }
054    
055    private void _setUpdateRequestProcessorChain(SolrRequest request)
056    {
057        if (request instanceof UpdateRequest)
058        {
059            ((UpdateRequest) request).setParam(UpdateParams.UPDATE_CHAIN, __UPDATE_CHAIN_NAME);
060        }
061    }
062}