From 2a8113a8928f183d9189d824a398c20d7e6e1c08 Mon Sep 17 00:00:00 2001
From: fgerlits <fgerlits@cfc7b370-4200-0410-a6e3-cb6bdb053afe>
Date: Thu, 20 Jan 2005 14:11:37 +0000
Subject: [PATCH] fixed the search methods

---
 .../LiveSupport/Storage/SearchCriteria.h      | 21 ++++++++--------
 .../Storage/StorageClientInterface.h          |  8 ++++---
 .../modules/storage/src/SearchCriteria.cxx    | 15 ++++++++----
 .../storage/src/SearchCriteriaTest.cxx        | 24 +++----------------
 .../modules/storage/src/TestStorageClient.cxx | 14 ++++-------
 .../modules/storage/src/TestStorageClient.h   |  8 ++++---
 .../storage/src/TestStorageClientTest.cxx     |  4 ++--
 .../modules/storage/src/WebStorageClient.cxx  |  4 +---
 .../modules/storage/src/WebStorageClient.h    |  8 ++++---
 .../storage/src/WebStorageClientTest.cxx      | 16 ++++++-------
 10 files changed, 54 insertions(+), 68 deletions(-)

diff --git a/livesupport/modules/storage/include/LiveSupport/Storage/SearchCriteria.h b/livesupport/modules/storage/include/LiveSupport/Storage/SearchCriteria.h
index 02b946c2d..63b4d3a09 100644
--- a/livesupport/modules/storage/include/LiveSupport/Storage/SearchCriteria.h
+++ b/livesupport/modules/storage/include/LiveSupport/Storage/SearchCriteria.h
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.2 $
+    Version  : $Revision: 1.3 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/include/LiveSupport/Storage/Attic/SearchCriteria.h,v $
 
 ------------------------------------------------------------------------------*/
@@ -62,9 +62,9 @@ class TestStorageClient;        // forward declaration of friend class
  * 
  *  Its fields are:
  *  <ul>
- *    <li>type     - mandatory, values in (audioClip | playlist | all)</li>
- *    <li>operator - values in (and | or); 
- *                            optional, default is <i>and</i></li>
+ *    <li>type     - values in (audioClip | playlist | all); the default is
+ *                          <i>audioClip</i></li>
+ *    <li>operator - values in (and | or); the default is <i>and</i></li>
  *    <li>condition1 : { key : string, comparison: string, value : string }
  *                          - a search condition, where <i>key</i> is one of the
  *                            fields in the metadata, and <i>comparison</i> is
@@ -72,16 +72,17 @@ class TestStorageClient;        // forward declaration of friend class
  *                                        | "<" | "<=" | ">" | ">=")</li>
  *    <li>...</li>
  *    <li>conditionN</li>
- *    <li>limit  : int - the maximum number of results to be returned; optional,
- *                       the default is 0, which means there is no limit</li>
- *    <li>offset : int - start at the <i>offset</i>+1-th result; optional,
+ *    <li>limit  : int - the maximum number of results to be returned;
+ *                       the default is 0, which means unlimited</li>
+ *    <li>offset : int - ignore the first <i>offset</i> matches;
  *                       the default is 0.</li>
  *  </ul>
  *
  *  Usage: construct a SearchCriteria object either directly using the 
- *  constructor with 4 string arguments, or in several steps using the setter
- *  methods; then pass this object to Storage::search() to search the local
- *  storage.
+ *  constructor with 4 string arguments, or in several steps using 
+ *  addCondition() and the setter methods; 
+ *  then pass this object to StorageClientInterface::search() 
+ *  to search the local storage.
  *
  *  The <i>key</i> and <i>value</i> fields are case-sensitive, all the other
  *  strings (type, operator names) are case-insensitive.
