All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.thematchbox.river.actions.DeleteItemsAction Maven / Gradle / Ivy

Go to download

This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.

There is a newer version: 1.1.3
Show newest version
package com.thematchbox.river.actions;

/* Copyright 2015 theMatchBox

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

import com.thematchbox.river.data.PersistentObject;
import com.thematchbox.river.sessions.SessionDelegator;
import com.thematchbox.river.sessions.SessionDelegatorFactory;
import com.thematchbox.river.sessions.SessionException;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

@SuppressWarnings("UnusedDeclaration")
public abstract class DeleteItemsAction, T extends PersistentObject, D extends SessionDelegator, F extends SessionDelegatorFactory> implements IndexAction {

    public static final Logger logger = LoggerFactory.getLogger(AbstractIndexAction.class);

    private int batchSize;
    private F sessionDelegatorFactory;

    protected abstract List getIdsToDelete(Client client, IndexJob indexJob, D sessionDelegator) throws SessionException;

    protected DeleteItemsAction(int batchSize, F sessionDelegatorFactory) {
        this.batchSize = batchSize;
        this.sessionDelegatorFactory = sessionDelegatorFactory;
    }

    @Override
    public void execute(Client client, IndexJob indexJob, IndexFeedback feedback) {
        String indexName = indexJob.indexKey.indexName;
        String indexType = indexJob.indexKey.indexType;

        try {
            D sessionDelegator = sessionDelegatorFactory.create();

            try {
                List idsToDelete;
                try {
                    sessionDelegator.openSession();
                    idsToDelete = getIdsToDelete(client, indexJob, sessionDelegator);
                } finally {
                    sessionDelegator.closeSession();
                }

                if (idsToDelete.isEmpty()) {
                    feedback.setIgnore(true);
                    return;
                }

                feedback.init(idsToDelete.size());
                BulkRequestBuilder bulkRequest = client.prepareBulk();
                int addedCount = 0;
                for (S id : idsToDelete) {
                    try {
                        bulkRequest.add(client.prepareDelete(indexName, indexType, id.toString()));

                        addedCount++;
                        if (addedCount == batchSize) {
                            BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                            if (bulkResponse.hasFailures()) {
                                logger.error(bulkResponse.buildFailureMessage());
                                feedback.incrementFailCount(addedCount);
                            } else {
                                feedback.incrementSuccessCount(addedCount);
                            }
                            addedCount = 0;
                            bulkRequest = client.prepareBulk();
                        }
                    } catch (Exception e) {
                        feedback.incrementFailCount(1);
                        logger.error(e.getMessage(), e);
                    }

                }

                if (addedCount > 0) {
                    BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                    if (bulkResponse.hasFailures()) {
                        feedback.incrementFailCount(addedCount);
                        logger.error(bulkResponse.buildFailureMessage());
                    } else {
                        feedback.incrementSuccessCount(addedCount);
                    }
                }


            } catch (SessionException e) {
                sessionDelegator.reset();
                throw e;
            }
        } catch (SessionException e) {
            logger.error(e.getMessage(), e);
        }

    }
}