oodt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From prami...@apache.org
Subject svn commit: r1378994 - /oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java
Date Thu, 30 Aug 2012 15:44:02 GMT
Author: pramirez
Date: Thu Aug 30 15:44:02 2012
New Revision: 1378994

URL: http://svn.apache.org/viewvc?rev=1378994&view=rev
Log:
OODT-489 Reduce number of queries performed by paging API.

Modified:
    oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java

Modified: oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java
URL: http://svn.apache.org/viewvc/oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java?rev=1378994&r1=1378993&r2=1378994&view=diff
==============================================================================
--- oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java
(original)
+++ oodt/trunk/filemgr/src/main/java/org/apache/oodt/cas/filemgr/catalog/LuceneCatalog.java
Thu Aug 30 15:44:02 2012
@@ -634,7 +634,7 @@ public class LuceneCatalog implements Ca
     public List<String> query(Query query, ProductType type) throws CatalogException
{
         // paginate products returns full products, but the query method
         // is expected to return product ids
-        List<Product> fullProducts = paginateQuery(query, type, -1);
+        List<Product> fullProducts = paginateQuery(query, type, -1, null);
         List<String> productIds = null;
 
         if (fullProducts != null && fullProducts.size() > 0) {
@@ -721,7 +721,7 @@ public class LuceneCatalog implements Ca
         Query query = new Query();
 
         for (int pageNum = 1; pageNum < numPages + 1; pageNum++) {
-            List<Product> pageProducts = paginateQuery(query, type, pageNum);
+            List<Product> pageProducts = paginateQuery(query, type, pageNum, null);
             products.addAll(pageProducts);
         }
 
@@ -756,12 +756,15 @@ public class LuceneCatalog implements Ca
      * @see org.apache.oodt.cas.filemgr.util.Pagination#getFirstPage(org.apache.oodt.cas.filemgr.structs.ProductType)
      */
     public ProductPage getFirstPage(ProductType type) {
-        ProductPage firstPage = null;
+        ProductPage firstPage = new ProductPage();
         List<Product> products = null;
         Query query = new Query();
-
+        
+        // now construct the page
+        firstPage.setPageNum(1);
+        firstPage.setPageSize(pageSize);
         try {
-            products = paginateQuery(query, type, 1);
+          products = paginateQuery(query, type, 1, firstPage);
         } catch (CatalogException e) {
             LOG.log(Level.WARNING,
                     "CatalogException getting first page for product type: ["
@@ -769,28 +772,13 @@ public class LuceneCatalog implements Ca
                             + "] from catalog: Message: " + e.getMessage());
             return null;
         }
-
+        // There are no products and thus no first page
         if (products == null || (products != null && products.size() == 0)) {
-            return firstPage;
-        } else {
-            // now construct the page
-            firstPage = new ProductPage();
-            firstPage.setPageNum(1);
-            firstPage.setPageSize(pageSize);
-            try {
-                firstPage.setTotalPages(PaginationUtils.getTotalPage(
-                        getNumHits(query, type), pageSize));
-            } catch (Exception e) {
-                LOG.log(Level.WARNING,
-                        "Exception getting total pages for query: [" + query
-                                + "]: Message: " + e.getMessage());
-                firstPage.setTotalPages(-1);
-            }
-
-            firstPage.setPageProducts(products);
-
+        		return null;
         }
 
+        firstPage.setPageProducts(products);
+
         return firstPage;
     }
 
@@ -800,32 +788,28 @@ public class LuceneCatalog implements Ca
      * @see org.apache.oodt.cas.filemgr.util.Pagination#getLastProductPage(org.apache.oodt.cas.filemgr.structs.ProductType)
      */
     public ProductPage getLastProductPage(ProductType type) {
-        ProductPage lastPage = null;
+        ProductPage lastPage = new ProductPage();
         ProductPage firstPage = getFirstPage(type);
         List<Product> products = null;
         Query query = new Query();
-
+        
+        // now construct the page
+        lastPage.setPageNum(firstPage.getTotalPages());
+        lastPage.setPageSize(pageSize);
         try {
-            products = paginateQuery(query, type, firstPage.getTotalPages());
+            products = paginateQuery(query, type, firstPage.getTotalPages(), lastPage);
         } catch (CatalogException e) {
-            LOG.log(Level.WARNING,
-                    "CatalogException getting last page for product type: ["
-                            + type.getProductTypeId()
-                            + "] from catalog: Message: " + e.getMessage());
-            return null;
+          	LOG.log(Level.WARNING,
+                  "CatalogException getting last page for product type: ["
+                          + type.getProductTypeId()
+                          + "] from catalog: Message: " + e.getMessage());
+          	return null;
         }
-
+        // There are no products thus there is no last page
         if (products == null || (products != null && products.size() == 0)) {
-            return lastPage;
-        } else {
-            // now construct the page
-            lastPage = new ProductPage();
-            lastPage.setPageNum(firstPage.getTotalPages());
-            lastPage.setPageSize(pageSize);
-            lastPage.setTotalPages(firstPage.getTotalPages());
-            lastPage.setPageProducts(products);
-
+        	  return null;
         }
+        lastPage.setPageProducts(products);
 
         return lastPage;
     }
@@ -846,30 +830,26 @@ public class LuceneCatalog implements Ca
         }
 
         List<Product> products = null;
-        ProductPage nextPage = null;
+        ProductPage nextPage = new ProductPage();
         Query query = new Query();
 
+        // now construct the page
+        nextPage.setPageNum(currentPage.getPageNum() + 1);
+        nextPage.setPageSize(pageSize);
         try {
-            products = paginateQuery(query, type, currentPage.getPageNum() + 1);
+            products = paginateQuery(query, type, currentPage.getPageNum() + 1, nextPage);
         } catch (CatalogException e) {
             LOG.log(Level.WARNING,
-                    "CatalogException getting next page for product type: ["
-                            + type.getProductTypeId()
-                            + "] from catalog: Message: " + e.getMessage());
+                  "CatalogException getting next page for product type: ["
+                          + type.getProductTypeId()
+                          + "] from catalog: Message: " + e.getMessage());
             return null;
         }
