Class JSort

java.lang.Object
  |
  +--java.rmi.server.RemoteObject
        |
        +--java.rmi.server.RemoteServer
              |
              +--java.rmi.server.UnicastRemoteObject
                    |
                    +--JSort

public class JSort
extends java.rmi.server.UnicastRemoteObject
implements JSortInterface, com.sun.jini.lookup.ServiceIDListener

JSort (Jini Sort)

This is a Jini service that performs a virtual sort of fixed length strings based on sort fields within the string. This is a familiar service to those who have written batch programs.

Use the r1.bat, r2.bat, and r3.bat files in JiniExample1 to start the Jini environment before running JSort or JClient.

Error Return Codes

    0 -- Sorted with no errors.
    1 -- Parameter error.
    3 -- Available memory work area is too small.
    4 -- Available memory too small for rec length.
    5 -- Memory allocation error.
    6 -- File creation error.
    8 -- Illegal item length.
    9 -- More than 32K records to sort.
   10 -- Write error during sorting (disk full?)
   11 -- Read error during sorting.
   12 -- Impossible to create new file (dir full?)
 

ToDo

  1. Fix the error reporting.
  2. Provide method to send records as an array of bytes rather than having to release them record by record.
  3. Provide method to fetch sorted file as an array of bytes rather than having to return it record by record.

Development Environment

Compiled under JDK 1.2.0, Jini 1.0.0

History

03/10/99 NDE Created the program.

See Also:
Serialized Form

Field Summary
(package private) static double HALFSEG
          Maximum value of 2 bytes signed.
(package private)  java.util.Hashtable htSortJobs
          key = "sortjob3", v = JSortJob object.
(package private) static double LONGLGTH
          Maximum value of a signed int.
(package private) static int MAXRPAGES
          The maximum number of real pages.
(package private) static int MAXSTACK
          Used in quickSort() for a local stack.
(package private) static int SECTSIZE
          Sector size for block reads and writes.
(package private) static double SEGLGTH
          Maximum value of 2 bytes unsigned.
(package private) static double USERSTACK
          Heap reserved for user stack.
 
Fields inherited from class java.rmi.server.UnicastRemoteObject
csf, port, portFactoryParamTypes, portParamTypes, serialVersionUID, ssf
 
Fields inherited from class java.rmi.server.RemoteServer
log, logname, serialVersionUID
 
Fields inherited from class java.rmi.server.RemoteObject
ref, serialVersionUID
 
Constructor Summary
JSort()
           
 
Method Summary
private  boolean adjustForFieldOrder(int iSortOrderIn, boolean bIsLessIn)
          If the sort order is ascending, this method will return the same boolean that was passed to it, otherwise it returns the opposite.
private  void allocateBuffers(JSortJob jobIn, java.util.Hashtable htParmsIn)
           
 java.util.Hashtable close(java.util.Hashtable htParmsIn)
           
private  void exchange(JSortJob jobIn, long i, long j, byte[] swoppost)
          This is called by quickSort() to exchange two sort item records.
private  java.lang.String getNextJobName()
           
private  void initPage(byte[] abIn)
           
private  boolean less(JSortJob jobIn, byte[] abIn, JSortItem itemIn)
          This compares two byte array items according to the sort fields for the job.
private  boolean less(JSortJob jobIn, JSortItem itemIn, byte[] abIn)
          This compares two byte array items according to the sort fields for the job.
static void main(java.lang.String[] args)
           
 java.util.Hashtable open(java.util.Hashtable htParmsIn)
           
private  void pageIn(JSortJob jobIn, long item_num, long keep_rec1, long keep_rec2)
          After calling this function the page that contains the requested item_num is in memory.
private  void pageOut(JSortJob jobIn, byte[] rpage_ptr, int vpage_num)
          Write the virtual page in the passed real memory location to the file.
private  void quickSort(JSortJob jobIn, byte[] swoppost, byte[] savez)
          This is a non-recursive version of the quicksort algorithm as given in Nicklaus Wirth ALGORITHMS + DATA STRUCTURES = PROGRAMS.
private  void register(java.lang.String sServiceNameIn, com.sun.jini.lookup.ServiceIDListener sidServiceIn)
           
 void releaseBlock(java.lang.String sJobNameIn, byte[] abIn)
          This processes a block of sort items, one item at a time.
 java.util.Hashtable releaseRecord(java.util.Hashtable htParmsIn)
          This function copies a record from the passed parameter hashtable into the virtual sort system.
 byte[] returnBlock(java.lang.String sJobNameIn, int iNumReqItemsIn)
          This accepts the number of requested sort items and returns a byte array that consists of the requested number of sort items.
 java.util.Hashtable returnRecord(java.util.Hashtable htParmsIn)
          This finds the current sort output item and returns it to the caller.
 java.util.Hashtable rewind(java.util.Hashtable htParmsIn)
          This resets the current item counter to 0 which allows returnRec to start over at the beginning of the sort work file.
 void serviceIDNotify(net.jini.core.lookup.ServiceID sidIn)
           
 java.util.Hashtable sort(java.util.Hashtable htParmsIn)
           
private  void test()
           
private  void writeDebug(JSortJob jobIn, java.lang.String sIn)
           
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
, clone, exportObject, exportObject, exportObject, exportObject, readObject, reexport, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub, writeObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

MAXRPAGES

static final int MAXRPAGES
The maximum number of real pages.

MAXSTACK

static final int MAXSTACK
Used in quickSort() for a local stack. Able to sort Log2(20) = 1 million records.

SECTSIZE

static final int SECTSIZE
Sector size for block reads and writes.

USERSTACK

