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 17:32:30 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:
Improved the ruby script. It can now handle multiple databases.

------------------------------------------------------------------------------
  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"
+ URL = "http://localhost:5984"
  
  # Set the minimum pause between calls to the database
  PAUSE = 1 # seconds
  
- # One for each design document
+ # One entry for each design document 
- VIEWS = ["one_design_document/view_name",
-          "other_design_document/other_view_name",
-          "third_design_document/still_another_view_name"]
+ # in each database
+ VIEWS = {"DATABASE_NAME"  => ["list_of/design_documents",
+                               "another/design_document"],
+          "resepies"       => ["category/most_popular"],
+          "ingredients"    => ["by/price"]}        
          
  
- 
- 
  ###################
- # "MAIN LOOP"     #
+ # RUNTIME         #
  ###################
  
  run = true
- number_of_changed_docs = 0
+ number_of_changed_docs = {}
  
  threads = []
  
@@ -53, +51 @@

  
    while run do
  
+     number_of_changed_docs.each_pair do |db_name, number_of_docs|
-     if number_of_changed_docs >= MIN_NUM_OF_CHANGED_DOCS
+       if number_of_docs >= MIN_NUM_OF_CHANGED_DOCS
+         
+         # Reset the value
+         number_of_changed_docs[db_name] = 0
+         
+         # If there are views in the database, get them
+         if VIEWS[db_name]
+           VIEWS[db_name].each do |view|
+             `curl #{URL}/#{db_name}/_view/#{view}?count=0`
+           end  
+         end
+                 
+       end
+     end
  
+     # Pause before starting over again
-       number_of_changed_docs = 0
-       
-       VIEWS.each do |view|
-         `curl #{URL}/_view/#{view}?count=0`
-       end
- 
-     end
-     
      sleep PAUSE
      
    end
@@ -74, +79 @@

  
    while run do
  
+     puts "Waiting for input:"
      update_call = gets
      
      # When CouchDB exits the script gets called with
@@ -81, +87 @@

      if update_call == nil
        run = false
      else
+       
+       # Get the database name out of the call data
+       # The data looks somethind like this:
+       # {"type":"updated","db":"DB_NAME"}\n
+       update_call =~ /\"db\":\"(\w+)\"/
+       database_name = $1
+       
+       # Set to 0 if it hasn't been initialized before
+       number_of_changed_docs[$1] ||= 0
+       
+       # Add one pending changed document to the list of documents
+       # in the DB
-       number_of_changed_docs += 1
+       number_of_changed_docs[$1] += 1
+       
      end
      
    end

Mime
View raw message