querrel
1.0.0
Querrel 可以輕鬆地使用 ActiveRecord 並行(線程)查詢多個資料庫,就像查詢一個資料庫一樣。
將此行新增至應用程式的 Gemfile 中:
gem 'querrel'
然後執行:
$ bundle
或自己安裝:
$ gem install querrel
您可以透過 Querrel 的頂級命名空間直接使用它,或者透過建立一個實例來使用:
all_brands = Querrel . query ( Brand . all , on : [ 'db1' , 'db2' , 'db3' ] )
# is the same as
all_brands = Querrel . new ( [ 'db1' , 'db2' , 'db3' ] ) . query ( Brand . all )
上述任一方法都會為您提供所有給定資料庫中所有品牌物件的陣列。這些記錄將被標記為唯讀。
query
將產生一個帶有傳入的ActiveRecord::Relation
區塊,這允許您在合併結果之前對結果執行其他操作,例如您可以使用 pluck:
all_brand_names = q . query ( Brand . all ) do | s |
s . pluck ( :name )
end
還有一個run
方法,它不是運行預定的範圍和合併,而是採用一個區塊,允許您對指定的資料庫連接執行任何您想要的操作,例如:
require 'thread'
all_brands = [ ]
b_s = Mutex . new
all_products = [ ]
p_s = Mutex . new
Querrel . run ( on : dbs ) do | db |
b_s . synchronize { all_brands += Brand . all . to_a }
p_s . synchronize { all_products += Product . all . to_a }
end
您可以透過三種方式指示 Querrel 使用哪些資料庫:
傳入一組環境,例如Querrel.new([:customer1, :customer2])
傳入資料庫名稱數組,例如Querrel.new(['dbs/customer1.sqlite3', 'dbs/customer2.sqlite3'], db_names: true)
傳入命名連接配置的雜湊值,例如:
Querrel . new ( {
one : {
adapter : "sqlite3" ,
database : "test/dbs/test_db_1.sqlite3"
} ,
two : {
adapter : "sqlite3" ,
database : "test/dbs/test_db_2.sqlite3"
}
} )
預設情況下,Querrel 將使用最多 20 個線程,但您可以使用:threads
選項進行調整:
q = Querrel . new ( dbs , threads : 50 )
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)