
com.arakelian.jdbc.elastic.StoreEventIndexer Maven / Gradle / Ivy
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
package com.arakelian.jdbc.elastic;
import java.io.IOException;
import java.util.concurrent.RejectedExecutionException;
import com.arakelian.elastic.bulk.BulkIndexer;
import com.arakelian.elastic.bulk.BulkOperation;
import com.arakelian.elastic.bulk.BulkOperation.Action;
import com.arakelian.elastic.bulk.BulkOperationFactory;
import com.arakelian.store.event.AbstractStoreEventHandler;
import com.arakelian.store.event.StoreEvent;
import com.arakelian.store.feature.HasId;
import com.google.common.base.Preconditions;
public class StoreEventIndexer extends AbstractStoreEventHandler {
private final BulkIndexer bulkIndexer;
private final BulkOperationFactory factory;
public StoreEventIndexer(final BulkIndexer bulkIndexer) {
this.bulkIndexer = bulkIndexer;
this.factory = bulkIndexer.getConfig().getBulkOperationFactory();
}
@Override
protected void handle(final StoreEvent event, final long sequence, final boolean endOfBatch)
throws Exception {
final StoreEvent.Action action = event.getAction();
Preconditions.checkArgument(action != null, "action must be non-null");
switch (action) {
case PUT:
final T value = event.getValue();
Preconditions.checkArgument(value != null, "value must be non-null");
bulkIndexer.index(value);
break;
case DELETE:
onDelete(event);
break;
}
}
/**
* Handle delete notification
*
* @param event
* information about which value or id was deleted
* @throws IOException
* if event could not be serialized
* @throws RejectedExecutionException
* if event queue is full
*/
protected void onDelete(final StoreEvent event) throws RejectedExecutionException, IOException {
final T value = event.getValue();
if (value != null) {
// deleting value has advantage of pulling version information
bulkIndexer.delete(value);
return;
}
// we just have an id, no version information
final String id = event.getId();
Preconditions.checkArgument(id != null, "id or value must be non-null");
final BulkOperation op = factory.createBulkOperation(Action.DELETE, id, null, null);
bulkIndexer.add(op);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy