Class: Caster::Execution

Inherits:
Object
  • Object
show all
Defined in:
lib/caster/execution.rb

Overview

defines an execution scope which is a set of documents over which migration operations run

Instance Method Summary collapse

Constructor Details

#initialize(db, view, query, &block) ⇒ Execution

Returns a new instance of Execution.



16
17
18
19
20
21
22
# File 'lib/caster/execution.rb', line 16

def initialize db, view, query, &block
  @db = db
  @view = view
  @query = query
  @ref_docs_cache = {}
  @block = block
end

Instance Method Details

#add(field, value) ⇒ Object Also known as: update



24
25
26
# File 'lib/caster/execution.rb', line 24

def add field, value
  @operations << Operation.new(@db, Add.new(field, value))
end

#create(params) ⇒ Object



37
38
39
# File 'lib/caster/execution.rb', line 37

def create params
  @operations << Operation.new(@db, Create.new(params))
end

#create_on(db_handle, params) ⇒ Object



45
46
47
# File 'lib/caster/execution.rb', line 45

def create_on db_handle, params
  @operations << Operation.new(db_handle, Clone.new(params))
end

#deleteObject



41
42
43
# File 'lib/caster/execution.rb', line 41

def delete
  @operations << Operation.new(@db, Delete.new)
end

#executeObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/caster/execution.rb', line 62

def execute
  limit = @query['limit'] || 1.0/0.0
  if Caster.config['batch_size'] == nil or limit < Caster.config['batch_size']
    execute_batch db_query(@db, @view, @query)['rows']
    return
  end

  @query['limit'] = Caster.config['batch_size'] + 1
  saved_docs = 0
  while saved_docs < limit do
    docs = db_query(@db, @view, @query)['rows']
    return if docs.length == 0

    @query['startkey_docid'] = docs.last['id']
    @query['startkey'] = docs.last['key']

    if docs.length <= Caster.config['batch_size']
      execute_batch docs
      return
    elsif saved_docs + docs.length - 1 > limit
      execute_batch docs.slice(0, limit - saved_docs)
      return
    else
      docs.pop
      execute_batch docs
      saved_docs += docs.length
    end
  end
end

#from(scope, query = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/caster/execution.rb', line 49

def from scope, query = {}
  if scope.scan('/').length == 1
    view = scope
    db = @db
  else
    database_name, view = scope.split('/', 2)
    db = CouchRest.database "http://#{Caster.config['host']}:#{Caster.config['port']}/#{database_name}"
  end
  key = [scope, query]
  @ref_docs_cache[key] = db_query(db, view, query)['rows'].map { |rdoc| rdoc['doc'] || rdoc['value'] } unless @ref_docs_cache.has_key? key
  Reference.new @ref_docs_cache[key]
end

#remove(field) ⇒ Object



29
30
31
# File 'lib/caster/execution.rb', line 29

def remove field
  @operations << Operation.new(@db, Remove.new(field))
end

#rename(old_name, new_name) ⇒ Object



33
34
35
# File 'lib/caster/execution.rb', line 33

def rename old_name, new_name
  @operations << Operation.new(@db, Rename.new(old_name, new_name))
end