lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Eugene Gluzberg" <drag...@yahoo.com>
Subject Re: Serializable RAMDirectory
Date Thu, 02 May 2002 14:52:19 GMT
Also add
 static final long serialVersionUID = 1;

if you going to make any class serializable.

----- Original Message -----
From: "Otis Gospodnetic" <otis_gospodnetic@yahoo.com>
To: <lucene-dev@jakarta.apache.org>
Sent: Thursday, May 02, 2002 9:03 AM
Subject: Serializable RAMDirectory


> Hello,
>
> Is there a reason why we can't make RAMDirectory Serializable?
> It's only a markup interface anyway, and some folks are obviously
> serializing their RAMDirectories.  Below is an example of such a
> RAMDirectory and, as you can see, it is nearly identical to
> RAMDirectory -
> identical except for implementing Serializable.  Furthermore,
> RAMDirectory
> could not be inherited because it is final, so all code has to be
> duplicated.
>
> Any reasons against making RAMDirectory implement Serializable?
>
> Thanks,
> Otis
>
> --- Karl Øie <karl@gan.no> wrote:
> > that is a great problem with lucene as it uses a FSDir to store it
> > has no
> > sence of transaction handling, for critical indexes i serialize a
> > RAMdir to a
> > database blob, so i can performe a rollback if needed, but this is a
> > enourmos
> > overhead....
>
>
>
>
> > import java.io.Serializable;
> > import java.io.IOException;
> > import java.util.Vector;
> > import java.util.Hashtable;
> > import java.util.Enumeration;
> >
> > import org.apache.lucene.store.Directory;
> > import org.apache.lucene.store.InputStream;
> > import org.apache.lucene.store.OutputStream;
> >
> >
>
//--------------------------------------------------------------------------
------------------------------------
> >
> > final public class SerializableRAMDirectory extends Directory
> > implements Serializable {
> >
> > Hashtable files = new Hashtable();
> >
> > public SerializableRAMDirectory() {
> > }
> >
> > /** Returns an array of strings, one for each file in the directory.
> > */
> > public final String[] list() {
> > String[] result = new String[files.size()];
> > int i = 0;
> > Enumeration names = files.keys();
> > while (names.hasMoreElements()) {
> > result[i++] = (String)names.nextElement();
> > }
> > return result;
> > }
> >
> > /** Returns true iff the named file exists in this directory. */
> > public final boolean fileExists(String name) {
> > SerializableRAMFile file = (SerializableRAMFile)files.get(name);
> > return file != null;
> > }
> >
> > /** Returns the time the named file was last modified. */
> > public final long fileModified(String name) throws IOException {
> > SerializableRAMFile file = (SerializableRAMFile)files.get(name);
> > return file.lastModified;
> > }
> >
> > /** Returns the length in bytes of a file in the directory. */
> > public final long fileLength(String name) {
> > SerializableRAMFile file = (SerializableRAMFile)files.get(name);
> > return file.length;
> > }
> >
> > /** Removes an existing file in the directory. */
> > public final void deleteFile(String name) {
> > files.remove(name);
> > }
> >
> > /** Removes an existing file in the directory. */
> > public final void renameFile(String from, String to) {
> > SerializableRAMFile file = (SerializableRAMFile)files.get(from);
> > files.remove(from);
> > files.put(to, file);
> > }
> >
> > /** Creates a new, empty file in the directory with the given name.
> > Returns a stream writing this file. */
> > public final OutputStream createFile(String name) {
> > SerializableRAMFile file = new SerializableRAMFile();
> > files.put(name, file);
> > return new SerializableRAMOutputStream(file);
> > }
> >
> > /** Returns a stream reading an existing file. */
> > public final InputStream openFile(String name) {
> > SerializableRAMFile file = (SerializableRAMFile)files.get(name);
> > return new SerializableRAMInputStream(file);
> > }
> >
> > /** Construct a {@link Lock}.
> > * @param name the name of the lock file
> > */
> > public final Lock makeLock(final String name) {
> > return new Lock() {
> > //--
> > public boolean obtain() throws IOException {
> > synchronized (files) {
> > if (!fileExists(name)) {
> > createFile(name).close();
> > return true;
> > }
> > return false;
> > }
> > }
> > public void release() {
> > deleteFile(name);
> > }
> > };
> >    //--
> > }
> >
> > /** Closes the store to future operations. */
> > public final void close() {
> > Enumeration fe = files.elements();
> > while (fe.hasMoreElements()) {
> > ((SerializableRAMFile)fe.nextElement()).buffers.trimToSize();
> > }
> > }
> > }
> >
> >
>
//--------------------------------------------------------------------------
------------------------------------
> >
> > final class SerializableRAMInputStream extends InputStream implements
> > Cloneable {
> > SerializableRAMFile file;
> > int pointer = 0;
> >
> > public SerializableRAMInputStream(SerializableRAMFile f) {
> > file = f;
> > length = file.length;
> > }
> >
> > /** InputStream methods */
> > public final void readInternal(byte[] dest, int destOffset, int len)
> > {
> > int remainder = len;
> > int start = pointer;
> > while (remainder != 0) {
> > int bufferNumber = start/InputStream.BUFFER_SIZE;
> > int bufferOffset = start%InputStream.BUFFER_SIZE;
> > int bytesInBuffer = InputStream.BUFFER_SIZE - bufferOffset;
> > int bytesToCopy = bytesInBuffer >= remainder ? remainder :
> > bytesInBuffer;
> > byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
> > System.arraycopy(buffer, bufferOffset, dest, destOffset,
> > bytesToCopy);
> > destOffset += bytesToCopy;
> > start += bytesToCopy;
> > remainder -= bytesToCopy;
> > }
> > pointer += len;
> > }
> >
> > public final void close() {
> > }
> >
> > /** Random-access methods */
> > public final void seekInternal(long pos) {
> > pointer = (int)pos;
> > }
> > }
> >
> >
>
//--------------------------------------------------------------------------
------------------------------------
> >
> > final class SerializableRAMOutputStream extends OutputStream {
> > SerializableRAMFile file;
> > int pointer = 0;
> >
> > public SerializableRAMOutputStream(SerializableRAMFile f) {
> > file = f;
> > }
> >
> > /** output methods: */
> > public final void flushBuffer(byte[] src, int len) {
> > int bufferNumber = pointer/OutputStream.BUFFER_SIZE;
> > int bufferOffset = pointer%OutputStream.BUFFER_SIZE;
> > int bytesInBuffer = OutputStream.BUFFER_SIZE - bufferOffset;
> > int bytesToCopy = bytesInBuffer >= len ? len : bytesInBuffer;
> >
> > if (bufferNumber == file.buffers.size())
> > file.buffers.addElement(new byte[OutputStream.BUFFER_SIZE]);
> >
> > byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
> > System.arraycopy(src, 0, buffer, bufferOffset, bytesToCopy);
> >
> > if (bytesToCopy < len) {   // not all in one buffer
> > int srcOffset = bytesToCopy;
> > bytesToCopy = len - bytesToCopy;   // remaining bytes
> > bufferNumber++;
> > if (bufferNumber == file.buffers.size())
> > file.buffers.addElement(new byte[OutputStream.BUFFER_SIZE]);
> > buffer = (byte[])file.buffers.elementAt(bufferNumber);
> > System.arraycopy(src, srcOffset, buffer, 0, bytesToCopy);
> > }
> > pointer += len;
> > if (pointer > file.length) {
> > file.length = pointer;
> > }
> > file.lastModified = System.currentTimeMillis();
> > }
> >
> > public final void close() throws IOException {
> > super.close();
> > }
> >
> > /** Random-access methods */
> > public final void seek(long pos) throws IOException {
> > super.seek(pos);
> > pointer = (int)pos;
> > }
> >
> > public final long length() throws IOException {
> > return file.length;
> > }
> >
> > }
> >
> >
>
//--------------------------------------------------------------------------
------------------------------------
> >
> > final class SerializableRAMFile implements Serializable {
> > Vector buffers = new Vector();
> > long length;
> > long lastModified = System.currentTimeMillis();
> > }
> >
> >
>
//--------------------------------------------------------------------------
------------------------------------
> >
> > > --
> > To unsubscribe, e-mail:
> > <mailto:lucene-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> <mailto:lucene-user-help@jakarta.apache.org>
>
>
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Health - your guide to health and wellness
> http://health.yahoo.com
>
> --
> To unsubscribe, e-mail:
<mailto:lucene-dev-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
<mailto:lucene-dev-help@jakarta.apache.org>


--
To unsubscribe, e-mail:   <mailto:lucene-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:lucene-dev-help@jakarta.apache.org>


Mime
View raw message