Sha256: 37bb07217cb6b636033d6e60f9537a2b06e56226af43e73c519a7553e8cab898

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

module Soulmate

  class Loader < Base

    def load(items)
      # delete the sorted sets for this type
      # wrap in multi/exec?
      phrases = Soulmate.redis.smembers(base)
      phrases.each do |p|
        Soulmate.redis.del("#{base}:#{p}")
      end
      Soulmate.redis.del(base)

      # Redis can continue serving cached requests for this type while the reload is
      # occuring. Some requests may be cached incorrectly as empty set (for requests
      # which come in after the above delete, but before the loading completes). But
      # everything will work itself out as soon as the cache expires again.

      # delete the data stored for this type
      Soulmate.redis.del(database)

      items_loaded = 0
      items.each_with_index do |item, i|
        id    = item["id"]
        term  = item["term"]
        score = item["score"]

        if id and term
          # store the raw data in a separate key to reduce memory usage
          Soulmate.redis.hset(database, id, JSON.dump(item))

          prefixes_for_phrase(term).each do |p|
            Soulmate.redis.sadd(base, p) # remember this prefix in a master set
            Soulmate.redis.zadd("#{base}:#{p}", score, id) # store the id of this term in the index
          end
          items_loaded += 1
        end
        puts "added #{i} entries" if i % 100 == 0 and i != 0
      end

      items_loaded
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
soulmate-0.0.3 lib/soulmate/loader.rb
soulmate-0.0.2 lib/soulmate/loader.rb
soulmate-0.0.1 lib/soulmate/loader.rb
soulmate-0.0.0 lib/soulmate/loader.rb