steenzout-dao 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
File without changes
@@ -0,0 +1,39 @@
1
+ module Steenzout
2
+ module DAO
3
+
4
+ class DAOManager
5
+
6
+ @config = Steenzout::ConfigurationManager.configuration_for_gem 'steenzout-dao'
7
+
8
+
9
+ def self.create_dao(name)
10
+
11
+ end
12
+
13
+
14
+
15
+ def self.commit_transaction()
16
+
17
+ end
18
+
19
+
20
+
21
+ def self.end_transaction()
22
+
23
+ end
24
+
25
+
26
+
27
+ def self.rollback_transaction()
28
+
29
+ end
30
+
31
+
32
+
33
+ def self.start_transaction()
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,140 @@
1
+ include TokyoCabinet
2
+
3
+ module Steenzout
4
+ module DAO
5
+
6
+ # Generic TokyoCabinet Data Access Object.
7
+ #
8
+ # @var name: the name of the implemented DAO.
9
+ # @var database: the TokyoCabinet database.
10
+ #
11
+ class TokyoCabinetDAO
12
+
13
+ attr_reader :database
14
+
15
+ # Constructs a generic TokyoCabinet DAO.
16
+ #
17
+ # @param name: the name of the implemented DAO.
18
+ # @param configuration: hash containing the DAO configuration.
19
+ # :kvstore => the TokyoCabinet database implementation.
20
+ # :location => the TokyoCabinet database location.
21
+ #
22
+ def initialize(name, configuration)
23
+
24
+ @name = name
25
+
26
+ raise ArgumentError, "TokyoCabinetDAO name is nil!" \
27
+ if name.nil?
28
+ raise ArgumentError, "TokyoCabinetDAO configuration nil!" \
29
+ if configuration.nil?
30
+ raise ArgumentError, "TokyoCabinetDAO configuration for #{@name} is missing the :kvstore property!" \
31
+ if !configuration.has_key? :kvstore
32
+ raise ArgumentError, "TokyoCabinetDAO configuration for #{@name} is missing the :location property!" \
33
+ if !configuration.has_key? :location
34
+
35
+
36
+ case configuration[:kvstore]
37
+ when :hdb, :hash
38
+ @database = HDB::new
39
+ when :bdb, :btree
40
+ @database = BDB::new
41
+ when :fdb, :'fixed-length'
42
+ @database = FDB::new
43
+ when :tdb, :table
44
+ @database = TDB::new
45
+ when :adb, :abstract
46
+ @database = ADB::new
47
+ else
48
+ raise ArgumentError, "Unknown TokyoCabinet database #{configuration[:kvstore]}!"
49
+ end
50
+
51
+ @location = configuration[:location]
52
+
53
+ end
54
+
55
+ def open()
56
+
57
+ if !@database.open(@location, HDB::OWRITER | HDB::OCREAT)
58
+ LOGGER.error('steenzout-dao.TokyoCabinetDAO][#{@name}') {"open error: #{@database.errmsg(@database.ecode)}"} if LOGGER.error?
59
+ end
60
+
61
+ end
62
+
63
+ def close()
64
+
65
+ if !@database.nil?
66
+ LOGGER.error('steenzout-dao.TokyoCabinetDAO[#{@name}') {"close error: #{@database.errmsg(@database.ecode)}"} if !@database.close
67
+ end
68
+
69
+ end
70
+
71
+
72
+
73
+ def delete(key)
74
+
75
+ @database.out key
76
+
77
+ LOGGER.info('steenzout-dao.TokyoCabinetDAO[#{@name}') {"deleted #{key} mapping."}
78
+
79
+ nil
80
+
81
+ end
82
+
83
+
84
+
85
+ def has?(key)
86
+
87
+ !@database[key].nil?
88
+
89
+ end
90
+
91
+
92
+
93
+ def get(key)
94
+
95
+ @database[key]
96
+
97
+ end
98
+
99
+
100
+
101
+ def list()
102
+
103
+ @database.iterinit
104
+ while key = @database.iternext
105
+ yield key, get(key)
106
+ end
107
+
108
+ end
109
+
110
+
111
+
112
+ def map(key, value)
113
+
114
+ status = @database.put key, value
115
+ if !status
116
+ LOGGER.debug('steenzout-dao.TokyoCabinetDAO][#{@name}') {"#{@database.ecode} : #{@database.errmsg(@database.ecode)}"}
117
+ error = "failed mapping #{key}->#{value}!"
118
+ LOGGER.error('steenzout-dao.TokyoCabinetDAO][#{@name}') {error}
119
+ raise Exception, error
120
+ end
121
+ LOGGER.info('steenzout-dao.TokyoCabinetDAO][#{@name}') {"created #{key}->#{value} mapping."}
122
+
123
+ end
124
+
125
+
126
+
127
+ def update(key, value)
128
+
129
+ if !has? key
130
+ raise ArgumentError, "There is no mapping for the given #{key} key."
131
+ end
132
+
133
+ map(key, value)
134
+
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'steenzout-cfg'
3
+
4
+
5
+ module Steenzout
6
+ module DAO
7
+
8
+ autoload :DAOManager, "#{File.dirname(__FILE__)}/dao/manager"
9
+ autoload :TokyoCabinetDAO, "#{File.dirname(__FILE__)}/dao/tokyocabinet"
10
+
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ require 'test/unit'
2
+ require 'lib/steenzout-dao'
3
+
4
+
5
+
6
+ class TestManager < Test::Unit::TestCase
7
+
8
+ def setup
9
+
10
+ end
11
+
12
+ def teardown
13
+
14
+ end
15
+
16
+
17
+
18
+ def test_commit_transaction
19
+
20
+ end
21
+
22
+ def test_end_transaction
23
+
24
+ end
25
+
26
+ def test_rollback_transaction
27
+
28
+ end
29
+
30
+ def test_start_transaction
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,130 @@
1
+ require 'test/unit'
2
+ require 'logger'
3
+ require 'tokyocabinet'
4
+ require 'lib/steenzout-dao'
5
+
6
+
7
+ LOGGER = Logger.new('log/development.log') if !defined? LOGGER
8
+
9
+
10
+
11
+ class TestTokyoCabinetDAO < Test::Unit::TestCase
12
+
13
+ KEY_STRING1 = 'string1'
14
+ KEY_STRING2 = 'string2'
15
+ KEY_UNKNOWN = 'UNKNOWN'
16
+
17
+ VALUE_STRING1 = 'value_string1'
18
+ VALUE_STRING2 = 'value_string2'
19
+
20
+
21
+
22
+ def setup()
23
+
24
+ @dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :hdb, :location => 'test/test.tch'})
25
+ @dao.open
26
+
27
+ @dao.map(KEY_STRING1, VALUE_STRING1)
28
+
29
+ end
30
+
31
+ def teardown()
32
+
33
+ @dao.database.vanish unless @dao.nil? or @dao.database.nil? # removes all records from the database
34
+
35
+ ensure
36
+ @dao.close if !@dao.nil?
37
+ end
38
+
39
+
40
+
41
+ def test_create_abstract()
42
+
43
+ dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :adb, :location => 'test/abstract.tch'})
44
+ assert(dao.database.instance_of? ADB)
45
+
46
+ end
47
+
48
+ def test_create_btree()
49
+
50
+ dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :bdb, :location => 'test/btree.tch'})
51
+ assert(dao.database.instance_of? BDB)
52
+
53
+ end
54
+
55
+ def test_create_hash()
56
+
57
+ dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :hdb, :location => 'test/hash.tch'})
58
+ assert(dao.database.instance_of? HDB)
59
+
60
+ end
61
+
62
+ def test_create_fixed_length()
63
+
64
+ dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :fdb, :location => 'test/fixed.tch'})
65
+ assert(dao.database.instance_of? FDB)
66
+
67
+ end
68
+
69
+ def test_create_table()
70
+
71
+ dao = Steenzout::DAO::TokyoCabinetDAO.new(:test_dao, {:kvstore => :tdb, :location => 'test/abstract.tch'})
72
+ assert(dao.database.instance_of? TDB)
73
+
74
+ end
75
+
76
+
77
+
78
+ def test_delete()
79
+
80
+ @dao.delete(KEY_STRING1)
81
+ assert_nil @dao.get(KEY_STRING1)
82
+
83
+ end
84
+
85
+ def test_delete_unknown_key()
86
+ assert_nil @dao.delete('UNKNOWN')
87
+ end
88
+
89
+
90
+ def test_get()
91
+
92
+ # 0. adding one more mapping
93
+ @dao.map(KEY_STRING2, VALUE_STRING2)
94
+
95
+ # 1. test
96
+ assert_equal VALUE_STRING1, @dao.get(KEY_STRING1)
97
+ assert_equal VALUE_STRING2, @dao.get(KEY_STRING2)
98
+
99
+ end
100
+
101
+ def test_get_unknown_key()
102
+ assert_nil @dao.get(KEY_UNKNOWN)
103
+ end
104
+
105
+
106
+ def test_has?()
107
+ assert @dao.has? KEY_STRING1
108
+ assert_equal false, @dao.has?('UNKNOWN')
109
+ end
110
+
111
+
112
+ def test_list()
113
+
114
+ # 1. with mappings
115
+ @dao.list { |key, value|
116
+ assert_equal key, KEY_STRING1
117
+ assert_equal value, VALUE_STRING1}
118
+
119
+ # 2. with no mappings
120
+ @dao.delete(KEY_STRING1)
121
+ assert_nil @dao.list
122
+
123
+ end
124
+
125
+
126
+ def test_map()
127
+ assert_equal VALUE_STRING1, @dao.get(KEY_STRING1)
128
+ end
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: steenzout-dao
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - ""
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-22 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 5
33
+ - 1
34
+ version: 1.5.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: steenzout-cfg
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 4
50
+ version: 1.0.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: tokyocabinet
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 53
62
+ segments:
63
+ - 1
64
+ - 29
65
+ version: "1.29"
66
+ type: :runtime
67
+ version_requirements: *id003
68
+ description: ""
69
+ email:
70
+ - ""
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - README.textile
77
+ files:
78
+ - lib/dao/manager.rb
79
+ - lib/dao/tokyocabinet.rb
80
+ - lib/steenzout-dao.rb
81
+ - README.textile
82
+ - test/unit/manager_test.rb
83
+ - test/unit/tokyocabinet_test.rb
84
+ has_rdoc: true
85
+ homepage: https://github.com/steenzout/steenzout-dao
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options: []
90
+
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.6.2
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: .
118
+ test_files:
119
+ - test/unit/manager_test.rb
120
+ - test/unit/tokyocabinet_test.rb