Revision d70e66fc1177 () - Diff

Link to this snippet: https://friendpaste.com/2jTD1QBPqQvTi3KW8uMtnJ
Embed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#
# CouchDB membership views, two approaches. Sho Fukamachi
# You will need to install edge DataMapper to run this
#

require 'rubygems'
require 'dm-core'

# Specify the db to be used. Create it in CouchDB yourself locally.

DataMapper.setup(:default, Addressable::URI.parse("couchdb://localhost:5984/phototest2"))

# create classes

class Tag
include DataMapper::Resource
property :id, String, :key => true, :field => :_id
property :rev, String, :field => :_rev
property :name, String
property :photos_cache, JsonObject, :default => {}
view :all, { "map" => "function(doc) { if (doc.type == 'tag') { emit(null, doc); } }" }
view :by_name, { "map" => "function(doc) { if (doc.type == 'tag') { emit(doc.name, doc); } }" }

view :by_photo_id, { "map" => " function(doc) {
if(doc.type == 'tag' && doc.photos_cache) {
for (i in doc.photos_cache) {
emit(i, doc);
}
}
}"}
view :by_photo_name, { "map" => " function(doc) {
if(doc.type == 'tag' && doc.photos_cache) {
for (i in doc.photos_cache) {
emit(doc.photos_cache[i]['photo_name'], doc);
}
}
}"}
def photo_array
Membership.photos_for_tag(self.id).first.value
end
def hash_for_cache
{:tag_name => self.name, :tag_id => self.id, :tag_rev => self.rev}
end

end

class Photo
include DataMapper::Resource
property :id, String, :key => true, :field => :_id
property :rev, String, :field => :_rev
property :name, String
property :tags_cache, JsonObject, :default => {}
property :test, JsonObject
view :all, { "map" => "function(doc) { if (doc.type == 'photo') { emit(null, doc); } }" }
view :by_name, { "map" => "function(doc) { if (doc.type == 'photo') { emit(doc.name, doc); } }" }
view :by_tag_id, { "map" => " function(doc) {
if(doc.type == 'photo' && doc.tags_cache) {
for (i in doc.tags_cache) {
emit(i, doc);
}
}
}"}
view :by_tag_name, { "map" => " function(doc) {
if(doc.type == 'photo' && doc.tags_cache) {
for (i in doc.tags_cache) {
emit(doc.tags_cache[i]['tag_name'], doc);
}
}
}"}
def hash_for_cache
{:photo_name => self.name, :photo_id => self.id, :photo_rev => self.rev}
end
def tags_array
Membership.tags_for_photo(self.id).first.value
end
def apply_tag(tag)
Membership.tag_photo(self, tag)
end
end

class Membership
include DataMapper::Resource
property :id, String, :key => true, :field => :_id
property :rev, String, :field => :_rev
property :tag_id, String
property :tag_name, String
property :tag_rev, String
property :photo_id, String
property :photo_name, String
property :photo_rev, String

after :save, :populate_caches

view :all, { "map" => "function(doc) { if (doc.type == 'membership') { emit(null, doc); } }" }
view :existence_check, { "map" => "function(doc) { if (doc.type == 'membership') { emit([doc.photo_id, doc.tag_id], doc); } }" }
view :tags_for_photo_by_photo_id, {
"map" => "function(doc) { if (doc.type == 'membership') { emit(doc.photo_id, doc); } }",
"reduce" => "function(keys, vals) {
results = [];
for (var i in vals)
{
results.push(vals[i]['tag_id']);
}
return results;
}"
}
view :photos_for_tag_by_tag_id, {
"map" => "function(doc) { if (doc.type == 'membership') { emit(doc.tag_id, doc); } }" ,
"reduce" => "function(keys, vals) {
results = [];
for (var i in vals)
{
results.push(vals[i]['photo_id']);
}
return results;
}"
}

