couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "RegeneratingViewsOnUpdate" by Sebastian Probst Eide
Date Mon, 28 Apr 2008 16:47:20 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by Sebastian Probst Eide:
http://wiki.apache.org/couchdb/RegeneratingViewsOnUpdate

The comment on the change is:
First version of the updater script in ruby

New page:
= Update views on document save =

CouchDB defaults to regenerating views the first time they are accessed. This behavior is
preferable in most cases as it optimizes the resource utilization on the database server.

On the other hand, in some situations the benefit of always having fast and updated views
far outweigh the cost of regenerating them every time the database server receives updates.
This can be achieved by supplying an updater script that calls the views when needed.

== Example using ruby ==

=== couch.ini ===
Add the following line to the couch.ini file {{{
	DbUpdateNotificationProcess=/PATH/TO/view_updater.rb
}}}

=== view_updater.rb ===
The following script updates the views for each tenth update made to the database or at most
once every second when a lot of saves are performed {{{
#!/usr/bin/ruby

###################
# CONF            #
###################

# The smallest amount of changed documents before the views are updated
MIN_NUM_OF_CHANGED_DOCS = 10

# URL to the DB on the CouchDB server
# Only supports one database at the moment as that is what I need
# Should be easy to improve for several databases if needed
URL = "http://localhost:5984/database_name"

# Set the minimum pause between calls to the database
PAUSE = 1 # seconds

# One for each design document
VIEWS = ["one_design_document/view_name",
         "other_design_document/other_view_name",
         "third_design_document/still_another_view_name"]
        



###################
# "MAIN LOOP"     #
###################

run = true
number_of_changed_docs = 0

threads = []

# Updates the views
threads << Thread.new do

  while run do

    if number_of_changed_docs >= MIN_NUM_OF_CHANGED_DOCS

      number_of_changed_docs = 0
      
      VIEWS.each do |view|
        `curl #{URL}/_view/#{view}?count=0`
      end

    end
    
    sleep PAUSE
    
  end
  
end

# Receives the update notification from CouchDB
threads << Thread.new do

  while run do

    update_call = gets
    
    # When CouchDB exits the script gets called with
    # a never ending series of nil
    if update_call == nil
      run = false
    else
      number_of_changed_docs += 1
    end
    
  end

end

# Good bye
threads.each {|thr| thr.join}

}}}

The view_updater.rb itself has to be made executable by CouchDB (chmod 0700?).

Mime
View raw message