Class: Leecher::MetalinkQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/leecher/metalink_queue.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ MetalinkQueue

Returns a new instance of MetalinkQueue.



7
8
9
10
# File 'lib/leecher/metalink_queue.rb', line 7

def initialize(filename)
  @store = YAML::Store.new(filename)
  @transaction_depth = 0
end

Instance Method Details

#add_uris(uris) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/leecher/metalink_queue.rb', line 47

def add_uris(uris)
  transaction do
    uris.each do |uri|
      set_store_entry(uri, :state => :brand_new)
    end
  end
end

#get_queueObject



13
14
15
16
17
# File 'lib/leecher/metalink_queue.rb', line 13

def get_queue
  transaction do
    @store[:queue] ||= {}
  end
end

#new_uri_entryObject



55
56
57
58
59
60
# File 'lib/leecher/metalink_queue.rb', line 55

def new_uri_entry
  {
    :created_at => Time.now.iso8601,
    :state => :brand_new,
  }
end

#set_store_entry(uri, properties = {}) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/leecher/metalink_queue.rb', line 39

def set_store_entry(uri, properties = {})
  transaction do
    @store[:queue] ||= {}
    @store[:queue][uri] ||= new_uri_entry
    @store[:queue][uri].merge!(properties)
  end
end

#transactionObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/leecher/metalink_queue.rb', line 19

def transaction
  @transaction_depth += 1
  
  if @transaction_depth == 1
    @store.transaction do
      begin
        yield
      ensure
        @transaction_depth = 0
      end
    end
  else
    begin
      yield
    ensure
      @transaction_depth -= 1
    end
  end
end

#update_uri_state(uri, state) ⇒ Object



62
63
64
65
66
# File 'lib/leecher/metalink_queue.rb', line 62

def update_uri_state(uri, state)
  transaction do
    set_store_entry(uri, :state => state)
  end
end