yrashk / strokedb

StrokeDB is an embeddable distributed document database written in Ruby

This URL has Read+Write access

strokedb / lib / strokedb / views / view.rb
100644 284 lines (239 sloc) 9.64 kb
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
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" => 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
    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
    # view.find(:key => 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(:key => doc, :limit => 10, :reverse => true)
    #
    def find(options = {})
      options = DEFAULT_FIND_OPTIONS.merge(options.stringify_keys)
      
      start_key = options['start_key']
      end_key = options['end_key']
      key = options['key']
      limit = options['limit']
      offset = options['offset']
      reverse = options['reverse']
      with_keys = options['with_keys']
      
      ugly_find(start_key, end_key, key, limit, offset, reverse, with_keys)
    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, key, limit, offset, reverse, with_keys)
    
      array = storage.find(start_key && encode_key(start_key),
                           end_key && encode_key(end_key),
                           key && encode_key(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
    
    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 [](name, nsurl = Meta.default_nsurl) # FIXME: Meta seems to be a bad place for default_nsurl now
      uuid = ::Util.sha1_uuid("view:#{nsurl}##{name}")
      StrokeDB.default_store.find(uuid)
    end
 
    alias :original_new :new
    # Define a view.
    #
    # Examples
    # View.define("view_name", :option => "value") do |viewdoc| ... end
    # View.define(:name => "view_name", :option => "value") do |viewdoc| ... end
    #
    def new(*args, &block)
      if args.first.is_a? String
        options = args[1] || {}
        options['name'] = args.first
      else
        options = args[0] || {}
      end
      
      options = options.stringify_keys
      
      unless name = options['name']
        raise ArgumentError, "View name must be specified!"
      end
      
      nsurl = options['nsurl'] ||= Meta.default_nsurl # FIXME: Meta seems to be a bad place for default_nsurl now
      
      options['uuid'] = ::Util.sha1_uuid("view:#{nsurl}##{name}")
      
      unless v = find(options['uuid'])
        v = original_new(options, &block)
      end
      v
    end
    
    alias :define :new
    alias :define! :create!
    
  end
  
 
  class InvalidViewError < StandardError ; end
  
end