Created
January 25, 2010 14:11
-
-
Save selman/285884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=begin | |
datamapper threaded repository sample with multiple mysql databases | |
modelling mysql => User table can be used administering mysql | |
=end | |
require 'dm-core' | |
Servers = { | |
:mysql1 => 'mysql1.foo.com', | |
:mysql2 => 'mysql2.foo.com', | |
:mysql3 => 'mysql3.foo.com', | |
:mysql4 => 'mysql4.foo.com', | |
:mysql5 => 'mysql5.foo.com', | |
:mysql6 => 'mysql6.foo.com', | |
:mysql7 => 'mysql7.foo.com', | |
:mysql8 => 'mysql8.foo.com' | |
} | |
DataMapper::Logger.new(STDOUT, :debug) | |
class User | |
include DataMapper::Resource | |
Servers.each do |s,d| | |
storage_names[s.to_sym] = 'user' | |
end | |
property :host, String, :field => 'Host', :default => 'localhost' | |
property :user, String, :field => 'User', :required => true, :key => true | |
property :password, String, :field => 'Password', :required => true | |
before :save do | |
self[:password] = self.repository.adapter.select("select password('#{self[:password]}')")[0] | |
end | |
after :save do | |
self.repository.adapter.execute('flush privileges') | |
end | |
end | |
Servers.each do |s, d| | |
DataMapper.setup(s.to_sym, "mysql://root:****@#{d}/mysql") | |
end | |
def searchUserLike(query) | |
thr = Array.new | |
Servers.each do |s, d| | |
thr << Thread.new do | |
repository(s.to_sym) do | |
user = User.all(:user.like => "#{query}") | |
user.each {|u| puts u[:user] << '=>' << u.repository.name} if user | |
end | |
end | |
end | |
thr.each { |t| t.join} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> searchUserLike('test%') | |
test_modul=>mysql1 | |
testuser=>mysql2 | |
test55=>mysql4 | |
testasarim-com=>mysql7 | |
test=>mysql5 | |
testturk=>mysql5 | |
testturkticaret=>mysql5 | |
=> [#<Thread:0x000000023eb440 dead>, #<Thread:0x000000023eae90 dead>, #<Thread:0x000000023eabb8 dead>, #<Thread:0x000000023ea6b0 dead>, #<Thread:0x000000023ea2c0 dead>, #<Thread:0x000000026a55a0 dead>, #<Thread:0x000000026a5450 dead>, #<Thread:0x000000026a5300 dead>] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> repository(:mysql5) {user = User.new(:user => 'selman', :password => 'selman'); user.save} | |
=> true | |
>> repository(:mysql5) {user = User.get('selman'); puts user[:user] << ' => ' << user[:password]} | |
selman => *095F8DE298368D30B240ED629533891525F59444 | |
=> nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[slmn@gabriel projects]$ irb -r dmmultitheaded.rb | |
>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment