Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kowsik
kowsik / gist:790044
Created January 21, 2011 17:32
total rows
{"total_rows":0,"offset":0,"rows":[]}
@kowsik
kowsik / gist:790038
Created January 21, 2011 17:27
Built-in reduce
map: function(doc) {
if (doc.type === 'queue') {
emit(null, 1);
}
},
reduce: '_sum'
@kowsik
kowsik / gist:790015
Created January 21, 2011 17:15
_bulk_delete
# Assuming the docs have created_at, delete everything older than
# 5 minutes. We assume an emit like this:
#
# emit(Date.parse(doc.created_at), doc._rev)
#
# Notice that the value in emitted kv pair is the doc's current _rev
old = (Time.now - 300).utc.to_i * 1000
opts = { :startkey => old, :descending => true }
@kowsik
kowsik / gist:789354
Created January 21, 2011 07:15
optimistic update_doc
def update_doc doc
loop do
yield doc
begin
save_doc doc
break
rescue RestClient::RequestFailed => e
if e.http_code == 409
doc = get doc['_id']
next
@kowsik
kowsik / gist:789343
Created January 21, 2011 07:03
include_docs
map: function(doc) {
if (something_or_the_other) {
emit(doc.attribute, doc);
}
}
@kowsik
kowsik / gist:789339
Created January 21, 2011 06:58
Monkey patch for RestClient
class RestClient::Request
Empty = {}.freeze
def default_headers
Empty
end
end
module RestAPI
Headers = { :content_type => :json }.freeze
def default_headers