/
view.rb
334 lines (286 loc) · 11.5 KB
/
view.rb
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
module StrokeDB
VIEW_CACHE = {}
VIEW_STORAGES = {}
View = Meta.new(:nsurl => STROKEDB_NSURL) do
DEFAULT_VIEW_OPTIONS = {
# Declare the size for a key to use optimized index file
# (size in bytes).
"key_size" => nil,
# By default, view index stores raw document's uuid as a value.
# If you need to store immediate value in the index file, you may
# override view.encode_value method.
# Set this option to a particular size (in bytes, for fixed-length data)
# or to <tt>false</tt> if the size is not fixed.
# Note: optimized storage is used when both keys and values are the fixed length.
# I.e. both "value_size" and "key_size" are set.
"value_size" => ::StrokeDB::Util::RAW_UUID_SIZE,
# strategy determines whether to index HEADs or particular versions
# When :heads is used, previous versions are removed from the index.
"strategy" => "heads", # heads|versions
}
on_new_document do |viewdoc|
viewdoc.reverse_update_slots(DEFAULT_VIEW_OPTIONS)
end
on_initialization do |viewdoc|
# pass viewdoc into initialization block:
# my_view = View.new(){ |view| ... }
if initialization_block = viewdoc.instance_variable_get(:@initialization_block) || initialization_block = VIEW_CACHE[viewdoc.uuid]
initialization_block.call(viewdoc)
end
viewdoc.store.register_view(viewdoc, viewdoc['only'])
end
after_save do |viewdoc|
VIEW_CACHE[viewdoc.uuid] = viewdoc.instance_variable_get(:@initialization_block)
end
DEFAULT_FIND_OPTIONS = {
:start_key => nil, # start search with a given prefix
:end_key => nil, # stop search with a given prefix
:limit => nil, # retrieve at most <N> entries
:offset => 0, # skip a given number of entries
:reverse => false, # reverse search direction and meaning of start_key & end_key
:key => nil, # prefix search (start_key == end_key)
:with_keys => false # returns [key, value] pairs instead of just values
}.stringify_keys.freeze
# Returns true if the index file is valid and will be used
# by #find method.
#
def index_exists?
storage.exists?
end
# Clear the index file (#index_exists? will give <tt>false</tt> then)
#
def clear!
storage.clear!
end
# Finds a set of values stored in the view filtered with the options used.
#
# * <tt>:start_key</tt>: Prefix to start a search with.
# * <tt>:end_key</tt>: Prefix to end a search with.
# * <tt>:key</tt>: Setting :key option is equivalent to set both <tt>:start_key</tt>
# and <tt>:end_key</tt> to the same value.
# By default, both keys are <tt>nil</tt> (and these are valid values).
#
# * <tt>:limit</tt>: Maximum number of items to be returned. Default is <tt>nil</tt> (no limit).
# * <tt>:offset</tt>: Skip a given number of items starting with <tt>:start_key</tt>.
# Default is <tt>0</tt> (skip nothing).
# * <tt>:reverse</tt>: Reverse the search direction. Search starts from the end of the
# index, goes to <tt>:start_key</tt> prefix and finishes before
# the <tt>:end_key</tt> value. This works like
# an <tt>ORDER BY ... DESC</tt> SQL statement.
# Default is <tt>false</tt> (i.e. direct search order).
# * <tt>:with_keys</tt>: Return a key-value pairs instead of just values.
# Default is <tt>false</tt>.
#
# Examples:
# view.find # returns all the items in a view
# view.find(:limit => 1) # returns the first document in a view
# view.find(:offset => 10, :limit => 1) # returns 11-th document in a view (Note: [doc] instead of doc)
# view.find(doc) # returns all items with a doc.uuid prefix
#
# # returns the latest 10 comments for a given document
# # (assuming the key defined as [comment.document, comment.created_at])
# has_many_comments.find(doc, :limit => 10, :reverse => true)
#
# comments.find([doc, 2.days.ago..Time.now]) # returns doc's comments within a time interval
# # example above is equivalent to:
# a) find(:key => [doc, 2.days.ago..Time.now])
# b) find(:start_key => [doc, 2.days.ago], :end_key => [doc, Time.now])
#
# Effectively, first argument sets :key option, which in turn
# specifies :start_key and :end_key.
# Low-level search is performed using :start_key and :end_key only.
#
def find(key = nil, options = {})
if !key || key.is_a?(Hash)
options = (key || {}).stringify_keys
else
options = (options || {}).stringify_keys
options['key'] ||= key
end
options = DEFAULT_FIND_OPTIONS.merge(options)
if key = options['key']
startk, endk = traverse_key(key)
options['start_key'] ||= !startk.blank? && startk || nil
options['end_key'] ||= !endk.blank? && endk || nil
end
start_key = options['start_key']
end_key = options['end_key']
limit = options['limit']
offset = options['offset']
reverse = options['reverse']
with_keys = options['with_keys']
ugly_find(start_key, end_key, limit, offset, reverse, with_keys)
end
# Traverses a user-friendly key in a #find method.
# Example:
# find(["prefix", 10..42, "sfx", "a".."Z" ])
# # => start_key == ["prefix", 10, "sfx", "a"]
# # => end_key == ["prefix", 42, "sfx", "Z"]
#
def traverse_key(key, sk = [], ek = [], s_inf = nil, e_inf = nil)
case key
when Range
a = key.begin
b = key.end
[
s_inf || a.infinite? ? sk : (sk << a),
e_inf || b.infinite? ? ek : (ek << b),
]
when Array
key.inject([sk, ek]) do |se, i|
traverse_key(i, se[0], se[1], s_inf, e_inf)
end
else
[s_inf ? sk : (sk << key), e_inf ? ek : (ek << key)]
end
end
# Ugly find accepts fixed set of arguments and works a bit faster,
# than a regular #find(options = {}) [probably insignificantly faster, TODO: check this]
# Some arguments can be nils.
#
def ugly_find(start_key, end_key, limit, offset, reverse, with_keys)
array = storage.find(start_key && encode_key(start_key),
end_key && encode_key(end_key),
limit,
offset,
reverse,
with_keys)
if with_keys
array.map do |ekey, evalue|
[ decode_key(ekey), decode_value(evalue) ]
end
else
array.map do |evalue|
decode_value(evalue)
end
end
end
# This is used by the storage to update index with a new version of document.
# Viewdoc contains a "strategy" slot, defining a strategy for index updates.
#
# * "heads" strategy removes a previous version from the index.
# * "versions" strategy just adds a new version to the index.
#
# See meta/papers/views.txt for more info.
#
def update(doc)
storage.set_options(:key_size => key_size,
:value_size => value_size)
if self['strategy'] == "heads"
update_head(doc)
else
update_version(doc)
end
# Way to optimize update! execution time (if it will matter)
# Please note that it will make persistent changes to a view instance object
# Here we go:
#
# if self["strategy"] == "heads"
# class << self
# alias_method :update, :update_head
# public :update
# end
# else
# class << self
# alias_method :update, :update_version
# public :update
# end
# end
#update(doc)
end
# Remove a previous version, add a new one.
#
def update_head(doc) #:nodoc
prev = doc.versions.previous or return update_version(doc)
old_pairs = map_with_encoding(prev.uuid, prev)
new_pairs = map_with_encoding(doc.uuid, doc)
storage.replace(old_pairs, new_pairs)
end
private :update_head
# Add a new version to the index.
#
def update_version(doc) #:nodoc
new_pairs = map_with_encoding(doc.uuid, doc)
storage.insert(new_pairs)
end
private :update_version
def map_with_encoding(key, value)
(map(key, value) || []).map do |k, v|
[encode_key(k), encode_value(v)]
end
end
private :map_with_encoding
def storage
# @storage ||= store.view_storages[self.uuid]
VIEW_STORAGES[uuid] ||= ViewStorage.new
end
private :storage
# These are defaults (to be overriden in View.new{|v| ... })
def map(key, value)
raise InvalidViewError, "#map method is not defined for a view #{self['name']}!"
end
def encode_key(json_key)
DefaultKeyEncoder.encode(json_key)
end
def decode_key(string_key)
DefaultKeyEncoder.decode(string_key)
end
# FIXME: for the "versions" strategy we must store UUID.VERSION
# instead of UUID only. Maybe, we should store UUID.VERSION always,
# or rely on some uuid_version method (which is different for
# Document and VersionedDocument)
# We should think about it.
def encode_value(value)
(value.is_a?(Document) ? value : RawData(value).save!).raw_uuid
end
def decode_value(evalue)
doc = self.store.find(evalue.to_formatted_uuid)
doc.is_a?(RawData) ? doc['data'] : doc
end
# By default, there's no split hinting
def split_by(json_key)
json_key
end
end
# Note: we don't simply do Views = View to avoid "Views" in a meta name.
# This class is for Views["name"] only.
class Views
def self.[](view_name)
View[view_name]
end
end
class << View
def [](*args) # FIXME: it is not nice
store = args.first.is_a?(Store) ? args.shift : StrokeDB.default_store
name = args[0]
nsurl = args[1] || (name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl)
uuid = ::StrokeDB::Util.sha1_uuid("view:#{nsurl}##{name}")
store.find(uuid)
end
alias :original_new :new
# Define a view.
#
# Examples
# View.new("view_name", :option => "value") do |viewdoc| ... end
# View.new(:name => "view_name", :option => "value") do |viewdoc| ... end
# View.new(store, "view_name", :option => "value") do |viewdoc| ... end
# View.new(store, :name => "view_name", :option => "value") do |viewdoc| ... end
#
def new(*args, &block)
store, name, options = extract(Store, String, Hash, args)
store ||= StrokeDB.default_store
options = options && options.stringify_keys || {}
name ||= options['name']
raise ArgumentError, "View name must be specified!" unless name
nsurl = options['nsurl'] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl # FIXME: it is not nice (and the same shit is in meta.rb)
options['uuid'] = ::StrokeDB::Util.sha1_uuid("view:#{nsurl}##{name}")
unless v = find(options['uuid'])
v = original_new(store, options, &block)
end
v
end
alias :define :new
alias :define! :create!
end
class InvalidViewError < StandardError ; end
end