lib/ruote/couch/database.rb in ruote-couch-2.1.11 vs lib/ruote/couch/database.rb in ruote-couch-2.2.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
@@ -20,13 +20,11 @@
# THE SOFTWARE.
#
# Made in Japan.
#++
-require 'cgi'
-
module Ruote::Couch
#
# A database corresponds to a Couch database (not a Couch server).
#
@@ -34,11 +32,10 @@
# expressions, ...)
#
class Database
attr_reader :type
-
attr_reader :couch
def initialize (host, port, type, name, opts={})
@couch = Rufus::Jig::Couch.new(host, port, name, opts)
@@ -82,15 +79,18 @@
# The get_many used by msgs, configurations and variables.
#
def get_many (key, opts)
- return query('_all_docs?include_docs=true') if ( ! key) && opts.size < 1
+ if key.nil? && opts.empty?
+ return @couch.all(:include_design_docs => false)
+ end
is = ids
return is.length if opts[:count]
+ return [] if is.empty?
is = is.reverse if opts[:descending]
if key
keys = Array(key).map { |k| k.is_a?(String) ? "!#{k}" : k }
@@ -100,26 +100,21 @@
skip = opts[:skip] || 0
limit = opts[:limit] || is.length
is = is[skip, limit]
- query_by_post('_all_docs?include_docs=true', is)
+ #return [] if is.empty?
+ # not necessary at this point, jig provides already this optimization.
- # TODO
- # maybe _count come be of use here
- # http://wiki.apache.org/couchdb/Built-In_Reduce_Functions
+ @couch.all(:keys => is)
end
# Returns a sorted list of the ids of all the docs in this database.
#
def ids
- @couch.get('_all_docs')['rows'].collect { |r|
- r['id']
- }.reject { |i|
- DESIGN_DOC_REGEX.match(i)
- }
+ @couch.ids(:include_design_docs => false)
end
def dump
s = "=== #{@type} ===\n"
@@ -141,71 +136,32 @@
# Deletes all the documents in this database.
#
def purge!
- @couch.http.cache.clear
- @couch.get('_all_docs')['rows'].each do |row|
- next if row['id'].match(/^\_design\//)
- doc = { '_id' => row['id'], '_rev' => row['value']['rev'] }
- @couch.delete(doc)
- end
+ #@couch.http.cache.clear
+ #@couch.all(
+ # :include_docs => false, :include_design_docs => false
+ #).each do |doc|
+ # @couch.delete(doc)
+ #end
+ docs = @couch.all(:include_docs => false, :include_design_docs => false)
+ @couch.bulk_delete(docs)
#
# which is faster than
#
#@couch.delete('.')
#@couch.put('.')
#@couch.http.cache.clear
end
protected
+ # Well, empty. Nothing to do for a index-less database
+ #
def prepare
-
- # nothing to do for a index-less database
end
-
- # These options are known and passed to CouchDB.
- #
- QUERY_OPTIONS = [ :skip, :limit, :descending ]
-
- # :limit and :skip support
- #
- def query_options (opts)
-
- opts = opts.select { |k, v| QUERY_OPTIONS.include?(k) && v != nil }
-
- s = opts.collect { |k, v| "#{k}=#{v}" }.join('&')
-
- s.length > 0 ? "&#{s}" : ''
- end
-
- # Used by #get_many and #ids when filtering design documents out of
- # '_all_docs'.
- #
- DESIGN_DOC_REGEX = /^\_design\//
-
- def filter_design_docs (docs)
-
- docs.reject { |d| DESIGN_DOC_REGEX.match(d['_id']) }
- end
-
- def query (uri)
-
- rs = @couch.get(uri)
-
- filter_design_docs(rs['rows'].collect { |e| e['doc'] })
- end
-
- def query_by_post (uri, keys)
-
- keys = { 'keys' => keys }
-
- rs = @couch.post(uri, keys)
-
- filter_design_docs(rs['rows'].collect { |e| e['doc'] }.uniq)
- end
end
#
# A Couch database with a by_wfid view.
#
@@ -217,18 +173,17 @@
return super(key, opts) unless key.is_a?(String)
# key is a wfid
- query("_design/ruote/_view/by_wfid?key=%22#{key}%22&include_docs=true")
+ @couch.query_for_docs('ruote:by_wfid', :key => key)
end
# Used by WorkitemDatabase#query
#
def by_wfid (wfid)
- #get_many(/!#{wfid}$/, {})
get_many(wfid, {})
end
# Returns the design document that goes with this class of database
#
@@ -265,39 +220,51 @@
d ||= design_doc
d['views'] = design_doc['views']
@couch.put(design_doc)
end
+
+ #--
+ #def try (&block)
+ # try = 0
+ # begin
+ # try = try + 1
+ # block.call
+ # rescue Rufus::Jig::HttpError => he
+ # raise he
+ # rescue
+ # retry unless try > 1
+ # end
+ #end
+ # keeping it frozen for now
+ #++
end
#
# A Couch database with a by_wfid view and a by_field view.
#
class WorkitemDatabase < WfidIndexedDatabase
- # This method is called by CouchStorage#by_field
+ # This method is called by Storage#by_field
#
def by_field (field, value=nil, opts={})
field = { field => value } if value
- field = CGI.escape(Rufus::Json.encode(field))
- query(
- "_design/ruote/_view/by_field?key=#{field}" +
- "&include_docs=true#{query_options(opts)}")
+ @couch.query_for_docs(
+ 'ruote:by_field', opts.merge(:key => field))
end
- # This method is called by CouchStorage#by_participant
+ # This method is called by Storage#by_participant
#
def by_participant (name, opts={})
- query(
- "_design/ruote/_view/by_participant_name?key=%22#{name}%22" +
- "&include_docs=true#{query_options(opts)}")
+ @couch.query_for_docs(
+ 'ruote:by_participant_name', opts.merge(:key => name))
end
- # This method is called by CouchStorage#query
+ # This method is called by Storage#query
#
def query_workitems (criteria)
offset = criteria.delete('offset')
limit = criteria.delete('limit')
@@ -316,12 +283,10 @@
cr = criteria.collect { |fname, fvalue| { fname => fvalue } }
opts = { :skip => offset, :limit => limit }
- hwis = query_by_post(
- "_design/ruote/_view/by_field?include_docs=true#{query_options(opts)}",
- cr)
+ hwis = @couch.query_for_docs('ruote:by_field', opts.merge(:keys => cr))
hwis = hwis.select { |hwi| hwi['fei']['wfid'] == wfid } if wfid
hwis.select { |hwi|
Ruote::StorageParticipant.matches?(hwi, pname, criteria)