iab-ActiveRecordBDBXml 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,116 @@
1
+ require 'bdbxml'
2
+ module BDBXML
3
+ #setup DB_ROOT to point to IABDB location
4
+ unless defined?(DB_ROOT)
5
+ db_path = APP_CONFIG['dbpath']
6
+ require 'pathname'
7
+ db_path = Pathname.new(db_path).cleanpath(true).to_s
8
+ DB_ROOT = db_path
9
+ DB_NAME = APP_CONFIG['dbcontainer']
10
+ end
11
+
12
+ def setup_env
13
+ puts "VERSION of BDB is #{BDB::VERSION}\n"
14
+ puts "VERSION of BDB::XML is #{BDB::XML::VERSION}\n"
15
+ puts "Database path is #{File.expand_path(DB_ROOT)}\n"
16
+
17
+ #setup UUID state file
18
+ UUID.config(:state_file => "#{DB_ROOT}/uuid.state",
19
+ :sequence => rand(0x100000000),
20
+ :mac_addr => '00:19:e3:36:60:f5')
21
+
22
+ @flag = BDB::INIT_TRANSACTION
23
+ @flag ||= BDB::INIT_LOMP
24
+ $env = BDB::Env.new(DB_ROOT, BDB::CREATE | @flag)
25
+ $man = $env.manager
26
+ if (!File.exist?("#{DB_ROOT}/#{DB_NAME}"))
27
+ if (@flag & BDB::INIT_TXN) != 0
28
+ $con = $man.create_container(DB_NAME, BDB::XML::TRANSACTIONAL)
29
+ else
30
+ $con = $man.create_container(DB_NAME)
31
+ end
32
+ else
33
+ if (@flag & BDB::INIT_TXN) != 0
34
+ $con = $man.open_container(DB_NAME, BDB::XML::TRANSACTIONAL)
35
+ else
36
+ $con = $man.open_container(DB_NAME)
37
+ end
38
+ end
39
+ $base = "."
40
+ end
41
+
42
+ def clean_env
43
+ puts "\nVERSION of BDB is #{BDB::VERSION}\n"
44
+ puts "\nVERSION of BDB::XML is #{BDB::XML::VERSION}\n"
45
+ puts "Cleaning #{DB_ROOT}/#{DB_NAME}\n"
46
+
47
+ Dir.foreach(DB_ROOT) do |x|
48
+ if FileTest.file?(DB_ROOT+"/#{x}")
49
+ File.unlink(DB_ROOT+"/#{x}")
50
+ end
51
+ end
52
+ end
53
+
54
+ def close_env
55
+ $man.close
56
+ $env.close
57
+ end
58
+
59
+ def create_key_and_doc(msg)
60
+ key = UUID.new
61
+ create_doc(msg,key)
62
+ key
63
+ end
64
+
65
+ def read_doc(key)
66
+ $con.get(key).content
67
+ end
68
+
69
+ def create_doc(msg,key)
70
+ $man.begin($con) do |txn, con|
71
+ a = $man.create_document
72
+ a.content = msg
73
+ con[key] = a
74
+ txn.commit
75
+ end
76
+ end
77
+
78
+ def replace_doc(msg,key)
79
+ $man.begin($con) do |txn, con|
80
+ a = $con.get(key)
81
+ a.content = msg
82
+ $con.update(a)
83
+ txn.commit
84
+ end
85
+ end
86
+
87
+ def find(query)
88
+ cxt = $man.create_query_context()
89
+ results = $man.query(query, cxt)
90
+ if (results.size == 0) then
91
+ return nil
92
+ end
93
+ #results is essentially an array of result lines
94
+ #a single result will span a number of output lines, e.g. outer joined
95
+ #MTA info will appear with a CRLF separator
96
+ #the following code makes up single logical result rows based on the lines between
97
+ #successive pairs of {} braces
98
+ #
99
+ #it should be noted that the result set from the xqueries is intended to be
100
+ #a valid ruby block
101
+ #this allows the result to be used to instantiate an active record object
102
+ #with no further manipulation required
103
+ rows = []
104
+ i = 0
105
+ results.each do |r|
106
+ if rows[i] == nil
107
+ rows[i] = ""
108
+ end
109
+ rows[i] << r.chomp
110
+ if (r.match(']}'))
111
+ i = i + 1
112
+ end
113
+ end
114
+ rows
115
+ end
116
+ end
@@ -0,0 +1,135 @@
1
+ module BDBXMLJE
2
+ include Java
3
+ require 'date'
4
+ require 'pathname'
5
+ require 'lib/db.jar'
6
+ require 'lib/dbxml.jar'
7
+
8
+ # Include all the Java and JE classes that we need.
9
+
10
+ include_class 'java.io.File' do |p, c|
11
+ "J#{c}"
12
+ end
13
+
14
+ include_class ['com.sleepycat.db.Environment','com.sleepycat.db.EnvironmentConfig'] do |p, c|
15
+ "J#{c}"
16
+ end
17
+ include_class 'com.sleepycat.db.Cursor'
18
+ include_class 'com.sleepycat.db.Database'
19
+ include_class 'com.sleepycat.db.DatabaseConfig'
20
+ include_class 'com.sleepycat.db.DatabaseType'
21
+ include_class 'com.sleepycat.db.DatabaseEntry'
22
+ include_class 'com.sleepycat.db.OperationStatus'
23
+ include_class 'com.sleepycat.db.Transaction'
24
+
25
+ include_class 'com.sleepycat.bind.tuple.IntegerBinding'
26
+ include_class 'com.sleepycat.bind.tuple.StringBinding'
27
+
28
+ include_class 'com.sleepycat.dbxml.XmlManager'
29
+ include_class 'com.sleepycat.dbxml.XmlManagerConfig'
30
+ include_class 'com.sleepycat.dbxml.XmlDocument'
31
+ include_class 'com.sleepycat.dbxml.XmlTransaction'
32
+
33
+ #setup DB_ROOT to point to IABDB location
34
+ unless defined?(DB_ROOT)
35
+ db_path = APP_CONFIG['dbpath']
36
+ db_path = Pathname.new(db_path).cleanpath(true).to_s
37
+ DB_ROOT = db_path
38
+ DB_NAME = APP_CONFIG['dbcontainer']
39
+ end
40
+
41
+ def setup_env
42
+ #setup UUID state file
43
+ UUID.config(:state_file => "#{DB_ROOT}/uuid.state",
44
+ :sequence => rand(0x100000000),
45
+ :mac_addr => '00:19:e3:36:60:f5')
46
+
47
+ begin
48
+ envConf = JEnvironmentConfig.new()
49
+ envConf.setAllowCreate(true)
50
+ envConf.setTransactional(true)
51
+ envConf.setInitializeCache(true);
52
+
53
+ f = JFile.new(DB_ROOT)
54
+ $env = JEnvironment.new(f, envConf);
55
+
56
+ manConf = XmlManagerConfig.new()
57
+ manConf.setAllowExternalAccess(true)
58
+ $man = XmlManager.new($env,manConf)
59
+
60
+ if (!File.exist?("#{DB_ROOT}/#{DB_NAME}"))
61
+ $con = $man.createContainer(DB_NAME)
62
+ else
63
+ $con = $man.openContainer(DB_NAME)
64
+ end
65
+
66
+ $base = "."
67
+ rescue NativeException => e
68
+ puts "Native exception's cause: #{e.cause}"
69
+ raise
70
+ end
71
+ end
72
+
73
+ def create_key_and_doc(msg)
74
+ key = UUID.new
75
+ create_doc(msg,key)
76
+ key
77
+ end
78
+
79
+ def read_doc(key)
80
+ $con.getDocument(key).getContentAsString()
81
+ end
82
+
83
+ def create_doc(msg,key)
84
+ #removed the transaction bit since causes error when running inside Tomcat under JRuby
85
+ #txn = $man.createTransaction()
86
+ a = $man.createDocument()
87
+ a.setContent(msg)
88
+ a.setName(key)
89
+ $con.putDocument(key,msg)
90
+ #$con.putDocument(txn,key,msg)
91
+ #txn.commit()
92
+ end
93
+
94
+ def replace_doc(msg,key)
95
+ #removed the transaction bit since causes error when running inside Tomcat under JRuby
96
+ #txn = $man.createTransaction()
97
+ #a = $con.getDocument(txn,key)
98
+ a = $con.getDocument(key)
99
+ a.setContent(msg)
100
+ #$con.updateDocument(txn,a)
101
+ $con.updateDocument(a)
102
+ #txn.commit
103
+ end
104
+
105
+ def find(query)
106
+ cxt = $man.createQueryContext()
107
+ results = $man.query(query, cxt)
108
+ if (results.size() == 0) then
109
+ return nil
110
+ end
111
+ #results is essentially an array of result lines
112
+ #a single result will span a number of output lines, e.g. outer joined
113
+ #MTA info will appear with a CRLF separator
114
+ #the following code makes up single logical result rows based on the lines between
115
+ #successive pairs of {} braces
116
+ #
117
+ #it should be noted that the result set from the xqueries is intended to be
118
+ #a valid ruby block
119
+ #this allows the result to be used to instantiate an active record object
120
+ #with no further manipulation required
121
+ rows = []
122
+ i = 0
123
+ while results.hasNext()
124
+ if rows[i] == nil
125
+ rows[i] = ""
126
+ end
127
+ r = results.next().asString()
128
+ rows[i] << r.chomp
129
+ if (r.match(']}'))
130
+ i = i + 1
131
+ end
132
+ end
133
+ rows
134
+ end
135
+ end
@@ -0,0 +1,50 @@
1
+ # Copyright (c) 2007-2008 Orangery Technology Limited
2
+ # You can redistribute it and/or modify it under the same terms as Ruby.
3
+ #
4
+ $:.unshift(File.dirname(__FILE__))
5
+ require 'rubygems'
6
+ require 'uuid'
7
+ require 'rexml/document'
8
+ require 'singleton'
9
+ require 'modbdbxml' unless defined?(JRUBY_VERSION)
10
+ require 'modbdbxmlje' if defined?(JRUBY_VERSION)
11
+
12
+ class Persist
13
+ include BDBXML unless defined?(JRUBY_VERSION)
14
+ include BDBXMLJE if defined?(JRUBY_VERSION)
15
+
16
+ include Singleton
17
+
18
+ def initialize
19
+ setup_env
20
+ end
21
+
22
+ def put(key,request)
23
+ begin
24
+ if (key == 'UUID') then
25
+ key = UUID.new
26
+ create_doc(request,key)
27
+ else
28
+ replace_doc(request,key)
29
+ end
30
+ key
31
+ end
32
+ end
33
+
34
+ def get(key)
35
+ read_doc(key)
36
+ end
37
+
38
+ #get with the insistence the document will be there
39
+ #if not then create it
40
+ def get!(key,request)
41
+ result=""
42
+ begin
43
+ result = read_doc(key)
44
+ rescue Exception => e #document not found
45
+ create_doc(request,key)
46
+ result = read_doc(key)
47
+ end
48
+ return result
49
+ end
50
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iab-ActiveRecordBDBXml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Mawdsley
@@ -42,6 +42,10 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - lib/Marshaller.rb
45
+ - lib/models
46
+ - lib/models/modbdbxmlje.rb
47
+ - lib/models/modbdbxml.rb
48
+ - lib/models/persist.rb
45
49
  has_rdoc: true
46
50
  homepage: http://github.com/iab/ActiveRecordBDBXml
47
51
  post_install_message: