lib/picky/bundle_realtime.rb in picky-4.30.0 vs lib/picky/bundle_realtime.rb in picky-4.31.0

- old
+ new

@@ -7,10 +7,12 @@ # each specific backends. # # Removes the given id from the indexes. # + # TODO Simplify (and slow) this again – remove the realtime index. + # def remove id # Is it anywhere? # str_or_syms = @realtime[id] @@ -44,37 +46,52 @@ # Returns a reference to the array where the id has been added. # # Does not add to realtime if static. # + # TODO What does static do again? + # TODO Why the realtime index? Is it really necessary? + # Not absolutely. It was for efficient deletion/replacement. + # def add id, str_or_sym, method: :unshift, static: false, force_update: false # If static, indexing will be slower, but will use less # space in the end. # if static ids = @inverted[str_or_sym] ||= [] - ids.send method, id unless ids.include? id + if force_update + ids.delete id + ids.send method, id + else + # TODO Adding should not change the array if it's already in. + # + if ids.include?(id) + # Do nothing. Not forced, and already in. + else + ids.send method, id + end + end else # Use a generalized strategy. # str_or_syms = (@realtime[id] ||= []) # (static ? nil : [])) # Inverted. # - ids = if str_or_syms && str_or_syms.include?(str_or_sym) + ids = if str_or_syms.include?(str_or_sym) ids = @inverted[str_or_sym] ||= [] - # If updates are not forced, then do not add it to the - # index if it's in there already. - unless force_update - return if ids.include? id + # If updates are forced or if it isn't in there already + # then remove and add to the index. + if force_update || !ids.include?(id) + ids.delete id + ids.send method, id end - ids.delete id - ids.send method, id + ids else # Update the realtime index. # - str_or_syms << str_or_sym # unless static + str_or_syms << str_or_sym # TODO Add has_key? to index backends. # ids = if @inverted.has_key?(str_or_sym) # @inverted[str_or_sym] # else # @inverted[str_or_sym] = [] @@ -127,14 +144,15 @@ # # Note: Experimental feature. Might be removed in 5.0. # # THINK Maybe load it and just replace the arrays with the corresponding ones. # - def build_realtime + def build_realtime symbol_keys clear_realtime @inverted.each_pair do |str_or_sym, ids| ids.each do |id| - str_or_syms = @realtime[id] ||= [] + str_or_syms = (@realtime[id] ||= []) + str_or_sym = str_or_sym.to_sym if symbol_keys @realtime[id] << str_or_sym unless str_or_syms.include? str_or_sym end end end