Class: ActiveTriples::BufferedTransaction

Inherits:
RDF::Repository::Implementation::SerializedTransaction
  • Object
show all
Defined in:
lib/active_triples/util/buffered_transaction.rb

Overview

A buffered trasaction for use with ‘ActiveTriples::ParentStrategy`.

If an ‘ActiveTriples::RDFSource` instance is passed as the underlying repository, this transaction will try to find an existing `BufferedTransaction` to use as the basis for a snapshot. When the transaction is executed, the inserts and deletes are replayed against the `RDFSource`.

If a ‘RDF::Transaction::TransactionError` is raised on commit, this transaction optimistically attempts to replay the changes.

Reads are projected onto a specialized “Extended Bounded Description” subgraph.

See Also:

  • Util::ExtendedBoundedDescription

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, ancestors: [], subject: nil, graph_name: nil, mutable: false, **options, &block) ⇒ BufferedTransaction

Returns a new instance of BufferedTransaction.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_triples/util/buffered_transaction.rb', line 30

def initialize(repository,
               ancestors:  [],
               subject:    nil, 
               graph_name: nil, 
               mutable:    false, 
               **options,
               &block)
  @subject   = subject
  @ancestors = ancestors

  if repository.is_a?(RDFSource)
    if repository.persistence_strategy.graph.is_a?(BufferedTransaction)
      super
      @snapshot = repository.persistence_strategy.graph.snapshot
      return
    else
      repository = repository.persistence_strategy.graph.data
    end
  end

  return super
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors.



28
# File 'lib/active_triples/util/buffered_transaction.rb', line 28

attr_reader :snapshot, :subject, :ancestors

#snapshotObject (readonly)

Returns RDF::Dataset.

Returns:

  • RDF::Dataset



28
29
30
# File 'lib/active_triples/util/buffered_transaction.rb', line 28

def snapshot
  @snapshot
end

#subjectObject (readonly)

Returns RDF::Term.

Returns:

  • RDF::Term



28
# File 'lib/active_triples/util/buffered_transaction.rb', line 28

attr_reader :snapshot, :subject, :ancestors

Instance Method Details

#dataBufferedTransaction

Returns self.

Returns:



63
64
65
# File 'lib/active_triples/util/buffered_transaction.rb', line 63

def data
  self
end

#delete_statement(statement) ⇒ Object

Adds statement to the ‘deletes` collection of the buffered changeset and updates the snapshot.

See Also:

  • RDF::Transaction#delete_statement


89
90
91
92
93
# File 'lib/active_triples/util/buffered_transaction.rb', line 89

def delete_statement(statement)
  @changes.delete(statement)
  @changes.inserts.delete(statement)
  super
end

#executeObject

Executes optimistically. If errors are encountered, we replay the buffer on the latest version.

If the ‘repository` is a transaction, we immediately replay the buffer onto it.

See Also:

  • RDF::Transaction#execute


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_triples/util/buffered_transaction.rb', line 103

def execute
  raise TransactionError, 'Cannot execute a rolled back transaction. ' \
                          'Open a new one instead.' if @rolledback
  return if changes.empty?
  return super unless repository.is_a?(ActiveTriples::RDFSource)

  repository.insert(changes.inserts)
  repository.delete(changes.deletes)
rescue RDF::Transaction::TransactionError => err
  raise err if @rolledback

  # replay changest on the current version of the repository
  repository.delete(*changes.deletes)
  repository.insert(*changes.inserts)
end

#insert_statement(statement) ⇒ Object

Adds statement to the ‘inserts` collection of the buffered changeset and updates the snapshot.

See Also:

  • RDF::Mutable#insert_statement


78
79
80
81
82
# File 'lib/active_triples/util/buffered_transaction.rb', line 78

def insert_statement(statement)
  @changes.insert(statement)
  @changes.deletes.delete(statement)
  super
end

#isolation_levelObject

Provides :repeatable_read isolation (???)

See Also:

  • RDF::Transaction#isolation_level


57
58
59
# File 'lib/active_triples/util/buffered_transaction.rb', line 57

def isolation_level
  :repeatable_read
end

#supports?(feature) ⇒ Boolean

Returns:

  • (Boolean)

See Also:

  • RDF::Mutable#supports


69
70
71
# File 'lib/active_triples/util/buffered_transaction.rb', line 69

def supports?(feature)
  return true if feature.to_sym == :snapshots
end