确定rufus-tokyo查询返回的记录数的最佳方法是什么?

时间:2010-02-21 18:58:55

标签: ruby tokyo-cabinet

我想确定在运行查询之前,Tokyo Cabinet Table上的查询将返回的记录数。我使用rufus-tokyo Ruby gem作为我的界面。这样做的最佳方式是什么?

1 个答案:

答案 0 :(得分:1)

仔细查看github上的代码,我想我找到了答案。它将使用db#query_count方法返回记录计数。以下是使用query_count分页记录的示例:

  db = Rufus::Tokyo::Table.new(@file)

  begin
    # Need to find the total number of records that would be returned.
    # could use db#size, except that our query filters out records after today.

    total_count = db.query_count { |q|
      q.add_condition 'date', :numle, @today
      q.order_by 'date', :strdesc
    }

    pager = IndexCards::Pager.new requested_page_num, total_count

    # Now let's pull the slice that we want.
    results = db.query { |q|
      q.add_condition 'date', :numle, @today
      q.order_by 'date', :strdesc
      q.limit(pager.limit, pager.offset)
    }
  ensure
    db.close
  end
相关问题