class Anemone::Storage::MongoDB
Constants
- BINARY_FIELDS
Public Class Methods
new(mongo_db, collection_name)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 14 def initialize(mongo_db, collection_name) @db = mongo_db @collection = @db[collection_name] @collection.remove @collection.create_index 'url' end
Public Instance Methods
[](url)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 21 def [](url) if value = @collection.find_one('url' => url.to_s) load_page(value) end end
[]=(url, page)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 27 def []=(url, page) hash = page.to_hash BINARY_FIELDS.each do |field| hash[field] = BSON::Binary.new(hash[field]) unless hash[field].nil? end @collection.update( {'url' => page.url.to_s}, hash, :upsert => true ) end
close()
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 73 def close @db.connection.close end
delete(url)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 39 def delete(url) page = self[url] @collection.remove('url' => url.to_s) page end
each() { |url.to_s, page| ... }
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 45 def each @collection.find do |cursor| cursor.each do |doc| page = load_page(doc) yield page.url.to_s, page end end end
has_key?(url)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 69 def has_key?(url) !!@collection.find_one('url' => url.to_s) end
keys()
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 63 def keys keys = [] self.each { |k, v| keys << k.to_s } keys end
merge!(hash)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 54 def merge!(hash) hash.each { |key, value| self[key] = value } self end
size()
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 59 def size @collection.count end
Private Instance Methods
load_page(hash)
click to toggle source
# File lib/anemone/storage/mongodb.rb, line 79 def load_page(hash) BINARY_FIELDS.each do |field| hash[field] = hash[field].to_s end Page.from_hash(hash) end