Skip to content

Instantly share code, notes, and snippets.

@actsasflinn
Created July 22, 2009 04:16
Show Gist options
  • Save actsasflinn/151797 to your computer and use it in GitHub Desktop.
Save actsasflinn/151797 to your computer and use it in GitHub Desktop.
require 'tokyo_tyrant'
t = TokyoTyrant::Table.new('127.0.0.1', 1978)
100.times do |i|
t[i] = { 'name' => "Pat #{i}", 'sex' => i % 2 > 0 ? 'male' : 'female' }
end
q = t.query
q.condition('sex', :streq, 'male')
q.limit(5)
# Get a list of IDs
ids = q.search
# => ["1", "3", "5", "7", "9"]
q.order_by(:name, :strdesc)
ids = q.search
# => ["99", "97", "95", "93", "91"]
# Get the actual records
q.get
# => [{:__id=>"99", :sex=>"male", :name=>"Pat 99"}, {:__id=>"97", :sex=>"male", :name=>"Pat 97"}, {:__id=>"95", :sex=>"male", :name=>"Pat 95"}, {:__id=>"93", :sex=>"male", :name=>"Pat 93"}, {:__id=>"91", :sex=>"male", :name=>"Pat 91"}]
# Alternative Syntax (better)
# Query using a block
t.query{ |q|
q.condition('sex', :streq, 'male')
q.limit(5)
}
# => ["1", "3", "5", "7", "9"]
# Get records for a query
t.find{ |q|
q.condition('sex', :streq, 'male')
q.limit(5)
}
# => [{:sex=>"male", :name=>"Pat 1", :__id=>"1"}, {:sex=>"male", :name=>"Pat 3", :__id=>"3"}, {:sex=>"male", :name=>"Pat 5", :__id=>"5"}, {:sex=>"male", :name=>"Pat 7", :__id=>"7"}, {:sex=>"male", :name=>"Pat 9", :__id=>"9"}]
# Prepare but don't run a search, return a prepared query object
q = t.prepare_query{ |q|
q.condition('sex', :streq, 'male')
q.limit(5)
}
# => #<TokyoTyrant::Query:0x247c14 @rdb=#<Object:0x2800a0>, @rdbquery=#<Object:0x247c00>>
q.search
# => ["1", "3", "5", "7", "9"]
q.get
# => [{:sex=>"male", :name=>"Pat 1", :__id=>"1"}, {:sex=>"male", :name=>"Pat 3", :__id=>"3"}, {:sex=>"male", :name=>"Pat 5", :__id=>"5"}, {:sex=>"male", :name=>"Pat 7", :__id=>"7"}, {:sex=>"male", :name=>"Pat 9", :__id=>"9"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment