Author: dr
Date: Tue Feb 26 11:18:50 2008
New Revision: 7446

Log:
- Added support for indexing transactions.

Modified:
    trunk/Search/src/handlers/solr.php
    trunk/Search/src/search_session.php
    trunk/Search/tests/handlers/solr_test.php
    trunk/Search/tests/session_test.php

Modified: trunk/Search/src/handlers/solr.php
==============================================================================
--- trunk/Search/src/handlers/solr.php [iso-8859-1] (original)
+++ trunk/Search/src/handlers/solr.php [iso-8859-1] Tue Feb 26 11:18:50 2008
@@ -28,6 +28,13 @@
     public $location;
 
     /**
+     * Stores the transaction nesting depth.
+     *
+     * @var integer
+     */
+    private $inTransaction;
+
+    /**
      * Creates a new Solr handler connection
      *
      * @param string
@@ -38,11 +45,42 @@
         $this->port = $port;
         $this->location = $location;
         $this->connection = @stream_socket_client( 
"tcp://{$this->host}:{$this->port}" );
+        $this->inTransaction = 0;
         if ( !$this->connection )
         {
             throw new ezcSearchCanNotConnectException( 'solr', 
"http://{$this->host}:{$this->port}{$this->location}" );
         }
 
+    }
+
+    /**
+     * Starts a transaction for indexing.
+     *
+     * When using a transaction, the amount of processing that solr does
+     * decreases, increasing indexing performance. Without this, the component
+     * sends a commit after every document that is indexed. Transactions can be
+     * nested, when commit() is called the same number of times as
+     * beginTransaction(), the component sends a commit.
+     */
+    public function beginTransaction()
+    {
+        $this->inTransaction++;
+    }
+
+    /**
+     * Ends a transaction and calls commit.
+     *
+     * As transactions can be nested, this method will only call commit when
+     * all the nested transactions have been ended.
+     */
+    public function commit()
+    {
+        $this->inTransaction--;
+
+        if ( $this->inTransaction == 0 )
+        {
+            $this->runCommit();
+        }
     }
 
     public function getLine( $maxLength = false )
@@ -270,6 +308,14 @@
         return $values;
     }
 
+    /**
+     * Runs a commit command to tell solr we're done indexing.
+     */
+    protected function runCommit()
+    {
+        $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
'<commit/>' );
+    }
+
     public function index( ezcSearchDocumentDefinition $definition, $document )
     {
         $xml = new XmlWriter();
@@ -296,7 +342,10 @@
         $doc = $xml->outputMemory( true );
 
         $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
$doc );
-        $r = $this->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
'<commit/>' );
+        if ( $this->inTransaction == 0 )
+        {
+            $this->runCommit();
+        }
     }
 
     public function createDeleteQuery()

Modified: trunk/Search/src/search_session.php
==============================================================================
--- trunk/Search/src/search_session.php [iso-8859-1] (original)
+++ trunk/Search/src/search_session.php [iso-8859-1] Tue Feb 26 11:18:50 2008
@@ -135,6 +135,30 @@
     }
 
     /**
+     * Starts a transaction for indexing.
+     *
+     * When using a transaction, the amount of processing that solr does
+     * decreases, increasing indexing performance. Without this, the component
+     * sends a commit after every document that is indexed. Transactions can be
+     * nested, when commit() is called the same number of times as
+     * beginTransaction(), the component sends a commit.
+     */
+    public function beginTransaction()
+    {
+        $this->handler->beginTransaction();
+    }
+
+    /**
+     * Ends a transaction and calls commit.
+     *
+     * As transactions can be nested, this method will only call commit when
+     * all the nested transactions have been ended.
+     */
+    public function commit()
+    {
+        $this->handler->commit();
+    }
+    /**
      * Indexes the new document $document to the search index.
      *
      * @throws ezcSearchException if $document

Modified: trunk/Search/tests/handlers/solr_test.php
==============================================================================
--- trunk/Search/tests/handlers/solr_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/handlers/solr_test.php [iso-8859-1] Tue Feb 26 11:18:50 
2008
@@ -102,7 +102,7 @@
         $r = $this->solr->sendRawPostCommand( 'update', array( 'wt' => 'json' 
), '<add><doc><field 
name="id">cfe5cc06-9b07-4e4b-930e-7e99f5202570</field><field 
name="name_s">solr</field></doc></add>' );
         $r = $this->solr->sendRawPostCommand( 'update', array( 'wt' => 'json' 
), '<commit/>' );
 
-        $r = $this->solr->search( 'solr', 'name_s', array( 'id', 'name_s', 
'score' ) );
+        $r = $this->solr->search( 'solr', 'name_s', array( 'id', 'name_s' ), 
array( 'id', 'name_s', 'score' ) );
         self::assertEquals( 1, $r->resultCount );
 
         $r = $this->solr->sendRawPostCommand( 'update', array( 'wt' => 'json' 
), '<delete><id>cfe5cc06-9b07-4e4b-930e-7e99f5202570</id></delete>' );

Modified: trunk/Search/tests/session_test.php
==============================================================================
--- trunk/Search/tests/session_test.php [iso-8859-1] (original)
+++ trunk/Search/tests/session_test.php [iso-8859-1] Tue Feb 26 11:18:50 2008
@@ -62,11 +62,12 @@
         $a = new Article( null, 'Test Article', 'This is an article to test', 
'the body of the article', time() );
 
         $session = new ezcSearchSession( $this->backend, new 
ezcSearchXmlManager( $this->testFilesDir ) );
+        $session->beginTransaction();
         for ( $i = 0; $i < 100; $i++ )
         {
             $session->index( $a );
         }
-        $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
'<commit/>' );
+        $session->commit();
 
         $r = $this->backend->search( 'Article', 'title_t' );
         self::assertEquals( 1, $r->resultCount );
@@ -81,7 +82,7 @@
         $session->index( $a );
         $this->backend->sendRawPostCommand( 'update', array( 'wt' => 'json' ), 
'<commit/>' );
 
-        $r = $this->backend->search( 'Rethans', 'author_t', array( 
'summary_t', 'title_t', 'body_t' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ) );
+        $r = $this->backend->search( 'Rethans', 'author_t', array( 
'summary_t', 'title_t', 'body_t' ), array( 'author_t', 'title_t', 'score', 
'summary_t', 'published_dt' ), array( 'author_t', 'title_t', 'score', 
'summary_t' ) );
         self::assertEquals( 1, $r->resultCount );
     }
 }


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to