001    package hep.aida.ref.sql.Test;
002    
003    import hep.aida.ref.sql.SQLTuple;
004    import hep.aida.ref.sql.Util;
005    import hep.aida.ref.sql.AIDAURL;
006    import hep.aida.ref.sql.SQLTupleException;
007    
008    // AIDA
009    import hep.aida.IAnalysisFactory;
010    import hep.aida.ITree;
011    import hep.aida.ITuple;
012    import hep.aida.ITupleFactory;
013    import hep.aida.IFilter;
014    
015    // Log4J
016    import org.apache.log4j.Logger;
017    
018    // Java
019    import java.util.Collection;
020    import java.io.IOException;
021    
022    /** <code>Read</code> reads one NTuple table. It gets
023      * a <code>token_sring</code> String from each row which
024      * satisfies a simple filter. {@link RowRepresentant}s are 
025      * created from those Strings and accumulated in
026      * an array. Database  and NTuple (table) should exist.
027      * <br>
028      * <code>Reading</code> is an example to show reading API and
029      * capabilities of SQLTuple.
030      * <p><font color="#880088">
031      * <pre>
032      * $Log: Read.java,v $
033      * Revision 1.28  2007/05/23 16:38:45  hrivnac
034      * logical connections for Plotter; better UML
035      *
036      * Revision 1.27  2004/10/29 22:27:25  hrivnac
037      * imports corrected
038      *
039      * Revision 1.26  2004/10/08 15:22:33  hrivnac
040      * JAS3 plugin works
041      *
042      * Revision 1.25  2004/05/22 15:12:59  hrivnac
043      * class id reformated
044      *
045      * Revision 1.24  2004/04/14 13:39:47  hrivnac
046      * 1.5 warnings fixed
047      *
048      * Revision 1.23  2004/04/13 15:45:54  hrivnac
049      * AIDA URL introduced
050      *
051      * Revision 1.22  2004/02/10 14:50:58  hrivnac
052      * JavaDoc tags completed
053      *
054      * Revision 1.21  2003/11/24 15:13:22  hrivnac
055      * Logging improved.
056      *
057      * Revision 1.20  2003/11/20 17:21:58  hrivnac
058      * Java 1.5 natively supported, Log4J reporting improved.
059      *
060      * Revision 1.19  2003/11/17 15:01:23  hrivnac
061      * More BM results.
062      *
063      * Revision 1.18  2003/11/17 10:18:29  hrivnac
064      * Cleaning.
065      *
066      * Revision 1.17  2003/11/13 15:43:23  hrivnac
067      * BenchMarking introduced.
068      *
069      * Revision 1.16  2003/11/05 19:46:22  hrivnac
070      * - FreeHEP 1.2.1
071      * - JAIDA 3.2.1
072      *
073      * Revision 1.9  2003/10/30 11:42:44  hrivnac
074      * ArrayList -> Collection
075      *
076      * Revision 1.6  2003/09/30 20:30:47  hrivnac
077      * special Pool functions
078      *
079      * Revision 1.5  2003/09/30 16:46:02  hrivnac
080      * - Writing test in TestWrite
081      * - SQLTuple projections supported
082      *
083      * Revision 1.4  2003/09/30 14:11:48  hrivnac
084      * Can created SQLTuples.
085      *
086      * Revision 1.3  2003/09/30 12:29:19  hrivnac
087      * Tuples can be written.
088      *
089      * Revision 1.2  2003/09/29 14:36:40  hrivnac
090      * Works per attribute.
091      *
092      * </pre>
093      * </font></p>
094      * @opt attributes
095      * @opt operations
096      * @opt types
097      * @opt visibility
098      * @version $Id: Read.java,v 1.28 2007/05/23 16:38:45 hrivnac Exp $
099      * @author <a href="mailto:Julius.Hrivnac@cern.ch">J.Hrivnac</a> */
100    public class Read {
101    
102      /** Initiate processing.
103        * @param args <ol>
104        *             <li>The URL of existing database,</li>
105        *             <li>the user name to be used,</li>
106        *             <li>the user password to be used.</li>
107        *             </ol> */
108      public static void main(String[] args) {
109        if (args.length != 3) {
110          log.fatal(Util.urlHelp("hep.aida.ref.sql.Test.Read <url> <user> <passwd>"));
111          System.exit(1);
112          }
113        try {
114          (new Read()).process(args[0], args[1], args[2]);
115          }
116        catch (Exception e) {
117          log.fatal(Util.report("*** Read failed ***", e), e);
118          }
119        }
120    
121      /** Perform processing.
122        * @param url    The URL of existing database.
123        * @param user   The user name to be used.
124        * @param passwd The user password to be used.
125        * @throws SQLTuple if the operation can't be performed. */
126      private void process(String url, 
127                           String user,
128                           String passwd) throws SQLTupleException {
129    
130        log.debug("Reading SQLTuple " + url + " as " + user);
131    
132        // Parse URL
133        AIDAURL aidaurl  = new AIDAURL(url);
134        String dbName    = aidaurl.db();
135        String tableName = aidaurl.table();
136    
137        // Create options
138        String options = "hep.aida.ref.sql.db="     + dbName + ";"
139                       + "hep.aida.ref.sql.user="   + user   + ";"
140                       + "hep.aida.ref.sql.passwd=" + passwd;
141    
142        // Create top-level factory (selection can be done 
143        // by -Dhep.aida.IAnalysisFactory command-line option)
144        IAnalysisFactory af = IAnalysisFactory.create();
145    
146        // Create Tree
147        ITree tree = null;
148        try {
149          tree = af.createTreeFactory().create(dbName, aidaurl.storeType(), true, false, options);
150          }
151        catch (IOException e) {
152          throw new SQLTupleException("Can't create Tree from following options: " + options, e);
153          }
154    
155        // Get NTuple
156        ITuple tuple = (ITuple)tree.find(tableName);
157    
158        // Create Filter
159        ITupleFactory tf = af.createTupleFactory(tree);
160        IFilter filter = tf.createFilter("attfloat > attdouble");
161    
162        // Start time measure
163        long start = System.currentTimeMillis(); 
164    
165        // Perform filtering
166        Collection<RowRepresentant> reps = null;
167        if (tuple instanceof SQLTuple) {    
168          reps = ((SQLTuple)tuple).getColumn("token_string", RowRepresentant.class, filter);
169          for (RowRepresentant r : reps) {
170            log.info(r);
171            }
172          }
173    
174        // Quatify
175        int rows  = tuple.rows();
176    
177        // Close Tree
178        try {
179          tree.close();
180          }
181        catch (IOException e) {
182          throw new SQLTupleException("Can't close Tree", e);
183          }
184    
185        // Report spent time and results
186        double delta = (System.currentTimeMillis() - start) / 1000.0;
187        log.info(rows + " rows read in " + delta + " s");
188        if (reps != null) {
189          log.info(reps.size() + " found");    
190          }
191    
192       }
193         
194      /** Logging . */
195      private static Logger log = Logger.getLogger(Read.class);
196    
197      }
198