Brent Dillingham wrote:
> Hi all,
>
> For fun, I'm writing a MUD with Ruby. I thought I'd query the hive
> mind here and ask some opinions on what library I might use for object
> persistence -- that is, storing objects like players and rooms
> somewhere.
>
> I could do something really simple like store everything in YAML, I
> could try DRb, or I could go all out and use an ORM like DataMapper...
> DataMapper is the first route I took, but I soon realized that I'll
> have to be really careful when using a lot of the ORM niceties while
> still ensuring that a database row only every represents one object in
> memory. i.e.:
>
> # assuming Player has_one :room ...
>
> player = Player.get(1)      # get Player with ID=1
> player.room                 # the player's current Room association
>
> ... player logs out, logs back in later ...
>
> player.room
>
> Oh noes! The ORM fetches the same room from the DB, but creates a
> totally new object to represent it. Two players could be in the same
> room ID, but actually reference different objects, and thus couldn't
> see or interact with one another. Bad.
>
> This problem is hackable by carefully keeping a cache of loaded
> objects -- but might end up negating the value of using an ORM like
> DataMapper. So, any suggestions out there of what I might do instead?
>
> Thanks! ^_^

I am working on a similar project. I started off using PStore but 
switched to using Marshal and gdbm, which is quite a bit faster. But, in 
either case, objects are not read and written to/from disk all the time. 
Most of the time they are in memory and only occasionally saved (a 
player would be saved when logging out, for example). To avoid the 
situation you mentioned, just keep track of which objects are in memory 
(give each a unique ID) and then only load them if they are not already 
loaded.
If keeping everything in memory doesn't sound like a good plan, you can 
also dynamically load/unload objects as needed.

I guess it just depends on how you want to approach it, though. I don't 
have a strong need for any kind of fancy database functions, so storing 
them with gdbm works fine for me.

-Justin