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 16:40:35 GMT
There are benefits and problems to setting the serialVersionUID for
serializable classes.

I usually do it for all my serializable classes to make new versions of
classes compatble with old versions of serialzation. So if people are
serializing the RAMDirectory, and we change the method signatures or fields
in the RAMDirectory for the next release, their serialized versions of
RAMDirectory will not load anymore unless we take care in making them
compatable, and the first step to that is to set the serialVersionUID.

However its only recomended if we really know what we are doing. Any time
you change the class after setting the serialVersionUID, you HAVE to make
sure that it REALLY is compatable. For instance if you add any new fields to
the class and you intend to make that new class backwards compatable for
serialization, every method that uses the new field has to assume that its
possible for that new field to be null at the start of the method.

Serial version uid only refers to the specific class. Even if two different
classes have the same serial version uid, they are only compatable if they
have the same name and package (class Hello with serialVersionUID of 1 is
not compatable with class Goodbye with serialVersionUID of 1).

It is a risky thing to do, but without that new versions of the class simply
wont load the old versions.

Up to you guys to descide.
In either case I am up for making the RAMDirectory serializable

----- Original Message -----
From: "Scott Ganyo" <scott.ganyo@eTapestry.com>
To: "'Lucene Developers List'" <lucene-dev@jakarta.apache.org>
Sent: Thursday, May 02, 2002 11:27 AM
Subject: RE: Serializable RAMDirectory


Personally, I wouldn't recommend this.  The only thing it does is circumvent
the built-in Serialization protection mechanism.

(It's somewhat akin to immediately slashing the seatbelt of your new care
because someday that belt might keep you from being thrown free of your
fire-enveloped car. ;)

Scott

> -----Original Message-----
> From: Eugene Gluzberg [mailto:drag0n2@yahoo.com]
> Sent: Thursday, May 02, 2002 9:52 AM
> To: Lucene Developers List
> Subject: Re: Serializable RAMDirectory
>
>
> 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>
>



--
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