Class: Mongar::Mongo::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongar/mongo/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Collection

Returns a new instance of Collection.



6
7
8
9
10
11
# 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

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/mongar/mongo/collection.rb', line 3

def logger
  @logger
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/mongar/mongo/collection.rb', line 3

def name
  @name
end

#replicaObject

Returns the value of attribute replica.



4
5
6
# File 'lib/mongar/mongo/collection.rb', line 4

def replica
  @replica
end

Instance Method Details

#collectionObject



25
26
27
# File 'lib/mongar/mongo/collection.rb', line 25

def collection
  database[name]
end

#connectionObject



17
18
19
# File 'lib/mongar/mongo/collection.rb', line 17

def connection
  mongodb.connection!
end

#create(document) ⇒ Object



91
92
93
94
95
96
# 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) ⇒ Object



112
113
114
115
116
117
118
# 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

#databaseObject



21
22
23
# File 'lib/mongar/mongo/collection.rb', line 21

def database
  mongodb.db
end

#delete(key) ⇒ Object



98
99
100
101
102
103
# 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_deletionObject



128
129
130
131
132
133
134
# 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) ⇒ Object



86
87
88
89
# File 'lib/mongar/mongo/collection.rb', line 86

def find(key)
  logger.debug "#{name}.find #{key.inspect}"
  collection.find_one(key)
end

#last_activity_atObject



80
81
82
83
84
# 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) ⇒ Object



46
47
48
49
50
51
# 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_atObject



33
34
35
36
37
# 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) ⇒ Object



39
40
41
42
43
44
# 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_activityObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_deletionObject



120
121
122
123
124
125
126
# 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

#mongodbObject



13
14
15
# File 'lib/mongar/mongo/collection.rb', line 13

def mongodb
  replica.mongodb
end

#should_log_activity?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/mongar/mongo/collection.rb', line 76

def should_log_activity?
  @last_logged_activity.nil? || Time.now - @last_logged_activity > 5
end

#status_collectionObject



29
30
31
# File 'lib/mongar/mongo/collection.rb', line 29

def status_collection
  mongodb.status_collection_accessor
end

#update(key, document) ⇒ Object



105
106
107
108
109
110
# 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