// Path: fhir-index-plugin/src/main/java/io/puntanegra/fhir/index/FhirIndexException.java
// public class FhirIndexException extends RuntimeException {
// 
// 	private static final long serialVersionUID = 2532456234653465436L;
// 
// 	/**
// 	 * Constructs a new index exception with the specified formatted detail
// 	 * message.
// 	 *
// 	 * @param message
// 	 *            the detail message.
// 	 * @param args
// 	 *            arguments referenced by the format specifiers in the format
// 	 *            message
// 	 */
// 	public FhirIndexException(String message, Object... args) {
// 		super(String.format(message, args));
// 	}
// 
// 	/**
// 	 * Constructs a new index exception with the specified formatted detail
// 	 * message.
// 	 *
// 	 * @param cause
// 	 *            the cause
// 	 * @param message
// 	 *            the detail message
// 	 * @param args
// 	 *            arguments referenced by the format specifiers in the format
// 	 *            message
// 	 */
// 	public FhirIndexException(Throwable cause, String message, Object... args) {
// 		super(String.format(message, args), cause);
// 	}
// 
// 	/**
// 	 * Constructs a new index exception with the specified cause.
// 	 *
// 	 * @param cause
// 	 *            the cause
// 	 */
// 	public FhirIndexException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import io.puntanegra.fhir.index.FhirIndexException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;
/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) 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 io.puntanegra.fhir.index.lucene;




/**
 * Class wrapping a Lucene RAM directory and its readers, writers and searchers
 * for NRT.
 *
 * @author Andres de la Pena {@literal <adelapena@stratio.com>}
 */
public class LuceneRAMIndex {

	private final Directory directory;
	private final IndexWriter indexWriter;

	/**
	 * Builds a new {@link LuceneRAMIndex}.
	 *
	 * @param analyzer
	 *            the index writer analyzer
	 */
	public LuceneRAMIndex(Analyzer analyzer) {
		try {
			directory = new RAMDirectory();
			indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer));
		} catch (Exception e) {
			throw new FhirIndexException(e, "Error while creating index");
		}
	}

	/**
	 * Adds the specified {@link Document}
	 *
	 * @param document
	 *            the {@link Document} to be added
	 */
	public void add(Document document) {
		try {
			indexWriter.addDocument(document);
		} catch (Exception e) {
// Path: fhir-index-plugin/src/main/java/io/puntanegra/fhir/index/FhirIndexException.java
// public class FhirIndexException extends RuntimeException {
// 
// 	private static final long serialVersionUID = 2532456234653465436L;
// 
// 	/**
// 	 * Constructs a new index exception with the specified formatted detail
// 	 * message.
// 	 *
// 	 * @param message
// 	 *            the detail message.
// 	 * @param args
// 	 *            arguments referenced by the format specifiers in the format
// 	 *            message
// 	 */
// 	public FhirIndexException(String message, Object... args) {
// 		super(String.format(message, args));
// 	}
// 
// 	/**
// 	 * Constructs a new index exception with the specified formatted detail
// 	 * message.
// 	 *
// 	 * @param cause
// 	 *            the cause
// 	 * @param message
// 	 *            the detail message
// 	 * @param args
// 	 *            arguments referenced by the format specifiers in the format
// 	 *            message
// 	 */
// 	public FhirIndexException(Throwable cause, String message, Object... args) {
// 		super(String.format(message, args), cause);
// 	}
// 
// 	/**
// 	 * Constructs a new index exception with the specified cause.
// 	 *
// 	 * @param cause
// 	 *            the cause
// 	 */
// 	public FhirIndexException(Throwable cause) {
// 		super(cause);
// 	}
// 
// }


// Path: fhir-index-plugin/src/main/java/io/puntanegra/fhir/index/lucene/LuceneRAMIndex.java
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import io.puntanegra.fhir.index.FhirIndexException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.search.IndexSearcher;

/*
 * Licensed to STRATIO (C) under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.  The STRATIO (C) 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 io.puntanegra.fhir.index.lucene;




/**
 * Class wrapping a Lucene RAM directory and its readers, writers and searchers
 * for NRT.
 *
 * @author Andres de la Pena {@literal <adelapena@stratio.com>}
 */
public class LuceneRAMIndex {

	private final Directory directory;
	private final IndexWriter indexWriter;

	/**
	 * Builds a new {@link LuceneRAMIndex}.
	 *
	 * @param analyzer
	 *            the index writer analyzer
	 */
	public LuceneRAMIndex(Analyzer analyzer) {
		try {
			directory = new RAMDirectory();
			indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer));
		} catch (Exception e) {
			throw new FhirIndexException(e, "Error while creating index");
		}
	}

	/**
	 * Adds the specified {@link Document}
	 *
	 * @param document
	 *            the {@link Document} to be added
	 */
	public void add(Document document) {
		try {
			indexWriter.addDocument(document);
		} catch (Exception e) {
			throw new FhirIndexException(e, "Error while indexing %s", document);