| a | b | |
|---|
| 0 | | - | require 'rubygems' |
|---|
| 0 | | - | require 'relaxdb' |
|---|
| 0 | | - | |
|---|
| 0 | | - | # Specify the db to be used. Create it in CouchDB yourself locally. |
|---|
| 0 | | - | |
|---|
| 0 | | - | RelaxDB::Database.set_std_db :host => "localhost", :port => 5984, :db => "photos" |
|---|
| 0 | | - | |
|---|
| 0 | | - | # |
|---|
| 0 | | - | # Define the classes |
|---|
| 0 | | - | # |
|---|
| 0 | | - | |
|---|
| 0 | | - | class Photo < RelaxDB::Document |
|---|
| 0 | | - | |
|---|
| 0 | | - | property :name |
|---|
| 0 | | - | property :tags |
|---|
| 0 | | - | |
|---|
| 0 | | - | end |
|---|
| 0 | | - | |
|---|
| 0 | | - | class Tag < RelaxDB::Document |
|---|
| 0 | | - | |
|---|
| 0 | | - | property :name |
|---|
| 0 | | - | property :photos |
|---|
| 0 | | - | |
|---|
| 0 | | - | end |
|---|
| 0 | | - | |
|---|
| 0 | | - | # |
|---|
| 0 | | - | # Create the views |
|---|
| 0 | | - | # |
|---|
| 0 | | - | |
|---|
| 0 | | - | tags_view = <<MAP_FUNC |
|---|
| 0 | | - | function(doc) { |
|---|
| 0 | | - | if(doc.class == "Tag" && doc.photos) { |
|---|
| 0 | | - | var i; |
|---|
| 0 | | - | for(i = 0; i < doc.photos.length; i++) { |
|---|
| 0 | | - | emit(doc.photos[i], doc); |
|---|
| 0 | | - | } |
|---|
| 0 | | - | } |
|---|
| 0 | | - | } |
|---|
| 0 | | - | MAP_FUNC |
|---|
| 0 | | - | |
|---|
| 0 | | - | photos_view = <<MAP_FUNC |
|---|
| 0 | | - | function(doc) { |
|---|
| 0 | | - | if(doc.class == "Photo" && doc.tags) { |
|---|
| 0 | | - | var i; |
|---|
| 0 | | - | for(i = 0; i < doc.tags.length; i++) { |
|---|
| 0 | | - | emit(doc.tags[i], doc); |
|---|
| 0 | | - | } |
|---|
| 0 | | - | } |
|---|
| 0 | | - | } |
|---|
| 0 | | - | MAP_FUNC |
|---|
| 0 | | - | |
|---|
| 0 | | - | DesignDocument.get(Photo).add_view_to_data("all_tags", tags_view).save |
|---|
| 0 | | - | DesignDocument.get(Tag).add_view_to_data("all_photos", photos_view).save |
|---|
| 0 | | - | |
|---|
| 0 | | - | # |
|---|
| 0 | | - | # Insert some data |
|---|
| 0 | | - | # |
|---|
| 0 | | - | |
|---|
| 0 | | - | Photo.destroy_all! |
|---|
| 0 | | - | Tag.destroy_all! |
|---|
| 0 | | - | |
|---|
| 0 | | - | mg = Photo.new(:_id => "mg", :name => "Migration").save |
|---|
| 0 | | - | des = Photo.new(:_id => "des", :name => "Dar es Salaam").save |
|---|
| 0 | | - | |
|---|
| 0 | | - | wd = Tag.new(:_id => "wd", :name => "wildebeest").save |
|---|
| 0 | | - | tz = Tag.new(:_id => "tz", :name => "tanzania").save |
|---|
| 0 | | - | |
|---|
| 0 | | - | # Being forced to use ids and set the relationship on both sides is clunky and error |
|---|
| 0 | | - | # prone, but it's no more than a current limitation of the library |
|---|
| 0 | | - | |
|---|
| 0 | | - | mg.tags = [wd._id, tz._id] |
|---|
| 0 | | - | des.tags = [tz._id] |
|---|
| 0 | | - | |
|---|
| 0 | | - | wd.photos = [mg._id] |
|---|
| 0 | | - | tz.photos = [mg._id, des._id] |
|---|
| 0 | | - | |
|---|
| 0 | | - | RelaxDB.bulk_save(mg, des, wd, tz) |
|---|
| 0 | | - | |
|---|
| 0 | | - | # |
|---|
| 0 | | - | # Examine the data, confirm in the CouchDB log that n+1 requests aren't being made |
|---|
| 0 | | - | # |
|---|
| 0 | | - | |
|---|
| 0 | | - | mg = Photo.all_by(:name) { |q| q.key = "Migration"}[0] |
|---|
| 0 | | - | mg_tags = Photo.view("all_tags?key=\"#{mg._id}\"") |
|---|
| 0 | | - | puts mg_tags.inject("Tags for #{mg.name}") { |m, t| m << " '#{t.name}'" } |
|---|
| 0 | | - | |
|---|
| 0 | | - | tz = Tag.all_by(:name) { |q| q.key = "tanzania" }[0] |
|---|
| 0 | | - | tz_photos = Tag.view("all_photos?key=\"#{tz._id}\"") |
|---|
| 0 | | - | puts tz_photos.inject("Photos for #{tz.name}") { |m, p| m << " '#{p.name}'" } |
|---|
| 0 | + | 4jcXow <a href="http://onozijbhtwri.com/">onozijbhtwri</a>, [url=http://oozgvgfvifok.com/]oozgvgfvifok[/url], [link=http://vfofaawzccey.com/]vfofaawzccey[/link], http://mgqcvamnixry.com/ |
|---|
| ... | |
|---|