Class: RDF::HamsterRepo
- Inherits:
-
Repository
- Object
- Repository
- RDF::HamsterRepo
- Defined in:
- lib/rdf/hamster_repo.rb
Overview
Sub-class of RDF::Repository with order-preserving properties.
Defined Under Namespace
Modules: VERSION
Constant Summary collapse
- DEFAULT_GRAPH =
false
Class Method Summary collapse
Instance Method Summary collapse
- #apply_changeset(changeset) ⇒ Object
- #count ⇒ Object
- #each_graph(&block) ⇒ Object
- #each_statement(&block) ⇒ Object (also: #each)
- #graph?(*args) ⇒ Object (also: #has_graph?)
- #graph_names(options = nil, &block) ⇒ Object
-
#initialize(uri: nil, title: nil, **options) {|repository| ... } ⇒ HamsterRepo
constructor
Initializes this repository instance.
- #isolation_level ⇒ Object
-
#snapshot ⇒ Dataset
A readable & queryable snapshot of the repository for isolated reads.
- #statement?(*args) ⇒ Object (also: #has_statement?)
-
#supports?(feature) ⇒ Boolean
Returns ‘true` if this respository supports the given `feature`.
-
#to_query ⇒ RDF::Query
Creates a query from the statements in this repository, turning blank nodes into non-distinguished variables.
Constructor Details
#initialize(uri: nil, title: nil, **options) {|repository| ... } ⇒ HamsterRepo
Initializes this repository instance.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rdf/hamster_repo.rb', line 36 def initialize(uri: nil, title: nil, **, &block) @data = .delete(:data) || Hamster::Hash.new super do if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end end |
Class Method Details
.extend_object(obj) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rdf/hamster_repo.rb', line 12 def self.extend_object(obj) obj.instance_variable_set(:@data, obj..delete(:data) || Hamster::Hash.new) obj.instance_variable_set(:@tx_class, obj..delete(:transaction_class) || RDF::Transaction::SerializedTransaction) super end |
Instance Method Details
#apply_changeset(changeset) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rdf/hamster_repo.rb', line 163 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 |
#count ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/rdf/hamster_repo.rb', line 76 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
116 117 118 119 120 121 122 123 |
# File 'lib/rdf/hamster_repo.rb', line 116 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
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/rdf/hamster_repo.rb', line 145 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, | yield RDF::Statement.new(s, p, o, .merge(graph_name: g.equal?(DEFAULT_GRAPH) ? nil : g)) end end end end end enum_statement end |
#graph? ⇒ Boolean #graph?(name) ⇒ Boolean Also known as: has_graph?
97 98 99 100 101 102 103 |
# File 'lib/rdf/hamster_repo.rb', line 97 def graph?(*args) case args.length when 0 then false when 1 then @data.key?(args.first) else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |
#graph_names(options = nil, &block) ⇒ Object
109 110 111 |
# File 'lib/rdf/hamster_repo.rb', line 109 def graph_names( = nil, &block) @data.keys.reject { |g| g == DEFAULT_GRAPH }.to_a end |
#isolation_level ⇒ Object
179 180 181 |
# File 'lib/rdf/hamster_repo.rb', line 179 def isolation_level :serializable end |
#snapshot ⇒ Dataset
A readable & queryable snapshot of the repository for isolated reads.
190 191 192 |
# File 'lib/rdf/hamster_repo.rb', line 190 def snapshot self.class.new(data: @data).freeze end |
#statement? ⇒ Boolean #statement?(statement) ⇒ Object Also known as: has_statement?
133 134 135 136 137 138 139 |
# File 'lib/rdf/hamster_repo.rb', line 133 def statement?(*args) case args.length when 0 then false when 1 then args.first && statement_in?(@data, args.first) else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |
#supports?(feature) ⇒ Boolean
Returns ‘true` if this respository supports the given `feature`.
This repository supports list_terms.
52 53 54 55 56 57 58 59 |
# File 'lib/rdf/hamster_repo.rb', line 52 def supports?(feature) case feature.to_sym when :atomic_write then true when :rdfstar then true when :snapshots then true else super end end |
#to_query ⇒ RDF::Query
Creates a query from the statements in this repository, turning blank nodes into non-distinguished variables. This can be used to determine if this repository is logically a subset of another repository.
65 66 67 68 69 70 71 |
# File 'lib/rdf/hamster_repo.rb', line 65 def to_query RDF::Query.new do |query| each do |statement| query.pattern RDF::Query::Pattern.from(statement, ndvars: true) end end end |