diff --git a/livesupport/modules/storage/include/LiveSupport/Storage/StorageClientInterface.h b/livesupport/modules/storage/include/LiveSupport/Storage/StorageClientInterface.h
index 6b192e5dd..4dd012d3d 100644
--- a/livesupport/modules/storage/include/LiveSupport/Storage/StorageClientInterface.h
+++ b/livesupport/modules/storage/include/LiveSupport/Storage/StorageClientInterface.h
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.7 $
+    Version  : $Revision: 1.8 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/include/LiveSupport/Storage/StorageClientInterface.h,v $
 
 ------------------------------------------------------------------------------*/
@@ -67,7 +67,7 @@ using namespace Core;
  *  An interface for storage clients.
  *
  *  @author  $Author: fgerlits $
- *  @version $Revision: 1.7 $
+ *  @version $Revision: 1.8 $
  */
 class StorageClientInterface
 {
@@ -372,7 +372,9 @@ class StorageClientInterface
          *
          *  @param sessionId the session ID from the authentication client
          *  @param searchCriteria an object containing the search criteria
-         *  @return the number of items found.
+         *  @return the number of items found; this may not be equal to the 
+         *          number of items returned: see SearchCriteria::setLimit()
+         *          and SearchCriteria::setOffset()
          *  @exception XmlRpcException if there is a problem with the XML-RPC
          *                             call.
          */
diff --git a/livesupport/modules/storage/src/SearchCriteria.cxx b/livesupport/modules/storage/src/SearchCriteria.cxx
index 668c64ced..9f5128991 100644
--- a/livesupport/modules/storage/src/SearchCriteria.cxx
+++ b/livesupport/modules/storage/src/SearchCriteria.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.1 $
+    Version  : $Revision: 1.2 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/Attic/SearchCriteria.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -95,7 +95,9 @@ SearchCriteria :: operator XmlRpc::XmlRpcValue() const
     XmlRpc::XmlRpcValue     returnValue;
     
     returnValue["filetype"] = type;
-    returnValue["operator"] = logicalOperator;
+    if (searchConditions.size() > 1) {
+        returnValue["operator"] = logicalOperator;
+    }
     
     XmlRpc::XmlRpcValue     conditionList;
     conditionList.setSize(searchConditions.size());
@@ -110,8 +112,13 @@ SearchCriteria :: operator XmlRpc::XmlRpcValue() const
     }
     returnValue["conditions"] = conditionList;
 
-    returnValue["limit"]    = limit;
-    returnValue["offset"]   = offset;
+    if (limit) {
+        returnValue["limit"]    = limit;
+    }
+    
+    if (offset) {
+        returnValue["offset"]   = offset;
+    }
     
     return returnValue;
 }
diff --git a/livesupport/modules/storage/src/SearchCriteriaTest.cxx b/livesupport/modules/storage/src/SearchCriteriaTest.cxx
index 39510440c..32d31c665 100644
--- a/livesupport/modules/storage/src/SearchCriteriaTest.cxx
+++ b/livesupport/modules/storage/src/SearchCriteriaTest.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.1 $
+    Version  : $Revision: 1.2 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/Attic/SearchCriteriaTest.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -85,24 +85,15 @@ SearchCriteriaTest :: firstTest(void)
     }
     CPPUNIT_ASSERT(xmlRpcValue.hasMember("filetype"));
     CPPUNIT_ASSERT(xmlRpcValue["filetype"] == "audioclip");
-    CPPUNIT_ASSERT(xmlRpcValue.hasMember("operator"));
-    CPPUNIT_ASSERT(xmlRpcValue["operator"] == "and");
     CPPUNIT_ASSERT(xmlRpcValue.hasMember("conditions"));
     CPPUNIT_ASSERT(xmlRpcValue["conditions"].getType() 
                                 == XmlRpc::XmlRpcValue::TypeArray);
     CPPUNIT_ASSERT(xmlRpcValue["conditions"].size() == 0);
