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

org.tinygroup.fulltext.impl.AbstractFullText Maven / Gradle / Ivy

The newest version!
package org.tinygroup.fulltext.impl;

import java.util.ArrayList;
import java.util.List;

import org.tinygroup.commons.tools.StringUtil;
import org.tinygroup.fulltext.DocumentListCreator;
import org.tinygroup.fulltext.FullText;
import org.tinygroup.fulltext.IndexOperator;
import org.tinygroup.fulltext.Pager;
import org.tinygroup.fulltext.document.Document;
import org.tinygroup.fulltext.exception.FullTextException;
import org.tinygroup.fulltext.field.Field;

@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class AbstractFullText implements FullText {

	/**
	 * 批量文档处理器列表
	 */
	private List documentListCreators;
	
	/**
	 * 具体的索引管理器
	 */
	private IndexOperator indexOperator;

	public List getDocumentListCreators() {
		return documentListCreators;
	}

	public void setDocumentListCreators(
			List documentListCreators) {
		this.documentListCreators = documentListCreators;
	}

	public IndexOperator getIndexOperator() {
		return indexOperator;
	}

	public void setIndexOperator(IndexOperator indexOperator) {
		this.indexOperator = indexOperator;
	}

	/**
	 * 检查类型
	 * 
	 * @param type
	 */
	protected void checkType(String type) {
		if (StringUtil.isEmpty(type)) {
			throw new FullTextException("类型参数不能为空!");
		}
	}

	/**
	 * 检查索引数据
	 * 
	 * @param data
	 */
	protected  void checkData(T data) {
		if (data == null) {
			throw new FullTextException("索引数据不能为空!");
		}
	}
	
	protected  boolean matchData(DocumentListCreator creator,T data){
		try{
			return creator.isMatch(data);	
		}catch(ClassCastException e){
			//忽略类型不配
			return false;
		}
	}

	public  void createIndex(String type, T data, Object... arguments) {
		checkType(type);
		checkData(data);
		for (DocumentListCreator creator : this.documentListCreators) {
			if (matchData(creator,data)) {
				List docs = creator.getDocument(type, data, arguments);
				this.indexOperator.createIndex(docs);
				return;
			}
		}
		throw new FullTextException("没有找到合适的索引处理器创建索引!");
	}

	public  void deleteIndex(String type, T data, Object... arguments) {
		checkType(type);
		checkData(data);
		
		for (DocumentListCreator creator : this.documentListCreators) {
			if (matchData(creator,data)) {
				List docs = creator.getDocument(type, data, arguments);
				List docIds = new ArrayList();
				for(Document doc:docs){
					docIds.add(doc.getId());
				}
				this.indexOperator.deleteIndex(docIds);
				return;
			}
		}
		throw new FullTextException("没有找到合适的索引处理器删除索引!");
	}
	
	public Pager search(String searchCondition) {
		return search(searchCondition, 0 , 10);
	}
	
	public Pager search(String searchCondition,int start,int limit) {
		return indexOperator.search(searchCondition, start, limit);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy