@@ -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
0 commit comments