Project

General

Profile

« Previous | Next » 

Revision 3567

Added by Jing Tao over 16 years ago

Add code to initialize Options and some code clean up

View differences:

QueryResultBuilderTest.java
56 56
	/** Return field value in xml_returnfield table*/
57 57
	private String[] returnFieldString = {"dataset/title", "entityName", "individualName/surName", "keyword"};
58 58
	/** Return field id value for above return field string in xml_returnfield*/
59
	private int returnFieldId = 4;
59
	private int returnFieldId =11;
60 60
	
61 61
	static
62 62
	  {
......
77 77
    {
78 78
        TestSuite suite = new TestSuite();
79 79
        suite.addTest(new QueryResultBuilderTest("initialize"));
80
        //suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
80
        suite.addTest(new QueryResultBuilderTest("testBuildRecords"));
81 81
        return suite;
82 82
    }
83 83
   
......
116 116
    
117 117

  
118 118
    /**
119
     * Test how long it will take to build records in xml_queryresult table
119
     * Tests how long it will take to build records in xml_queryresult table
120 120
     */
121 121
    public void testBuildRecords() throws Exception
122 122
    {
123 123
        double start = System.currentTimeMillis()/1000;
124 124
        DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
125 125
        int serialNumber = dbconn.getCheckOutSerialNumber();
126
        String sql = "SELECT docid, nodedata from xml_path_index where path like ?";
126
        String sql = "SELECT docid, nodedata, path FROM xml_path_index WHERE path IN (";
127 127
        PreparedStatement pstmt = null;
128 128
        ResultSet result = null;
129
        //Gets the document information such as doctype, docname and et al
130
        Hashtable docsInformation = getDocumentsInfo();
129 131
        //The key of the hash is docid, the element is the return field value (in xml_format)
130 132
        Hashtable table = new Hashtable();
131 133
        int size = returnFieldString.length;
132
        // Loop the return filed value in xml_path_index table and put the docid and value into hashtable
134
        // Constructs the sql base on the return fields
135
        boolean first = true;
133 136
        for (int i=0; i< size; i++)
134 137
        {
138
        	if (!first)
139
        	{
140
        		sql=sql+",";
141
        	}
142
        	sql=sql+"'";
135 143
        	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();
144
        	 sql = sql+path;
145
        	 sql=sql+"'";
146
        	 first = false;
169 147
        }
148
        sql = sql+")";
149
        System.out.println("The final sql is "+sql);
150
    	pstmt = dbconn.prepareStatement(sql);
151
        pstmt.execute();
152
        result = pstmt.getResultSet();
153
        boolean hasNext = result.next();
154
        // Gets returning value for docids and puts them into hashtable
155
        while (hasNext)
156
        {
157
        	String docid = result.getString(1);
158
        	String value = result.getString(2);
159
        	String path  = result.getString(3);
160
        	// Gets document information from document information hash table.
161
        	String docInfo = (String)docsInformation.get(docid);
162
        	StringBuffer buffer  = new StringBuffer();
163
        	if (docInfo != null)
164
        	{
165
        		buffer.append(docInfo);
166
        	}
167
        	buffer.append("<param name=\"");
168
            buffer.append(path);
169
            buffer.append("\">");
170
            buffer.append(value);
171
            buffer.append("</param>");
172
            String xmlValue = buffer.toString();
173
            //If the hashtable already has key for this docid,
174
            //we should append obove value to the old value
175
        	if (table.containsKey(docid))
176
        	{
177
        		String oldValue = (String)table.get(docid);
178
        		String newValue = oldValue+xmlValue;
179
        		table.put(docid, newValue);
180
        	}
181
        	else
182
        	{
183
        		table.put(docid, xmlValue);
184
        	}
185
        	hasNext = result.next();
186
        }
187
        result.close();
188
        pstmt.close();
170 189
        // Insert the hashtable value into xml_queryresult table
171 190
        Enumeration docids = table.keys();
172 191
        while (docids.hasMoreElements())
......
190 209
        DBConnectionPool.returnDBConnection(dbconn, serialNumber);
191 210
        assertTrue(1 == 1);
192 211
    }
212
    
213
    /*
214
     * Gets a Hashtable which contains documents' information. 
215
     * The key of this Hashtable is docid. The element value is the information, like
216
     * docid, docname,doctype and et al. This method will be used in testBuildRecords 
217
     * method to combine a completed return information for query.
218
     */
219
    private Hashtable getDocumentsInfo() throws Exception
220
    {
221
    	DBConnection dbconn = DBConnectionPool.getDBConnection("DBQuery.findDocuments");
222
        int serialNumber = dbconn.getCheckOutSerialNumber();
223
        String sql = "SELECT docid, rev, docname, doctype, date_created,date_updated from xml_documents";
224
        PreparedStatement pstmt = null;
225
        ResultSet result = null;
226
        //The key of the hash is docid, the element is the document information (in xml_format)
227
        Hashtable table = new Hashtable();
228
        pstmt = dbconn.prepareStatement(sql);
229
        pstmt.execute();
230
        result = pstmt.getResultSet();
231
        boolean hasNext = result.next();
232
        // Get  values from the xml_document table
233
        while (hasNext)
234
        {
235
        	String             docid = result.getString(1);
236
        	int                      rev = result.getInt(2);
237
        	String        docname = result.getString(3);
238
        	String         doctype = result.getString(4);
239
        	String   createDate = result.getString(5);
240
        	String updateDate = result.getString(6);
241
        	String completeDocid = docid
242
                             + MetaCatUtil.getOption("accNumSeparator");
243
            completeDocid += rev;
244
            // Put everything into a string buffer
245
            StringBuffer document = new StringBuffer();
246
            document.append("<docid>").append(completeDocid).append("</docid>");
247
            if (docname != null)
248
            {
249
                document.append("<docname>" + docname + "</docname>");
250
            }
251
            if (doctype != null)
252
            {
253
               document.append("<doctype>" + doctype + "</doctype>");
254
            }
255
            if (createDate != null)
256
            {
257
                document.append("<createdate>" + createDate + "</createdate>");
258
            }
259
            if (updateDate != null)
260
            {
261
              document.append("<updatedate>" + updateDate + "</updatedate>");
262
            }
263
            String information = document.toString();
264
            // Put the docid and info into Hashtable
265
            table.put(docid, information);
266
            hasNext = result.next();
267
        }
268
        result.close();
269
        pstmt.close();
270
        return table;
271
    }
193 272

  
194 273
}

Also available in: Unified diff