Class: Polipus::QueueOverflow::MongoQueueCapped

Inherits:
MongoQueue
  • Object
show all
Defined in:
lib/polipus/queue_overflow/mongo_queue_capped.rb

Instance Method Summary collapse

Methods inherited from MongoQueue

#clear, #empty?, #length, #pop

Constructor Details

#initialize(mongo_db, queue_name, options = {}) ⇒ MongoQueueCapped

Returns a new instance of MongoQueueCapped.



5
6
7
8
# File 'lib/polipus/queue_overflow/mongo_queue_capped.rb', line 5

def initialize(mongo_db, queue_name, options = {})
  super
  @max = @options[:max]
end

Instance Method Details

#push(data) ⇒ Object Also known as: enc, <<



10
11
12
13
14
15
16
17
18
19
# File 'lib/polipus/queue_overflow/mongo_queue_capped.rb', line 10

def push data
  super
  @semaphore.synchronize {
    s = size
    if s > @max
      docs = @mongo_db[@collection_name].find({},{:sort => {:_id => 1}, :fields => [:_id]}).limit(s-@max).map { |e| e['_id'] }
      @mongo_db[@collection_name].remove({:_id => {'$in' => docs}, '$isolated' => 1})
    end
  }
end