Skip to content

Commit

Permalink
A first hack at a thread safe transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Johnson committed Jun 4, 2016
1 parent 274a0df commit c8080e2
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/rdf/repository.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'thread'

module RDF
##
# An RDF repository.
Expand Down Expand Up @@ -248,6 +250,7 @@ module Implementation
def self.extend_object(obj)
obj.instance_variable_set(:@data, obj.options.delete(:data) ||
Hamster::Hash.new)
obj.instance_variable_set(:@mutex, Mutex.new)
obj.instance_variable_set(:@tx_class,
obj.options.delete(:transaction_class) ||
SerializedTransaction)
Expand Down Expand Up @@ -366,6 +369,12 @@ def snapshot
self.class.new(data: @data).freeze
end

##
# @private
def mutex
@mutex
end

protected

##
Expand Down Expand Up @@ -566,13 +575,15 @@ def execute
raise TransactionError, 'Cannot execute a rolled back transaction. ' \
'Open a new one instead.' if @rolledback

# `Hamster::Hash#==` will use a cheap `#equal?` check first, but fall
# back on a full Ruby Hash comparison if required.
raise TransactionError, 'Error merging transaction. Repository' \
'has changed during transaction time.' unless
repository.send(:data) == @base_snapshot.send(:data)
repository.mutex.synchronize do
# `Hamster::Hash#==` will use a cheap `#equal?` check first, but fall
# back on a full Ruby Hash comparison if required.
raise TransactionError, 'Error merging transaction. Repository' \
'has changed during transaction time.' unless
repository.send(:data) == @base_snapshot.send(:data)

repository.send(:data=, @snapshot.send(:data))
repository.send(:data=, @snapshot.send(:data))
end
end
end
end # Implementation
Expand Down

0 comments on commit c8080e2

Please sign in to comment.