rdf-mongo 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Pius Uzamere, Quotation.ws
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # rdf-mongo :: MongoDB storage adapter for RDF.rb
2
+
3
+ This is an RDF.rb storage adapter for MongoDB.
4
+
5
+ See <http://blog.datagraph.org/2010/04/rdf-repository-howto> for an overview.
6
+
7
+ ## Versioning and backwards compatibility.
8
+
9
+ Moving forward, the versioning will reflect the RDF.rb version number for which all rdf-specs are passing.
10
+
11
+ It should also be noted that prior to 1.0, there are no guarantees of backwards compatibility for data stored using previous versions of the gem. This is to make optimizing the schema for MongoDB easy.
12
+
13
+ ## Requirements
14
+
15
+ You'll need the 'mongo', 'rdf', 'rdf-spec', and 'rspec' libraries. The easiest way to install these is via RubyGems.
16
+
17
+ $ sudo gem install mongo rdf rdf-spec rspec rdf-mongo
18
+
19
+ ## Ruby 1.9 Compatibility
20
+
21
+ Please note that you'll need to use my forks of rdf and rdf-spec for Ruby 1.9 compatibility until my patches are pushed into the mainline (which'll hopefully happen within a few days). To use them, uninstall the legacy rdf and rdf-spec gems and install the compatibility gems:
22
+
23
+ $ sudo gem install rdf-ruby19 rdf-spec-ruby19
24
+
25
+
26
+ ### Support
27
+
28
+ Please post questions or feedback to the [W3C-ruby-rdf mailing list][].
29
+
30
+ ### Authors
31
+ * Pius Uzamere | <pius@alum.mit.edu> | <http://github.com/pius> | <http://pius.me>
32
+
33
+ ### Thank you
34
+
35
+ * Ben Lavender (author of the adapter skeleton) | <blavender@gmail.com> | <http://github.com/bhuga> | <http://bhuga.net> | <http://blog.datagraph.org>
36
+
37
+ ### License
38
+
39
+ MIT License
40
+
41
+ [W3C-ruby-rdf mailing list]: http://lists.w3.org/Archives/Public/public-rdf-ruby/
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.7
data/lib/rdf/mongo.rb ADDED
@@ -0,0 +1,172 @@
1
+ require 'rdf'
2
+ require 'enumerator'
3
+ require 'mongo'
4
+
5
+ module Mongo
6
+ class Cursor
7
+ def rdf_each(&block)
8
+ if block_given?
9
+ each {|statement| block.call(RDF::Statement.from_mongo(statement)) }
10
+ else
11
+ self#each {|statement| RDF::Statement.from_mongo(statement) }
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module RDF
18
+ class Statement
19
+ def to_mongo
20
+ self.to_hash.merge({:context => self.context}).inject({}) { |hash, (place_in_statement, entity)|
21
+ hash.merge(RDF::Mongo::Conversion.to_mongo(entity, place_in_statement))
22
+ }
23
+ end
24
+
25
+ def self.from_mongo(statement)
26
+ RDF::Statement.new(
27
+ :subject => RDF::Mongo::Conversion.from_mongo(statement['s'], statement['st'], statement['sl']),
28
+ :predicate => RDF::Mongo::Conversion.from_mongo(statement['p'], statement['pt'], statement['pl']),
29
+ :object => RDF::Mongo::Conversion.from_mongo(statement['o'], statement['ot'], statement['ol']),
30
+ :context => RDF::Mongo::Conversion.from_mongo(statement['c'], statement['ct'], statement['cl']))
31
+ end
32
+ end
33
+
34
+ module Mongo
35
+ class Conversion
36
+ #TODO: Add support for other types of entities
37
+
38
+ def self.to_mongo(value, place_in_statement)
39
+ case value
40
+ when RDF::URI
41
+ v, k = value.to_s, :u
42
+ when RDF::Literal
43
+ v, k, ll = value.value, :l, value.language
44
+ when nil
45
+ v, k = nil, nil
46
+ else
47
+ v, k = value.to_s, :u
48
+ end
49
+
50
+ case place_in_statement
51
+ when :subject
52
+ t, k1, lt = :st, :s, :sl
53
+ when :predicate
54
+ t, k1, lt = :pt, :p, :pl
55
+ when :object
56
+ t, k1, lt = :ot, :o, :ol
57
+ when :context
58
+ t, k1, lt = :ct, :c, :cl
59
+ end
60
+ h = {k1 => (v == '' ? nil : v), t => (k == '' ? nil : k), lt => ll}
61
+ h.delete_if {|k,v| h[k].nil?}
62
+ end
63
+
64
+ def self.from_mongo(value, value_type = :u, lang = nil)
65
+ case value_type
66
+ when :u
67
+ RDF::URI.new(value)
68
+ when :l
69
+ RDF::Literal.new(value, :language => lang)
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+ class Repository < ::RDF::Repository
76
+
77
+ def db
78
+ @db
79
+ end
80
+
81
+ def coll
82
+ @coll
83
+ end
84
+
85
+ def initialize(options = {:host => 'localhost', :port => 27017, :db => 'quadb'})
86
+ @db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
87
+ @coll = @db['quads']
88
+ end
89
+
90
+ # @see RDF::Enumerable#each.
91
+ def each(&block)
92
+ if block_given?
93
+ statements = @coll.find()
94
+ statements.each {|statement| block.call(RDF::Statement.from_mongo(statement)) }
95
+ else
96
+ statements = @coll.find()
97
+ enumerator!.new(statements,:rdf_each)
98
+ #nasty ... in Ruby 1.9, Enumerator doesn't exist under Enumerable
99
+ end
100
+ end
101
+
102
+ # @see RDF::Mutable#insert_statement
103
+ def insert_statement(statement)
104
+ @coll.update(statement.to_mongo, statement.to_mongo, :upsert => true)
105
+ end
106
+
107
+ # @see RDF::Mutable#delete_statement
108
+ def delete_statement(statement)
109
+ @coll.remove(statement.to_mongo)
110
+ end
111
+
112
+ def count
113
+ @coll.count
114
+ end
115
+
116
+ def query(pattern, &block)
117
+ case pattern
118
+ when RDF::Statement
119
+ query(pattern.to_hash)
120
+ when Array
121
+ query(RDF::Statement.new(*pattern))
122
+ when Hash
123
+
124
+ statements = query_hash(pattern)
125
+ the_statements = statements || []
126
+ case block_given?
127
+ when true
128
+ the_statements.each {|s| block.call(RDF::Statement.from_mongo(s))}
129
+ else
130
+ #e = enumerator!.new(statements.extend(RDF::Queryable),:rdf_each)
131
+ #s = the_statements.extend(RDF::Enumerable, RDF::Queryable)
132
+ def the_statements.each(&block)
133
+ if block_given?
134
+ super {|statement| block.call(RDF::Statement.from_mongo(statement)) }
135
+ else
136
+ enumerator!.new(the_statements,:rdf_each)
137
+ end
138
+ end
139
+
140
+ def the_statements.size
141
+ count
142
+ end
143
+ s = the_statements
144
+ end
145
+ else
146
+ super(pattern)
147
+ end
148
+ end
149
+
150
+ def query_hash(hash)
151
+ return @coll.find if hash.empty?
152
+ h = RDF::Statement.new(hash).to_mongo
153
+ # h = {}
154
+ # (h[:s] = hash[:subject]) if hash[:subject]
155
+ # (h[:p] = hash[:predicate]) if hash[:predicate]
156
+ # (h[:o] = hash[:object]) if hash[:object]
157
+ # (h[:c] = hash[:context]) if hash[:context]
158
+ @coll.find(h)
159
+ end
160
+
161
+
162
+ private
163
+
164
+ def enumerator! # @private
165
+ require 'enumerator' unless defined?(::Enumerable)
166
+ @@enumerator_klass = defined?(::Enumerable::Enumerator) ? ::Enumerable::Enumerator : ::Enumerator
167
+ end
168
+
169
+
170
+ end
171
+ end
172
+ end
@@ -0,0 +1,24 @@
1
+ $:.unshift File.dirname(__FILE__) + "/../lib/"
2
+
3
+ require 'rdf'
4
+ require 'rdf/spec/repository'
5
+ require 'rdf/mongo'
6
+ require 'enumerator'
7
+
8
+ describe RDF::Mongo::Repository do
9
+ context "Mongo RDF Repository" do
10
+ before :each do
11
+ @repository = RDF::Mongo::Repository.new() # TODO: Do you need constructor arguments?
12
+ @repository.coll.drop
13
+ end
14
+
15
+ after :each do
16
+ @repository.coll.drop
17
+ end
18
+
19
+ # @see lib/rdf/spec/repository.rb in RDF-spec
20
+ it_should_behave_like RDF_Repository
21
+ end
22
+
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdf-mongo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 7
9
+ version: 0.1.7
10
+ platform: ruby
11
+ authors:
12
+ - Pius Uzamere
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-04-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rdf-ruby19
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 1
30
+ - 7
31
+ version: 0.1.7
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rdf-spec-ruby19
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 1
44
+ - 6
45
+ version: 0.1.6
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 3
58
+ - 0
59
+ version: 1.3.0
60
+ type: :development
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ - 5
72
+ - 3
73
+ version: 0.5.3
74
+ type: :development
75
+ version_requirements: *id004
76
+ - !ruby/object:Gem::Dependency
77
+ name: addressable
78
+ prerelease: false
79
+ requirement: &id005 !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ segments:
84
+ - 2
85
+ - 1
86
+ - 1
87
+ version: 2.1.1
88
+ type: :runtime
89
+ version_requirements: *id005
90
+ description: rdf-mongo is a storage adapter for integrating MongoDB and rdf.rb, a Ruby library for working with Resource Description Framework (RDF) data.
91
+ email: pius@alum.mit.edu
92
+ executables: []
93
+
94
+ extensions: []
95
+
96
+ extra_rdoc_files: []
97
+
98
+ files:
99
+ - LICENSE
100
+ - VERSION
101
+ - README.md
102
+ - lib/rdf/mongo.rb
103
+ has_rdoc: false
104
+ homepage: http://github.com/pius/rdf-mongo
105
+ licenses:
106
+ - MIT License
107
+ post_install_message: Have fun! :)
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 1
118
+ - 8
119
+ - 2
120
+ version: 1.8.2
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.6
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: A storage adapter for integrating MongoDB and rdf.rb, a Ruby library for working with Resource Description Framework (RDF) data.
135
+ test_files:
136
+ - spec/mongo_repository.spec