Search code examples
rubytokyo-cabinet

What is the best way to determine the count of records that will be returned by a query with rufus-tokyo?


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?


Solution

  • 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