Skip to content

Instantly share code, notes, and snippets.

View durran's full-sized avatar

Durran Jordan durran

View GitHub Profile
@durran
durran / moped.txt
Created February 16, 2012 10:59
First run perf numbers, Moped.
##################################################################
# ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.0.0]
# 10gen: mongo-1.5.2
# bson-1.5.2 (BSON::BSON_C)
##################################################################
user system total real
10gen: insert 10,000 blank documents 0.670000 0.060000 0.730000 ( 0.744400)
10gen: insert 10,000 blank documents safe mode 1.200000 0.140000 1.340000 ( 1.800714)
10gen: insert 1,000 normal documents 0.090000 0.010000 0.100000 ( 0.091035)
@durran
durran / inheritance.rb
Created February 15, 2012 13:54
Embedded documents with inheritance.
class Contact
include Mongoid::Document
embeds_one :email
embeds_one :phone
end
class Detail
include Mongoid::Document
end
@durran
durran / sample.rb
Created January 30, 2012 09:38
Sample Mongoid multi-connection/db/collection persistence.
class Band
include Mongoid::Document
store_in collection: "artists", database: "musik", session: "primary"
end
Band.with(collection: "bands").create
Band.new.with(session: "secondary").save!
Band.where(name: "Depeche Mode").with(database: "legends")
@durran
durran / sessions.rb
Created January 29, 2012 09:58
Flushing out Mongoid sessions API to Moped.
# encoding: utf-8
require "mongoid/sessions/factory"
module Mongoid #:nodoc:
module Sessions
extend ActiveSupport::Concern
included do
class_attribute :storage_options
end
@durran
durran / query.txt
Created January 13, 2012 12:38
Generated query...
Mongoid logged query:
MONGODB mongoid_sandbox['availability_updates'].find({"availabilities"=>{"$elemMatch"=>{"inventory_id"=>"EZ_2_NR", "dateranges"=>{"$elemMatch"=>{"to"=>{"$gte"=>2011-12-29 00:00:00 UTC}, "from"=>{"$lte"=>2011-12-29 23:59:59 UTC}}}}}}).skip(20).limit(10)
Kyle's gist logged query:
MONGODB mongoid_sandbox['availability_updates'].find({"availabilities"=>{"$elemMatch"=>{"inventory_id"=>"EZ_2_NR", "dateranges"=>{"$elemMatch"=>{"to"=>{"$gte"=>2011-12-29 00:00:00 UTC}, "from"=>{"$lte"=>2011-12-29 23:59:59 UTC}}}}}})
@durran
durran / sequence.rb
Created December 29, 2011 12:41
Integer sequence ids in Mongo
Mongoid.database.add_stored_function "sequence", <<-__
function(name) {
var ret = db.counters.findAndModify({ query: { _id: name}, update: { $inc : { next: 1}}, "new" :true, upsert: true});
return ret.next;
}
__
class Sequence
include Mongoid::Fields::Serializable
@durran
durran / time.rb
Created December 23, 2011 22:14
Customize Mongoid to persist times as integers +/- epoch.
module Windows
class Time
include Mongoid::Fields::Serializable
def deserialize(time)
Time.at(time)
end
def serialize(time)
time.to_i
@durran
durran / post-receive
Created December 14, 2011 10:05
Git deploy with Trinidad
#!/bin/sh
set -e
cd ..
unset GIT_DIR
env -i git reset --hard
/home/user/.rvm/bin/rvm jruby@app exec bundle install
touch /data/app/tmp/restart.txt
@durran
durran / trinidad
Created December 13, 2011 10:56
Trinidad init.d
#! /bin/sh
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Trinidad server"
NAME=trinidad
RVM=/home/vader/.rvm/bin/rvm
RVM_EXEC_ARGS="jruby@yoda exec"
RACK_DIR=/data/yoda
PIDFILE=/var/run/trinidad.pid
DAEMON=$RVM
@durran
durran / query.rb
Created December 9, 2011 09:40
ML query
# Get the count of notifications for this user
# and this post that need to be sent or have *not*
# been sent in the past 24 hours
User.
where({ _id: user._id, "notifs.post_id" => post_id }).
any_of(
{ "notifs.needs_send" => true },
{ "notifs.last_send" => { '$exists' => false }},
{ "notifs.last_send" => { "$lt" => 1.days.ago.utc }}