Skip to content

Commit 29f33e2

Browse files
committed
Use updated mongo gem ~> 2.2. This changes the calling sequence when using a hash; URI form encouraged.
1 parent 4228512 commit 29f33e2

3 files changed

Lines changed: 62 additions & 55 deletions

File tree

lib/rdf/mongo.rb

Lines changed: 53 additions & 48 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,54 +108,63 @@ 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-
# URI in the form mongodb://host:port/db/collection
126-
# @option options [String, #to_s] :title (nil)
127-
# @option options [String] :host
128-
# @option options [Integer] :port
129-
# @option options [String] :db ('quadb')
130-
# @option options [String] :user for authentication
131-
# @option options [String] :password for authentication
132-
# @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+
#
133137
# @yield [repository]
134138
# @yieldparam [Repository] repository
135139
def initialize(options = {}, &block)
140+
collection = nil
136141
if options[:uri]
137142
options = options.dup
138-
uri = RDF::URI(options[:uri])
139-
options[:host] ||= uri.host
140-
options[:port] ||= uri.port
141-
_, db, collection = uri.path.split('/')
142-
options[:db] ||= db
143-
options[:collection] ||= collection
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)
144149
else
145150
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)
146156
end
147157

148-
options = {host: 'localhost', port: 27017, db: 'quadb', collection: 'quads'}.merge(options)
149-
@db = ::Mongo::Connection.new(options[:host], options[:port]).db(options[:db])
150-
@db.authenticate(options[:user], options[:password]) if options[:user] && options[:password]
151-
@coll = @db[options[:collection]]
152-
@coll.create_index("s")
153-
@coll.create_index("p")
154-
@coll.create_index("o")
155-
@coll.create_index("c")
156-
@coll.create_index([["s", ::Mongo::ASCENDING], ["p", ::Mongo::ASCENDING]])
157-
@coll.create_index([["s", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
158-
@coll.create_index([["p", ::Mongo::ASCENDING], ["o", ::Mongo::ASCENDING]])
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+
])
159168
super(options, &block)
160169
end
161170

@@ -173,16 +182,16 @@ def insert_statement(statement)
173182
st_mongo = statement.to_mongo
174183
st_mongo[:ct] ||= :default # Indicate statement is in the default graph
175184
#puts "insert statement: #{st_mongo.inspect}"
176-
@coll.update(st_mongo, st_mongo, upsert: true)
185+
@collection.update_one(st_mongo, st_mongo, upsert: true)
177186
end
178187

179188
# @see RDF::Mutable#delete_statement
180189
def delete_statement(statement)
181190
case statement.graph_name
182191
when nil
183-
@coll.remove(statement.to_mongo.merge('ct'=>:default))
192+
@collection.delete_one(statement.to_mongo.merge('ct'=>:default))
184193
else
185-
@coll.remove(statement.to_mongo)
194+
@collection.delete_one(statement.to_mongo)
186195
end
187196
end
188197

@@ -194,35 +203,33 @@ def durable?; true; end
194203
##
195204
# @private
196205
# @see RDF::Countable#empty?
197-
def empty?; @coll.count == 0; end
206+
def empty?; @collection.count == 0; end
198207

199208
##
200209
# @private
201210
# @see RDF::Countable#count
202211
def count
203-
@coll.count
212+
@collection.count
204213
end
205214

206215
def clear_statements
207-
@coll.remove
216+
@collection.delete_many
208217
end
209218

210219
##
211220
# @private
212221
# @see RDF::Enumerable#has_statement?
213222
def has_statement?(statement)
214-
!!@coll.find_one(statement.to_mongo)
223+
@collection.find(statement.to_mongo).count > 0
215224
end
216225
##
217226
# @private
218227
# @see RDF::Enumerable#each_statement
219228
def each_statement(&block)
220229
@nodes = {} # reset cache. FIXME this should probably be in Node.intern
221230
if block_given?
222-
@coll.find() do |cursor|
223-
cursor.each do |data|
224-
block.call(RDF::Statement.from_mongo(data))
225-
end
231+
@collection.find().each do |document|
232+
block.call(RDF::Statement.from_mongo(document))
226233
end
227234
end
228235
enum_statement
@@ -233,7 +240,7 @@ def each_statement(&block)
233240
# @private
234241
# @see RDF::Enumerable#has_graph?
235242
def has_graph?(value)
236-
!!@coll.find_one(RDF::Mongo::Conversion.to_mongo(value, :graph_name))
243+
@collection.find(RDF::Mongo::Conversion.to_mongo(value, :graph_name)).count > 0
237244
end
238245

239246
protected
@@ -250,10 +257,8 @@ def query_pattern(pattern, options = {}, &block)
250257
pm = pattern.to_mongo
251258
pm.merge!(c: nil, ct: :default) if pattern.graph_name == false
252259
#puts "query using #{pm.inspect}"
253-
@coll.find(pm) do |cursor|
254-
cursor.each do |data|
255-
block.call(RDF::Statement.from_mongo(data))
256-
end
260+
@collection.find(pm).each do |document|
261+
block.call(RDF::Statement.from_mongo(document))
257262
end
258263
end
259264

rdf-mongo.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ Gem::Specification.new do |gem|
2929

3030
gem.required_ruby_version = '>= 2.0'
3131
gem.requirements = []
32-
gem.add_runtime_dependency 'rdf', '>= 1.99', '< 3'
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', '< 3'
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
describe RDF::Mongo::Repository do
88
before :each do
9-
@load_durable = lambda {RDF::Mongo::Repository.new uri: "mongodb://localhost:27017/quadb/quads"}
9+
@logger = RDF::Spec.logger
10+
@load_durable = lambda {RDF::Mongo::Repository.new uri: "mongodb://localhost:27017/rdf-mongo/specs", logger: @logger}
1011
@repository = @load_durable.call
11-
@repository.coll.drop
12+
@repository.collection.drop
1213
end
1314

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

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

0 commit comments

Comments
 (0)