You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by eh...@apache.org on 2007/01/27 16:26:15 UTC

svn commit: r500545 - /lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb

Author: ehatcher
Date: Sat Jan 27 07:26:15 2007
New Revision: 500545

URL: http://svn.apache.org/viewvc?view=rev&rev=500545
Log:
SOLR-120: Add persistent HTTP connections. (contributed by Coda Hale).

Also, some additional commenting and cleanup.


Modified:
    lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb

Modified: lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb?view=diff&rev=500545&r1=500544&r2=500545
==============================================================================
--- lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb (original)
+++ lucene/solr/trunk/client/ruby/solrb/lib/solr/connection.rb Sat Jan 27 07:26:15 2007
@@ -14,7 +14,7 @@
 
 module Solr
   class Connection
-    attr_reader :url, :autocommit
+    attr_reader :url, :autocommit, :connection
    
     # create a connection to a solr instance using the url for the solr
     # application context:
@@ -37,7 +37,10 @@
       # only calls to Connection#add/#update/#delete, though a Connection#send(AddDocument.new(...))
       # does not autocommit.  Maybe #send should check for the request types that require a commit and
       # commit in #send instead of the individual methods?
-      @autocommit = opts[:autocommit] == :on ? true : false
+      @autocommit = opts[:autocommit] == :on
+      
+      # Not actually opening the connection yet, just setting up the persistent connection.
+      @connection = Net::HTTP.new(@url.host, @url.port)
     end
 
     # add a document to the index. you can pass in either a hash
@@ -87,6 +90,16 @@
       response = send(Solr::Request::Commit.new)
       return response.ok?
     end
+    
+    # TODO add optimize, which can be hacked like this, interestingly!
+    # class OptimizeRequest
+    #  def handler 
+    #    "update"
+    #  end
+    #  def to_s
+    #    "<optimize/>"
+    #  end
+    # end
 
     # pings the connection and returns true/false if it is alive or not
     def ping
@@ -122,12 +135,9 @@
     # send the http post request to solr; for convenience there are shortcuts
     # to some requests: add(), query(), commit(), delete() or send()
     def post(request)
-      post = Net::HTTP::Post.new(@url.path + "/" + request.handler)
-      post.body = request.to_s
-      post.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
-      response = Net::HTTP.start(@url.host, @url.port) do |http|
-        http.request(post)
-      end
+      response = @connection.post(@url.path + "/" + request.handler,
+                                  request.to_s,
+                                  { "Content-Type" => "application/x-www-form-urlencoded; charset=utf-8" })
       
       case response
       when Net::HTTPSuccess then response.body