Module: BDBXML

Included in:
Persist
Defined in:
lib/models/modbdbxml.rb

Constant Summary collapse

DB_ROOT =
db_path
DB_NAME =
APP_CONFIG['dbcontainer']

Instance Method Summary collapse

Instance Method Details

#clean_envObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/models/modbdbxml.rb', line 42

def clean_env
  puts "\nVERSION of BDB is #{BDB::VERSION}\n"
  puts "\nVERSION of BDB::XML is #{BDB::XML::VERSION}\n"
  puts "Cleaning #{DB_ROOT}/#{DB_NAME}\n"
  
  Dir.foreach(DB_ROOT) do |x|
    if FileTest.file?(DB_ROOT+"/#{x}")
       File.unlink(DB_ROOT+"/#{x}")
    end
  end
end

#close_envObject



54
55
56
57
# File 'lib/models/modbdbxml.rb', line 54

def close_env
  $man.close
  $env.close
end

#create_doc(msg, key) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/models/modbdbxml.rb', line 69

def create_doc(msg,key)
  $man.begin($con) do |txn, con|
    a = $man.create_document
    a.content = msg
    con[key] = a
    txn.commit
  end
end

#create_key_and_doc(msg) ⇒ Object



59
60
61
62
63
# File 'lib/models/modbdbxml.rb', line 59

def create_key_and_doc(msg)
  key = UUID.new
  create_doc(msg,key)
  key
end

#find(query) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/models/modbdbxml.rb', line 87

def find(query)
  cxt = $man.create_query_context()
  results = $man.query(query, cxt)
  if (results.size == 0) then
    return nil
  end
  #results is essentially an array of result lines
  #a single result will span a number of output lines, e.g. outer joined
  #MTA info will appear with a CRLF separator
  #the following code makes up single logical result rows based on the lines between
  #successive pairs of {} braces
  #
  #it should be noted that the result set from the xqueries is intended to be
  #a valid ruby block
  #this allows the result to be used to instantiate an active record object
  #with no further manipulation required
  rows = []
  i = 0
  results.each do |r|
    if rows[i] == nil
      rows[i] = ""
    end
    r = r.to_s
    rows[i] << r.chomp
    if (r.match(']}'))
      i = i + 1
    end
  end
  rows
end

#read_doc(key) ⇒ Object



65
66
67
# File 'lib/models/modbdbxml.rb', line 65

def read_doc(key)
  $con.get(key).content
end

#replace_doc(msg, key) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/models/modbdbxml.rb', line 78

def replace_doc(msg,key)
  $man.begin($con) do |txn, con|
    a = $con.get(key)
    a.content = msg
    $con.update(a)
    txn.commit
  end
end

#setup_envObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/models/modbdbxml.rb', line 12

def setup_env
  puts "VERSION of BDB is #{BDB::VERSION}\n"
  puts "VERSION of BDB::XML is #{BDB::XML::VERSION}\n"
  puts "Database path is #{File.expand_path(DB_ROOT)}\n"
  
  #setup UUID state file
  UUID.config(:state_file => "#{DB_ROOT}/uuid.state",
                :sequence => rand(0x100000000),
                :mac_addr => '00:19:e3:36:60:f5')
              
  @flag = BDB::INIT_TRANSACTION
  @flag ||= BDB::INIT_LOMP
  $env = BDB::Env.new(DB_ROOT, BDB::CREATE | @flag)
  $man = $env.manager
  if (!File.exist?("#{DB_ROOT}/#{DB_NAME}"))
    if (@flag & BDB::INIT_TXN) != 0
       $con = $man.create_container(DB_NAME, BDB::XML::TRANSACTIONAL)
    else
       $con = $man.create_container(DB_NAME)
    end
  else
    if (@flag & BDB::INIT_TXN) != 0
       $con = $man.open_container(DB_NAME, BDB::XML::TRANSACTIONAL)
    else
       $con = $man.open_container(DB_NAME)
    end
  end
  $base = "."
end