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 login The login to authenticate on the Solr app
040     * @param password The password to authenticate on the Solr app
041     * @param solrSocketTimeout The Solr socket timeout (in millis)
042     * @param collectionName The name of the collection, on which operations will be allowed. Operations on other collections will be forbidden.
043     * @param queueSize The buffer size before the documents are sent to the server
044     * @param threadCount The number of background threads used to empty the queue
045     * @param logger internal logger
046     */
047    public NoAutoCommitUpdateClient(String solrServerUrl, String login, String password, Optional<Integer> solrSocketTimeout, String collectionName, int queueSize, int threadCount, Logger logger)
048    {
049        super(solrServerUrl, login, password, solrSocketTimeout, collectionName, queueSize, threadCount, logger);
050    }
051    
052    @Override
053    public NamedList<Object> request(SolrRequest request, String collection) throws SolrServerException, IOException
054    {
055        _setUpdateRequestProcessorChain(request);
056        return super.request(request, collection);
057    }
058    
059    private void _setUpdateRequestProcessorChain(SolrRequest request)
060    {
061        if (request instanceof UpdateRequest)
062        {
063            ((UpdateRequest) request).setParam(UpdateParams.UPDATE_CHAIN, __UPDATE_CHAIN_NAME);
064        }
065    }
066}