Module: CouchPillow::MultiDBDirective
- Included in:
- Document
- Defined in:
- lib/couchpillow/directives/multi_db.rb
Instance Method Summary collapse
- #_default_db ⇒ Object
- #_secondary_dbs ⇒ Object
- #_threads ⇒ Object
- #_write_to_secondary_dbs(&block) ⇒ Object
-
#db(conn) ⇒ Object
Set a DB connection.
-
#wait ⇒ Object
Blocks until all pending tasks has completed.
Instance Method Details
#_default_db ⇒ Object
25 26 27 |
# File 'lib/couchpillow/directives/multi_db.rb', line 25 def _default_db @default_db ||= (@primary_db || CouchPillow.db) end |
#_secondary_dbs ⇒ Object
30 31 32 |
# File 'lib/couchpillow/directives/multi_db.rb', line 30 def _secondary_dbs @secondary_dbs ||= [] end |
#_threads ⇒ Object
46 47 48 |
# File 'lib/couchpillow/directives/multi_db.rb', line 46 def _threads @threads ||= [] end |
#_write_to_secondary_dbs(&block) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/couchpillow/directives/multi_db.rb', line 35 def _write_to_secondary_dbs &block unless _secondary_dbs.empty? _threads << Thread.new do _secondary_dbs.each do |db| block.call(db) end end end end |
#db(conn) ⇒ Object
Set a DB connection. Overrides the default CouchPillow.db connection for the first time this method gets called. Subsequent calls will set secondary connections, which will only be used for write only.
Example:
db primary_db # use for both read and write
db backup_db1 # write only
db backup_db2 # write only
14 15 16 17 18 19 20 21 22 |
# File 'lib/couchpillow/directives/multi_db.rb', line 14 def db conn # set the primary db connection @primary_db ||= conn # insert as backup db connections if conn && @primary_db && conn != @primary_db _secondary_dbs << conn end end |
#wait ⇒ Object
Blocks until all pending tasks has completed. Returns the result of those tasks in an array.
54 55 56 57 58 59 60 61 62 |
# File 'lib/couchpillow/directives/multi_db.rb', line 54 def wait result = [] until _threads.empty? f = _threads.shift result << f.value end result end |