-    CPPUNIT_ASSERT(xmlRpcValue.hasMember("limit"));
-    CPPUNIT_ASSERT(xmlRpcValue["limit"].getType() 
-                                            == XmlRpc::XmlRpcValue::TypeInt);
-    CPPUNIT_ASSERT(int(xmlRpcValue["limit"]) == 0);
-    CPPUNIT_ASSERT(xmlRpcValue.hasMember("offset"));
-    CPPUNIT_ASSERT(xmlRpcValue["offset"].getType() 
-                                            == XmlRpc::XmlRpcValue::TypeInt);
-    CPPUNIT_ASSERT(int(xmlRpcValue["offset"]) == 0);
 
     try {
         SearchCriteria      secondCriteria("playlist", "Or");
         secondCriteria.setLimit(50);
+        secondCriteria.setOffset(100);
         secondCriteria.addCondition("dc:title", "PREFIX", "My ");
         secondCriteria.addCondition("DcTerms:Extent", "<", "180");
         xmlRpcValue = secondCriteria;
@@ -141,7 +132,7 @@ SearchCriteriaTest :: firstTest(void)
     CPPUNIT_ASSERT(xmlRpcValue.hasMember("offset"));
     CPPUNIT_ASSERT(xmlRpcValue["offset"].getType() 
                                             == XmlRpc::XmlRpcValue::TypeInt);
-    CPPUNIT_ASSERT(int(xmlRpcValue["offset"]) == 0);
+    CPPUNIT_ASSERT(int(xmlRpcValue["offset"]) == 100);
 
     try {
         SearchCriteria      thirdCriteria("all", "dc:creator", "partial", "X");
@@ -163,14 +154,5 @@ SearchCriteriaTest :: firstTest(void)
     CPPUNIT_ASSERT(condition0["op"] == "partial");
     CPPUNIT_ASSERT(condition0.hasMember("val"));
     CPPUNIT_ASSERT(condition0["val"] == "X");
-
-    CPPUNIT_ASSERT(xmlRpcValue.hasMember("limit"));
-    CPPUNIT_ASSERT(xmlRpcValue["limit"].getType() 
-                                            == XmlRpc::XmlRpcValue::TypeInt);
-    CPPUNIT_ASSERT(int(xmlRpcValue["limit"]) == 0);
-    CPPUNIT_ASSERT(xmlRpcValue.hasMember("offset"));
-    CPPUNIT_ASSERT(xmlRpcValue["offset"].getType() 
-                                            == XmlRpc::XmlRpcValue::TypeInt);
-    CPPUNIT_ASSERT(int(xmlRpcValue["offset"]) == 0);
 }
 
diff --git a/livesupport/modules/storage/src/TestStorageClient.cxx b/livesupport/modules/storage/src/TestStorageClient.cxx
index cce9cc83e..cf72b32d8 100644
--- a/livesupport/modules/storage/src/TestStorageClient.cxx
+++ b/livesupport/modules/storage/src/TestStorageClient.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.32 $
+    Version  : $Revision: 1.33 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -743,13 +743,10 @@ TestStorageClient :: search(Ptr<SessionId>::Ref      sessionId,
         AudioClipMapType::const_iterator    it = audioClipMap.begin();
         while (it != audioClipMap.end()) {
             if (matchesCriteria(it->second, searchCriteria)) {
-                if (counter >= first) {
+                if (counter >= first && (!last || counter < last)) {
                     audioClipIds->push_back(it->second->getId());
                 }
                 ++counter;
-                if (last && counter >= last) {
-                    return (counter - first);
-                }
             }
             ++it;
         }
@@ -759,19 +756,16 @@ TestStorageClient :: search(Ptr<SessionId>::Ref      sessionId,
         PlaylistMapType::const_iterator    it = playlistMap.begin();
         while (it != playlistMap.end()) {
             if (matchesCriteria(it->second, searchCriteria)) {
-                if (counter >= first) {
+                if (counter >= first && (!last || counter < last)) {
                     playlistIds->push_back(it->second->getId());
                 }
                 ++counter;
-                if (last && counter >= last) {
-                    return (counter - first);
-                }
             }
             ++it;
         }
     }
     
-    return (counter - first);
+    return counter;
 }
 
 
diff --git a/livesupport/modules/storage/src/TestStorageClient.h b/livesupport/modules/storage/src/TestStorageClient.h
index b5317fabd..c98d683d1 100644
--- a/livesupport/modules/storage/src/TestStorageClient.h
+++ b/livesupport/modules/storage/src/TestStorageClient.h
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.26 $
+    Version  : $Revision: 1.27 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClient.h,v $
 
 ------------------------------------------------------------------------------*/
@@ -86,7 +86,7 @@ using namespace LiveSupport::Core;
  *  </code></pre>
  *
  *  @author  $Author: fgerlits $
- *  @version $Revision: 1.26 $
+ *  @version $Revision: 1.27 $
  */
 class TestStorageClient :
                     virtual public Configurable,
@@ -503,7 +503,9 @@ class TestStorageClient :
          *
          *  @param sessionId the session ID from the authentication client
          *  @param searchCriteria an object containing the search criteria
-         *  @return the number of items found.
+         *  @return the number of items found; this may not be equal to the 
+         *          number of items returned: see SearchCriteria::setLimit()
+         *          and SearchCriteria::setOffset()
          *  @exception XmlRpcException if there is a problem with the XML-RPC
          *                             call.
          */
diff --git a/livesupport/modules/storage/src/TestStorageClientTest.cxx b/livesupport/modules/storage/src/TestStorageClientTest.cxx
index 769b338e2..554997d82 100644
--- a/livesupport/modules/storage/src/TestStorageClientTest.cxx
+++ b/livesupport/modules/storage/src/TestStorageClientTest.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.23 $
+    Version  : $Revision: 1.24 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/TestStorageClientTest.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -431,7 +431,7 @@ TestStorageClientTest :: searchTest(void)
         criteria->setLimit(2);
         criteria->setOffset(1);
         int numberFound = tsc->search(dummySessionId, criteria);
-        CPPUNIT_ASSERT(numberFound == 2);
+        CPPUNIT_ASSERT(numberFound == 4);
         CPPUNIT_ASSERT(tsc->getAudioClipIds()->size() == 1);
         CPPUNIT_ASSERT(tsc->getAudioClipIds()->at(0)->getId() == 0x10003);
         CPPUNIT_ASSERT(tsc->getPlaylistIds()->size() == 1);
diff --git a/livesupport/modules/storage/src/WebStorageClient.cxx b/livesupport/modules/storage/src/WebStorageClient.cxx
index c8e2c4cf6..09aa5347c 100644
--- a/livesupport/modules/storage/src/WebStorageClient.cxx
+++ b/livesupport/modules/storage/src/WebStorageClient.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.30 $
+    Version  : $Revision: 1.31 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/WebStorageClient.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -1947,7 +1947,6 @@ WebStorageClient :: search(Ptr<SessionId>::Ref      sessionId,
             = sessionId->getId();
     parameters[searchCriteriaParamName] 
             = *searchCriteria;
-// std::cerr << "\nparams: " << parameters.toXml() << "\n";
 
     result.clear();
     if (!xmlRpcClient.execute(searchMethodName.c_str(),
@@ -1960,7 +1959,6 @@ WebStorageClient :: search(Ptr<SessionId>::Ref      sessionId,
     }
     xmlRpcClient.close();
 
-// std::cerr << "result: " << result.toXml() << "\n";
     if (xmlRpcClient.isFault()) {
         std::stringstream eMsg;
         eMsg << "XML-RPC method '" 
diff --git a/livesupport/modules/storage/src/WebStorageClient.h b/livesupport/modules/storage/src/WebStorageClient.h
index 6f3ed90dc..83a00099b 100644
--- a/livesupport/modules/storage/src/WebStorageClient.h
+++ b/livesupport/modules/storage/src/WebStorageClient.h
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.21 $
+    Version  : $Revision: 1.22 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/WebStorageClient.h,v $
 
 ------------------------------------------------------------------------------*/
@@ -96,7 +96,7 @@ using namespace LiveSupport::Core;
  *  </code></pre>
  *
  *  @author  $Author: fgerlits $
- *  @version $Revision: 1.21 $
+ *  @version $Revision: 1.22 $
  */
 class WebStorageClient :
                     virtual public Configurable,
@@ -533,7 +533,9 @@ class WebStorageClient :
          *
          *  @param sessionId the session ID from the authentication client
          *  @param searchCriteria an object containing the search criteria
-         *  @return the number of items found.
+         *  @return the number of items found; this may not be equal to the 
+         *          number of items returned: see SearchCriteria::setLimit()
+         *          and SearchCriteria::setOffset()
          *  @exception XmlRpcException if there is a problem with the XML-RPC
          *                             call.
          */
diff --git a/livesupport/modules/storage/src/WebStorageClientTest.cxx b/livesupport/modules/storage/src/WebStorageClientTest.cxx
index 79b2abdea..ae0426b8a 100644
--- a/livesupport/modules/storage/src/WebStorageClientTest.cxx
+++ b/livesupport/modules/storage/src/WebStorageClientTest.cxx
@@ -22,7 +22,7 @@
  
  
     Author   : $Author: fgerlits $
-    Version  : $Revision: 1.31 $
+    Version  : $Revision: 1.32 $
     Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/storage/src/WebStorageClientTest.cxx,v $
 
 ------------------------------------------------------------------------------*/
@@ -591,7 +591,6 @@ void
 WebStorageClientTest :: searchTest(void)
                                                 throw (CPPUNIT_NS::Exception)
 {
-/*
     try {
         wsc->reset();
     } catch (XmlRpcException &e) {
@@ -615,23 +614,23 @@ WebStorageClientTest :: searchTest(void)
     try {
         Ptr<SearchCriteria>::Ref    criteria(new SearchCriteria(
                                             "audioClip",
-                                            "dc:title", "prefix", "File"));
-        int results = wsc->search(sessionId, criteria);
-        CPPUNIT_ASSERT(results == 2);
+                                            "dc:title", "prefix", "File "));
+        int numberFound = wsc->search(sessionId, criteria);
+        CPPUNIT_ASSERT(numberFound == 2);
     } catch (XmlRpcException &e) {
         CPPUNIT_FAIL(e.what());
     }
     CPPUNIT_ASSERT(wsc->getAudioClipIds()->size() == 2);
     CPPUNIT_ASSERT(*wsc->getAudioClipIds()->at(0) == *audioClip0);
-    CPPUNIT_ASSERT(*wsc->getAudioClipIds()->at(1) == *audioClip1);
+    CPPUNIT_ASSERT(*wsc->getAudioClipIds()->at(1) == *audioClip2);
     
     try {
         Ptr<SearchCriteria>::Ref    criteria(new SearchCriteria(
                                                             "playlist", "or"));
         criteria->addCondition("dcterms:extent", ">=", "0");
         criteria->setLimit(10);
-        int results = wsc->search(sessionId, criteria);
-        CPPUNIT_ASSERT(results == 1);
+        int numberFound = wsc->search(sessionId, criteria);
+        CPPUNIT_ASSERT(numberFound == 1);
     } catch (XmlRpcException &e) {
         CPPUNIT_FAIL(e.what());
     }
@@ -643,6 +642,5 @@ WebStorageClientTest :: searchTest(void)
     } catch (XmlRpcException &e) {
         CPPUNIT_FAIL(e.what());
     }
-*/
 }