RAFo C/S

RandomAccessFile for Objects

com.linxpda.raf
Class RAFo

java.lang.Object
  |
  +--com.linxpda.raf.RAFo

public class RAFo
extends java.lang.Object

This class reads and writes Java™ Objects to a file, using String keys to reference objects stored. It maintains an in-memory index of file keys for faster reading and writing operations.

A typical RAF/o session would go:

 import com.linxpda.raf.*;
	import java.util.Date;
	
	public class FileTest {
		
		public FileTest(String filename) {
			try {
				RAFo file = new RAFo(filename, 64);
				Date data = new Date();
				file.insertObject("created", date);
				file.insertObject("created-by", "Tom Cole");
				//update a record
				file.updateObject("created-by", "Sid Thomas");
				//delete a record
				file.removeObject("created-by");
				Date date = (Date) file.getObject("created");
				System.out.println("File created: " + date.toGMTString());
				file.close();
			}
			catch(IOException ioe) {
				ioe.printStackTrace();
			}
			catch(RAFException dbe) {
				dbe.printStackTrace();
			}
			catch(ClassNotFoundException cnfe) {
				cbfe.printStackTrace();
			}
			finally {
				System.exit(0);
			}
		}
		
		public static void main(String args[]) {
			if (args.length != 1) {
				System.out.println("USEAGE: java FileTest ");
				System.exit(0);
			}
			else {

				new FileTest(args[0]);
			}
		}
	}	
To connect as a client to a remote database server, simply replace the RAFo constructor with a call to the static method getRemoteInstance. All other method calls against the remote database are exactly the same as local calls.


Constructor Summary
RAFo(java.lang.String filename)
          Default operation that attempts to open an existing RAFo file with the given filename.
RAFo(java.lang.String filename, int initialCapacity)
          Creates a new RAFo file with the given filename and initial header capacity.
RAFo(java.lang.String filename, java.lang.String access)
          Opens an existing RAFo file with the given filename and using the provided access restrictions.
 
Method Summary
 void close()
          Closes the data file accessed by this instance of RAFo.
 boolean containsKey(java.lang.String key)
          Tests whether the data file accessed by this instance of RAFo contains the given key.
 java.util.Enumeration getKeys()
          Returns a java.util.Enumeration of all the keys contained in the data file.
 java.lang.Object getObject(java.lang.String key)
          Searches the data file for the given key and returns the object associated with it.
 int getObjectCount()
          Returns the number of objects currently stored in the data file.
static RAFo getRemoteInstance(java.lang.String filename, java.lang.String url)
          Returns an instance of RAFo that is connected to a remote RAFoRMIServer for thread-safe client/user access.
 void insertObject(java.lang.String key, java.lang.Object value)
          Stores the supplied object value in the data file and associates it with the supplied key.
 void removeObject(java.lang.String key)
          Removes the object with the associated key from the data file and removes the key from the header.
 void updateObject(java.lang.String key, java.lang.Object value)
          Replaces the object currently associated with the given key with the new object value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RAFo

public RAFo(java.lang.String filename)
     throws java.io.IOException,
            RAFException
Default operation that attempts to open an existing RAFo file with the given filename. If the file does not exist, then a new one is created.

This constructor provided as a convenience method.

Parameters:
filename - The filename of the local data file from which to retrieve and store objects.
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the file cannot be opened or created.

RAFo

public RAFo(java.lang.String filename,
            java.lang.String access)
     throws java.io.IOException,
            RAFException
Opens an existing RAFo file with the given filename and using the provided access restrictions.
Parameters:
filename - The filename of the file to open.
access - The access string denoting file access privileges. Cann be "r" for read only or "rw" for read-write.
Throws:
java.io.IOException - upon a system level access violation.
RAFException - if the requested file does not exist.
See Also:
RAFo(String)

RAFo

public RAFo(java.lang.String filename,
            int initialCapacity)
     throws java.io.IOException,
            RAFException
Creates a new RAFo file with the given filename and initial header capacity.
Parameters:
filename - The filename for the new data file.
initialCapacity - The requested initial capacity for the header file.
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the requested file already exists.
See Also:
RAFo(String)
Method Detail

getRemoteInstance

public static RAFo getRemoteInstance(java.lang.String filename,
                                     java.lang.String url)
                              throws java.rmi.RemoteException,
                                     java.io.IOException,
                                     RAFException,
                                     java.rmi.NotBoundException
Returns an instance of RAFo that is connected to a remote RAFoRMIServer for thread-safe client/user access.
Parameters:
filename - The filename to create or open on the remote filesystem.
url - The URL encoded location for the RAFoRMIServer. Must be in the form '//IPAddress:port/remote_name' as defined in the java.rmi bootstrap whitepapers.
Throws:
java.rmi.RemoteException - If there is a connection error between the remote server and this class.
java.rmi.NotBoundException - If the server has not been bound or cannot be found at the specified URL.
RAFException - If there is an access violation creating or opening the requested database file.
java.io.IOException - Upon a system level access violation.

close

public void close()
           throws java.io.IOException,
                  RAFException
Closes the data file accessed by this instance of RAFo.
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the existing file is already closed.

containsKey

public boolean containsKey(java.lang.String key)
                    throws RAFException
Tests whether the data file accessed by this instance of RAFo contains the given key.
Parameters:
key - The key to search for
Returns:
true if the file contains the given key
Throws:
RAFException - if the file is not accessible (closed)

getKeys

public java.util.Enumeration getKeys()
                              throws RAFException
Returns a java.util.Enumeration of all the keys contained in the data file.
Returns:
An Enumeration of all the keys available.
Throws:
RAFException - if the file is not accessible.

getObject

public java.lang.Object getObject(java.lang.String key)
                           throws java.io.IOException,
                                  RAFException,
                                  java.lang.ClassNotFoundException
Searches the data file for the given key and returns the object associated with it.
Parameters:
key - The key that is associated with the desired object
Returns:
The object associated with the given key or null if the key does not exist.
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the file is not accessible.

getObjectCount

public int getObjectCount()
                   throws RAFException
Returns the number of objects currently stored in the data file.
Returns:
An int specifying the number of obejcts currently stored in the data file.
Throws:
RAFException - if the data file is not accessible.

insertObject

public void insertObject(java.lang.String key,
                         java.lang.Object value)
                  throws java.io.IOException,
                         RAFException
Stores the supplied object value in the data file and associates it with the supplied key.
Parameters:
key - The key to associate with the given value
value - The object to store
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the key already exists or if the file is not accessible.

removeObject

public void removeObject(java.lang.String key)
                  throws java.io.IOException,
                         RAFException
Removes the object with the associated key from the data file and removes the key from the header.
Parameters:
key - The key for the object to remove
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the supplied key does not exist in the header or the file is not accessible.

updateObject

public void updateObject(java.lang.String key,
                         java.lang.Object value)
                  throws java.io.IOException,
                         RAFException
Replaces the object currently associated with the given key with the new object value.
Parameters:
key - The key associated with the object to be replaced
value - The new object to store with the key
Throws:
java.io.IOException - upon a system level access violation
RAFException - if the header does not contain the give key or the file is not accessible.

(c)2001 Linxpda, Inc. www.linxpda.com