Class: Polipus::Storage::MongoStore

Inherits:
Base
  • Object
show all
Defined in:
lib/polipus/storage/mongo_store.rb

Constant Summary collapse

BINARY_FIELDS =
%w(body headers data)

Instance Attribute Summary

Attributes inherited from Base

#include_query_string_in_uuid

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MongoStore

Returns a new instance of MongoStore.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/polipus/storage/mongo_store.rb', line 11

def initialize(options = {})
  @mongo      = options[:mongo]
  @collection = options[:collection]
  begin
    @mongo[@collection].ensure_index(:uuid, unique: true, dropDups: true, background: true)
  rescue StandardError
  end

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

Instance Method Details

#add(page) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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].force_encoding('UTF-8').encode('UTF-8')) unless obj[field].nil?
    end

    # We really need 2.0.6+ version for this to work
    # https://jira.mongodb.org/browse/RUBY-881
    @mongo[@collection].find(uuid: uuid(page)).replace_one(obj, upsert: true)

    obj['uuid']
  end
end

#clearObject



75
76
77
# File 'lib/polipus/storage/mongo_store.rb', line 75

def clear
  @mongo[@collection].drop
end

#countObject



62
63
64
# File 'lib/polipus/storage/mongo_store.rb', line 62

def count
  @mongo[@collection].find.count
end

#eachObject



66
67
68
69
70
71
72
73
# File 'lib/polipus/storage/mongo_store.rb', line 66

def each
  @mongo[@collection].find.no_cursor_timeout do |cursor|
    cursor.each do |doc|
      page = load_page(doc)
      yield doc['uuid'], page
    end
  end
end

#exists?(page) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/polipus/storage/mongo_store.rb', line 42

def exists?(page)
  @semaphore.synchronize do
    doc = @mongo[@collection].find(uuid: uuid(page)).projection(_id: 1).limit(1).first
    !doc.nil?
  end
end

#get(page) ⇒ Object



49
50
51
52
53
54
# File 'lib/polipus/storage/mongo_store.rb', line 49

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) ⇒ Object



56
57
58
59
60
# File 'lib/polipus/storage/mongo_store.rb', line 56

def remove(page)
  @semaphore.synchronize do
    @mongo[@collection].find(uuid: uuid(page)).delete_one
  end
end