Created
May 21, 2009 20:20
-
-
Save spraints/115709 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To run this script, you need to install ruby, rubygems, the httparty gem, | |
# and any of the json gems. | |
require 'rubygems' | |
require 'httparty' | |
require 'json' | |
require 'pp' | |
class CouchDb | |
include HTTParty | |
format :json | |
base_uri 'http://localhost:5984/' | |
class << self | |
# Add some logging | |
[:post, :get, :delete, :put].each do |method| | |
define_method method do |*args| | |
puts "", "#{method}(#{args.inspect})" | |
result = super | |
pp result.delegate | |
#puts result.inspect | |
result | |
end | |
end | |
def temp_view(db, view_definition) | |
post(db + '/_temp_view', :body => view_definition.to_json, :headers => { 'Content-type' => 'application/json' }) | |
end | |
end | |
end | |
def urlencode(str) | |
str.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0]) } | |
end | |
EmployeeDb = '/employees_example' | |
BlogDb = '/blog_example' | |
CouchDb.get("/_all_dbs", :format => :txt) | |
CouchDb.delete(EmployeeDb) | |
CouchDb.put(EmployeeDb) | |
resp = CouchDb.put(EmployeeDb + '/gwashington', :body => {}.to_json) | |
CouchDb.post(EmployeeDb + '/gwashington', :body => | |
{:name => "George Washington", :rev => resp['rev'], :titles => [ | |
{"begin" => 1773, "end" => 1785, "title" => "Commander of the Continental Army"}, | |
{"begin" => 1789, "end" => 1797, "title" => "President of the United States"}, | |
{"begin" => 1798, "end" => 1799, "title" => "United States Army Senior Officer"} | |
]}.to_json) | |
CouchDb.put(EmployeeDb + '/jadams', :body => {:name => "John Adams", :titles => [ | |
{"begin" => 1789, "end" => 1797, "title" => "Vice President of the United States"}, | |
{"begin" => 1797, "end" => 1801, "title" => "President of the United States"} | |
]}.to_json) | |
puts "", "Default View" | |
CouchDb.temp_view(EmployeeDb, {:map => <<END_MAP}) | |
function(doc) { emit(null, doc); } | |
END_MAP | |
puts "", "Titles, by year" | |
by_year_map = <<END_MAP | |
function(doc) { | |
var i, title, year; | |
if(doc.titles && doc.name) | |
{ | |
for(i = 0; i < doc.titles.length; i++) | |
{ | |
title = doc.titles[i]; | |
for (year = title.begin; year <= title.end; year++) | |
{ | |
emit(year, {"name":doc.name, "begin":title.begin, "end": title.end, "title": title.title}); | |
} | |
} | |
} | |
} | |
END_MAP | |
CouchDb.temp_view(EmployeeDb, {:map => by_year_map}) | |
puts "", "Permanent Views" | |
CouchDb.put(EmployeeDb + '/_design/titles', :body => { | |
:language => 'javascript', | |
:views => { | |
:by_year => { | |
:map => by_year_map | |
} | |
} | |
}.to_json) | |
CouchDb.get(EmployeeDb + '/_design/titles/_view/by_year') | |
CouchDb.get(EmployeeDb + '/_design/titles/_view/by_year?key=1796') | |
CouchDb.delete(BlogDb) | |
CouchDb.put(BlogDb) | |
CouchDb.put(BlogDb + '/post1', :body => { | |
:type => 'post', | |
:title => 'My First Post!', | |
:content => 'Welcome to my blog! I\'ll tell you all about the stuff I do, day to day.' | |
}.to_json) | |
CouchDb.put(BlogDb + '/post2', :body => { | |
:type => 'post', | |
:title => 'Report', | |
:content => 'Today, I typed a bunch of TPS reports.' | |
}.to_json) | |
CouchDb.post(BlogDb + '/', :body => { | |
:type => 'comment', | |
:post_id => 'post1', | |
:title => 'Awesome!', | |
:content => 'I\'m totally looking forward to reading this blog!', | |
}.to_json) | |
CouchDb.post(BlogDb + '/', :body => { | |
:type => 'comment', | |
:post_id => 'post2', | |
:content => 'You had to do that too??!?!?! No freakin\' way!' | |
}.to_json) | |
CouchDb.post(BlogDb + '/', :body => { | |
:type => 'comment', | |
:post_id => 'post2', | |
:title => 'Good work!', | |
:content => 'I read your reports, and they are really top notch!' | |
}.to_json) | |
all_posts_map = <<END_MAP | |
function(doc) | |
{ | |
if(doc.type && doc.type == 'post') | |
{ | |
emit(doc._id, doc); | |
} | |
} | |
END_MAP | |
with_comments_map = <<END_MAP | |
function(doc) | |
{ | |
if(doc.type) | |
{ | |
if(doc.type == 'post') emit([doc._id, 0], doc); | |
if(doc.type == 'comment') emit([doc.post_id, 1], doc); | |
} | |
} | |
END_MAP | |
comments_per_post_map = <<END_MAP | |
function(doc) | |
{ | |
if(doc.type && doc.type == 'comment') | |
{ | |
emit(doc.post_id, 1); | |
} | |
} | |
END_MAP | |
comments_per_post_reduce = <<END_REDUCE | |
function(key, values, rereduce) | |
{ | |
return sum(values); | |
} | |
END_REDUCE | |
CouchDb.put(BlogDb + '/_design/posts', :body => { | |
:language => 'javascript', | |
:views => { | |
:all => { | |
:map => all_posts_map | |
}, | |
:with_comments => { | |
:map => with_comments_map | |
}, | |
:comments_per_post => { | |
:map => comments_per_post_map, | |
:reduce => comments_per_post_reduce | |
}, | |
} | |
}.to_json) | |
CouchDb.get(BlogDb + '/_design/posts/_view/all') | |
CouchDb.get(BlogDb + '/_design/posts/_view/with_comments?startkey=' + urlencode(['post2', 0].to_json) + '&endkey=' + urlencode(['post2', 1].to_json)) | |
CouchDb.get(BlogDb + '/_design/posts/_view/comments_per_post?group=true') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment