Class: Rhoconnect::SourceSync
- Inherits:
-
Object
- Object
- Rhoconnect::SourceSync
- Defined in:
- lib/rhoconnect/source_sync.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
Instance Method Summary collapse
-
#async(job_type, queue_name, params = nil) ⇒ Object
Enqueue a job for the source based on job type.
-
#create ⇒ Object
CUD Operations.
- #delete ⇒ Object
- #do_cud ⇒ Object
- #do_query(params = nil) ⇒ Object
- #fast_delete(delete_objs, timeout = 10, raise_on_expire = false) ⇒ Object
- #fast_insert(new_objs, timeout = 10, raise_on_expire = false) ⇒ Object
- #fast_update(orig_hash, new_hash, timeout = 10, raise_on_expire = false) ⇒ Object
-
#initialize(source) ⇒ SourceSync
constructor
A new instance of SourceSync.
-
#pass_through_cud(cud_params, query_params) ⇒ Object
Pass through CUD to adapter, no data stored.
- #process_cud ⇒ Object
- #process_query(params = nil) ⇒ Object
- #push_deletes(objects, timeout = 10, raise_on_expire = false, rebuild_md = true) ⇒ Object
- #push_objects(objects, timeout = 10, raise_on_expire = false, rebuild_md = true) ⇒ Object
-
#read(client_id = nil, params = nil) ⇒ Object
Read Operation; params are query arguments.
- #search(client_id = nil, params = nil) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(source) ⇒ SourceSync
Returns a new instance of SourceSync.
5 6 7 8 9 10 |
# File 'lib/rhoconnect/source_sync.rb', line 5 def initialize(source) @source = source raise InvalidArgumentError.new('Invalid source') if @source.nil? raise InvalidArgumentError.new('Invalid app for source') unless @source.app @adapter = SourceAdapter.create(@source) end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
3 4 5 |
# File 'lib/rhoconnect/source_sync.rb', line 3 def adapter @adapter end |
Instance Method Details
#async(job_type, queue_name, params = nil) ⇒ Object
Enqueue a job for the source based on job type
108 109 110 111 112 |
# File 'lib/rhoconnect/source_sync.rb', line 108 def async(job_type,queue_name,params=nil) SourceJob.queue = queue_name Resque.enqueue(SourceJob,job_type,@source.id, @source.app_id,@source.user_id,params) end |
#create ⇒ Object
CUD Operations
13 14 15 |
# File 'lib/rhoconnect/source_sync.rb', line 13 def create _measure_and_process_cud('create') end |
#delete ⇒ Object
21 22 23 |
# File 'lib/rhoconnect/source_sync.rb', line 21 def delete _measure_and_process_cud('delete') end |
#do_cud ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/rhoconnect/source_sync.rb', line 75 def do_cud return if _auth_op('login') == false self.create self.update self.delete _auth_op('logoff') end |
#do_query(params = nil) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rhoconnect/source_sync.rb', line 91 def do_query(params=nil) result = nil @source.if_need_refresh do Rhoconnect::Stats::Record.update("source:query:#{@source.name}") do if _auth_op('login') result = self.read(nil,params) _auth_op('logoff') end # re-wind refresh time in case of error query_failure = Store.exists?(@source.docname(:errors)) @source.rewind_refresh_time(query_failure) end end result end |
#fast_delete(delete_objs, timeout = 10, raise_on_expire = false) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/rhoconnect/source_sync.rb', line 129 def fast_delete(delete_objs, timeout=10,raise_on_expire=false) @source.lock(:md,timeout,raise_on_expire) do |s| diff_count = -delete_objs.size @source.delete_data(:md, delete_objs) @source.update_count(:md_size,diff_count) end end |
#fast_insert(new_objs, timeout = 10, raise_on_expire = false) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/rhoconnect/source_sync.rb', line 114 def fast_insert(new_objs, timeout=10,raise_on_expire=false) @source.lock(:md,timeout,raise_on_expire) do |s| diff_count = new_objs.size @source.put_data(:md, new_objs, true) @source.update_count(:md_size,diff_count) end end |
#fast_update(orig_hash, new_hash, timeout = 10, raise_on_expire = false) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/rhoconnect/source_sync.rb', line 122 def fast_update(orig_hash, new_hash, timeout=10,raise_on_expire=false) @source.lock(:md,timeout,raise_on_expire) do |s| @source.delete_data(:md, orig_hash) @source.put_data(:md, new_hash, true) end end |
#pass_through_cud(cud_params, query_params) ⇒ Object
Pass through CUD to adapter, no data stored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rhoconnect/source_sync.rb', line 26 def pass_through_cud(cud_params,query_params) return if _auth_op('login') == false res,processed_objects = {},[] begin ['create','update','delete'].each do |op| key,objects = op,cud_params[op] objects.each do |key,value| case op when 'create' @adapter.send(op.to_sym,value) when 'update' value['id'] = key @adapter.send(op.to_sym,value) when 'delete' value['id'] = key @adapter.send(op.to_sym,value) end processed_objects << key end if objects end rescue Exception => e log "Error in pass through method: #{e.}" res['error'] = {'message' => e. } end _auth_op('logoff') res['processed'] = processed_objects res.to_json end |
#process_cud ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/rhoconnect/source_sync.rb', line 67 def process_cud if @source.cud_queue or @source.queue async(:cud,@source.cud_queue || @source.queue) else do_cud end end |
#process_query(params = nil) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/rhoconnect/source_sync.rb', line 83 def process_query(params=nil) if @source.query_queue or @source.queue async(:query,@source.query_queue || @source.queue,params) else do_query(params) end end |
#push_deletes(objects, timeout = 10, raise_on_expire = false, rebuild_md = true) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rhoconnect/source_sync.rb', line 164 def push_deletes(objects,timeout=10,raise_on_expire=false,rebuild_md=true) @source.lock(:md,timeout,raise_on_expire) do |s| diff_count = 0 if(rebuild_md) # in case of rebuild_md # we clean-up and rebuild the whole :md doc # on every request doc = @source.get_data(:md) orig_doc_size = doc.size objects.each do |id| doc.delete(id) end diff_count = doc.size - orig_doc_size @source.put_data(:md,doc) else # if rebuild_md == false # we only operate on specific set values # which brings a big optimization # in case of small transactions diff_count = -@source.remove_objects(:md, objects) end @source.update_count(:md_size,diff_count) end end |
#push_objects(objects, timeout = 10, raise_on_expire = false, rebuild_md = true) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/rhoconnect/source_sync.rb', line 137 def push_objects(objects,timeout=10,raise_on_expire=false,rebuild_md=true) @source.lock(:md,timeout,raise_on_expire) do |s| diff_count = 0 # in case of rebuild_md # we clean-up and rebuild the whole :md doc # on every request if(rebuild_md) doc = @source.get_data(:md) orig_doc_size = doc.size objects.each do |id,obj| doc[id] ||= {} doc[id].merge!(obj) end diff_count = doc.size - orig_doc_size @source.put_data(:md,doc) else # if rebuild_md == false # we only operate on specific set values # which brings a big optimization # in case of small transactions diff_count = @source.update_objects(:md, objects) end @source.update_count(:md_size,diff_count) end end |
#read(client_id = nil, params = nil) ⇒ Object
Read Operation; params are query arguments
56 57 58 |
# File 'lib/rhoconnect/source_sync.rb', line 56 def read(client_id=nil,params=nil) _read('query',client_id,params) end |
#search(client_id = nil, params = nil) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/rhoconnect/source_sync.rb', line 60 def search(client_id=nil,params=nil) return if _auth_op('login',client_id) == false res = _read('search',client_id,params) _auth_op('logoff',client_id) res end |
#update ⇒ Object
17 18 19 |
# File 'lib/rhoconnect/source_sync.rb', line 17 def update _measure_and_process_cud('update') end |