Skip to content

Commit 6b1fb84

Browse files
committed
Finish 2.0.0.beta1
2 parents 4d690c9 + 29f33e2 commit 6b1fb84

7 files changed

Lines changed: 86 additions & 58 deletions

File tree

.travis.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ script: "bundle exec rspec spec"
44
env:
55
- CI=true
66
rvm:
7-
- 1.9.3
87
- 2.0
9-
- 2.1
10-
- 2.2
11-
- jruby
8+
- 2.1.8
9+
- 2.2.4
10+
- 2.3.0
11+
- jruby-9.0.4.0
1212
- rbx-2
1313
services:
1414
- mongodb
1515
cache: bundler
16+
matrix:
17+
allow_failures:
18+
- rvm: rbx-2

Gemfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ source "http://rubygems.org"
33
gemspec
44

55
gem "rdf", git: "git://github.com/ruby-rdf/rdf.git", branch: "develop"
6+
gem "rdf-spec", git: "git://github.com/ruby-rdf/rdf-spec.git", branch: "develop"
7+
gem 'rdf-isomorphic', git: "git://github.com/ruby-rdf/rdf-isomorphic.git", branch: "develop"
68

7-
group :development do
8-
gem "rdf-spec", git: "git://github.com/ruby-rdf/rdf-spec.git", branch: "develop"
9+
group :debug do
10+
gem "byebug", platforms: :mri
911
gem "wirble"
1012
end
1113

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.99.0
1+
2.0.0.beta1

lib/rdf/mongo.rb

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def self.to_mongo(value, place_in_statement)
8080
t, k1, lt = :ct, :c, :cl
8181
end
8282
h = {k1 => v, t => k, lt => ll}
83-
h.delete_if {|k,v| h[k].nil?}
83+
h.delete_if {|kk,_| h[kk].nil?}
8484
end
8585

8686
##
@@ -108,48 +108,71 @@ def self.from_mongo(value, value_type = :u, literal_extra = nil)
108108

109109
class Repository < ::RDF::Repository
110110
# The Mongo database instance
111-
# @!attribute [r] db
112111
# @return [Mongo::DB]
113-
attr_reader :db
112+
attr_reader :client
114113

115114
# The collection used for storing quads
116-
# @!attribute [r] coll
117115
# @return [Mongo::Collection]
118-
attr_reader :coll
116+
attr_reader :collection
119117

