Skip to content

Instantly share code, notes, and snippets.

@selman
Created January 25, 2010 14:11
Show Gist options
  • Save selman/285884 to your computer and use it in GitHub Desktop.
Save selman/285884 to your computer and use it in GitHub Desktop.
=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
>> 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>]
>> 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
[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