Module: RDF::Transactable

Included in:
Graph, Repository
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/mixin/transactable.rb

Overview

A transaction application mixin.

Classes that include this module must provide a #begin_transaction method returning an Transaction.

Examples:

running a read/write transaction with block syntax

repository = RDF::Repository.new # or other transactable

repository.transaction(mutable: true) do |tx|
  tx.insert [:node, RDF.type, RDF::OWL.Thing]
  # ...
end

See Also:

Since:

  • 2.0.0

Instance Method Summary collapse

Instance Method Details

#begin_transaction(mutable: false, graph_name: nil) ⇒ RDF::Transaction (protected)

Begins a new transaction.

Subclasses implementing transaction-capable storage adapters may wish to override this method in order to begin a transaction against the underlying storage.

Parameters:

  • mutable (Boolean) (defaults to: false)

    Create a mutable or immutable transaction.

  • graph_name (Boolean) (defaults to: nil)

    A default graph name for statements inserted or deleted (default: nil)

Returns:

Raises:

  • (NotImplementedError)

Since:

  • 2.0.0



81
82
83
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/mixin/transactable.rb', line 81

def begin_transaction(mutable: false, graph_name: nil)
  raise NotImplementedError
end

#commit_transaction(tx) (protected)

This method returns an undefined value.

Commits the given transaction.

Subclasses implementing transaction-capable storage adapters may wish to override this method in order to commit the given transaction to the underlying storage.

Parameters:

Since:

  • 0.3.0



105
106
107
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/mixin/transactable.rb', line 105

def commit_transaction(tx)
  tx.execute
end

#rollback_transaction(tx) (protected)

This method returns an undefined value.

Rolls back the given transaction.

Parameters:

Since:

  • 0.3.0



91
92
93
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/mixin/transactable.rb', line 91

def rollback_transaction(tx)
  tx.rollback
end

#transaction(mutable: false) ⇒ RDF::Transaction #transaction(mutable: false) {|tx| ... } ⇒ self Also known as: transact

Executes the given block in a transaction.

Raising an error within the transaction block causes automatic rollback.

Examples:

running a transaction

repository.transaction(mutable: true) do |tx|
  tx.insert [RDF::URI("https://rubygems.org/gems/rdf"), RDF::RDFS.label, "RDF.rb"]
end

manipulating a live transaction

tx = repository.transaction(mutable: true)
tx.insert [RDF::URI("https://rubygems.org/gems/rdf"), RDF::RDFS.label, "RDF.rb"]
tx.execute

Overloads:

  • #transaction(mutable: false) ⇒ RDF::Transaction

    Returns an open transaction; the client is responsible for closing the transaction via #execute or #rollback.

    Parameters:

    • mutable (Boolean) (defaults to: false)

    Returns:

    • (RDF::Transaction)

      an open transaction; the client is responsible for closing the transaction via #execute or #rollback

  • #transaction(mutable: false) {|tx| ... } ⇒ self

    Parameters:

    • mutable (Boolean) (defaults to: false)

      allows changes to the transaction, otherwise it is a read-only snapshot of the underlying repository.

    Yields:

    • (tx)

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

    Returns:

    • (self)

See Also:

Since:

  • 0.3.0



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/mixin/transactable.rb', line 50

def transaction(mutable: false, &block)
  tx = begin_transaction(mutable: mutable)
  return tx unless block_given?

  begin
    case block.arity
      when 1 then block.call(tx)
      else tx.instance_eval(&block)
    end
  rescue => error
    rollback_transaction(tx)
    raise error
  end
  commit_transaction(tx)
  self
end