Class: RDF::Mongo::Repository
- Inherits:
-
Repository
- Object
- Repository
- RDF::Mongo::Repository
- Defined in:
- lib/rdf/mongo.rb
Instance Attribute Summary collapse
-
#client ⇒ Mongo::DB
readonly
The Mongo database instance.
-
#collection ⇒ Mongo::Collection
readonly
The collection used for storing quads.
Instance Method Summary collapse
- #apply_changeset(changeset) ⇒ Object
- #clear_statements ⇒ Object
- #delete_statement(statement) ⇒ Object
-
#initialize(**options) {|repository| ... } ⇒ Repository
constructor
Initializes this repository instance.
- #insert_statement(statement) ⇒ Object
- #supports?(feature) ⇒ Boolean
Constructor Details
#initialize(**options, &block) ⇒ Repository #initialize(**options, &block) ⇒ Repository
Initializes this repository instance.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rdf/mongo.rb', line 139 def initialize(**, &block) collection = nil if [:uri] = .dup uri = RDF::URI(.delete(:uri)) _, db, coll = uri.path.split('/') collection = coll || .delete(:collection) db ||= "quadb" uri.path = "/#{db}" if coll @client = ::Mongo::Client.new(uri.to_s, **) else warn "[DEPRECATION] RDF::Mongo::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless .empty? [:database] ||= .delete(:db) # 1.x compat [:database] ||= 'quadb' hosts = Array([:host] || 'localhost') hosts.map! {|h| "#{h}:#{[:port]}"} if [:port] @client = ::Mongo::Client.new(hosts, **) end @collection = @client[.delete(:collection) || 'quads'] @collection.indexes.create_many([ {key: {s: 1}}, {key: {p: 1}}, {key: {o: "hashed"}}, {key: {c: 1}}, {key: {s: 1, p: 1}}, #{key: {s: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed #{key: {p: 1, o: "hashed"}}, # Muti-key hashed indexes not allowed ]) super(**, &block) end |
Instance Attribute Details
#client ⇒ Mongo::DB (readonly)
The Mongo database instance
112 113 114 |
# File 'lib/rdf/mongo.rb', line 112 def client @client end |
#collection ⇒ Mongo::Collection (readonly)
The collection used for storing quads
116 117 118 |
# File 'lib/rdf/mongo.rb', line 116 def collection @collection end |
Instance Method Details
#apply_changeset(changeset) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/rdf/mongo.rb', line 182 def apply_changeset(changeset) ops = [] changeset.deletes.each do |d| ops << { delete_one: { filter: statement_to_delete(d)} } end changeset.inserts.each do |i| ops << { update_one: { filter: statement_to_insert(i), update: statement_to_insert(i), upsert: true} } end @collection.bulk_write(ops, ordered: true) end |
#clear_statements ⇒ Object
224 225 226 |
# File 'lib/rdf/mongo.rb', line 224 def clear_statements @collection.delete_many end |
#delete_statement(statement) ⇒ Object
202 203 204 205 |
# File 'lib/rdf/mongo.rb', line 202 def delete_statement(statement) st_mongo = statement_to_delete(statement) @collection.delete_one(st_mongo) end |
#insert_statement(statement) ⇒ Object
196 197 198 199 |
# File 'lib/rdf/mongo.rb', line 196 def insert_statement(statement) st_mongo = statement_to_insert(statement) @collection.update_one(st_mongo, st_mongo, upsert: true) end |
#supports?(feature) ⇒ Boolean
172 173 174 175 176 177 178 179 180 |
# File 'lib/rdf/mongo.rb', line 172 def supports?(feature) case feature.to_sym when :graph_name then true when :atomic_write then true when :validity then @options.fetch(:with_validity, true) when :literal_equality then true else false end end |