static final double USERSTACK
Heap reserved for user stack.

LONGLGTH

static final double LONGLGTH
Maximum value of a signed int.

SEGLGTH

static final double SEGLGTH
Maximum value of 2 bytes unsigned.

HALFSEG

static final double HALFSEG
Maximum value of 2 bytes signed.

htSortJobs

java.util.Hashtable htSortJobs
key = "sortjob3", v = JSortJob object.
Constructor Detail

JSort

public JSort()
      throws java.rmi.RemoteException
Method Detail

main

public static void main(java.lang.String[] args)

adjustForFieldOrder

private boolean adjustForFieldOrder(int iSortOrderIn,
                                    boolean bIsLessIn)
If the sort order is ascending, this method will return the same boolean that was passed to it, otherwise it returns the opposite.

allocateBuffers

private void allocateBuffers(JSortJob jobIn,
                             java.util.Hashtable htParmsIn)

close

public java.util.Hashtable close(java.util.Hashtable htParmsIn)
                          throws java.rmi.RemoteException
Specified by:
close in interface JSortInterface

exchange

private void exchange(JSortJob jobIn,
                      long i,
                      long j,
                      byte[] swoppost)
This is called by quickSort() to exchange two sort item records.
Parameters:
i - - first item number.
j - - second item number.
swoppost - - storage within which an item can be saved during swop.

getNextJobName

private java.lang.String getNextJobName()

initPage

private void initPage(byte[] abIn)

less

private boolean less(JSortJob jobIn,
                     JSortItem itemIn,
                     byte[] abIn)
This compares two byte array items according to the sort fields for the job. It the first item is < the second item, then this returns true.
Returns:
true if the first byte array item is < the second one.

less

private boolean less(JSortJob jobIn,
                     byte[] abIn,
                     JSortItem itemIn)
This compares two byte array items according to the sort fields for the job. It the first item is < the second item, then this returns true.
Returns:
true if the first byte array item is < the second one.

open

public java.util.Hashtable open(java.util.Hashtable htParmsIn)
                         throws java.rmi.RemoteException
Specified by:
open in interface JSortInterface

pageIn

private void pageIn(JSortJob jobIn,
                    long item_num,
                    long keep_rec1,
                    long keep_rec2)
After calling this function the page that contains the requested item_num is in memory. If item numbers keep_rec1 or keep_rec2 are in real memory, they will be kept in memory and not swapped to disk.
Parameters:
jobIn - - the sort job object.
item_num - - item number (rec number) whose page is sought.
keep_rec1 - - 1st item number to not swap out of memory.
keep_rec2 - - 2nd item number to not swap out of memory.

pageOut

private void pageOut(JSortJob jobIn,
                     byte[] rpage_ptr,
                     int vpage_num)
Write the virtual page in the passed real memory location to the file. Notice that the records in the file are pages full of fixed length sort items. The record numbers are the virtual page numbers and begin with page 0.
Parameters:
jobIn - - The sort job object.
rpage_ptr - - the real page to store.
vpage_num - - number of the virtual page.

quickSort

private void quickSort(JSortJob jobIn,
                       byte[] swoppost,
                       byte[] savez)
This is a non-recursive version of the quicksort algorithm as given in Nicklaus Wirth ALGORITHMS + DATA STRUCTURES = PROGRAMS.
Parameters:
jobIn - - job object that contains the real and virtual page arrays.
swoppost - - save area during the sort exchange.
savez - - save area for the middle record during the sort.

register

private void register(java.lang.String sServiceNameIn,
                      com.sun.jini.lookup.ServiceIDListener sidServiceIn)

releaseBlock

public void releaseBlock(java.lang.String sJobNameIn,
                         byte[] abIn)
                  throws java.rmi.RemoteException
This processes a block of sort items, one item at a time. This improves input performance by optimizing network transfer.
Specified by:
releaseBlock in interface JSortInterface

releaseRecord

public java.util.Hashtable releaseRecord(java.util.Hashtable htParmsIn)
                                  throws java.rmi.RemoteException
This function copies a record from the passed parameter hashtable into the virtual sort system. If real memory becomes full, this creates the sort work file.
Specified by:
releaseRecord in interface JSortInterface

returnBlock

public byte[] returnBlock(java.lang.String sJobNameIn,
                          int iNumReqItemsIn)
                   throws java.rmi.RemoteException
This accepts the number of requested sort items and returns a byte array that consists of the requested number of sort items. if the number of items that is returned is less than the requested number, the end of the sortwork file has been reached.
Specified by:
returnBlock in interface JSortInterface

returnRecord

public java.util.Hashtable returnRecord(java.util.Hashtable htParmsIn)
                                 throws java.rmi.RemoteException
This finds the current sort output item and returns it to the caller. When the last record is fetched, the job's current item is set to -1. Rewind sets it to 0.
Specified by:
returnRecord in interface JSortInterface

rewind

public java.util.Hashtable rewind(java.util.Hashtable htParmsIn)
                           throws java.rmi.RemoteException
This resets the current item counter to 0 which allows returnRec to start over at the beginning of the sort work file.
Specified by:
rewind in interface JSortInterface

serviceIDNotify

public void serviceIDNotify(net.jini.core.lookup.ServiceID sidIn)
Specified by:
serviceIDNotify in interface com.sun.jini.lookup.ServiceIDListener

sort

public java.util.Hashtable sort(java.util.Hashtable htParmsIn)
                         throws java.rmi.RemoteException
Specified by:
sort in interface JSortInterface

test

private void test()

writeDebug

private void writeDebug(JSortJob jobIn,
                        java.lang.String sIn)