Datamapper 中多对多关系的策略性预加载?

发布于 2024-08-03 14:18:47 字数 325 浏览 5 评论 0原文

我正在使用 DataMapper,这是一个用于 ruby​​ 的开源 ORM,我很渴望想抓挠。目前,DataMapper 可以对一对多关系使用策略性预加载 (SEL),但不能对发生 N+1 查询的多对多关系使用。我想设法使其正常工作,但我找不到在哪里可以做到这一点。所以问题分为两部分:

  1. 如何运行测试套件,以便它显示此测试失败(注意,现在所有应该失败的规范都标记为待处理)?
  2. 在哪里以及如何为一对多关系实施 SEL?

I'm using DataMapper, an open source ORM for ruby, and I have in itch I would like to scratch. At the moment, DataMapper can use Strategic Eager Loading(SEL) for one-to-many relationships, but not many-to-many, where N+1 queries occur. I would like to hack around with making this work correctly, but I cannot find where to do it. So two part question:

  1. How to I run the test suite so it will show this to be failing (nb. right now all the specs that should be failing are marked as pending)?
  2. Where and how is SEL implemented for one-to-many relationships?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-08-10 14:18:47

对于第二个问题,您可以尝试深入研究代码:

/lib/dm-core/associations/relationship.rb

  # Eager load the collection using the source as a base
  #
  # @param [Collection] source
  #   the source collection to query with
  # @param [Query, Hash] query
  #   optional query to restrict the collection
  #
  # @return [Collection]
  #   the loaded collection for the source
  #
  # @api private
  def eager_load(source, query = nil)
    targets = source.model.all(query_for(source, query))

    # FIXME: cannot associate targets to m:m collection yet
    if source.loaded? && !source.kind_of?(ManyToMany::Collection)
      associate_targets(source, targets)
    end

    targets
  end

./lib/dm-core/associations/one_to_many.rb:

    def lazy_load(source)
      return if loaded?(source)

      # SEL: load all related resources in the source collection
      if source.saved? && (collection = source.collection).size > 1
        eager_load(collection)
      end

      unless loaded?(source)
        set!(source, collection_for(source))
      end
    end

For second question, you could try dive into code:

/lib/dm-core/associations/relationship.rb

  # Eager load the collection using the source as a base
  #
  # @param [Collection] source
  #   the source collection to query with
  # @param [Query, Hash] query
  #   optional query to restrict the collection
  #
  # @return [Collection]
  #   the loaded collection for the source
  #
  # @api private
  def eager_load(source, query = nil)
    targets = source.model.all(query_for(source, query))

    # FIXME: cannot associate targets to m:m collection yet
    if source.loaded? && !source.kind_of?(ManyToMany::Collection)
      associate_targets(source, targets)
    end

    targets
  end

./lib/dm-core/associations/one_to_many.rb:

    def lazy_load(source)
      return if loaded?(source)

      # SEL: load all related resources in the source collection
      if source.saved? && (collection = source.collection).size > 1
        eager_load(collection)
      end

      unless loaded?(source)
        set!(source, collection_for(source))
      end
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文