stack depth

15 views
Skip to first unread message

Volatility

unread,
Oct 16, 2012, 9:50:04 PM10/16/12
to mongo...@googlegroups.com
Hello, I'm wondering if there is something fundamentally wrong with the model design below.  When we try to add 1,000 instances of the History embedded document to the history collection in the document, the spec test receives a "stack level too deep" at about 100 items.   

require 'mongomodel'

module MyModule

  # Provides a model of game player constructs.
  class Player < MongoModel::Document

    # Provides the history for the player.
    class History < MongoModel::EmbeddedDocument

      property :instance_id, String, :unique => true, :required => true
      property :when_started, Date, :default => Date.today
      property :play_duration, Integer, :default => 0
      property :play_metrics, String

      belongs_to :history, :class => Player
    end

    property :player_id, String, :index => true, :unique => true, :required => true
    property :level, String
    property :preferences, String
    property :history, Collection[History], :default => History.new

    timestamps!

    validates_presence_of :player_id

    end

end

To add each individual history item the following is done in a modular Sinatra application.  To add 1,000 items we call this 1,000 times.

        player = MyModule::Player.find_by_player_id(player_id)

        if player.nil?
          error 404, "Player not found: " + player_id
        end

        # create and populate a new instance
        body = Yajl::Parser.parse(request.body.read)
        values = body['history'][0]

        hi = MyModule::Player::History.new
        hi.instance_id = values['instance_id']
        ...set other fields...

        player.history << hi
        player.save!


This seems to work fine for the first few dozen entries, but fails later.  Ruby 1.8.7 on Mac OS X.  Is there something missing from the model or is the update done incorrectly?

  





Sam

unread,
Oct 16, 2012, 10:03:34 PM10/16/12
to mongo...@googlegroups.com
What you have looks mostly right, but there's a few things I'd suggest which may help:

1. The belongs_to in the History class (should that be belongs_to :player?) should not be necessary. Instead you can use the parent_document method within the History object to access the player who owns the History object.

2. The default value for the history property in Player should probably be an empty array, i.e. [].

If these suggestions don't work, could you post the full stack trace?

Thanks,
Sam

Volatility

unread,
Oct 17, 2012, 7:48:53 PM10/17/12
to mongo...@googlegroups.com
Thanks for you help.  We've made both changes and improved the model.  We still have the stack issue however.  I've posted a backtrace (exception.backtrace text file attachment) deep in save embedded callbacks.  
backtrace.txt
Reply all
Reply to author
Forward
0 new messages