120118
##
121119
# Initializes this repository instance.
122120
#
123-
# @param [Hash{Symbol => Object}] options
124-
# @option options [URI, #to_s] :uri (nil)
125-
# @option options [String, #to_s] :title (nil)
126-
# @option options [String] :host
127-
# @option options [Integer] :port
128-
# @option options [String] :db
129-
# @option options [String] :user for authentication
130-
# @option options [String] :password for authentication
131-
# @option options [String] :collection ('quads')
121+
# @overload initialize(options = {}, &block)
122+
# @param [Hash{Symbol => Object}] options
123+
# @option options [String, #to_s] :title (nil)
124+
# @option options [URI, #to_s] :uri (nil)
125+
# URI in the form `mongodb://host:port/db`. The URI should also identify the collection use, but appending a `collection` path component such as `mongodb://host:port/db/collection`, this ensures that the collection will be maintained if cloned. See [Mongo::Client options](https://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial-2-0/#uri-options-conversions) for more information on Mongo URIs.
126+
#
127+
# @overload initialize(options = {}, &block)
128+
# @param [Hash{Symbol => Object}] options
129+
# See [Mongo::Client options](https://docs.mongodb.org/ecosystem/tutorial/ruby-driver-tutorial-2-0/#uri-options-conversions) for more information on Mongo Client options.
130+
# @option options [String, #to_s] :title (nil)
131+
# @option options [String] :host
132+
# a single address or an array of addresses, which may contain a port designation
133+
# @option options [Integer] :port (27017) applied to host address(es)
134+
# @option options [String] :database ('quadb')
135+
# @option options [String] :collection ('quads')
136+
#
132137
# @yield [repository]
133138
# @yieldparam [Repository] repository
134139
def initialize(options = {}, &block)
135-
options = {host: 'localhost', port: 27017, db: 'quadb', collection: 'quads'}.merge(options)
136-
@db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
137-
@db.authenticate(options[:user], options[:password]) if options[:user] && options[:password]
138-
@coll = @db[options[:collection]]
139-
@coll.create_index("s")
140-
@coll.create_index("p")
141-
@coll.create_index("o")
142-
@coll.create_index("c")
143-
@coll.create_index([["s", ::Mongo::ASCENDING], ["p", ::Mongo::ASCENDING]])
144-
@coll.create_index([["s", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
145-
@coll.create_index([["p", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
140+
collection = nil
141+
if options[:uri]
142+
options = options.dup
143+
uri = RDF::URI(options.delete(:uri))
144+
_, db, coll = uri.path.split('/')
145+
collection = coll || options.delete(:collection)
146+
db ||= "quadb"
147+
uri.path = "/#{db}" if coll
148+
@client = ::Mongo::Client.new(uri.to_s, options)
149+
else
150+
warn "[DEPRECATION] RDF::Mongo::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless options.empty?
151+
options[:database] ||= options.delete(:db) # 1.x compat
152+
options[:database] ||= 'quadb'
153+
hosts = Array(options[:host] || 'localhost')
154+
hosts.map! {|h| "#{h}:#{options[:port]}"} if options[:port]
155+
@client = ::Mongo::Client.new(hosts, options)
156+
end
157+
158+
@collection = @client[options.delete(:collection) || 'quads']
159+
@collection.indexes.create_many([
160+
{key: {s: 1}},
161+
{key: {p: 1}},
162+
{key: {o: "hashed"}},
163+
{key: {c: 1}},
164+
{key: {s: 1, p: 1}},
165+
#{key: {s: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed
166+
#{key: {p: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed
167+
])
146168
super(options, &block)
147169
end
148170

149171
# @see RDF::Mutable#insert_statement
150172
def supports?(feature)
151173
case feature.to_sym
152174
when :graph_name then true
175+
when :validity then @options.fetch(:with_validity, true)
153176
else false
154177
end
155178
end
@@ -159,16 +182,16 @@ def insert_statement(statement)
159182
st_mongo = statement.to_mongo
160183
st_mongo[:ct] ||= :default # Indicate statement is in the default graph
161184
#puts "insert statement: #{st_mongo.inspect}"
162-
@coll.update(st_mongo, st_mongo, upsert: true)
185+
@collection.update_one(st_mongo, st_mongo, upsert: true)
163186
end
164187

165188
# @see RDF::Mutable#delete_statement
166189
def delete_statement(statement)
167190
case statement.graph_name
168191
when nil
169-
@coll.remove(statement.to_mongo.merge('ct'=>:default))
192+
@collection.delete_one(statement.to_mongo.merge('ct'=>:default))
170193
else
171-
@coll.remove(statement.to_mongo)
194+
@collection.delete_one(statement.to_mongo)
172195
end
173196
end
174197

@@ -180,35 +203,33 @@ def durable?; true; end
180203
##
181204
# @private
182205
# @see RDF::Countable#empty?
183-
def empty?; @coll.count == 0; end
206+
def empty?; @collection.count == 0; end
184207

185208
##
186209
# @private
187210
# @see RDF::Countable#count
188211
def count
189-
@coll.count
212+
@collection.count
190213
end
191214

192215
def clear_statements
193-
@coll.remove
216+
@collection.delete_many
194217
end
195218

196219
##
197220
# @private
198221
# @see RDF::Enumerable#has_statement?
199222
def has_statement?(statement)
200-
!!@coll.find_one(statement.to_mongo)
223+
@collection.find(statement.to_mongo).count > 0
201224
end
202225
##
203226
# @private
204227
# @see RDF::Enumerable#each_statement
205228
def each_statement(&block)
206229
@nodes = {} # reset cache. FIXME this should probably be in Node.intern
207230
if block_given?
208-
@coll.find() do |cursor|
209-
cursor.each do |data|
210-
block.call(RDF::Statement.from_mongo(data))
211-
end
231+
@collection.find().each do |document|
232+
block.call(RDF::Statement.from_mongo(document))
212233
end
213234
end
214235
enum_statement
@@ -219,7 +240,7 @@ def each_statement(&block)
219240
# @private
220241
# @see RDF::Enumerable#has_graph?
221242
def has_graph?(value)
222-
!!@coll.find_one(RDF::Mongo::Conversion.to_mongo(value, :context))
243+
@collection.find(RDF::Mongo::Conversion.to_mongo(value, :graph_name)).count > 0
223244
end
224245

225246
protected
@@ -228,17 +249,16 @@ def has_graph?(value)
228249
# @private
229250
# @see RDF::Queryable#query_pattern
230251
# @see RDF::Query::Pattern
231-
def query_pattern(pattern, &block)
252+
def query_pattern(pattern, options = {}, &block)
253+
return enum_for(:query_pattern, pattern, options) unless block_given?
232254
@nodes = {} # reset cache. FIXME this should probably be in Node.intern
233255

234256
# A pattern graph_name of `false` is used to indicate the default graph
235257
pm = pattern.to_mongo
236258
pm.merge!(c: nil, ct: :default) if pattern.graph_name == false
237259
#puts "query using #{pm.inspect}"
238-
@coll.find(pm) do |cursor|
239-
cursor.each do |data|
240-
block.call(RDF::Statement.from_mongo(data))
241-
end
260+
@collection.find(pm).each do |document|
261+
block.call(RDF::Statement.from_mongo(document))
242262
end
243263
end
244264

lib/rdf/mongo/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ def self.to_str() STRING end
1515

1616
##
1717
# @return [Array(Integer, Integer, Integer)]
18-
def self.to_a() [MAJOR, MINOR, TINY] end
18+
def self.to_a() STRING.split(".") end
1919
end
2020
end

rdf-mongo.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ Gem::Specification.new do |gem|
2727
gem.test_files = Dir.glob('spec/*.spec')
2828
gem.has_rdoc = false
2929

30-
gem.required_ruby_version = '>= 1.9.3'
30+
gem.required_ruby_version = '>= 2.0'
3131
gem.requirements = []
32-
gem.add_runtime_dependency 'rdf', '~> 1.99'
33-
gem.add_runtime_dependency 'mongo', '~> 1.10'
32+
gem.add_runtime_dependency 'rdf', '>= 2.0.0.beta', '< 3'
33+
gem.add_runtime_dependency 'mongo', '~> 2.2'
3434

35-
gem.add_development_dependency 'rdf-spec', '~> 1.99'
35+
gem.add_development_dependency 'rdf-spec', '>= 2.0.0.beta', '< 3'
3636
gem.add_development_dependency 'rspec', '~> 3.0'
3737
gem.add_development_dependency 'rspec-its', '~> 1.0'
3838
gem.add_development_dependency 'yard' , '~> 0.8'

spec/mongo_repository_spec.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
describe RDF::Mongo::Repository do
88
before :each do
9-
@repository = RDF::Mongo::Repository.new() # TODO: Do you need constructor arguments?
10-
@repository.coll.drop
9+
@logger = RDF::Spec.logger
10+
@load_durable = lambda {RDF::Mongo::Repository.new uri: "mongodb://localhost:27017/rdf-mongo/specs", logger: @logger}
11+
@repository = @load_durable.call
12+
@repository.collection.drop
1113
end
1214

13-
after :each do
14-
@repository.coll.drop
15+
after :each do |example|
16+
#puts @logger.to_s if example.exception
17+
@repository.collection.drop
1518
end
1619

1720
# @see lib/rdf/spec/repository.rb in RDF-spec

0 commit comments

Comments
 (0)