package com.tropo.lucene;

import com.lumos.lucene.index.*;
import java.util.*;

import org.apache.lucene.store.*;
import org.apache.lucene.index.*;

/**
 * Ram directory conversion.
 */
public final class Rammer
{
	private Rammer()
	{
	}
	
	/**
	 * Create a RAMDirectory from a disk rep.
	 * @param path the path to the dir that stores the index
	 */
	public static RAMDirectory convert( String path)
		throws java.io.IOException
	{
		final RAMDirectory ram = new RAMDirectory();
		final Directory d = FSDirectory.getDirectory( path, false);
		final String[] ar = d.list();
		for ( int i = 0; i< ar.length; i++)
		{
			// make place on ram disk
			OutputStream os = ram.createFile( ar[ i]);
			// read current file
			InputStream is = d.openFile( ar[ i]);
			// and copy to ram disk
			int len = (int) is.length();			
			byte[] buf = new byte[ len];
			is.readBytes( buf, 0, len);
			os.writeBytes( buf, len);
			// graceful cleanup
			is.close();
			os.close();
		}
		return ram;
	}

	private static java.io.PrintStream o = System.out;
}
