Class: Driq

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/driq.rb,
lib/driq.rb,
lib/driq/version.rb

Direct Known Subclasses

DriqDownstream

Defined Under Namespace

Classes: EventSource, EventStream

Constant Summary collapse

VERSION =
"0.4.3"

Instance Method Summary collapse

Constructor Details

#initialize(size = 100) ⇒ Driq

Returns a new instance of Driq.



8
9
10
11
12
13
14
15
16
# File 'lib/driq.rb', line 8

def initialize(size=100)
  super()
  @last = 0
  @floor = size
  @ceil = size * 2
  @list = []
  @event = new_cond
  @closed = false
end

Instance Method Details

#closeObject



58
59
60
61
62
63
# File 'lib/driq.rb', line 58

def close
  synchronize do
    @closed = true; 
    @event.broadcast 
  end
end

#last(if_empty = nil) ⇒ Object



33
34
35
36
37
# File 'lib/driq.rb', line 33

def last(if_empty=nil)
  synchronize do
    @list.empty? ? if_empty : read(@last - 1)
  end
end

#last_keyObject



39
40
41
# File 'lib/driq.rb', line 39

def last_key
  @last
end

#read(key, timeout = nil) ⇒ Object



29
30
31
# File 'lib/driq.rb', line 29

def read(key, timeout=nil)
  readpartial(key, 1, timeout).first
end

#readpartial(key, size, timeout = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/driq.rb', line 43

def readpartial(key, size, timeout=nil)
  synchronize do
    key = @last unless key
    raise ClosedQueueError if @closed
    idx = seek(key)
    if idx.nil?
      key = @last
      @event.wait(timeout)
      idx = seek(key)
    end
    raise ClosedQueueError if @closed
    idx ? @list.slice(idx, size) : []
  end
end

#write(value) ⇒ Object Also known as: push



18
19
20
21
22
23
24
25
26
# File 'lib/driq.rb', line 18

def write(value)
  synchronize do
    cell = [make_key, value]
    @list.push(cell)
    @list = @list.last(@floor) if @list.size > @ceil
    @event.broadcast
    return cell.first
  end
end