class Polipus::Storage::MongoStore

Constants

BINARY_FIELDS

Public Class Methods

new(options = {}) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 10
def initialize(options = {})
  @mongo      = options[:mongo]
  @collection = options[:collection]
  @mongo.create_collection(@collection)
  begin
    @mongo[@collection].ensure_index(:uuid, unique: true, dropDups: true, background: true)
  rescue Exception
  end

  @compress_body = options[:compress_body] ||= true
  @except = options[:except] ||= []
  @semaphore = Mutex.new
end

Public Instance Methods

add(page) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 24
def add(page)
  @semaphore.synchronize do
    obj = page.to_hash
    @except.each { |e| obj.delete e.to_s }
    obj['uuid'] = uuid(page)
    obj['body'] = Zlib::Deflate.deflate(obj['body']) if @compress_body && obj['body']
    BINARY_FIELDS.each do |field|
      obj[field] = BSON::Binary.new(obj[field]) unless obj[field].nil?
    end
    @mongo[@collection].update({ uuid: obj['uuid'] }, obj, upsert: true, w: 1)
    obj['uuid']
  end
end
clear() click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 71
def clear
  @mongo[@collection].drop
end
count() click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 58
def count
  @mongo[@collection].count
end
each() { |doc, page| ... } click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 62
def each
  @mongo[@collection].find({}, timeout: false) do |cursor|
    cursor.each do |doc|
      page = load_page(doc)
      yield doc['uuid'], page
    end
  end
end
exists?(page) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 38
def exists?(page)
  @semaphore.synchronize do
    doc = @mongo[@collection].find({ uuid: uuid(page) }, { fields: [:_id] }).limit(1).first
    !doc.nil?
  end
end
get(page) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 45
def get(page)
  @semaphore.synchronize do
    data = @mongo[@collection].find(uuid: uuid(page)).limit(1).first
    return load_page(data) if data
  end
end
remove(page) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 52
def remove(page)
  @semaphore.synchronize do
    @mongo[@collection].remove(uuid: uuid(page))
  end
end

Private Instance Methods

load_page(hash) click to toggle source
# File lib/polipus/storage/mongo_store.rb, line 77
def load_page(hash)
  BINARY_FIELDS.each do |field|
    hash[field] = hash[field].to_s
  end
  hash['body'] = Zlib::Inflate.inflate(hash['body']) if @compress_body && hash['body'] && !hash['body'].empty?
  page = Page.from_hash(hash)
  if page.fetched_at.nil?
    page.fetched_at = hash['_id'].generation_time.to_i
  end
  page
end