views:

42

answers:

2

I would like to determine the number of records that a query on a Tokyo Cabinet Table will return before I run the query. I am using the rufus-tokyo Ruby gem as my interface. What is the best way to do this?

+1  A: 

Looking thorough the code on github, I think I found the answer. It would use db#query_count method which returns a record count. Here's an example of paging records using 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
Jamal Hansen
+1  A: 

The mailing list for rufus-tokyo is at http://groups.google.com/group/rufus-ruby

jmettraux