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;
019import java.util.Optional;
020
021import org.apache.solr.client.solrj.SolrClient;
022import org.apache.solr.client.solrj.SolrRequest;
023import org.apache.solr.client.solrj.SolrServerException;
024import org.apache.solr.client.solrj.request.UpdateRequest;
025import org.apache.solr.common.params.UpdateParams;
026import org.apache.solr.common.util.NamedList;
027import org.slf4j.Logger;
028
029/**
030 * Update {@link SolrClient} which uses the Solr `noAutoCommit` Update Request Processor chain, so as to not trigger the autoCommit mechanism.
031 */
032public class NoAutoCommitUpdateClient extends AbstractAmetysConcurrentUpdateClient
033{
034    private static final String __UPDATE_CHAIN_NAME = "noAutoCommit";
035    
036    /**
037     * Constructor
038     * @param solrServerUrl The Solr server URL
039     * @param solrSocketTimeout The Solr socket timeout (in millis)
040     * @param collectionName The name of the collection, on which operations will be allowed. Operations on other collections will be forbidden.
041     * @param queueSize The buffer size before the documents are sent to the server
042     * @param threadCount The number of background threads used to empty the queue
043     * @param logger internal logger
044     */
045    public NoAutoCommitUpdateClient(String solrServerUrl, Optional<Integer> solrSocketTimeout, String collectionName, int queueSize, int threadCount, Logger logger)
046    {
047        super(solrServerUrl, solrSocketTimeout, collectionName, queueSize, threadCount, logger);
048    }
049    
050    @Override
051    public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException
052    {
053        _setUpdateRequestProcessorChain(request);
054        return super.request(request, collection);
055    }
056    
057    private void _setUpdateRequestProcessorChain(SolrRequest request)
058    {
059        if (request instanceof UpdateRequest)
060        {
061            ((UpdateRequest) request).setParam(UpdateParams.UPDATE_CHAIN, __UPDATE_CHAIN_NAME);
062        }
063    }
064}