view :tags_for_photo_by_photo_name, {
"map" => "function(doc) { if (doc.type == 'membership') { emit(doc.photo_name, doc); } }",
"reduce" => "function(keys, vals) {
results = [];
for (var i in vals)
{
results.push(vals[i]['tag_name']);
}
return results;
}"
}
view :photos_for_tag_by_tag_name, {
"map" => "function(doc) { if (doc.type == 'membership') { emit(doc.tag_name, doc); } }" ,
"reduce" => "function(keys, vals) {
results = [];
for (var i in vals)
{
results.push(vals[i]['photo_name']);
}
return results;
}"
}


def assemble_hash_for_caches(remote_hash)
remote_hash[:membership_id] = self.id
remote_hash[:membership_rev] = self.rev
remote_hash
end

def populate_caches
photo = Photo.get(self.photo_id)
tag = Tag.get(self.tag_id)
tag.photos_cache.delete(photo.id)
photo.tags_cache.delete(tag.id)
tag.photos_cache[photo.id] = assemble_hash_for_caches(photo.hash_for_cache)
photo.tags_cache[tag.id] = assemble_hash_for_caches(tag.hash_for_cache)
tag.save
photo.save
end
def self.tag_photo(photo, tag)
print "trying to apply tag #{tag.name} to photo #{photo.name} .."
if self.existence_check([photo.id, tag.id]).blank?
hash = photo.hash_for_cache.merge(tag.hash_for_cache)
m = Membership.new(hash).save
print "done"
else
print "tag exists, skipping"
end
puts
end

def self.renew_all_caches
Photo.all.each do |p|
p.tags_cache = {}
p.save
end
Tag.all.each do |t|
t.photos_cache = {}
t.save
end
Membership.all.each do |m|
m.populate_caches
end
end

end


#
# Create the views
#

Photo.auto_migrate!
Tag.auto_migrate!
Membership.auto_migrate!


#
# Insert some data
#


mg = Photo.create(:id => "mg", :name => "Migration")
des = Photo.create(:id => "des", :name => "Dar es Salaam")
syd = Photo.create(:id => "syd", :name => "Sydney")
tok = Photo.create(:id => "tok", :name => "Tokyo")

wd = Tag.create(:id => "wd", :name => "wildebeest")
tz = Tag.create(:id => "tz", :name => "tanzania")
city = Tag.create(:id => "city", :name => "City")
night = Tag.create(:id => "night", :name => "Night View")
au = Tag.create(:id => "au", :name => "Australia")
jp = Tag.create(:id => "tz", :name => "Japan")

Membership.tag_photo(mg, wd)
Membership.tag_photo(des, tz)
Membership.tag_photo(syd, city)
Membership.tag_photo(tok, city)
Membership.tag_photo(des, night)
Membership.tag_photo(tok, night)
Membership.tag_photo(syd, au)
Membership.tag_photo(tok, jp)
Membership.tag_photo(des, city)


#
# Make various requests using the different methods, confirm that they all work
#

# first up, using the memberships views and the cached information therein

puts "perform some lookups using membership views"

tagged_city = Membership.photos_for_tag_by_tag_name('City').first.value.join(', ')
puts "Photos tagged 'city': #{tagged_city}"
tagged_night = Membership.photos_for_tag_by_tag_name('Night View').first.value.join(', ')
puts "Photos tagged 'Night View': #{tagged_night}"
tags_for_des = Membership.tags_for_photo_by_photo_name('Dar es Salaam').first.value.join(', ')
puts "Tags for photo 'Dar es Salaam': #{tags_for_des}"


# now try and do the same via in-document caches

puts "perform some lookups using in-record caches"

tagged_city = Photo.by_tag_name('City').collect {|x| x.name}.join(', ')
puts "Photos tagged 'city': #{tagged_city}"
tagged_night = Photo.by_tag_name('Night View').collect {|x| x.name}.join(', ')
puts "Photos tagged 'Night View': #{tagged_night}"
tags_for_des = Tag.by_photo_name('Dar es Salaam').collect {|x| x.name}.join(', ')
puts "Tags for photo 'Dar es Salaam': #{tags_for_des}"

puts 'done!'