File: indexlookup.rb

package info (click to toggle)
libdb-ruby 0.6.5-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,596 kB
  • ctags: 3,978
  • sloc: ansic: 13,984; cpp: 8,739; ruby: 7,864; sh: 47; makefile: 6
file content (125 lines) | stat: -rwxr-xr-x 3,744 bytes parent folder | download | duplicates (4)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
117
118
119
120
121
122
123
124
125
#!/usr/bin/ruby 
$LOAD_PATH.unshift("../../src", "..")
require 'bdbxml'

include BDB

File::unlink("exa.dbxml") rescue nil

man = XML::Manager.new
con = man.create_container("exa.dbxml", XML::INDEX_NODES)
at_exit {
   con.close
   man.close
}

upd = man.create_update_context

con.add_index("", "foo", "node-element-equality-string")
con.add_index("http://www.example.com/schema", "foo", "node-element-equality-string")
con.add_index("", "foo", "node-element-presence")
con.add_index("http://www.example.com/schema", "foo", "node-element-presence")
con.add_index("", "len", "edge-attribute-equality-decimal")
con.add_index("", "len", "edge-attribute-presence")
con.add_index("", "date", "edge-element-equality-date")

con.put("docA", <<-EOT, upd)
<docA>
  <foo>hello</foo>
  <foo>charlie</foo>
  <foo>brown</foo>
  <foo>aNd</foo>
  <foo>Lucy</foo>
</docA>
EOT
con.put("docB", <<-EOT, upd)
<docB xmlns:bar='http://www.example.com/schema'>
  <bar:foo>hello</bar:foo>
  <bar:foo>charlie</bar:foo>
  <bar:foo>brown</bar:foo>
  <bar:foo>aNd</bar:foo>
  <bar:foo>Lucy</bar:foo>
</docB>
EOT
con.put("docC",  <<-EOT, upd)
<docC>
  <foobar>
    <baz len='6.7'>tall guy</baz>
    <baz len='75'>30 yds</baz>
    <baz len='75'>30 yds</baz>
    <baz len='5.0'>five feeet</baz>
    <baz len='0.2'>point two</baz>
    <baz len='60.2'>five feet</baz>
  </foobar>
</docC>
EOT
con.put("docD", <<-EOT, upd)
<docD>
 <dates1>
  <date>2005-08-02</date>
  <date>2003-06-12</date>
  <date>1005-12-12</date>
 </dates1>
 <dates2>
  <date>1492-05-30</date>
  <date>2000-01-01</date>
  <date>1984-12-25</date>
 </dates2>
</docD>
EOT

puts "content"
con.each do |value|
   puts "\n\tDocument #{value.to_document.name}"
   puts value
end

puts "\nnode-element-presence\n"
xil = man.create_index_lookup(con, "", "foo", "node-element-presence")
["", "http://www.example.com/schema" ].each do |uri|
   xil.node_uri = uri
   puts "\tnode : #{xil.node.inspect}"
   xil.execute.each {|p| puts "\t\t#{p}"}
end

puts "\nnode-element-equality-string : charlie"
xil = man.create_index_lookup(con, "", "foo", "node-element-equality-string")
[[XML::IndexLookup::LT, "<"], [XML::IndexLookup::LTE, "<="],
 [XML::IndexLookup::GT, ">"], [XML::IndexLookup::GTE, ">="]].each do 
   |comp, rep|
   xil.low_bound = ["charlie", comp]
   puts "\n low_bound : charlie #{rep} #{xil.low_bound[0]}"
   ["", "http://www.example.com/schema" ].each do |uri|
      xil.node_uri = uri
      puts "\tnode : #{xil.node.inspect}"
      xil.execute.each {|p| puts "\t\t#{p}"}
      puts "\tnode : #{xil.node.inspect} -- reverse"
      xil.execute(nil, XML::REVERSE_ORDER).each {|p| puts "\t\t#{p}"}
   end
end

puts "\nedge-attribute-equality-decimal : 40"
xil = man.create_index_lookup(con, "", "len", "edge-attribute-equality-decimal")
xil.parent = ["", "baz"]
[[XML::IndexLookup::LT, "<"], [XML::IndexLookup::LTE, "<="],
 [XML::IndexLookup::GT, ">"], [XML::IndexLookup::GTE, ">="]].each do 
   |comp, rep|
   xil.low_bound = [XML::Value.new(XML::Value::DECIMAL, 40), comp]
   puts "\n low_bound : len #{rep} #{xil.low_bound[0]}"
   puts "\tnode : #{xil.node.inspect}"
   xil.execute.each {|p| puts "\t\t#{p}"}
   puts "\tnode : #{xil.node.inspect} -- reverse"
   xil.execute(nil, XML::REVERSE_ORDER).each {|p| puts "\t\t#{p}"}
end


puts "\nedge-element-equality-date : date == 2003-06-12"
xil = man.create_index_lookup(con, "", "date", "edge-element-equality-date",
                              XML::Value.new(XML::Value::DATE, "2003-06-12"),
                              XML::IndexLookup::EQ)
xil.parent = ["", "dates1"]
puts "\tparent = dates1"
xil.execute.each {|p| puts "\t\t#{p}"}
xil.parent = ["", "dates2"]
puts "\tparent = dates2"
xil.execute(nil, XML::REVERSE_ORDER).each {|p| puts "\t\t#{p}"}