net.tooan.ynpay.third.mongodb.lucene.backend.IndexRemoveTask.java Source code

Java tutorial

Introduction

Here is the source code for net.tooan.ynpay.third.mongodb.lucene.backend.IndexRemoveTask.java

Source

/*
 * Copyright (c) www.bugull.com
 * 
 * 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.
 */

package net.tooan.ynpay.third.mongodb.lucene.backend;

import net.tooan.ynpay.third.jfinal.log.Logger;
import net.tooan.ynpay.third.mongodb.cache.FieldsCache;
import net.tooan.ynpay.third.mongodb.cache.IndexWriterCache;
import net.tooan.ynpay.third.mongodb.mapper.MapperUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;

import java.io.IOException;

/**
 * @author Frank Wen(xbwen@hotmail.com)
 */
public class IndexRemoveTask implements Runnable {

    private final static Logger logger = Logger.getLogger(IndexRemoveTask.class);

    private Class<?> clazz;
    private String id;

    public IndexRemoveTask(Class<?> clazz, String id) {
        this.clazz = clazz;
        this.id = id;
    }

    @Override
    public void run() {
        String[] name = MapperUtil.getEntityName(clazz);
        IndexWriterCache cache = IndexWriterCache.getInstance();
        IndexWriter writer = cache.get(StringUtils.join(name, "."));
        Term term = new Term(FieldsCache.getInstance().getIdFieldName(clazz), id);
        try {
            writer.deleteDocuments(term);
        } catch (CorruptIndexException ex) {
            logger.error("IndexWriter can not delete a document from the lucene index", ex);
        } catch (IOException ex) {
            logger.error("IndexWriter can not delete a document from the lucene index", ex);
        }
    }

}