Skip to content

Instantly share code, notes, and snippets.

View ggoodale's full-sized avatar

Grant Goodale ggoodale

View GitHub Profile
db_config = YAML::load(File.read(RAILS_ROOT + "/config/database.yml"))
if db_config[Rails.env]
mongo = db_config[Rails.env]
mongo_host = mongo['host'] || 'localhost'
mongo_port = mongo['port'] || 27017
RAILS_DEFAULT_LOGGER.error("Connecting to #{mongo_host}:#{mongo_port}")
MongoMapper.connection = Mongo::Connection.new(mongo_host, mongo_port, :logger => RAILS_DEFAULT_LOGGER)
MongoMapper.database = File.basename(mongo['database']).split(".")[0]
end
@collection = db.collection('docs')
loop do
# Find a record to work on
new_record = @collection.find_one({:state => "NEW"}, {:sort => "created_at asc"})
# Set the state to my process id, but only if it's still in the "NEW" state
response = @collection.update({:_id => new_record.id, :state => "NEW"},
{'$set' => {:state => Process.pid}},
{:upsert => false, :safe => true})
@ggoodale
ggoodale / gist:300896
Created February 10, 2010 21:56
Same query with a date obj in the $gt clause.
> db.users.find({last_logged_in : {'$gt': new Date(0)}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : "Wed Dec 31 1969 16:00:00 GMT-0800 (PST)"
},
"endKey" : {
"last_logged_in" : "Tue Jan -2147483647 584556020 06:25:52 GMT-0800 (PST)"
},
"nscanned" : 8,
@ggoodale
ggoodale / gist:300880
Created February 10, 2010 21:40
Same query using $gt: 0
> db.users.find({last_logged_in : {'$gt': 0}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : 0
},
"endKey" : {
"last_logged_in" : 1.7976931348623157e+308
},
"nscanned" : 0,
@ggoodale
ggoodale / gist:300874
Created February 10, 2010 21:33
Using $gt instead of $ne
> db.users.find({last_logged_in : {'$gt': null}}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : null
},
"endKey" : {
"last_logged_in" : {
"$maxElement" : 1
}
@ggoodale
ggoodale / gist:300870
Created February 10, 2010 21:32
Same query, with a hint
> db.users.find({last_logged_in : {'$ne': null}}).hint({last_logged_in: 1}).explain()
{
"cursor" : "BtreeCursor last_logged_in_1",
"startKey" : {
"last_logged_in" : {
"$minElement" : 1
}
},
"endKey" : {
"last_logged_in" : {
@ggoodale
ggoodale / gist:300869
Created February 10, 2010 21:30
Explain() on a query in mongodb
> db.users.find({last_logged_in : {'$ne': null}}).explain()
{
"cursor" : "BasicCursor",
"startKey" : {
},
"endKey" : {
},
"nscanned" : 536299,
# Note that I have Rails 2.3.4 installed on this machine already.
[12] <ci> [01:17 PM] /opt/app>sudo gem install awesome_nested_set
Successfully installed activesupport-2.3.5
Successfully installed activerecord-2.3.5
Successfully installed awesome_nested_set-1.4.3
3 gems installed
Installing ri documentation for activesupport-2.3.5...
Installing ri documentation for activerecord-2.3.5...
Installing ri documentation for awesome_nested_set-1.4.3...
for repo in *
do
[ ! -d $repo ] && continue
[ ! -d $repo/.git ] && continue
cd $repo;
echo $repo;
git pull;
cd - > /dev/null
done
>> class Session < ActiveRecord::Base; end # So we can access the sessions table directly
=> nil
>> s = Session.first
=> #<Session id: 1, session_id: "bd0568033d143fafaa1f9752c43b61d7", data: "BAh7BiIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g...", created_at: "2008-12-10 15:50:58", updated_at: "2008-12-10 17:50:47">
>> session_data = Marshal.load(Base64::decode64(s.data))
=> {"flash" => {}}
>> Rails.cache.write("rack:session:#{s.session_id}", session_data)
=> true