From: m_seki@... Date: 2003-08-08T07:50:20+09:00 Subject: Re: [DRb] Using DRb to implement object database Hello, Steve. Do you want it? This database pass a root object by reference. --- foo.rb --- class Foo def initialize(x, y) @x = x @y = y end attr_accessor :x, :y end --- db.rb --- require 'pstore' require 'drb/drb' require 'drb/timeridconv' require 'foo' class Database < PStore include DRbUndumped def [](key) if Thread.current['DRb'] return DRbObject.new(super) else super end end end store = Database.new('test.db') DRb.install_id_conv(DRb::TimerIdConv.new) DRb.start_service('druby://localhost:12345', store) DRb.thread.join --- client.rb --- require 'drb/drb' require 'foo' def main DRb.start_service db = DRbObject.new_with_uri('druby://localhost:12345') db.transaction do db['foo'] = Foo.new(1, 2) unless db.root?('foo') end db.transaction do foo = db['foo'] # pass by reference foo.x = foo.x + 1 end db.transaction do foo = db['foo'] p [foo.x, foo.y, foo.class] end end main --- client2.rb --- require 'drb/drb' require 'foo' def main DRb.start_service db = DRbObject.new_with_uri('druby://localhost:12345') db.transaction do db['foo'] = Foo.new(1, 2) unless db.root?('foo') db['bar'] = Foo.new(db['foo'], 2) unless db.root?('bar') end db.transaction do bar = db['bar'] # pass by reference p bar p bar.x # pass by value bar.x.x = bar.x.x + 1 # can't change bar.x.x bar.y = bar.y + 1 # can change bar.y end db.transaction do bar = db['bar'] p [bar.x, bar.y, bar.class] end end main