// HashMap Connector v. 1.0
// written by Eddie Hartman - be my guest: borrow, copy, swipe
//
// This Connector pre-reads the data references by the Iterator
// named in the connectorName variable into a java.util.HashMap
// and allows searches (lookup). Good for making files randomly
// accessible.
//
// Note that all Entries in the Iterator's result set are loaded into
// memory. Note also that this implementation does not support
// duplicates with the same key value.
//
var connectorName = "ReadCSV";    // Connector to use
var keyAttribute = "id";          // Attribute to provide search key for




var hsh = new java.util.HashMap();
var keyset = null;
var itr = null;                   // for the set Iterator
var fileConn = system.loadConnector("Connectors/" + connectorName);








selectEntries(); // Call selectEntries() for initial load of the HashMap

function selectEntries()
{
   var e; // local Entry variable.

   try { // try-catch used in case connector not initialized
      fileConn.terminate();
   } catch (ignoreException) {  } // Do nothing

   fileConn.initialize(null);
   fileConn.selectEntries();

   hsh.clear(); // Clear the Hastable.

   // Load entries from the Iterator into the HashMap
   while ((e = fileConn.getNextEntry()) != null) 
      hsh.put(e.getString(keyAttribute), e);

   // Set up an Iterator for Iterator Mode
   itr = hsh.keySet().iterator();
}

function getNextEntry ()
{
   if (!itr.hasNext()) {
      result.setStatus (0);
      return;
   }

   nxt = itr.next();

   entry.merge(hsh.get(nxt));
   result.setStatus (1); // Set status to "entry returned"
}

function findEntry ()
{
   var e; // entry
   var crit_vector = search.getCriteria();
   var crit = crit_vector.get(0);
   var cval = crit.value;

   var e = hsh.get(cval);

   if (e != null) {
      entry.merge(e);
      result.setStatus (1); // Set status to "entry(s) returned"
   } else 
      result.setStatus (0); // Status = "none found"
}

