
com.thematchbox.river.indexers.MatchBoxIndexer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AbstractRiver Show documentation
Show all versions of AbstractRiver Show documentation
This project contains an abstract implementation of an ElasticSearch River and is used as a basis for custom river implementations.
package com.thematchbox.river.indexers;
/* 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.actions.ActionDelegator;
import com.thematchbox.river.actions.IndexFeedback;
import com.thematchbox.river.actions.IndexJob;
import org.elasticsearch.client.Client;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class MatchBoxIndexer {
private Logger logger = LoggerFactory.getLogger(MatchBoxIndexer.class);
private final UniqueLinkedList indexJobs = new UniqueLinkedList<>();
private final ProcessorThread processorThread = new ProcessorThread();
private final LinkedList feedbackLog = new LinkedList<>();
private Client client;
private int maxIndexLogSize;
private ActionDelegator actionDelegator;
public MatchBoxIndexer(Client client, int maxIndexLogSize, ActionDelegator actionDelegator) {
this.client = client;
this.maxIndexLogSize = maxIndexLogSize;
this.actionDelegator = actionDelegator;
}
public boolean addIndexJob(IndexJob indexJob) {
synchronized (indexJobs) {
if (indexJobs.offer(indexJob)) {
indexJobs.notify();
return true;
} else {
return false;
}
}
}
public void start() {
processorThread.start();
}
public void stop() {
processorThread.shutDown();
synchronized (indexJobs) {
indexJobs.notify();
}
}
public List getFeedbackLog() {
List results = new ArrayList<>();
synchronized (feedbackLog) {
for (IndexFeedback status : feedbackLog) {
results.add(status.copy());
}
}
return results;
}
public int clearFeedbackLog() {
int count = 0;
synchronized (feedbackLog) {
Iterator iterator = feedbackLog.iterator();
while (iterator.hasNext()) {
IndexFeedback next = iterator.next();
switch (next.getState()) {
case SCHEDULED:
case INDEXING:
break;
case FINISHED:
case FAILED:
count++;
iterator.remove();
break;
}
}
}
return count;
}
private final class ProcessorThread extends Thread {
private boolean run = true;
public synchronized boolean isRun() {
return run;
}
public synchronized void shutDown() {
this.run = false;
}
@Override
public void run() {
while (isRun()) {
IndexJob indexJob;
synchronized (indexJobs) {
while(indexJobs.isEmpty()) {
try {
indexJobs.wait();
if (!isRun()) {
break;
}
} catch (InterruptedException e) {
// Do nothing
}
}
// Do not remove yet, to avoid double having double entries in the queue, while the peek request is being handled.
indexJob = indexJobs.peek();
}
if (indexJob != null) {
try {
IndexFeedback feedback = new IndexFeedback(indexJob);
synchronized (feedbackLog) {
if (feedbackLog.size() == maxIndexLogSize) {
feedbackLog.poll();
}
feedbackLog.offer(feedback);
}
try {
executeRequest(indexJob, feedback);
if (feedback.isIgnore()) {
synchronized (feedbackLog) {
feedbackLog.remove(feedback);
}
}
} finally {
indexJobs.poll();
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
}
public void executeRequest(IndexJob indexJob, IndexFeedback indexFeedback) {
switch (indexJob.actionType) {
case FULL_REBUILD:
actionDelegator.getRebuildAction(indexJob.indexKey).execute(client, indexJob, indexFeedback);
break;
case INCREMENTAL_UPDATE:
actionDelegator.getUpdateAction(indexJob.indexKey).execute(client, indexJob, indexFeedback);
break;
case INCREMENTAL_DELETE:
actionDelegator.getDeleteAction(indexJob.indexKey).execute(client, indexJob, indexFeedback);
break;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy