Source for file Balancer.php

Documentation is available at Balancer.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2007-2011, Servigistics, Inc.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *
  9.  *  - Redistributions of source code must retain the above copyright notice,
  10.  *    this list of conditions and the following disclaimer.
  11.  *  - Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  *  - Neither the name of Servigistics, Inc. nor the names of
  15.  *    its contributors may be used to endorse or promote products derived from
  16.  *    this software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @copyright Copyright 2007-2011 Servigistics, Inc. (http://servigistics.com)
  31.  * @license http://solr-php-client.googlecode.com/svn/trunk/COPYING New BSD
  32.  * @version $Id: Balancer.php 54 2011-02-04 16:29:18Z donovan.jimenez $
  33.  *
  34.  * @package Apache
  35.  * @subpackage Solr
  36.  * @author Donovan Jimenez <djimenez@conduit-it.com>, Dan Wolfe
  37.  */
  38.  
  39. // See Issue #1 (http://code.google.com/p/solr-php-client/issues/detail?id=1)
  40. // Doesn't follow typical include path conventions, but is more convenient for users
  41. require_once(dirname(dirname(__FILE__)) '/Service.php');
  42.  
  43. require_once(dirname(dirname(__FILE__)) '/NoServiceAvailableException.php');
  44.  
  45. /**
  46.  * Reference Implementation for using multiple Solr services in a distribution. Functionality
  47.  * includes:
  48.  *     routing of read / write operations
  49.  *     failover (on selection) for multiple read servers
  50.  */
  51. {
  52.     /**
  53.      * SVN Revision meta data for this class
  54.      */
  55.     const SVN_REVISION '$Revision: 54 $';
  56.  
  57.     /**
  58.      * SVN ID meta data for this class
  59.      */
  60.     const SVN_ID '$Id: Balancer.php 54 2011-02-04 16:29:18Z donovan.jimenez $';
  61.  
  62.     protected $_createDocuments = true;
  63.  
  64.     protected $_readableServices = array();
  65.     protected $_writeableServices = array();
  66.  
  67.     protected $_currentReadService = null;
  68.     protected $_currentWriteService = null;
  69.  
  70.     protected $_readPingTimeout = 2;
  71.     protected $_writePingTimeout = 4;
  72.  
  73.     // Configuration for server selection backoff intervals
  74.     protected $_useBackoff = false;        // Set to true to use more resillient write server selection
  75.     protected $_backoffLimit = 600;        // 10 minute default maximum
  76.     protected $_backoffEscalation = 2.0;     // Rate at which to increase backoff period
  77.     protected $_defaultBackoff = 2.0;        // Default backoff interval
  78.  
  79.     /**
  80.      * Escape a value for special query characters such as ':', '(', ')', '*', '?', etc.
  81.      *
  82.      * NOTE: inside a phrase fewer characters need escaped, use {@link Apache_Solr_Service::escapePhrase()} instead
  83.      *
  84.      * @param string $value 
  85.      * @return string 
  86.      */
  87.     static public function escape($value)
  88.     {
  89.         return Apache_Solr_Service::escape($value);
  90.     }
  91.  
  92.     /**
  93.      * Escape a value meant to be contained in a phrase for special query characters
  94.      *
  95.      * @param string $value 
  96.      * @return string 
  97.      */
  98.     static public function escapePhrase($value)
  99.     {
  100.         return Apache_Solr_Service::escapePhrase($value);
  101.     }
  102.  
  103.     /**
  104.      * Convenience function for creating phrase syntax from a value
  105.      *
  106.      * @param string $value 
  107.      * @return string 
  108.      */
  109.     static public function phrase($value)
  110.     {
  111.         return Apache_Solr_Service::phrase($value);
  112.     }
  113.  
  114.     /**
  115.      * Constructor. Takes arrays of read and write service instances or descriptions
  116.      *
  117.      * @param array $readableServices 
  118.      * @param array $writeableServices 
  119.      */
  120.     public function __construct($readableServices array()$writeableServices array())
  121.     {
  122.         //setup readable services
  123.         foreach ($readableServices as $service)
  124.         {
  125.             $this->addReadService($service);
  126.         }
  127.  
  128.         //setup writeable services
  129.         foreach ($writeableServices as $service)
  130.         {
  131.             $this->addWriteService($service);
  132.         }
  133.     }
  134.  
  135.     public function setReadPingTimeout($timeout)
  136.     {
  137.         $this->_readPingTimeout = $timeout;
  138.     }
  139.  
  140.     public function setWritePingTimeout($timeout)
  141.     {
  142.         $this->_writePingTimeout = $timeout;
  143.     }
  144.  
  145.     public function setUseBackoff($enable)
  146.     {
  147.         $this->_useBackoff = $enable;
  148.     }
  149.  
  150.     /**
  151.      * Generates a service ID
  152.      *
  153.      * @param string $host 
  154.      * @param integer $port 
  155.      * @param string $path 
  156.      * @return string 
  157.      */
  158.     protected function _getServiceId($host$port$path)
  159.     {
  160.         return $host ':' $port $path;
  161.     }
  162.  
  163.     /**
  164.      * Adds a service instance or service descriptor (if it is already
  165.      * not added)
  166.      *
  167.      * @param mixed $service 
  168.      *
  169.      * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
  170.      */
  171.     public function addReadService($service)
  172.     {
  173.         if ($service instanceof Apache_Solr_Service)
  174.         {
  175.             $id $this->_getServiceId($service->getHost()$service->getPort()$service->getPath());
  176.  
  177.             $this->_readableServices[$id$service;
  178.         }
  179.         else if (is_array($service))
  180.         {
  181.             if (isset($service['host']&& isset($service['port']&& isset($service['path']))
  182.             {
  183.                 $id $this->_getServiceId((string)$service['host'](int)$service['port'](string)$service['path']);
  184.  
  185.                 $this->_readableServices[$id$service;
  186.             }
  187.             else
  188.             {
  189.                 throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
  190.             }
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Removes a service instance or descriptor from the available services
  196.      *
  197.      * @param mixed $service 
  198.      *
  199.      * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
  200.      */
  201.     public function removeReadService($service)
  202.     {
  203.         $id '';
  204.  
  205.         if ($service instanceof Apache_Solr_Service)
  206.         {
  207.             $id $this->_getServiceId($service->getHost()$service->getPort()$service->getPath());
  208.         }
  209.         else if (is_array($service))
  210.         {
  211.             if (isset($service['host']&& isset($service['port']&& isset($service['path']))
  212.             {
  213.                 $id $this->_getServiceId((string)$service['host'](int)$service['port'](string)$service['path']);
  214.             }
  215.             else
  216.             {
  217.                 throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
  218.             }
  219.         }
  220.         else if (is_string($service))
  221.         {
  222.             $id $service;
  223.         }
  224.  
  225.         if ($id && isset($this->_readableServices[$id]))
  226.         {
  227.             unset($this->_readableServices[$id]);
  228.         }
  229.     }
  230.  
  231.     /**
  232.      * Adds a service instance or service descriptor (if it is already
  233.      * not added)
  234.      *
  235.      * @param mixed $service 
  236.      *
  237.      * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
  238.      */
  239.     public function addWriteService($service)
  240.     {
  241.         if ($service instanceof Apache_Solr_Service)
  242.         {
  243.             $id $this->_getServiceId($service->getHost()$service->getPort()$service->getPath());
  244.  
  245.             $this->_writeableServices[$id$service;
  246.         }
  247.         else if (is_array($service))
  248.         {
  249.             if (isset($service['host']&& isset($service['port']&& isset($service['path']))
  250.             {
  251.                 $id $this->_getServiceId((string)$service['host'](int)$service['port'](string)$service['path']);
  252.  
  253.                 $this->_writeableServices[$id$service;
  254.             }
  255.             else
  256.             {
  257.                 throw new Apache_Solr_InvalidArgumentException('A Writeable Service description array does not have all required elements of host, port, and path');
  258.             }
  259.         }
  260.     }
  261.  
  262.     /**
  263.      * Removes a service instance or descriptor from the available services
  264.      *
  265.      * @param mixed $service 
  266.      *
  267.      * @throws Apache_Solr_InvalidArgumentException If service descriptor is not valid
  268.      */
  269.     public function removeWriteService($service)
  270.     {
  271.         $id '';
  272.  
  273.         if ($service instanceof Apache_Solr_Service)
  274.         {
  275.             $id $this->_getServiceId($service->getHost()$service->getPort()$service->getPath());
  276.         }
  277.         else if (is_array($service))
  278.         {
  279.             if (isset($service['host']&& isset($service['port']&& isset($service['path']))
  280.             {
  281.                 $id $this->_getServiceId((string)$service['host'](int)$service['port'](string)$service['path']);
  282.             }
  283.             else
  284.             {
  285.                 throw new Apache_Solr_InvalidArgumentException('A Readable Service description array does not have all required elements of host, port, and path');
  286.             }
  287.         }
  288.         else if (is_string($service))
  289.         {
  290.             $id $service;
  291.         }
  292.  
  293.         if ($id && isset($this->_writeableServices[$id]))
  294.         {
  295.             unset($this->_writeableServices[$id]);
  296.         }
  297.     }
  298.  
  299.     /**
  300.      * Iterate through available read services and select the first with a ping
  301.      * that satisfies configured timeout restrictions (or the default)
  302.      *
  303.      * @return Apache_Solr_Service 
  304.      *
  305.      * @throws Apache_Solr_NoServiceAvailableException If there are no read services that meet requirements
  306.      */
  307.     protected function _selectReadService($forceSelect false)
  308.     {
  309.         if (!$this->_currentReadService || !isset($this->_readableServices[$this->_currentReadService]|| $forceSelect)
  310.         {
  311.             if ($this->_currentReadService && isset($this->_readableServices[$this->_currentReadService]&& $forceSelect)
  312.             {
  313.                 // we probably had a communication error, ping the current read service, remove it if it times out
  314.                 if ($this->_readableServices[$this->_currentReadService]->ping($this->_readPingTimeout=== false)
  315.                 {
  316.                     $this->removeReadService($this->_currentReadService);
  317.                 }
  318.             }
  319.  
  320.             if (count($this->_readableServices))
  321.             {
  322.                 // select one of the read services at random
  323.                 $ids array_keys($this->_readableServices);
  324.  
  325.                 $id $ids[rand(0count($ids1)];
  326.                 $service $this->_readableServices[$id];
  327.  
  328.                 if (is_array($service))
  329.                 {
  330.                     //convert the array definition to a client object
  331.                     $service new Apache_Solr_Service($service['host']$service['port']$service['path']);
  332.                     $this->_readableServices[$id$service;
  333.                 }
  334.  
  335.                 $service->setCreateDocuments($this->_createDocuments);
  336.                 $this->_currentReadService = $id;
  337.             }
  338.             else
  339.             {
  340.                 throw new Apache_Solr_NoServiceAvailableException('No read services were available');
  341.             }
  342.         }
  343.  
  344.         return $this->_readableServices[$this->_currentReadService];
  345.     }
  346.  
  347.     /**
  348.      * Iterate through available write services and select the first with a ping
  349.      * that satisfies configured timeout restrictions (or the default)
  350.      *
  351.      * @return Apache_Solr_Service 
  352.      *
  353.      * @throws Apache_Solr_NoServiceAvailableException If there are no write services that meet requirements
  354.      */
  355.     protected function _selectWriteService($forceSelect false)
  356.     {
  357.         if($this->_useBackoff)
  358.         {
  359.             return $this->_selectWriteServiceSafe($forceSelect);
  360.         }
  361.  
  362.         if (!$this->_currentWriteService || !isset($this->_writeableServices[$this->_currentWriteService]|| $forceSelect)
  363.         {
  364.             if ($this->_currentWriteService && isset($this->_writeableServices[$this->_currentWriteService]&& $forceSelect)
  365.             {
  366.                 // we probably had a communication error, ping the current read service, remove it if it times out
  367.                 if ($this->_writeableServices[$this->_currentWriteService]->ping($this->_writePingTimeout=== false)
  368.                 {
  369.                     $this->removeWriteService($this->_currentWriteService);
  370.                 }
  371.             }
  372.  
  373.             if (count($this->_writeableServices))
  374.             {
  375.                 // select one of the read services at random
  376.                 $ids array_keys($this->_writeableServices);
  377.  
  378.                 $id $ids[rand(0count($ids1)];
  379.                 $service $this->_writeableServices[$id];
  380.  
  381.                 if (is_array($service))
  382.                 {
  383.                     //convert the array definition to a client object
  384.                     $service new Apache_Solr_Service($service['host']$service['port']$service['path']);
  385.                     $this->_writeableServices[$id$service;
  386.                 }
  387.  
  388.                 $this->_currentWriteService = $id;
  389.             }
  390.             else
  391.             {
  392.                 throw new Apache_Solr_NoServiceAvailableException('No write services were available');
  393.             }
  394.         }
  395.  
  396.         return $this->_writeableServices[$this->_currentWriteService];
  397.     }
  398.  
  399.     /**
  400.      * Iterate through available write services and select the first with a ping
  401.      * that satisfies configured timeout restrictions (or the default).  The
  402.      * timeout period will increase until a connection is made or the limit is
  403.      * reached.   This will allow for increased reliability with heavily loaded
  404.      * server(s).
  405.      *
  406.      * @return Apache_Solr_Service 
  407.      *
  408.      * @throws Apache_Solr_NoServiceAvailableException If there are no write services that meet requirements
  409.      */
  410.  
  411.     protected function _selectWriteServiceSafe($forceSelect false)
  412.     {
  413.         if (!$this->_currentWriteService || !isset($this->_writeableServices[$this->_currentWriteService]|| $forceSelect)
  414.         {
  415.             if (count($this->_writeableServices))
  416.             {
  417.                 $backoff $this->_defaultBackoff;
  418.  
  419.                 do {
  420.                     // select one of the read services at random
  421.                     $ids array_keys($this->_writeableServices);
  422.  
  423.                     $id $ids[rand(0count($ids1)];
  424.                     $service $this->_writeableServices[$id];
  425.  
  426.                     if (is_array($service))
  427.                     {
  428.                         //convert the array definition to a client object
  429.                         $service new Apache_Solr_Service($service['host']$service['port']$service['path']);
  430.                         $this->_writeableServices[$id$service;
  431.                     }
  432.  
  433.                     $this->_currentWriteService = $id;
  434.  
  435.                     $backoff *= $this->_backoffEscalation;
  436.  
  437.                     if($backoff $this->_backoffLimit)
  438.                     {
  439.                         throw new Apache_Solr_NoServiceAvailableException('No write services were available.  All timeouts exceeded.');
  440.                     }
  441.  
  442.                 while($this->_writeableServices[$this->_currentWriteService]->ping($backoff=== false);
  443.             }
  444.             else
  445.             {
  446.                 throw new Apache_Solr_NoServiceAvailableException('No write services were available');
  447.             }
  448.         }
  449.  
  450.         return $this->_writeableServices[$this->_currentWriteService];
  451.     }
  452.  
  453.     /**
  454.      * Set the create documents flag. This determines whether {@link Apache_Solr_Response} objects will
  455.      * parse the response and create {@link Apache_Solr_Document} instances in place.
  456.      *
  457.      * @param boolean $createDocuments 
  458.      */
  459.     public function setCreateDocuments($createDocuments)
  460.     {
  461.         $this->_createDocuments = (bool) $createDocuments;
  462.  
  463.         // set on current read service
  464.         if ($this->_currentReadService)
  465.         {
  466.             $service $this->_selectReadService();
  467.             $service->setCreateDocuments($createDocuments);
  468.         }
  469.     }
  470.  
  471.     /**
  472.      * Get the current state of teh create documents flag.
  473.      *
  474.      * @return boolean 
  475.      */
  476.     public function getCreateDocuments()
  477.     {
  478.         return $this->_createDocuments;
  479.     }
  480.     
  481.     /**
  482.      * Raw Add Method. Takes a raw post body and sends it to the update service.  Post body
  483.      * should be a complete and well formed "add" xml document.
  484.      *
  485.      * @param string $rawPost 
  486.      * @return Apache_Solr_Response 
  487.      *
  488.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  489.      */
  490.     public function add($rawPost)
  491.     {
  492.         $service $this->_selectWriteService();
  493.  
  494.         do
  495.         {
  496.             try
  497.             {
  498.                 return $service->add($rawPost);
  499.             }
  500.             catch (Apache_Solr_HttpTransportException $e)
  501.             {
  502.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  503.                 {
  504.                     throw $e;
  505.                 }
  506.             }
  507.  
  508.             $service $this->_selectWriteService(true);
  509.         while ($service);
  510.  
  511.         return false;
  512.     }
  513.  
  514.     /**
  515.      * Add a Solr Document to the index
  516.      *
  517.      * @param Apache_Solr_Document $document 
  518.      * @param boolean $allowDups 
  519.      * @param boolean $overwritePending 
  520.      * @param boolean $overwriteCommitted 
  521.      * @return Apache_Solr_Response 
  522.      *
  523.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  524.      */
  525.     public function addDocument(Apache_Solr_Document $document$allowDups false$overwritePending true$overwriteCommitted true)
  526.     {
  527.         $service $this->_selectWriteService();
  528.  
  529.         do
  530.         {
  531.             try
  532.             {
  533.                 return $service->addDocument($document$allowDups$overwritePending$overwriteCommitted);
  534.             }
  535.             catch (Apache_Solr_HttpTransportException $e)
  536.             {
  537.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  538.                 {
  539.                     throw $e;
  540.                 }
  541.             }
  542.  
  543.             $service $this->_selectWriteService(true);
  544.         while ($service);
  545.  
  546.         return false;
  547.     }
  548.  
  549.     /**
  550.      * Add an array of Solr Documents to the index all at once
  551.      *
  552.      * @param array $documents Should be an array of Apache_Solr_Document instances
  553.      * @param boolean $allowDups 
  554.      * @param boolean $overwritePending 
  555.      * @param boolean $overwriteCommitted 
  556.      * @return Apache_Solr_Response 
  557.      *
  558.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  559.      */
  560.     public function addDocuments($documents$allowDups false$overwritePending true$overwriteCommitted true)
  561.     {
  562.         $service $this->_selectWriteService();
  563.  
  564.         do
  565.         {
  566.             try
  567.             {
  568.                 return $service->addDocuments($documents$allowDups$overwritePending$overwriteCommitted);
  569.             }
  570.             catch (Apache_Solr_HttpTransportException $e)
  571.             {
  572.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  573.                 {
  574.                     throw $e;
  575.                 }
  576.             }
  577.  
  578.             $service $this->_selectWriteService(true);
  579.         while ($service);
  580.  
  581.         return false;
  582.     }
  583.  
  584.     /**
  585.      * Send a commit command.  Will be synchronous unless both wait parameters are set
  586.      * to false.
  587.      *
  588.      * @param boolean $waitFlush 
  589.      * @param boolean $waitSearcher 
  590.      * @return Apache_Solr_Response 
  591.      *
  592.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  593.      */
  594.     public function commit($optimize true$waitFlush true$waitSearcher true$timeout 3600)
  595.     {
  596.         $service $this->_selectWriteService();
  597.  
  598.         do
  599.         {
  600.             try
  601.             {
  602.                 return $service->commit($optimize$waitFlush$waitSearcher$timeout);
  603.             }
  604.             catch (Apache_Solr_HttpTransportException $e)
  605.             {
  606.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  607.                 {
  608.                     throw $e;
  609.                 }
  610.             }
  611.  
  612.             $service $this->_selectWriteService(true);
  613.         while ($service);
  614.  
  615.         return false;
  616.     }
  617.  
  618.     /**
  619.      * Raw Delete Method. Takes a raw post body and sends it to the update service. Body should be
  620.      * a complete and well formed "delete" xml document
  621.      *
  622.      * @param string $rawPost 
  623.      * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
  624.      * @return Apache_Solr_Response 
  625.      *
  626.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  627.      */
  628.     public function delete($rawPost$timeout 3600)
  629.     {
  630.         $service $this->_selectWriteService();
  631.  
  632.         do
  633.         {
  634.             try
  635.             {
  636.                 return $service->delete($rawPost$timeout);
  637.             }
  638.             catch (Apache_Solr_HttpTransportException $e)
  639.             {
  640.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  641.                 {
  642.                     throw $e;
  643.                 }
  644.             }
  645.  
  646.             $service $this->_selectWriteService(true);
  647.         while ($service);
  648.  
  649.         return false;
  650.     }
  651.  
  652.     /**
  653.      * Create a delete document based on document ID
  654.      *
  655.      * @param string $id 
  656.      * @param boolean $fromPending 
  657.      * @param boolean $fromCommitted 
  658.      * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
  659.      * @return Apache_Solr_Response 
  660.      *
  661.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  662.      */
  663.     public function deleteById($id$fromPending true$fromCommitted true$timeout 3600)
  664.     {
  665.         $service $this->_selectWriteService();
  666.  
  667.         do
  668.         {
  669.             try
  670.             {
  671.                 return $service->deleteById($id$fromPending$fromCommitted$timeout);
  672.             }
  673.             catch (Apache_Solr_HttpTransportException $e)
  674.             {
  675.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  676.                 {
  677.                     throw $e;
  678.                 }
  679.             }
  680.  
  681.             $service $this->_selectWriteService(true);
  682.         while ($service);
  683.  
  684.         return false;
  685.     }
  686.  
  687.     /**
  688.      * Create and post a delete document based on multiple document IDs.
  689.      *
  690.      * @param array $ids Expected to be utf-8 encoded strings
  691.      * @param boolean $fromPending 
  692.      * @param boolean $fromCommitted 
  693.      * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
  694.      * @return Apache_Solr_Response 
  695.      *
  696.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  697.      */
  698.     public function deleteByMultipleIds($ids$fromPending true$fromCommitted true$timeout 3600)
  699.     {
  700.         $service $this->_selectWriteService();
  701.  
  702.         do
  703.         {
  704.             try
  705.             {
  706.                 return $service->deleteByMultipleId($ids$fromPending$fromCommitted$timeout);
  707.             }
  708.             catch (Apache_Solr_HttpTransportException $e)
  709.             {
  710.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  711.                 {
  712.                     throw $e;
  713.                 }
  714.             }
  715.  
  716.             $service $this->_selectWriteService(true);
  717.         while ($service);
  718.  
  719.         return false;
  720.     }
  721.  
  722.     /**
  723.      * Create a delete document based on a query and submit it
  724.      *
  725.      * @param string $rawQuery 
  726.      * @param boolean $fromPending 
  727.      * @param boolean $fromCommitted 
  728.      * @param float $timeout Maximum expected duration of the delete operation on the server (otherwise, will throw a communication exception)
  729.      * @return Apache_Solr_Response 
  730.      *
  731.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  732.      */
  733.     public function deleteByQuery($rawQuery$fromPending true$fromCommitted true$timeout 3600)
  734.     {
  735.         $service $this->_selectWriteService();
  736.  
  737.         do
  738.         {
  739.             try
  740.             {
  741.                 return $service->deleteByQuery($rawQuery$fromPending$fromCommitted$timeout);
  742.             }
  743.             catch (Apache_Solr_HttpTransportException $e)
  744.             {
  745.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  746.                 {
  747.                     throw $e;
  748.                 }
  749.             }
  750.  
  751.             $service $this->_selectWriteService(true);
  752.         while ($service);
  753.  
  754.         return false;
  755.     }
  756.     
  757.     /**
  758.      * Use Solr Cell to extract document contents. See {@link http://wiki.apache.org/solr/ExtractingRequestHandler} for information on how
  759.      * to use Solr Cell and what parameters are available.
  760.      *
  761.      * NOTE: when passing an Apache_Solr_Document instance, field names and boosts will automatically be prepended by "literal." and "boost."
  762.      * as appropriate. Any keys from the $params array will NOT be treated this way. Any mappings from the document will overwrite key / value
  763.      * pairs in the params array if they have the same name (e.g. you pass a "literal.id" key and value in your $params array but you also
  764.      * pass in a document isntance with an "id" field" - the document's value(s) will take precedence).
  765.      *
  766.      * @param string $file Path to file to extract data from
  767.      * @param array $params optional array of key value pairs that will be sent with the post (see Solr Cell documentation)
  768.      * @param Apache_Solr_Document $document optional document that will be used to generate post parameters (literal.* and boost.* params)
  769.      * @param string $mimetype optional mimetype specification (for the file being extracted)
  770.      *
  771.      * @return Apache_Solr_Response 
  772.      *
  773.      * @throws Apache_Solr_InvalidArgumentException if $file, $params, or $document are invalid.
  774.      */
  775.     public function extract($file$params array()$document null$mimetype 'application/octet-stream')
  776.     {
  777.         $service $this->_selectWriteService();
  778.  
  779.         do
  780.         {
  781.             try
  782.             {
  783.                 return $service->extract($file$params$document$mimetype);
  784.             }
  785.             catch (Apache_Solr_HttpTransportException $e)
  786.             {
  787.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  788.                 {
  789.                     throw $e;
  790.                 }
  791.             }
  792.  
  793.             $service $this->_selectWriteService(true);
  794.         while ($service);
  795.  
  796.         return false;
  797.     }
  798.     
  799.     /**
  800.      * Use Solr Cell to extract document contents. See {@link http://wiki.apache.org/solr/ExtractingRequestHandler} for information on how
  801.      * to use Solr Cell and what parameters are available.
  802.      *
  803.      * NOTE: when passing an Apache_Solr_Document instance, field names and boosts will automatically be prepended by "literal." and "boost."
  804.      * as appropriate. Any keys from the $params array will NOT be treated this way. Any mappings from the document will overwrite key / value
  805.      * pairs in the params array if they have the same name (e.g. you pass a "literal.id" key and value in your $params array but you also
  806.      * pass in a document isntance with an "id" field" - the document's value(s) will take precedence).
  807.      *
  808.      * @param string $data Data that will be passed to Solr Cell
  809.      * @param array $params optional array of key value pairs that will be sent with the post (see Solr Cell documentation)
  810.      * @param Apache_Solr_Document $document optional document that will be used to generate post parameters (literal.* and boost.* params)
  811.      * @param string $mimetype optional mimetype specification (for the file being extracted)
  812.      *
  813.      * @return Apache_Solr_Response 
  814.      *
  815.      * @throws Apache_Solr_InvalidArgumentException if $file, $params, or $document are invalid.
  816.      *
  817.      * @todo Should be using multipart/form-data to post parameter values, but I could not get my implementation to work. Needs revisisted.
  818.      */
  819.     public function extractFromString($data$params array()$document null$mimetype 'application/octet-stream')
  820.     {
  821.         $service $this->_selectWriteService();
  822.  
  823.         do
  824.         {
  825.             try
  826.             {
  827.                 return $service->extractFromString($data$params$document$mimetype);
  828.             }
  829.             catch (Apache_Solr_HttpTransportException $e)
  830.             {
  831.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  832.                 {
  833.                     throw $e;
  834.                 }
  835.             }
  836.  
  837.             $service $this->_selectWriteService(true);
  838.         while ($service);
  839.  
  840.         return false;
  841.     }
  842.     
  843.     /**
  844.      * Send an optimize command.  Will be synchronous unless both wait parameters are set
  845.      * to false.
  846.      *
  847.      * @param boolean $waitFlush 
  848.      * @param boolean $waitSearcher 
  849.      * @param float $timeout Maximum expected duration of the optimize operation on the server (otherwise, will throw a communication exception)
  850.      * @return Apache_Solr_Response 
  851.      *
  852.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  853.      */
  854.     public function optimize($waitFlush true$waitSearcher true$timeout 3600)
  855.     {
  856.         $service $this->_selectWriteService();
  857.  
  858.         do
  859.         {
  860.             try
  861.             {
  862.                 return $service->optimize($waitFlush$waitSearcher$timeout);
  863.             }
  864.             catch (Apache_Solr_HttpTransportException $e)
  865.             {
  866.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  867.                 {
  868.                     throw $e;
  869.                 }
  870.             }
  871.  
  872.             $service $this->_selectWriteService(true);
  873.         while ($service);
  874.  
  875.         return false;
  876.     }
  877.  
  878.     /**
  879.      * Simple Search interface
  880.      *
  881.      * @param string $query The raw query string
  882.      * @param int $offset The starting offset for result documents
  883.      * @param int $limit The maximum number of result documents to return
  884.      * @param array $params key / value pairs for query parameters, use arrays for multivalued parameters
  885.      * @param string $method The HTTP method (Apache_Solr_Service::METHOD_GET or Apache_Solr_Service::METHOD::POST)
  886.      * @return Apache_Solr_Response 
  887.      *
  888.      * @throws Apache_Solr_HttpTransportException If an error occurs during the service call
  889.      */
  890.     public function search($query$offset 0$limit 10$params array()$method Apache_Solr_Service::METHOD_GET)
  891.     {
  892.         $service $this->_selectReadService();
  893.  
  894.         do
  895.         {
  896.             try
  897.             {
  898.                 return $service->search($query$offset$limit$params$method);
  899.             }
  900.             catch (Apache_Solr_HttpTransportException $e)
  901.             {
  902.                 if ($e->getCode(!= 0//IF NOT COMMUNICATION ERROR
  903.                 {
  904.                     throw $e;
  905.                 }
  906.             }
  907.  
  908.             $service $this->_selectReadService(true);
  909.         while ($service);
  910.  
  911.         return false;
  912.     }
  913. }

Documentation generated on Wed, 04 May 2011 11:01:10 -0400 by phpDocumentor 1.4.3