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.



8
9
10
11
12
13
14
15
16
# File 'lib/polipus/storage/mongo_store.rb', line 8

def initialize(options = {})
  @mongo      = options[:mongo]
  @collection = options[:collection]
  @mongo.create_collection(@collection)
  @mongo[@collection].ensure_index(:uuid, :unique => true, :drop_dups => true, :background => true)
  @compress_body = options[:compress_body] ||= true
  @except = options[:except] ||= []
  @semaphore = Mutex.new
end

Instance Method Details

#add(page) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/polipus/storage/mongo_store.rb', line 18

def add page
  @semaphore.synchronize {
    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

#clearObject



67
68
69
# File 'lib/polipus/storage/mongo_store.rb', line 67

def clear
  @mongo[@collection].drop
end

#countObject



54
55
56
# File 'lib/polipus/storage/mongo_store.rb', line 54

def count
  @mongo[@collection].count
end

#eachObject



58
59
60
61
62
63
64
65
# File 'lib/polipus/storage/mongo_store.rb', line 58

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

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/polipus/storage/mongo_store.rb', line 32

def exists?(page)
  @semaphore.synchronize {
    doc = @mongo[@collection].find({:uuid => uuid(page)}, {:fields => [:_id]}).limit(1).first
    !doc.nil?
  }
end

#get(page) ⇒ Object



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

def get page
  @semaphore.synchronize {
    data = @mongo[@collection].find({:uuid => uuid(page)}).limit(1).first
    if data
      return load_page(data)
    end
  }
end

#remove(page) ⇒ Object



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

def remove page
  @semaphore.synchronize {
    @mongo[@collection].remove({:uuid => uuid(page)})
  }
end