Project

General

Profile

« Previous | Next » 

Revision 3342

Added by Jing Tao over 16 years ago

Add a cache mechanism for public user.

View differences:

DBQuery.java
81 81
    Vector docidOverride = new Vector();
82 82
    
83 83
    // a hash table serves as query reuslt cache. Key of hashtable
84
    // is a query string and value is ResultDocumentSet Object
84
    // is a query string and value is result xml string
85 85
    private static Hashtable queryResultCache = new Hashtable();
86 86
    
87 87
    // Capacity of the query result cache
......
393 393
    HttpSession sess = (HttpSession)sessionHash.get(sessionid);
394 394

  
395 395
    
396
    
397 396
    resultset.append("<?xml version=\"1.0\"?>\n");
398 397
    resultset.append("<resultset>\n");
399 398
    resultset.append("  <pagestart>" + pagestart + "</pagestart>\n");
......
418 417

  
419 418
        //print out the search result
420 419
        // search the doc list
421
        resultset = findResultDoclist(qspec, resultset, out, user, groups,
420
        StringBuffer resultContent = findResultDoclist(qspec, out, user, groups,
422 421
                                      dbconn, useXMLIndex, pagesize, pagestart, 
423 422
                                      sessionid);
423
        resultset.append(resultContent);
424 424
      } //try
425 425
      catch (IOException ioe)
426 426
      {
......
459 459
     * Find the doc list which match the query
460 460
     */
461 461
    private StringBuffer findResultDoclist(QuerySpecification qspec,
462
                                      StringBuffer resultsetBuffer,
463 462
                                      PrintWriter out,
464 463
                                      String user, String[]groups,
465 464
                                      DBConnection dbconn, boolean useXMLIndex,
466 465
                                      int pagesize, int pagestart, String sessionid)
467 466
                                      throws Exception
468 467
    {
468
      StringBuffer resultsetBuffer = new StringBuffer();
469 469
      String query = null;
470 470
      int count = 0;
471 471
      int index = 0;
......
539 539
        
540 540
      }
541 541
      logMetacat.warn("============ final selection query: " + query);
542
      
543
      // we only get cache for public
544
      if (user != null && user.equalsIgnoreCase("public") 
545
     		 && pagesize == 0 && MetaCatUtil.getOption("query_cache_on").equals("true"))
546
      {
547
   	      String cachedResult = getResultXMLFromCache(query);
548
   	      //System.out.println("==========the string from cache is "+cachedResult);
549
   	      if (cachedResult != null)
550
   	      {
551
   	    	 if (out != null)
552
   	         {
553
   	             out.println(cachedResult);
554
   	         }
555
   	    	 resultsetBuffer.append(cachedResult);
556
   	    	 return resultsetBuffer;
557
   	      }
558
      }
559
      
542 560
      startTime = System.currentTimeMillis() / 1000;
543 561
      pstmt = dbconn.prepareStatement(query);
544 562
      rs = pstmt.executeQuery();
......
554 572
      
555 573
      if(pagesize == 0)
556 574
      { //this makes sure we get all results if there is no paging
557
        pagesize = 99999;
558
        pagestart = 99999;
575
        pagesize = 9999999;
576
        pagestart = 9999999;
559 577
      } 
560 578
      
561 579
      int currentIndex = 0;
......
638 656
       {
639 657
         ResultDocumentSet pagedResultsHash = new ResultDocumentSet();
640 658
         //get the last page of information then break
641
         if(pagesize != 99999)
659
         if(pagesize != 9999999)
642 660
         {
643 661
           for(int i=pagesize*pagestart; i<docListResult.size(); i++)
644 662
           {
......
663 681
     //if docListResult is not empty, it need to be sent.
664 682
     if (docListResult.size() != 0)
665 683
     {
684
      
666 685
       handleSubsetResult(qspec,resultsetBuffer, out, docListResult,
667 686
                              user, groups,dbconn, useXMLIndex);
668 687
     }
......
672 691
     {
673 692
         out.println("\n<lastpage>" + lastpage + "</lastpage>\n");
674 693
     }
694
     
695
     // now we only cached none-paged query and user is public
696
     if (user != null && user.equalsIgnoreCase("public") 
697
    		 && pagesize == 9999999 && MetaCatUtil.getOption("query_cache_on").equals("true"))
698
     {
699
       //System.out.println("the string stored into cache is "+ resultsetBuffer.toString());
700
  	   storeQueryResultIntoCache(query, resultsetBuffer.toString());
701
     }
675 702
          
676 703
     return resultsetBuffer;
677 704
    }//findReturnDoclist
......
1251 1278
   }
1252 1279
   
1253 1280
   /*
1254
    * Method to store query string and DocumentResultSet into query result
1281
    * Method to store query string and result xml string into query result
1255 1282
    * cache. If the size alreay reache the limitation, the cache will be
1256 1283
    * cleared first, then store them.
1257 1284
    */
1258
   private void storeQueryResultIntoCache(String query, ResultDocumentSet resultSet)
1285
   private void storeQueryResultIntoCache(String query, String resultXML)
1259 1286
   {
1260 1287
	   synchronized (queryResultCache)
1261 1288
	   {
......
1263 1290
		   {
1264 1291
			   queryResultCache.clear();
1265 1292
		   }
1266
		   queryResultCache.put(query, resultSet);
1293
		   queryResultCache.put(query, resultXML);
1267 1294
		   
1268 1295
	   }
1269 1296
   }
1270 1297
   
1271 1298
   /*
1272
    * Method to get DocumentResultSet from query result cache. 
1273
    * Note: the returned ResultDoucmentSet can be null.
1299
    * Method to get result xml string from query result cache. 
1300
    * Note: the returned string can be null.
1274 1301
    */
1275
   private ResultDocumentSet getResultDocumentSetFromCache(String query)
1302
   private String getResultXMLFromCache(String query)
1276 1303
   {
1277
	   ResultDocumentSet resultSet = null;
1304
	   String resultSet = null;
1278 1305
	   synchronized (queryResultCache)
1279 1306
	   {
1280 1307
          try
1281 1308
          {
1282
		     resultSet = (ResultDocumentSet)queryResultCache.get(query);
1309
		     resultSet = (String)queryResultCache.get(query);
1283 1310
		   
1284 1311
          }
1285 1312
          catch (Exception e)

Also available in: Unified diff