lucene-general mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lfrohman <lfroh...@gmail.com>
Subject Directory Implementation problem
Date Mon, 12 Dec 2011 20:45:32 GMT
I am using Lucene 3.5.0, and have set up an index on several files. When I
use
FSDirectory, everything works fine, but when I try to implement my own
Directory implementation, I get different results. I have tried a few
implementations
and these always get the same wrong results, so I must have something wrong.
Is there a simple sample Directory implementation that works? I made a
simple
RandomAccessFile impl below.

thanks

==============

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.Lock;

public class FileDirectory extends Directory {
	private File file;

	private class FileIndexInput extends IndexInput {
		private final RandomAccessFile raf;

		private FileIndexInput(String path) throws IOException {
			super(path);
			raf = new RandomAccessFile(path, "r");
		}

		@Override
		public void close() throws IOException {}

		@Override
		public long getFilePointer() {
			try {
				return raf.getFilePointer();
			}
			catch (IOException x) {
				x.printStackTrace(System.out);
				return 0;
			}
		}

		@Override
		public void seek(long pos) throws IOException {
			raf.seek(pos);
		}

		@Override
		public long length() {
			try {
				return raf.length();
			}
			catch (IOException x) {
				x.printStackTrace(System.out);
				return 0;
			}
		}

		@Override
		public byte readByte() throws IOException {
			return raf.readByte();
		}

		@Override
		public void readBytes(byte[] b, int offset, int len) throws IOException {
			raf.read(b, offset, len);
		}
	}

	public FileDirectory(String path) {
		file = new File(path);
	}

	@Override
	public String[] listAll() throws IOException {
		return file.list();
	}

	@Override
	public boolean fileExists(String name) throws IOException {
		return Arrays.asList(listAll()).contains(name);
	}

	@Override
	public long fileLength(String name) throws IOException {
		return new File(file.getAbsolutePath() + "/" + name).length();
	}

	@Override
	public IndexInput openInput(String name) throws IOException {
		return new FileIndexInput(file.getAbsolutePath() + "/" + name);
	}

	@Override
	public void close() throws IOException {
	}

	// the following methods are not implemented since this is a read only
implementation
	@Override public IndexOutput createOutput(String name) throws IOException {
return null; }
	@Override public void deleteFile(String name) throws IOException {}
	@Override public void touchFile(String name) throws IOException {}
	@Override public long fileModified(String name) throws IOException { return
0; }
	@Override
    public Lock makeLock(String name) {
        return new Lock() {
            public boolean obtain() throws IOException { return true; }
            public void release() {}
            public boolean isLocked() { return false; }
        };
    }
}


--
View this message in context: http://lucene.472066.n3.nabble.com/Directory-Implementation-problem-tp3580606p3580606.html
Sent from the Lucene - General mailing list archive at Nabble.com.

Mime
View raw message