File: retrieve_db.rb

package info (click to toggle)
libdb-ruby 0.6.5-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,612 kB
  • ctags: 3,979
  • sloc: ansic: 13,984; cpp: 8,739; ruby: 7,864; sh: 47; makefile: 8
file content (29 lines) | stat: -rwxr-xr-x 817 bytes parent folder | download | duplicates (11)
1
2
3
4
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
#!/usr/bin/ruby -I../.. -I../../../src

require 'bdbxml'

def value(manager, document, xpath, context)
   query = manager.prepare(xpath, context)
   result = query.execute(document, context).to_a
   if result.size != 1
      raise "Expected 1 got #{result.size}"
   end
   result[0].to_s
end

options = {'home' => 'env', 'container' => 'name.xml'}

env = BDB::Env.new(options['home'], BDB::INIT_TRANSACTION)
man = env.manager
context = man.create_query_context
man.begin do |txn|
   con = txn.open_container(options['container'])
   bdb = txn.open_db(BDB::Btree, 'bdb', nil, 'a')
   txn.query("collection('#{options['container']}')/vendor") do |doc|
      res = value(txn, doc, "/vendor/salesrep/name/text()", context)
      puts "For key '#{res}', retrieved : "
      puts "\t #{bdb[res]}"
   end
   txn.commit
end