Class: RDF::Mongo::Repository

Inherits:
Repository
  • Object
show all
Defined in:
lib/rdf/mongo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options, &block) ⇒ Repository #initialize(**options, &block) ⇒ Repository

Initializes this repository instance.

Overloads:

  • #initialize(**options, &block) ⇒ Repository

    Parameters:

    • options (Hash{Symbol => Object})

    Options Hash (**options):

    • :title (String, #to_s) — default: nil
    • :uri (URI, #to_s) — default: nil

      URI in the form mongodb://host:port/db. The URI should also identify the collection use, but appending a collection path component such as mongodb://host:port/db/collection, this ensures that the collection will be maintained if cloned. See Mongo::Client options for more information on Mongo URIs.

  • #initialize(**options, &block) ⇒ Repository

    Parameters:

    • options (Hash{Symbol => Object})

      See Mongo::Client options for more information on Mongo Client options.

    Options Hash (**options):

    • :title (String, #to_s) — default: nil
    • :host (String)

      a single address or an array of addresses, which may contain a port designation

    • :port (Integer) — default: 27017

      applied to host address(es)

    • :database (String) — default: 'quadb'
    • :collection (String) — default: 'quads'

Yields:

  • (repository)

Yield Parameters:



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(**options, &block)
  collection = nil
  if options[:uri]
    options = options.dup
    uri = RDF::URI(options.delete(:uri))
    _, db, coll = uri.path.split('/')
    collection = coll || options.delete(:collection)
    db ||= "quadb"
    uri.path = "/#{db}" if coll
    @client = ::Mongo::Client.new(uri.to_s, **options)
  else
    warn "[DEPRECATION] RDF::Mongo::Repository#initialize expects a uri argument. Called from #{Gem.location_of_caller.join(':')}" unless options.empty?
    options[:database] ||= options.delete(:db) # 1.x compat
    options[:database] ||= 'quadb'
    hosts = Array(options[:host] || 'localhost')
    hosts.map! {|h| "#{h}:#{options[:port]}"} if options[:port]
    @client = ::Mongo::Client.new(hosts, **options)
  end

  @collection = @client[options.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(**options, &block)
end

Instance Attribute Details

#clientMongo::DB (readonly)

The Mongo database instance

Returns:

  • (Mongo::DB)


112
113
114
# File 'lib/rdf/mongo.rb', line 112

def client
  @client
end

#collectionMongo::Collection (readonly)

The collection used for storing quads

Returns:

  • (Mongo::Collection)


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_statementsObject



224
225
226
# File 'lib/rdf/mongo.rb', line 224

def clear_statements
  @collection.delete_many
end

#delete_statement(statement) ⇒ Object

See Also:

  • RDF::Mutable#delete_statement


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

Returns:

  • (Boolean)

See Also:

  • RDF::Mutable#insert_statement


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