class RDF::Repository

An RDF repository.

@example Creating a transient in-memory repository

repository = RDF::Repository.new

@example Checking whether a repository is readable/writable

repository.readable?
repository.writable?

@example Checking whether a repository is persistent or transient

repository.persistent?
repository.transient?

@example Checking whether a repository is empty

repository.empty?

@example Checking how many statements a repository contains

repository.count

@example Checking whether a repository contains a specific statement

repository.has_statement?(statement)

@example Enumerating statements in a repository

repository.each_statement { |statement| statement.inspect! }

@example Inserting statements into a repository

repository.insert(*statements)
repository.insert(statement)
repository.insert([subject, predicate, object])
repository << statement
repository << [subject, predicate, object]

@example Deleting statements from a repository

repository.delete(*statements)
repository.delete(statement)
repository.delete([subject, predicate, object])

@example Deleting all statements from a repository

repository.clear!

Repositories support transactions with a variety of ACID semantics:

Atomicity is indicated by `#supports?(:atomic_write)`. When atomicity is supported, writes through `#transaction`, `#apply_changeset` and `#delete_insert` are applied atomically.

Consistency should be guaranteed, in general. Repositories that don't support consistency, or that have specialized definitions of consistency above those declared by the RDF data model, should advertise this fact in their documentation.

Isolation may be supported at various levels, indicated by `#isolation_level`:

- `:read_uncommitted`: Inserts & deletes in an uncommitted transaction 
   scope may be visible to other transactions (or via `#each`, etc...)
- `:read_committed`: Inserts & deletes may be visible to other 
   transactions once committed
- `:repeatable_read`: Phantom reads may be possible
- `:snapshot`: A transaction reads a consistent snapshot of the data. 
   Write skew anomalies may occur (for various definitions of consistency)
- `:serializable`: A transaction reads a consistent snapshot of the data.
   When two or more transactions attempt conflicting writes, only one of
   them may succeed.

Durability is noted via `RDF::Durable` support and `#durable?` /`#nondurable?`.

@example Transational read from a repository

repository.transaction do |tx|
  tx.has_statement?(statement)
  tx.query([:s, :p, :o])
end

@example Transational read/write of a repository

repository.transaction(mutable: true) do |tx|
  tx.insert(*statements)
  tx.insert(statement)
  tx.insert([subject, predicate, object])
  tx.delete(*statements)
  tx.delete(statement)
  tx.delete([subject, predicate, object])
end

Constants

DEFAULT_TX_CLASS

Attributes

options[R]

Returns the options passed to this repository when it was constructed.

@return [Hash{Symbol => Object}]

title[R]

Returns the title of this repository.

@return [String]

uri[R]

Returns the {URI} of this repository.

@return [URI]

url[R]

Returns the {URI} of this repository.

@return [URI]

Public Class Methods

load(urls, options = {}, &block) click to toggle source

Loads one or more RDF files into a new transient in-memory repository.

@param [String, Array<String>] urls @param [Hash{Symbol => Object}] options

Options from {RDF::Repository#initialize} and {RDF::Mutable#load}

@yield [repository] @yieldparam [Repository] @return [void]

# File lib/rdf/repository.rb, line 121
def self.load(urls, options = {}, &block)
  self.new(options) do |repository|
    Array(urls).each do |url|
      repository.load(url, options)
    end

    if block_given?
      case block.arity
        when 1 then block.call(repository)
        else repository.instance_eval(&block)
      end
    end
  end
end
new(uri: nil, title: nil, **options, &block) click to toggle source

Initializes this repository instance.

@param [URI, to_s] uri (nil) @param [String, to_s] title (nil) @param [Hash{Symbol => Object}] options @option options [Boolean] :with_graph_name (true)

Indicates that the repository supports named graphs, otherwise,
only the default graph is supported.

@option options [Boolean] :with_validity (true)

Indicates that the repository supports named validation.

@option options [Boolean] :transaction_class (DEFAULT_TX_CLASS)

Specifies the RDF::Transaction implementation to use in this Repository.

@yield [repository] @yieldparam [Repository] repository

# File lib/rdf/repository.rb, line 151
def initialize(uri: nil, title: nil, **options, &block)
  @options = {with_graph_name: true, with_validity: true}.merge(options)
  @uri     = uri
  @title   = title

  # Provide a default in-memory implementation:
  send(:extend, Implementation) if self.class.equal?(RDF::Repository)

  @tx_class ||= @options.delete(:transaction_class) { DEFAULT_TX_CLASS }

  if block_given?
    case block.arity
      when 1 then block.call(self)
      else instance_eval(&block)
    end
  end
end

Public Instance Methods

delete_insert(deletes, inserts) click to toggle source

Performs a set of deletes and inserts as a combined operation within a transaction. The Repository's transaction semantics apply to updates made through this method.

@see RDF::Mutable#delete_insert

Calls superclass method RDF::Mutable#delete_insert
# File lib/rdf/repository.rb, line 195
def delete_insert(deletes, inserts)
  return super unless supports?(:atomic_write)

  transaction(mutable: true) do
    deletes.respond_to?(:each_statement) ? delete(deletes) : delete(*deletes)
    inserts.respond_to?(:each_statement) ? insert(inserts) : insert(*inserts)
  end
end
isolation_level() click to toggle source

@see RDF::Dataset#isolation_level

Calls superclass method RDF::Dataset#isolation_level
# File lib/rdf/repository.rb, line 214
def isolation_level
  supports?(:snapshot) ? :repeatable_read : super
end
project_graph(graph_name, &block) click to toggle source

@private @see RDF::Enumerable#project_graph

# File lib/rdf/repository.rb, line 207
def project_graph(graph_name, &block)
  RDF::Graph.new(graph_name: graph_name, data: self).
    project_graph(graph_name, &block)
end
snapshot() click to toggle source

A queryable snapshot of the repository for isolated reads.

@return [Dataset] an immutable Dataset containing a current snapshot of

the Repository contents.
# File lib/rdf/repository.rb, line 223
def snapshot
  raise NotImplementedError.new("#{self.class}#snapshot")
end
supports?(feature) click to toggle source

Returns `true` if this respository supports the given `feature`.

Supported features include those from {RDF::Enumerable#supports?} along

with the following:
 * `:atomic_write` supports atomic write in transaction scopes
 * `:snapshots` supports creation of immutable snapshots of current 
      contents via #snapshot.

@see RDF::Enumerable#supports?

# File lib/rdf/repository.rb, line 178
def supports?(feature)
  case feature.to_sym
  when :graph_name   then @options[:with_graph_name]
  when :inference    then false  # forward-chaining inference
  when :validity     then @options.fetch(:with_validity, true)
  when :atomic_write then false
  when :snapshots    then false
  else false
  end
end

Protected Instance Methods

begin_transaction(mutable: false, graph_name: nil) click to toggle source

@private @see RDF::Transactable#begin_transaction @since 0.3.0

# File lib/rdf/repository.rb, line 233
def begin_transaction(mutable: false, graph_name: nil)
  @tx_class.new(self, mutable: mutable, graph_name: graph_name)
end