package votorola.a.trust; // Copyright 2010, 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import java.sql.*; import java.util.*; import votorola.a.*; import votorola.a.voter.*; import votorola.g.*; import votorola.g.lang.*; import votorola.g.sql.*; /** A registrant's membership in a division. * * @see zelea.com/w/Property:Division */ public final class Membership { /** Creates a Membership with default values. */ Membership( IDPair _registrant, String _division ) { if( _registrant == null || _division == null ) throw new NullPointerException(); // fail fast registrant = _registrant; division = _division; isChanged = true; } // ------------------------------------------------------------------------------------ /** The registrant. */ public IDPair registrant() { return registrant; } private final IDPair registrant; /** A division in which the registrant is a member. */ public String division() { return division; } private final String division; /** Writes this membership to the table if it has unwritten changes. */ void write( final Table table ) throws SQLException { if( !isChanged ) return; table.put( Membership.this ); isChanged = false; } // - O b j e c t ---------------------------------------------------------------------- /** Constructs a description of the membership. */ public @Override String toString() { return "Membership[" + registrant() + "," + division() + "]"; } // ==================================================================================== /** The relational store of memberships that (in part) backs a compiled network trace. * * @see votorola.a.trust.TraceNodeW.Table */ public static @ThreadSafe final class Table { /** Constructs a Table. */ public Table( final ReadyDirectory readyDirectory, final Database database ) throws IOException, SQLException { this.readyDirectory = readyDirectory; this.database = database; synchronized( database ) { database.ensureSchema( SCHEMA_NAME ); } final String snapSuffix = OutputStore.suffix( readyDirectory.snapDirectory().getName() ); if( !OutputStore.isY4MDS( snapSuffix )) throw new VotorolaRuntimeException( "improperly suffixed snap directory parent of ready directory: " + readyDirectory ); tableName = snapSuffix.substring(1) + OutputStore.SUFFIX_DELIMITER + "membership" + OutputStore.suffix(readyDirectory.getCanonicalFile().getName()); statementKeyBase = getClass().getName() + ":" + SCHEMA_NAME + "/" + tableName + "."; } private final String statementKeyBase; private final String tableName; // -------------------------------------------------------------------------------- /** Creates this table in the database. */ void create() throws SQLException { final String sKey = statementKeyBase + "create"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "CREATE TABLE \"" + SCHEMA_NAME + "\".\"" + tableName + "\"" + " (registrantEmail character varying," + " division character varying," + " PRIMARY KEY (registrantEmail, division))" ); // Changing table structure? Then also increment NetworkTrace.serialVersionUID. database.statementCache().put( sKey, s ); } s.execute(); } } /** The database in which this table is stored. */ @Warning("thread restricted object") Database database() { return database; } private final Database database; /** Retrieves the set of all divisions for the specified registrant. * * @return a newly constructed set. */ public HashSet divisionSet( final String registrantEmail ) throws SQLException { final String sKey = statementKeyBase + "divisionSet"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "SELECT division" + " FROM \"" + SCHEMA_NAME + "\".\"" + tableName + "\"" + " WHERE registrantEmail = ?" ); database.statementCache().put( sKey, s ); } s.setString( 1, registrantEmail ); final ResultSet r = s.executeQuery(); try { final HashSet set = new HashSet(); while( r.next() ) set.add( r.getString( 1 )); return set; } finally{ r.close(); } } } /** Drops this table from the database if it exists. * * @return true if any rows were actually removed as a result, false otherwise. */ final boolean drop() throws SQLException { final String sKey = statementKeyBase + "drop"; synchronized( database ) { PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "DROP TABLE IF EXISTS \"" + SCHEMA_NAME + "\".\"" + tableName + "\"" ); database.statementCache().put( sKey, s ); } final int updatedRows = s.executeUpdate(); return updatedRows > 0; } } /** Stores a membership in this table. */ void put( final Membership membership ) throws SQLException { synchronized( database ) { final String sKey = statementKeyBase + "put"; PreparedStatement s = database.statementCache().get( sKey ); if( s == null ) { s = database.connection().prepareStatement( "INSERT INTO \"" + SCHEMA_NAME + "\".\"" + tableName + "\"" + " (registrantEmail, division) VALUES (?, ?)" ); database.statementCache().put( sKey, s ); } s.setString( 1, membership.registrant().email() ); s.setString( 2, membership.division() ); s.executeUpdate(); } } /** The file-based counterpart to this table. */ ReadyDirectory readyDirectory() { return readyDirectory; } private final ReadyDirectory readyDirectory; // final after init /** The name of the table's schema. */ static final String SCHEMA_NAME = "out_trace"; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private boolean isChanged; }