apache / lucene

Apache Lucene open-source search software
https://lucene.apache.org/
Apache License 2.0
2.43k stars 964 forks source link

Enhanced FSDirectory that allow lock disable via API [LUCENE-84] #1162

Closed asfimport closed 18 years ago

asfimport commented 21 years ago

Below is a new version of FSDirectory.java. It is based on 1/30/2003 source.

I have added one new version each of getDirectory(File) and getDirectory (String). They take a new third argument 'boolean useLocks'.

The previous 'private static final boolean DISABLE_LOCKS' has been changed to 'private static boolean _disableLocks' and is initialized to false. I also added a new method 'private boolean locksDisabledByProp' that checks the 'disableLuceneLocks' system property.

Method makeLock now checks the static _disableLocks as the first term in an OR clause, the second of which is a call to locksDisabledByProp. This allows use in an applet that does not have write access to the local filesystem, and when the API is used in that way prevents the query of the system property that is also disallowed in an applet by default (at least in Mozilla/Netscape).

From my applet, I can now invoke:

Searcher searcher = new IndexSearcher( IndexReader.open(FSDirectory.getDirectory(indexFile, false, false)));

and I get an IndexSearcher that will work in the applet with no special permissions other than applet JAR signing.

Obviously, email me (jmethot@bea.com) if you have any questions.

************************* FSDirectory.java *******************************

package org.apache.lucene.store;

/* ====================================================================

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

import org.apache.lucene.util.Constants;

/**

public final class FSDirectory extends Directory { /** This cache of directories ensures that there is a unique Directory

final class FSInputStream extends InputStream { private class Descriptor extends RandomAccessFile { public long position; public Descriptor(File file, String mode) throws IOException { super(file, mode); } }

Descriptor file = null; boolean isClone;

public FSInputStream(File path) throws IOException { file = new Descriptor(path, "r"); length = file.length(); }

/** InputStream methods */ protected final void readInternal(byte[] b, int offset, int len) throws IOException { synchronized (file) { long position = getFilePointer(); if (position != file.position) { file.seek(position); file.position = position; } int total = 0; do { int i = file.read(b, offset+total, len-total); if (i == -1) throw new IOException("read past EOF"); file.position += i; total += i; } while (total < len); } }

public final void close() throws IOException { if (!isClone) file.close(); }

/** Random-access methods */ protected final void seekInternal(long position) throws IOException { }

protected final void finalize() throws IOException { close(); // close the file }

public Object clone() { FSInputStream clone = (FSInputStream)super.clone(); clone.isClone = true; return clone; } }

final class FSOutputStream extends OutputStream { RandomAccessFile file = null;

public FSOutputStream(File path) throws IOException { file = new RandomAccessFile(path, "rw"); }

/** output methods: */ public final void flushBuffer(byte[] b, int size) throws IOException { file.write(b, 0, size); } public final void close() throws IOException { super.close(); file.close(); }

/** Random-access methods */ public final void seek(long pos) throws IOException { super.seek(pos); file.seek(pos); } public final long length() throws IOException { return file.length(); }

protected final void finalize() throws IOException { file.close(); // close the file }

}


Migrated from LUCENE-84 by John Methot, resolved Sep 27 2005 Environment:

Operating System: All
Platform: PC

Attachments: ASF.LICENSE.NOT.GRANTED--FSDirectory.java (versions: 2)

asfimport commented 21 years ago

John Methot (migrated from JIRA)

Created an attachment (id=4743) Proposed new code FSDirectory.java

asfimport commented 21 years ago

Otis Gospodnetic (migrated from JIRA)

John - just so I understand - your change does not add any new functionality, it simply allows you to disable locks by using one of your 2 new methods. Is this because you cannot set the system properly when writing an applet? Couldn't you just call System.setProperty(String key, String value) to set disableLuceneLocks property to true before calling FSDirectory and achieve the same thing?

asfimport commented 21 years ago

Otis Gospodnetic (migrated from JIRA)

The diff is a bit hard to read, contains some formatting changes in addition to changes to the way locking is disabled, but I think it looks good, it compiles, and it doesn't break any unit tests. This is also a modification made to one of the older versions of FSDirectory, but that should be easy to handle.

Anyhow, I'm checking this in, unless somebody complains by tomorrow.

asfimport commented 20 years ago

Otis Gospodnetic (migrated from JIRA)

I do not remember if the attached code was ever committed, but I believe we've had the have support to disable locking for a while now. I'm closing this issue for now.

asfimport commented 20 years ago

John Methot (migrated from JIRA)

Otis, I never saw your comments in the bug, thus my failure to reply. I'm not clear whether your comment in the bug today means you're removing my changes from the codebase (your previous comment says you checked it in).

Vis a vis your prior comment:

"John - just so I understand - your change does not add any new functionality..."

One can't call System.setProperty() from an applet unless the applet has specific permission to do so, as configured on the end-user's machine. The only way to disable locking wihtout special permission, to my knowledge, is via the API.

asfimport commented 20 years ago

John Methot (migrated from JIRA)

Created an attachment (id=10558) New version based on 1.3-final source - easier diffs

asfimport commented 20 years ago

John Methot (migrated from JIRA)

Well, I completely misunderstood Otis' comments that meant he hadn't checked in the change. I didn't realize it hadn't been checked in until I updated to 1.3 and the fix wasn't there.

So here it is again. I've rewritten the changes against 1.3-final source, so the diffs should be a lot more intelligible.

asfimport commented 18 years ago

Otis Gospodnetic (@otisg) (migrated from JIRA)

It looks like this old issue was fixed at some point, as FSDirectory now has the following method:

public static void setDisableLocks(boolean doDisableLocks) { ...