ch.javasoft.jbase.concurrent
Class MultiplexedAppendTable<E>

java.lang.Object
  extended by ch.javasoft.jbase.concurrent.MultiplexedAppendTable<E>
All Implemented Interfaces:
Table<E>

public class MultiplexedAppendTable<E>
extends Object
implements Table<E>

The MultiplexedAppendTable uses a queue with limited or unlimited capacity to cache appended entities. A separate thread copies the added entities from the queue to the underlying base table. Multiple threads can concurrently add entities to this table.

Note that all method except for add(Object), flush() and close(boolean) throw an UnsupportedOperationException.


Constructor Summary
MultiplexedAppendTable(Table<E> baseTable)
          Constructor with cache capacity of Integer.MAX_VALUE.
MultiplexedAppendTable(Table<E> baseTable, int cacheSize)
          Constructor with specified cache capacity
 
Method Summary
 int add(E entity)
          Adds an entry, initially to the cache.
 void close(boolean erase)
          Closes the table.
 void flush()
          Flushes the table.
 E get(int index)
          Always throws an UnsupportedOperationException
 void remove(int index)
          Always throws an UnsupportedOperationException
 void removeAll()
          Always throws an UnsupportedOperationException
 void set(int index, E entity)
          Always throws an UnsupportedOperationException
 int size()
          Always throws an UnsupportedOperationException
 void swap(int indexA, int indexB)
          Always throws an UnsupportedOperationException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultiplexedAppendTable

public MultiplexedAppendTable(Table<E> baseTable)
Constructor with cache capacity of Integer.MAX_VALUE.

Parameters:
baseTable - the base table to which the entries are written from the cache

MultiplexedAppendTable

public MultiplexedAppendTable(Table<E> baseTable,
                              int cacheSize)
Constructor with specified cache capacity

Parameters:
baseTable - the base table to which the entries are written from the cache
Method Detail

add

public int add(E entity)
        throws IOException
Adds an entry, initially to the cache. This method might block if the cache size is limited until the copy thread takes some entries from the cache and writes them to the underlying base table.

Note that this method always returns -1, since we do not know where the new entry has been put. If the table is already closed, an exception is thrown.

Specified by:
add in interface Table<E>
Parameters:
entity - the entity to add
Returns:
always -1 since the final position of the entry is not known
Throws:
IOException - if an i/o exception occurs, or if the table has already been closed

flush

public void flush()
           throws IOException
Flushes the table. Blocks until at least one flush call has been made to the underlying table. Note, however, that other threads might still be adding entries concurrently, causing the caller to wait for an undefined time.

This method guarantees that the queue was empty once between the time this method was called, and the call returns. This also guarantees that entries which have been added by the calling thread are flushed to the underlying base table. However, it is important to know that if concurrent threads are constantly adding entries, this method might not return and leave the caller in a blocked state.

Specified by:
flush in interface Table<E>
Throws:
IOException

close

public void close(boolean erase)
           throws IOException
Closes the table. Subsequent add calls will cause an exception. The method waits for the copying thread, that is, until all cached entries are flushed to the underlying base table. The call returns when the cached entries have been written to the base table, and the base table is has also been closed.

Specified by:
close in interface Table<E>
Throws:
IOException

get

public E get(int index)
      throws IOException
Always throws an UnsupportedOperationException

Specified by:
get in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException

removeAll

public void removeAll()
               throws IOException
Always throws an UnsupportedOperationException

Specified by:
removeAll in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException

remove

public void remove(int index)
            throws IOException
Always throws an UnsupportedOperationException

Specified by:
remove in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException

set

public void set(int index,
                E entity)
         throws IOException
Always throws an UnsupportedOperationException

Specified by:
set in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException

swap

public void swap(int indexA,
                 int indexB)
          throws IOException
Always throws an UnsupportedOperationException

Specified by:
swap in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException

size

public int size()
         throws IOException
Always throws an UnsupportedOperationException

Specified by:
size in interface Table<E>
Throws:
UnsupportedOperationException - always
IOException