Module: RDF::Repository::Implementation

Defined in:
lib/rdf/repository.rb

Overview

A default Repository implementation supporting atomic writes and serializable transactions.

See Also:

Defined Under Namespace

Classes: SerializedTransaction

Constant Summary collapse

DEFAULT_GRAPH =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extend_object(obj) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/rdf/repository.rb', line 251

def self.extend_object(obj)
  obj.instance_variable_set(:@data, obj.options.delete(:data) || 
                                    Hamster::Hash.new)
  obj.instance_variable_set(:@tx_class, 
                            obj.options.delete(:transaction_class) || 
                            SerializedTransaction)
  super
end

Instance Method Details

#apply_changeset(changeset) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rdf/repository.rb', line 343

def apply_changeset(changeset)
  data = @data
  changeset.deletes.each do |del|
    if del.constant?
      data = delete_from(data, del)
    else
      # we need this condition to handle wildcard statements
      query_pattern(del) { |stmt| data = delete_from(data, stmt) }
    end
  end
  changeset.inserts.each { |ins| data = insert_to(data, ins) }
  @data = data
end

#countObject

See Also:



279
280
281
282
283
284
285
286
287
# File 'lib/rdf/repository.rb', line 279

def count
  count = 0
  @data.each do |_, ss|
    ss.each do |_, ps|
      ps.each { |_, os| count += os.size }
    end
  end
  count
end

#each_graph(&block) ⇒ Object



306
307
308
309
310
311
312
313
# File 'lib/rdf/repository.rb', line 306

def each_graph(&block)
  if block_given?
    @data.each_key do |gn|
      yield RDF::Graph.new(graph_name: (gn == DEFAULT_GRAPH ? nil : gn), data: self)
    end
  end
  enum_graph
end

#each_statement(&block) ⇒ Object Also known as: each



325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/rdf/repository.rb', line 325

def each_statement(&block)
  if block_given?
    @data.each do |g, ss|
      ss.each do |s, ps|
        ps.each do |p, os|
          os.each do |o, object_options|
            yield RDF::Statement.new(s, p, o, object_options.merge(graph_name: g.equal?(DEFAULT_GRAPH) ? nil : g))
          end
        end
      end
    end
  end
  enum_statement
end

#graph_names(options = nil, &block) ⇒ Object



299
300
301
# File 'lib/rdf/repository.rb', line 299

def graph_names(options = nil, &block)        
  @data.keys.reject { |g| g == DEFAULT_GRAPH }.to_a
end

#has_graph?(graph) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



292
293
294
# File 'lib/rdf/repository.rb', line 292

def has_graph?(graph)
  @data.has_key?(graph)
end

#has_statement?(statement) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



318
319
320
# File 'lib/rdf/repository.rb', line 318

def has_statement?(statement)
  has_statement_in?(@data, statement)
end

#isolation_levelObject



359
360
361
# File 'lib/rdf/repository.rb', line 359

def isolation_level
  :serializable
end

#snapshotDataset

A readable & queryable snapshot of the repository for isolated reads.

Returns:

  • (Dataset)

    an immutable Dataset containing a current snapshot of the Repository contents.

See Also:



370
371
372
# File 'lib/rdf/repository.rb', line 370

def snapshot
  self.class.new(data: @data).freeze
end

#supports?(feature) ⇒ Boolean

Returns:

  • (Boolean)

See Also:



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rdf/repository.rb', line 263

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 true
  when :rdfstar          then true
  when :snapshots        then true
  else false
  end
end