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

com.jaeksoft.searchlib.cache.TermVectorCache Maven / Gradle / Ivy

Go to download

OpenSearchServer is a powerful, enterprise-class, search engine program. Using the web user interface, the crawlers (web, file, database, ...) and the REST/RESTFul API you will be able to integrate quickly and easily advanced full-text search capabilities in your application. OpenSearchServer runs on Windows and Linux/Unix/BSD.

There is a newer version: 1.5.14
Show newest version
/**   
 * License Agreement for OpenSearchServer
 *
 * Copyright (C) 2008-2012 Emmanuel Keller / Jaeksoft
 * 
 * http://www.open-search-server.com
 * 
 * This file is part of OpenSearchServer.
 *
 * OpenSearchServer is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 * OpenSearchServer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenSearchServer. 
 *  If not, see .
 **/

package com.jaeksoft.searchlib.cache;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.lucene.index.TermFreqVector;

import com.jaeksoft.searchlib.function.expression.SyntaxError;
import com.jaeksoft.searchlib.index.FieldContentCacheKey;
import com.jaeksoft.searchlib.index.IndexConfig;
import com.jaeksoft.searchlib.index.ReaderLocal;
import com.jaeksoft.searchlib.query.ParseException;
import com.jaeksoft.searchlib.util.Timer;

public class TermVectorCache extends LRUCache {

	private IndexConfig indexConfig;

	public TermVectorCache(IndexConfig indexConfig) {
		super("TermVector cache", indexConfig.getTermVectorCache());
		this.indexConfig = indexConfig;
	}

	final public String[] get(final ReaderLocal reader, final int docId,
			final String field, final Timer timer) throws IOException,
			ParseException, SyntaxError {
		FieldContentCacheKey key = new FieldContentCacheKey(field, docId);
		String[] terms = getAndPromote(key);
		if (terms != null)
			return terms;
		TermFreqVector termFreqVector = reader.getTermFreqVector(docId, field);
		if (termFreqVector == null)
			return null;
		terms = termFreqVector.getTerms();
		if (terms == null)
			return null;
		put(key, terms);
		return terms;
	}

	final public void put(final ReaderLocal reader, final int[] docIds,
			final String field, Collection termVectors)
			throws IOException {
		if (docIds == null)
			return;
		List termVectorList = new ArrayList(docIds.length);
		Map keysMap = new LinkedHashMap();
		int i = 0;
		for (int docId : docIds) {
			FieldContentCacheKey key = new FieldContentCacheKey(field, docId);
			String[] terms = getAndPromote(key);
			termVectorList.add(terms);
			if (terms == null)
				keysMap.put(key, i);
			i++;
		}
		if (keysMap.size() > 0) {
			int[] getDocIds = new int[keysMap.size()];
			i = 0;
			for (Map.Entry entry : keysMap
					.entrySet())
				getDocIds[i++] = entry.getKey().docId;
			List termFreqVectors = new ArrayList(
					getDocIds.length);
			reader.putTermFreqVectors(getDocIds, field, termFreqVectors);
			i = 0;
			for (Map.Entry entry : keysMap
					.entrySet()) {
				final String[] terms = termFreqVectors.get(i++).getTerms();
				termVectorList.set(entry.getValue(), terms);
				put(entry.getKey(), terms);
			}
		}
		for (String[] terms : termVectorList)
			termVectors.add(terms);
	}

	@Override
	public void setMaxSize(int newMaxSize) {
		super.setMaxSize(newMaxSize);
		indexConfig.setTermVectorCache(newMaxSize);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy