util
Class PersistentMap

java.lang.Object
  |
  +--java.util.AbstractMap
        |
        +--util.PersistentMap
All Implemented Interfaces:
Map

public class PersistentMap
extends AbstractMap

A persistent hashtable (Map), which in this case maps to a SQL database.

This is based on AbstractMap, and for the most part is using AbstractMap's methods for doing lookups. This means that hash access is probably unbelievably slow, so using this for speed-critical code will probably not make you happy.

Developer's Note: This is probably more correct anyway, since for lookups we want to compare every item in the hashtable against the test object using the equals() method in order to be sure we've got the right thing.


Inner classes inherited from class java.util.Map
Map.Entry
 
Field Summary
static String Qkey
           
protected  String tableName
          the name of the current table
 
Constructor Summary
PersistentMap(AgentID aid, String tabname, Connection cnxn)
          This constructor works like the regular one except that it creates a table name specific to the agent instance.
PersistentMap(String tabname, Connection cnxn)
          Creates a new persistent map file.
 
Method Summary
 void destroy()
          Destroy the underlying table.
 Set entrySet()
          Get all map entries as a set.
protected  int execSQL(String s)
          Execute the given SQL updates
protected  int execSQL(String s, boolean loud)
          Execute the given SQL updates.
static void main(String[] args)
          Main method for testing
 Object put(Object k, Object v)
          Put a new value into the database.
protected  ResultSet querySQL(String s)
          execute queries with automatic reconnection.
protected  ResultSet querySQL(String s, boolean loud)
          execute queries with automatic reconnection.
 
Methods inherited from class java.util.AbstractMap
clear, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, putAll, remove, size, toString, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

tableName

protected String tableName
the name of the current table

Qkey

public static String Qkey
Constructor Detail

PersistentMap

public PersistentMap(String tabname,
                     Connection cnxn)
Creates a new persistent map file. Takes a designation and a database connection to create the table on the back-end. If the table already exists, the class will assume that it holds old data. If the existing table is of the wrong format, expect a large number of exceptions.
Parameters:
tabname - The name of the map. This is used to create a database table (any non-alphanumeric characters will be changed to underscores so that SQL doesn't choke on it).
cnxn - The database connection url. See the java.sql Connection class for more information.
See Also:
Connection

PersistentMap

public PersistentMap(AgentID aid,
                     String tabname,
                     Connection cnxn)
This constructor works like the regular one except that it creates a table name specific to the agent instance. Takes into account all parts of the agentID: society, occupation and designation.
Parameters:
aid - the agent requesting a table for itself
tabname - The name of the map. This is used to create a database table (any non-alphanumeric characters will be changed to underscores so that SQL doesn't choke on it).
cnxn - The database connection url. See the java.sql Connection class for more information.
See Also:
Connection
Method Detail

entrySet

public Set entrySet()
Get all map entries as a set. Users of this class can use the set's iterator to get all the entry information, or can use AbstractMap's access methods to make their lives easier.
Overrides:
entrySet in class AbstractMap
Returns:
the set of entries

put

public Object put(Object k,
                  Object v)
Put a new value into the database. As of this version, this doesn't look up the old value, and just returns null, so applications that are dependent on the old value should probably do a get() beforehand.
Overrides:
put in class AbstractMap
Parameters:
k - the new key object
v - the new value object
Returns:
always null as of this version

destroy

public void destroy()
Destroy the underlying table. This method should probably be rarely (if ever) called, since it gets rid of the very thing that makes this persistent data. If any other methods are called after this one, they will probably spew exceptions to the standard error stream.

execSQL

protected int execSQL(String s)
Execute the given SQL updates
Parameters:
s - a string containing SQL commands
Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing

execSQL

protected int execSQL(String s,
                      boolean loud)
Execute the given SQL updates. If loud is true, then errors will be printed in the command window. If loud is false, errors will still be printed if db_verbose is true.
Parameters:
s - a string containing SQL commands
loud - enable|disable error reporting (disabling only works if db_verbose is also false)
Returns:
either the row count for INSERT, UPDATE or DELETE statements, or 0 for SQL statements that return nothing

querySQL

protected ResultSet querySQL(String s)
execute queries with automatic reconnection.
Parameters:
s - the SQL command string
Returns:
the resulting data from the query

querySQL

protected ResultSet querySQL(String s,
                             boolean loud)
execute queries with automatic reconnection. If loud is true, then errors will be printed in the command window. If loud is false, errors will still be printed if db_verbose is true.
Parameters:
s - a string containing SQL commands
loud - enable|disable error reporting (disabling only works if db_verbose is also false)
Returns:
the results of the query

main

public static void main(String[] args)
Main method for testing
Parameters:
args - [driver]