Project

General

Profile

« Previous | Next » 

Revision 3546

Added by Jing Tao about 17 years ago

Test file to see how long it will take to build records in xml_queryresult table.

View differences:

test/edu/ucsb/nceas/metacattest/QueryResultBuilderTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
package edu.ucsb.nceas.metacattest;
25

  
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29
import java.sql.PreparedStatement;
30
import java.sql.ResultSet;
31
import java.util.Enumeration;
32
import java.util.Hashtable;
33
import java.util.Iterator;
34

  
35
import edu.ucsb.nceas.metacat.DBConnection;
36
import edu.ucsb.nceas.metacat.DBConnectionPool;
37
import edu.ucsb.nceas.metacat.MetaCatUtil;
38
import edu.ucsb.nceas.metacat.Version;
39
import edu.ucsb.nceas.utilities.Options;
40

  
41
import junit.framework.Test;
42
import junit.framework.TestCase;
43
import junit.framework.TestSuite;
44

  
45

  
46
/**
47
 * @author jones
48
 *
49
 * Test class for getting idea how long it will take - to build 
50
 * records xml_queryresult table for a given return fields
51
 */
52
public class QueryResultBuilderTest extends TestCase
53
{
54
	/** DBConnection pool*/
55
	private DBConnectionPool connPool = null;
56
	/** Return field value in xml_returnfield table*/
57
	private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
58
	/** Return field id value for above return field string in xml_returnfield*/
59
	private int returnFieldId = 4;
60
	
61
	static
62
	  {
63
		  try
64
		  {
65
			  Options.initialize(new File("build/tests/metacat.properties"));
66
		  }
67
		  catch(Exception e)
68
		  {
69
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
70
		  }
71
	  }
72
	
73
	/**
74
     * Create a suite of tests to be run together
75
     */
76
    public static Test suite()
77
    {
78
        TestSuite suite = new TestSuite();
79
        suite.addTest(new QueryResultBuilderTest("initialize"));
80
        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
81
        return suite;
82
    }
83
   
84
    /**
85
     * Constructor to build the test
86
     * 
87
     * @param name the name of the test method
88
     */
89
    public QueryResultBuilderTest(String name)
90
    {
91
        super(name);
92
    }
93
	
94
	/*
95
     * @see TestCase#setUp()
96
     */
97
    protected void setUp() throws Exception
98
    {
99
        super.setUp();
100
        try {
101
        	connPool = DBConnectionPool.getInstance();      
102
        } catch (Exception e) {
103
            fail(e.getMessage());
104
        }
105
    }
106
    
107
    /**
108
     * Run an initial test that always passes to check that the test
109
     * harness is working.
110
     */
111
    public void initialize()
112
    {
113
        assertTrue(1 == 1);
114
    }
115
    
116
    
117

  
118
    /**
119
     * Test how long it will take to build records in xml_queryresult table
120
     */
121
    public void testBuildRecords() throws Exception
122
    {
123
        double start = System.currentTimeMillis()/1000;
124
        DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
125
        int serialNumber = dbconn.getCheckOutSerialNumber();
126
        String sql = "SELECT docid, nodedata from xml_path_index where path like ?";
127
        PreparedStatement pstmt = null;
128
        ResultSet result = null;
129
        //The key of the hash is docid, the element is the return field value (in xml_format)
130
        Hashtable table = new Hashtable();
131
        int size = returnFieldString.length;
132
        // Loop the return filed value in xml_path_index table and put the docid and value into hashtable
133
        for (int i=0; i< size; i++)
134
        {
135
        	String path = returnFieldString[i];
136
        	pstmt = dbconn.prepareStatement(sql);
137
            pstmt.setString(1,path);
138
            pstmt.execute();
139
            result = pstmt.getResultSet();
140
            boolean hasNext = result.next();
141
            // Get path value for one docid
142
            while (hasNext)
143
            {
144
            	String docid = result.getString(1);
145
            	String value = result.getString(2);
146
            	StringBuffer buffer  = new StringBuffer();
147
            	buffer.append("<param name=\"");
148
                buffer.append(path);
149
                buffer.append("\">");
150
                buffer.append(value);
151
                buffer.append("</param>");
152
                String xmlValue = buffer.toString();
153
                //If the hashtable already has key for this docid,
154
                //we should append obove value to the old value
155
            	if (table.containsKey(docid))
156
            	{
157
            		String oldValue = (String)table.get(docid);
158
            		String newValue = oldValue+xmlValue;
159
            		table.put(docid, newValue);
160
            	}
161
            	else
162
            	{
163
            		table.put(docid, xmlValue);
164
            	}
165
            	hasNext = result.next();
166
            }
167
            result.close();
168
            pstmt.close();
169
        }
170
        // Insert the hashtable value into xml_queryresult table
171
        Enumeration docids = table.keys();
172
        while (docids.hasMoreElements())
173
        {
174
        	String docId = (String)docids.nextElement();
175
        	String returnString = (String)table.get(docId);
176
        	String query = "INSERT INTO xml_queryresult (returnfield_id, docid, "
177
                + "queryresult_string) VALUES (?, ?, ?)";
178
         
179
            pstmt = dbconn.prepareStatement(query);
180
            pstmt.setInt(1,returnFieldId );
181
            pstmt.setString(2,docId);
182
            pstmt.setString(3, returnString);
183
           	pstmt.execute();     
184
            pstmt.close();
185
           
186
        }
187
        
188
        double end = System.currentTimeMillis()/1000;
189
        System.out.println("The time to build xml_queryresult is "+(end-start));
190
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
191
        assertTrue(1 == 1);
192
    }
193

  
194
}
0 195

  

Also available in: Unified diff