jimm.evote
Class VoteServer

java.lang.Object
  extended byjimm.evote.VoteServer

public class VoteServer
extends java.lang.Object

Stores Votes that come from Booths and runs a few hard-coded queries.


Field Summary
protected  VSDbEnv dbEnvironment
          Manages database housekeeping.
 
Constructor Summary
VoteServer(java.lang.String databaseDirectory)
          Opens the databases read-write.
VoteServer(java.lang.String databaseDirectory, boolean readOnly)
          Opens the databases.
 
Method Summary
 void acceptVotesAtomically(Booth booth)
          Accepts the votes from a voting booth.
 void emptyDatabases()
          Empties the databases.
 java.lang.String getSomeBoothState()
          Returns a state string.
static void main(java.lang.String[] args)
          usage: VoteServer /path/to/database/directory
 void printPresidentialResults()
          Prints the number of presidential race votes by party, then announces the winner.
 void printPresidentialVotes()
          Runs a query that prints the total number of presidential votes.
 void printSingleStateBoothList()
          Finds a state and prints the list of booths in that state.
 void printSingleStateBoothListUsingCursor(java.lang.String state, byte[] key)
          Prints the booths in state using a cursor.
 void printSingleStateBoothListUsingIterator(java.lang.String state, byte[] key)
          Prints the booths in state using a map and an iterator.
 void printSingleVote()
          Runs a query that uses a specific key to find a single record.
 void printTotalVotes()
          Print the number of votes in the vote database.
 void runQueries()
          Runs a few hard-coded queries and outputs the results.
 void shutDown()
          Gracefully shuts down the databases.
protected  void storeBooth(Transaction txn, Booth booth)
          Stores booth in the booth database.
protected  void storeVote(Transaction txn, Vote vote)
          Stores vote in the vote database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dbEnvironment

protected VSDbEnv dbEnvironment
Manages database housekeeping.

Constructor Detail

VoteServer

public VoteServer(java.lang.String databaseDirectory)
Opens the databases read-write.

Parameters:
databaseDirectory - path to the database directory

VoteServer

public VoteServer(java.lang.String databaseDirectory,
                  boolean readOnly)
Opens the databases. If we are going to write to the databases and they do not yet exist, they are created.

Parameters:
databaseDirectory - path to the database directory
readOnly - if true, databases will be opened read-only
Method Detail

shutDown

public void shutDown()
Gracefully shuts down the databases.


acceptVotesAtomically

public void acceptVotesAtomically(Booth booth)
Accepts the votes from a voting booth.

This method must be thread safe, not because the transactions require it but because ElectionDay uses multiple threads when it tells booths to send votes to the server. Being thread safe does not mean it has to be synchronized, nor do we have to worry about using dbEnvironment safely because the objects it holds (environment, database) are already thread safe.

Parameters:
booth - a voting booth ripe with votes

storeBooth

protected void storeBooth(Transaction txn,
                          Booth booth)
                   throws DatabaseException,
                          java.io.IOException
Stores booth in the booth database. A BoothBinding translates the booth into a format suitable for storage.

Parameters:
txn - a transaction; if null and the database is transactional then autocommit is used
booth -
Throws:
DatabaseException
java.io.IOException

storeVote

protected void storeVote(Transaction txn,
                         Vote vote)
                  throws DatabaseException,
                         java.io.IOException
Stores vote in the vote database.

Parameters:
txn - a transaction; if null and the database is transactional then autocommit is used
vote - a vote
Throws:
DatabaseException
java.io.IOException

runQueries

public void runQueries()
Runs a few hard-coded queries and outputs the results.


printTotalVotes

public void printTotalVotes()
Print the number of votes in the vote database.


printSingleVote

public void printSingleVote()
                     throws java.io.UnsupportedEncodingException,
                            DatabaseException,
                            java.io.IOException
Runs a query that uses a specific key to find a single record. Shows a simple query that uses a key from the primary database.

Throws:
java.io.UnsupportedEncodingException
DatabaseException
java.io.IOException

printSingleStateBoothList

public void printSingleStateBoothList()
                               throws java.io.UnsupportedEncodingException,
                                      DatabaseException,
                                      java.io.IOException
Finds a state and prints the list of booths in that state. The same query is run two times in two different ways: first with a cursor and then with a map and iterator.

Throws:
java.io.UnsupportedEncodingException
DatabaseException
java.io.IOException

printSingleStateBoothListUsingCursor

public void printSingleStateBoothListUsingCursor(java.lang.String state,
                                                 byte[] key)
Prints the booths in state using a cursor. Reads from the secondary database that has state as key.


printSingleStateBoothListUsingIterator

public void printSingleStateBoothListUsingIterator(java.lang.String state,
                                                   byte[] key)
Prints the booths in state using a map and an iterator. Notice how the iterator methods don't throw database exceptions.


getSomeBoothState

public java.lang.String getSomeBoothState()
                                   throws DatabaseException,
                                          java.io.IOException
Returns a state string. We look for a known booth and return its state. We can't just return a random state because we may not have generated voting booths for all states.

Throws:
DatabaseException
java.io.IOException

printPresidentialVotes

public void printPresidentialVotes()
                            throws java.io.IOException
Runs a query that prints the total number of presidential votes. Shows how to create a map and find all values that match a key.

Throws:
java.io.IOException

printPresidentialResults

public void printPresidentialResults()
                              throws java.io.UnsupportedEncodingException,
                                     java.io.IOException
Prints the number of presidential race votes by party, then announces the winner.

Throws:
java.io.UnsupportedEncodingException
java.io.IOException

emptyDatabases

public void emptyDatabases()
Empties the databases.


main

public static void main(java.lang.String[] args)
usage: VoteServer /path/to/database/directory



Copyright (c) 2004 by Jim Menard jimm@io.com