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.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.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
Returns the options passed to this repository when it was constructed.
@return [Hash{Symbol => Object}]
Returns the title of this repository.
@return [String]
Returns the {URI} of this repository.
@return [URI]
Returns the {URI} of this repository.
@return [URI]
Public Class Methods
Source
# 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
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]
Source
# 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
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
Public Instance Methods
Source
# File lib/rdf/repository.rb, line 198 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
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
RDF::Mutable#delete_insert
Source
# File lib/rdf/repository.rb, line 218 def isolation_level supports?(:snapshots) ? :repeatable_read : super end
@see RDF::Dataset#isolation_level
RDF::Dataset#isolation_level
Source
# File lib/rdf/repository.rb, line 210 def project_graph(graph_name, &block) graph = RDF::Graph.new(graph_name: graph_name, data: self) graph.each(&block) if block_given? graph end
@private @see RDF::Enumerable#project_graph
Source
# File lib/rdf/repository.rb, line 227 def snapshot raise NotImplementedError.new("#{self.class}#snapshot") end
A queryable snapshot of the repository for isolated reads.
@return [Dataset] an immutable Dataset
containing a current snapshot of
the Repository contents.
Source
# 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 :literal_equality then true when :atomic_write then false when :rdf_full then false when :base_direction then false when :snapshots then false else false end end
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.
Protected Instance Methods
Source
# File lib/rdf/repository.rb, line 237 def begin_transaction(mutable: false, graph_name: nil) @tx_class.new(self, mutable: mutable, graph_name: graph_name) end
@private @see RDF::Transactable#begin_transaction
@since 0.3.0