@@ -8,11 +8,11 @@ class Statement
88 # Creates a BSON representation of the statement.
99 # @return [Hash]
1010 def to_mongo
11- self . to_hash . inject ( { } ) do |hash , ( place_in_statement , entity ) |
12- hash . merge ( RDF ::Mongo ::Conversion . to_mongo ( entity , place_in_statement ) )
11+ self . to_hash . inject ( { } ) do |hash , ( place_in_statement , entity ) |
12+ hash . merge ( RDF ::Mongo ::Conversion . to_mongo ( entity , place_in_statement ) )
1313 end
1414 end
15-
15+
1616 ##
1717 # Create BSON for a statement representation. Note that if the statement has no graph name,
1818 # a value of `false` will be used to indicate the default context
@@ -27,7 +27,7 @@ def self.from_mongo(statement)
2727 graph_name : RDF ::Mongo ::Conversion . from_mongo ( statement [ 'c' ] , statement [ 'ct' ] , statement [ 'cl' ] ) )
2828 end
2929 end
30-
30+
3131 module Mongo
3232 autoload :VERSION , "rdf/mongo/version"
3333
@@ -68,7 +68,7 @@ def self.to_mongo(value, place_in_statement)
6868 v , k = value . to_s , :u
6969 end
7070 v = nil if v == ''
71-
71+
7272 case place_in_statement
7373 when :subject
7474 t , k1 , lt = :st , :s , :sl
@@ -114,7 +114,7 @@ class Repository < ::RDF::Repository
114114 # The collection used for storing quads
115115 # @return [Mongo::Collection]
116116 attr_reader :collection
117-
117+
118118 ##
119119 # Initializes this repository instance.
120120 #
@@ -171,28 +171,36 @@ def initialize(options = {}, &block)
171171 # @see RDF::Mutable#insert_statement
172172 def supports? ( feature )
173173 case feature . to_sym
174- when :graph_name then true
174+ when :graph_name then true
175+ when :atomic_write then true
175176 when :validity then @options . fetch ( :with_validity , true )
176177 else false
177178 end
178179 end
179-
180+
181+ def apply_changeset ( changeset )
182+ ops = [ ]
183+
184+ changeset . deletes . each do |d |
185+ ops << { delete_one : { filter : statement_to_delete ( d ) } }
186+ end
187+
188+ changeset . inserts . each do |i |
189+ ops << { update_one : { filter : statement_to_insert ( i ) , update : statement_to_insert ( i ) , upsert : true } }
190+ end
191+
192+ @collection . bulk_write ( ops , ordered : true )
193+ end
194+
180195 def insert_statement ( statement )
181- raise ArgumentError , "Statement #{ statement . inspect } is incomplete" if statement . incomplete?
182- st_mongo = statement . to_mongo
183- st_mongo [ :ct ] ||= :default # Indicate statement is in the default graph
184- #puts "insert statement: #{st_mongo.inspect}"
196+ st_mongo = statement_to_insert ( statement )
185197 @collection . update_one ( st_mongo , st_mongo , upsert : true )
186198 end
187199
188200 # @see RDF::Mutable#delete_statement
189201 def delete_statement ( statement )
190- case statement . graph_name
191- when nil
192- @collection . delete_one ( statement . to_mongo . merge ( 'ct' => :default ) )
193- else
194- @collection . delete_one ( statement . to_mongo )
195- end
202+ st_mongo = statement_to_delete ( statement )
203+ @collection . delete_one ( st_mongo )
196204 end
197205
198206 ##
@@ -261,13 +269,27 @@ def query_pattern(pattern, options = {}, &block)
261269 block . call ( RDF ::Statement . from_mongo ( document ) )
262270 end
263271 end
264-
272+
265273 private
266274
267275 def enumerator! # @private
268276 require 'enumerator' unless defined? ( ::Enumerable )
269277 @@enumerator_klass = defined? ( ::Enumerable ::Enumerator ) ? ::Enumerable ::Enumerator : ::Enumerator
270278 end
279+
280+ def statement_to_insert ( statement )
281+ raise ArgumentError , "Statement #{ statement . inspect } is incomplete" if statement . incomplete?
282+ st_mongo = statement . to_mongo
283+ st_mongo [ :ct ] ||= :default # Indicate statement is in the default graph
284+ #puts "insert statement: #{st_mongo.inspect}"
285+ st_mongo
286+ end
287+
288+ def statement_to_delete ( statement )
289+ st_mongo = statement . to_mongo
290+ st_mongo [ :ct ] = :default if statement . graph_name . nil?
291+ st_mongo
292+ end
271293 end
272294 end
273295end
0 commit comments