Class: StompServer::DBMQueue

Inherits:
Queue
  • Object
show all
Defined in:
lib/stomp_server_ng/queue/dbm_queue.rb

Instance Attribute Summary

Attributes inherited from Queue

#checkpoint_interval

Instance Method Summary collapse

Methods inherited from Queue

#assign_id, #close_queue, #dequeue, #enqueue, #message_for?, #monitor, #open_queue, #readframe, #requeue, #save_queue_state, #stop, #writeframe

Constructor Details

#initialize(*args) ⇒ DBMQueue

Returns a new instance of DBMQueue.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 5

def initialize *args
  super

  @@log = Logger.new(STDOUT)
  @@log.level = StompServer::LogHelper.get_loglevel()

  # Please don't use dbm files for storing large frames, it's problematic at best and uses large amounts of memory.
  # sdbm croaks on marshalled data that contains certain characters, so we don't use it at all
  @dbm = false
  if RUBY_PLATFORM =~/linux|bsd/
    types = ['bdb','dbm','gdbm']
  else
    types = ['bdb','gdbm']
  end
  types.each do |dbtype|
    begin
      require dbtype
      @dbm = dbtype
      @@log.info "#{@dbm} loaded"
      break
    rescue LoadError => e
    end
  end
  raise "No DBM library found. Tried bdb,dbm,gdbm" unless @dbm
  @db = Hash.new
  @queues.keys.each {|q| _open_queue(q)}
end

Instance Method Details

#_close_queue(dest) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 53

def _close_queue(dest)
  @db[dest][:dbh].close
  dbname = @db[dest][:dbname]
  File.delete(dbname) if File.exists?(dbname)
  File.delete("#{dbname}.db") if File.exists?("#{dbname}.db")
  File.delete("#{dbname}.pag") if File.exists?("#{dbname}.pag")
  File.delete("#{dbname}.dir") if File.exists?("#{dbname}.dir")
end

#_open_queue(dest) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 44

def _open_queue(dest)
  queue_name = dest.gsub('/', '_')
  dbname = @directory + '/' + queue_name
  @db[dest] = Hash.new
  @db[dest][:dbh] = dbmopen(dbname)
  @db[dest][:dbname] = dbname
end

#_readframe(dest, msgid) ⇒ Object



66
67
68
69
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 66

def _readframe(dest,msgid)
  frame_image = @db[dest][:dbh][msgid]
  Marshal::load(frame_image)
end

#_writeframe(dest, frame, msgid) ⇒ Object



62
63
64
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 62

def _writeframe(dest,frame,msgid)
  @db[dest][:dbh][msgid] = Marshal::dump(frame)
end

#dbmopen(dbname) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/stomp_server_ng/queue/dbm_queue.rb', line 33

def dbmopen(dbname)
  if @dbm == 'bdb'
    BDB::Hash.new(dbname, nil, "a")
  elsif @dbm == 'dbm'
    DBM.open(dbname)
  elsif @dbm == 'gdbm'
    GDBM.open(dbname)
  end
end