class Mongar::Mongo::Collection

Attributes

logger[R]
name[R]
replica[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/mongar/mongo/collection.rb, line 6
def initialize(args = {})
  @name = args[:name]
  @replica = args[:replica]
  @logger = args[:logger] || Logger.new(nil)
  @last_logged_activity = nil
end

Public Instance Methods

collection() click to toggle source
# File lib/mongar/mongo/collection.rb, line 25
def collection
  database[name]
end
connection() click to toggle source
# File lib/mongar/mongo/collection.rb, line 17
def connection
  mongodb.connection!
end
create(document) click to toggle source
# File lib/mongar/mongo/collection.rb, line 91
def create(document)
  log_activity
  
  logger.debug "#{name}.create #{document.inspect}"
  !collection.insert(document, { :safe => true }).nil?
end
create_or_update(key, document) click to toggle source
# File lib/mongar/mongo/collection.rb, line 112
def create_or_update(key, document)
  log_activity
  
  logger.debug "#{name}.create_or_update #{key.inspect} with #{document.inspect}"
  
  collection.update(key, document, {:upsert => true, :safe => true})
end
database() click to toggle source
# File lib/mongar/mongo/collection.rb, line 21
def database
  mongodb.db
end
delete(key) click to toggle source
# File lib/mongar/mongo/collection.rb, line 98
def delete(key)
  log_activity
  
  logger.debug "#{name}.delete #{key.inspect}"
  collection.remove(key, { :safe => true })
end
delete_all_items_pending_deletion() click to toggle source
# File lib/mongar/mongo/collection.rb, line 128
def delete_all_items_pending_deletion
  log_activity
  
  logger.info "   * Deleting all items in #{name} that are pending deletion"
  
  collection.remove({ :pending_deletion => true }, { :safe => true })
end
find(key) click to toggle source
# File lib/mongar/mongo/collection.rb, line 86
def find(key)
  logger.debug "#{name}.find #{key.inspect}"
  collection.find_one(key)
end
last_activity_at() click to toggle source
# File lib/mongar/mongo/collection.rb, line 80
def last_activity_at
  status = status_collection.find_one({ :collection_name => name })
  return nil unless status && status['last_activity_at']
  status['last_activity_at']
end
last_activity_at=(date) click to toggle source
# File lib/mongar/mongo/collection.rb, line 46
def last_activity_at=(date)
  logger.debug "Saving #{name} last_activity_at to #{date}"
  status_collection.update({ :collection_name => name },
                           { '$set' => { :collection_name => name, :last_activity_at => date } },
                           { :upsert => true })
end
last_replicated_at() click to toggle source
# File lib/mongar/mongo/collection.rb, line 33
def last_replicated_at
  status = status_collection.find_one({ :collection_name => name })
  return Time.parse("1/1/1902 00:00:00") unless status && status['last_replicated_at']
  status['last_replicated_at']
end
last_replicated_at=(date) click to toggle source
# File lib/mongar/mongo/collection.rb, line 39
def last_replicated_at=(date)
  logger.info "   * Updating #{name}.last_replicated_at to #{date}"
  status_collection.update({ :collection_name => name }, 
                           { '$set' => { :collection_name => name, :last_replicated_at => date } }, 
                           { :upsert => true })
end
log_activity() click to toggle source
# File lib/mongar/mongo/collection.rb, line 53
def log_activity
  return unless should_log_activity?
  logger.debug "Logging activity for #{name}"

  # MongoDB 2.6+ supports currentDate, so let's try that first.
  begin
    status_collection.update(
      { :collection_name => name },
      { '$currentDate' => { :last_activity_at => true }, '$set' => { :collection_name => name } },
      { :upsert => true }
    )
  rescue => e
    raise e unless e.to_s =~ /Invalid modifier specified \$currentDate/
    # Fallback to an $eval to get the date (gross).
    status_collection.update(
      { :collection_name => name },
      { '$set' => { :collection_name => name, :last_activity_at => mongodb.time_on_server } },
      { :upsert => true }
    )
  end
  @last_logged_activity = Time.now
end
mark_all_items_pending_deletion() click to toggle source
# File lib/mongar/mongo/collection.rb, line 120
def mark_all_items_pending_deletion
  log_activity
  
  logger.info "   * Marking all items in #{name} for pending deletion"
  
  collection.update({ '_id' => { '$exists' => true } }, { "$set" => { :pending_deletion => true } }, { :multi => true, :safe => true })
end
mongodb() click to toggle source
# File lib/mongar/mongo/collection.rb, line 13
def mongodb
  replica.mongodb
end
should_log_activity?() click to toggle source
# File lib/mongar/mongo/collection.rb, line 76
def should_log_activity?
  @last_logged_activity.nil? || Time.now - @last_logged_activity > 5
end
status_collection() click to toggle source
# File lib/mongar/mongo/collection.rb, line 29
def status_collection
  mongodb.status_collection_accessor
end
update(key, document) click to toggle source
# File lib/mongar/mongo/collection.rb, line 105
def update(key, document)
  log_activity
  
  logger.debug "#{name}.update #{key.inspect} with #{document.inspect}"
  collection.update(key, document, { :upsert => true, :safe => true })
end