-
+        // There are no products and thus no next page
         if (products == null || (products != null && products.size() == 0)) {
-            return nextPage;
-        } else {
-            // now construct the page
-            nextPage = new ProductPage();
-            nextPage.setPageNum(currentPage.getPageNum() + 1);
-            nextPage.setPageSize(pageSize);
-            nextPage.setTotalPages(currentPage.getTotalPages());
-            nextPage.setPageProducts(products);
-
+        	  return null;
         }
+        nextPage.setPageProducts(products);
 
         return nextPage;
     }
@@ -889,11 +869,15 @@ public class LuceneCatalog implements Ca
             return currentPage;
         }
         List<Product> products = null;
-        ProductPage prevPage = null;
+        ProductPage prevPage = new ProductPage();
         Query query = new Query();
 
+        // now construct the page
+        prevPage = new ProductPage();
+        prevPage.setPageNum(currentPage.getPageNum() - 1);
+        prevPage.setPageSize(pageSize);
         try {
-            products = paginateQuery(query, type, currentPage.getPageNum() - 1);
+            products = paginateQuery(query, type, currentPage.getPageNum() - 1, prevPage);
         } catch (CatalogException e) {
             LOG.log(Level.WARNING,
                     "CatalogException getting prev page for product type: ["
@@ -901,18 +885,12 @@ public class LuceneCatalog implements Ca
                             + "] from catalog: Message: " + e.getMessage());
             return null;
         }
-
+        
+        // There are no products and thus no pages
         if (products == null || (products != null && products.size() == 0)) {
-            return prevPage;
-        } else {
-            // now construct the page
-            prevPage = new ProductPage();
-            prevPage.setPageNum(currentPage.getPageNum() - 1);
-            prevPage.setPageSize(pageSize);
-            prevPage.setTotalPages(currentPage.getTotalPages());
-            prevPage.setPageProducts(products);
-
+        	  return null;
         }
+        prevPage.setPageProducts(products);
 
         return prevPage;
     }
@@ -929,9 +907,7 @@ public class LuceneCatalog implements Ca
             ProductPage retPage = new ProductPage();
             retPage.setPageNum(pageNum);
             retPage.setPageSize(pageSize);
-            retPage.setTotalPages(PaginationUtils.getTotalPage(getNumHits(
-                    query, type), pageSize));
-            retPage.setPageProducts(paginateQuery(query, type, pageNum));
+            retPage.setPageProducts(paginateQuery(query, type, pageNum, retPage));
             return retPage;
         } catch (Exception e) {
             e.printStackTrace();
@@ -1229,11 +1205,9 @@ public class LuceneCatalog implements Ca
             for (QueryCriteria queryCriteria : query.getCriteria()) 
                 booleanQuery.add(this.getQuery(queryCriteria), BooleanClause.Occur.MUST);
 
-            Sort sort = new Sort(new SortField("CAS.ProductReceivedTime",
-                    SortField.STRING, true));
             LOG.log(Level.FINE, "Querying LuceneCatalog: q: [" + booleanQuery
                     + "]");
-            Hits hits = searcher.search(booleanQuery, sort);
+            Hits hits = searcher.search(booleanQuery);
             numHits = hits.length();
         } catch (IOException e) {
             LOG.log(Level.WARNING,
@@ -1254,7 +1228,7 @@ public class LuceneCatalog implements Ca
         return numHits;
     }
 
-    private List<Product> paginateQuery(Query query, ProductType type, int pageNum)
+    private List<Product> paginateQuery(Query query, ProductType type, int pageNum,
ProductPage page)
             throws CatalogException {
         List<Product> products = null;
         IndexSearcher searcher = null;
@@ -1285,6 +1259,12 @@ public class LuceneCatalog implements Ca
             LOG.log(Level.FINE, "Querying LuceneCatalog: q: [" + booleanQuery
                     + "]");
             Hits hits = searcher.search(booleanQuery, sort);
+            
+            // Calculate page size and set it while we have the results
+            if (page != null) {
+            	page.setTotalPages(PaginationUtils.getTotalPage(hits.length(), pageSize));
+            }
+            
             if (hits.length() > 0) {
 
                 int startNum = (pageNum - 1) * pageSize;



Mime
View raw message