Module: Arango::DocumentCollection::InstanceMethods
- Defined in:
- lib/arango/document_collection/instance_methods.rb
Overview
Arango DocumentCollection InstanceMethods
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
- #id ⇒ String
-
#is_system ⇒ Boolean
readonly
If true, create a system collection.
-
#name ⇒ String
The name of the collection.
Instance Method Summary collapse
-
#arango_object_id ⇒ String
The collections ArangoDB object_id, not to be confused with the collections ruby object_id.
-
#checksum(with_revisions: false, with_data: false) ⇒ Object
Returns a checksum for the specified collection.
-
#create ⇒ Arango::DocumentCollection
Stores the collection in the database.
-
#delete ⇒ NilClass
Deletes a collection.
-
#has_more? ⇒ Boolean
Check if more results are available for a cursor.
-
#initialize(database: Arango.current_database, graph: nil, name:, id: nil, globally_unique_id: nil, is_system: false, status: nil, type: :document, properties: {}) ⇒ Arango::DocumentCollection
Instantiate a new collection.
- #journal_size=(n) ⇒ Object
-
#key_options ⇒ Arango::Result
Additional options for key generation.
-
#load_indexes_into_memory ⇒ Arango::DocumentCollection
Load Indexes into Memory Note: For the time being this function is only useful on RocksDB storage engine, as in MMFiles engine all indexes are in memory anyways.
-
#load_into_memory ⇒ Arango::DocumentCollection
Loads a collection into ArangoDBs memory.
- #method_missing(property, *args, &block) ⇒ Object
-
#next_batch ⇒ Object
Request next batch from a cursor.
-
#recalculate_count ⇒ Arango::DocumentCollection
recalculates the document count of a collection Note: This function is only useful on RocksDB storage engine.
-
#reload ⇒ Arango::DocumentCollection
(also: #refresh, #revert)
Reload collection properties and name from the database, reverting any changes.
-
#revision ⇒ String
Retrieve the collections revision id.
-
#rotate_journal ⇒ Arango::DocumentCollection
Rotates the journal of a collection.
-
#save ⇒ Arango::DocumentCollection
(also: #update)
Save changed collection properties and name changed, to the database.
- #server ⇒ Object
-
#shards(details: false) ⇒ Array, Hash
Return the shard ids of a collection Note: This method only works on a cluster coordinator.
-
#size ⇒ Integer
(also: #count, #length)
Counts the documents in a collection.
-
#statistics ⇒ Hash
Fetch the statistics of a collection.
-
#status ⇒ Symbol
The status of the collection as symbol, one of: - :unknown - :new_born - :unloaded - :loaded - :being_unloaded - :deleted - :loading.
-
#truncate ⇒ Arango::DocumentCollection
Truncates a collection.
-
#type ⇒ Symbol
The type of the collection to create.
-
#unload_from_memory ⇒ Arango::DocumentCollection
Unloads a collection into ArangoDBs memory.
- #wait_for_sync=(n) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(property, *args, &block) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/arango/document_collection/instance_methods.rb', line 45 def method_missing(property, *args, &block) property_s = property.to_s.underscore property_y = property_s.to_sym return @properties[property_y] = args[0] if property_s.end_with?('=') return @properties[property_y] if @properties.key?(property_y) super(property, *args, &block) end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
43 44 45 |
# File 'lib/arango/document_collection/instance_methods.rb', line 43 def database @database end |
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
43 44 45 |
# File 'lib/arango/document_collection/instance_methods.rb', line 43 def graph @graph end |
#id ⇒ String
54 55 56 57 58 |
# File 'lib/arango/document_collection/instance_methods.rb', line 54 def id i = @changed_attributes[:_id] || @attributes[:_id] return i if i "#{collection.name}/#{key}" end |
#is_system ⇒ Boolean (readonly)
If true, create a system collection. In this case collection-name should start with an underscore. End users should normally create non-system collections only. API implementors may be required to create system collections in very special occasions, but normally a regular collection will do. (The default is false) Can only be set by calling the constructor with the is_system param.
61 62 63 |
# File 'lib/arango/document_collection/instance_methods.rb', line 61 def is_system @is_system end |
#name ⇒ String
The name of the collection.
65 66 67 |
# File 'lib/arango/document_collection/instance_methods.rb', line 65 def name @name end |
Instance Method Details
#arango_object_id ⇒ String
The collections ArangoDB object_id, not to be confused with the collections ruby object_id.
69 70 71 |
# File 'lib/arango/document_collection/instance_methods.rb', line 69 def arango_object_id @properties[:object_id] end |
#checksum(with_revisions: false, with_data: false) ⇒ Object
Returns a checksum for the specified collection
214 215 216 217 218 219 220 221 |
# File 'lib/arango/document_collection/instance_methods.rb', line 214 def checksum (with_revisions: false, with_data: false) params = { withRevisions: with_revisions, withData: with_data } args = { name: @name, type: @type } Arango::Requests::Collection::Checksum.execute(server: server, args: args, params: params).checksum end |
#create ⇒ Arango::DocumentCollection
Stores the collection in the database.
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/arango/document_collection/instance_methods.rb', line 130 def create @name_changed = false @journal_size_changed = false @wait_for_sync_changed = false body = { name: @name, type: @type } @properties.each do |k, v| body[k.to_s.camelize(:lower).to_sym] = v unless v.nil? end if body[:keyOptions] body[:keyOptions].delete_if{|_,v| v.nil?} body[:keyOptions].transform_keys! { |key| key.to_s.camelize(:lower).to_sym } end enforce_replication_factor = body.delete(:enforceReplicationFactor) wait_for_sync_replication = body.delete(:waitForSyncReplication) params = nil if enforce_replication_factor || wait_for_sync_replication params = {} params[:enforceReplicationFactor] = enforce_replication_factor unless enforce_replication_factor.nil? params[:waitForSyncReplication] = wait_for_sync_replication unless wait_for_sync_replication.nil? end result = Arango::Requests::Collection::Create.execute(server: server, body: body, params: params) _update_attributes(result) self end |
#delete ⇒ NilClass
Deletes a collection.
162 163 164 165 166 |
# File 'lib/arango/document_collection/instance_methods.rb', line 162 def delete params = { isSystem: @is_system } args = { name: @name, type: @type } Arango::Requests::Collection::Delete.execute(server: server, args: args, params: params) end |
#has_more? ⇒ Boolean
Check if more results are available for a cursor
336 337 338 339 |
# File 'lib/arango/document_collection/instance_methods.rb', line 336 def has_more? return false unless @cursor @cursor[:has_more] end |
#initialize(database: Arango.current_database, graph: nil, name:, id: nil, globally_unique_id: nil, is_system: false, status: nil, type: :document, properties: {}) ⇒ Arango::DocumentCollection
Instantiate a new collection. For param description see the attributes descriptions. All params except name and database are optional.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/arango/document_collection/instance_methods.rb', line 14 def initialize(database: Arango.current_database, graph: nil, name:, id: nil, globally_unique_id: nil, is_system: false, status: nil, type: :document, properties: {}) @database = database if database @graph = graph if graph @cursor = nil @batch_proc = nil @id = id @globally_unique_id = globally_unique_id @is_system = is_system _set_name(name) @name_changed = false @original_name = name _set_status(status) _set_type(type) _set_properties(properties) end |
#journal_size=(n) ⇒ Object
109 110 111 112 |
# File 'lib/arango/document_collection/instance_methods.rb', line 109 def journal_size=(n) @journal_size_changed = true @properties[:journal_size] = n end |
#key_options ⇒ Arango::Result
Additional options for key generation. If specified, then key_options should be a Hash containing the following attributes:
-
type: specifies the type of the key generator. The currently available generators are traditional, autoincrement, uuid and padded.
-
allow_user_keys: if set to true, then it is allowed to supply own key values in the _key attribute of a document. If set to false, then the key generator will solely be responsible for generating keys and supplying own key values in the _key attribute of documents is considered an error.
-
increment: increment value for autoincrement key generator. Not used for other key generator types.
-
offset: Initial offset value for autoincrement key generator. Not used for other key generator types.
Can only be set by calling the constructor with the key_options param.
82 83 84 |
# File 'lib/arango/document_collection/instance_methods.rb', line 82 def Arango::Result.new(@properties[:key_options]) end |
#load_indexes_into_memory ⇒ Arango::DocumentCollection
Load Indexes into Memory Note: For the time being this function is only useful on RocksDB storage engine, as in MMFiles engine all indexes are in memory anyways.
244 245 246 247 248 249 250 |
# File 'lib/arango/document_collection/instance_methods.rb', line 244 def load_indexes_into_memory if @database.server.rocksdb? args = { name: @name, type: @type } Arango::Requests::Collection::LoadIndexesIntoMemory.execute(server: server, args: args) end self end |
#load_into_memory ⇒ Arango::DocumentCollection
Loads a collection into ArangoDBs memory. Returns the collection on success.
225 226 227 228 229 230 |
# File 'lib/arango/document_collection/instance_methods.rb', line 225 def load_into_memory args = { name: @name, type: @type, count: false } result = Arango::Requests::Collection::Load.execute(server: server, args: args).status _set_status(result) self end |
#next_batch ⇒ Object
Request next batch from a cursor.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/arango/document_collection/instance_methods.rb', line 319 def next_batch return unless has_more? @cursor = Arango::Requests::Cursor::NextBatch.execute(server: self.server, args: { id: @cursor[:id] }) final_result = if @batch_proc @batch_proc.call(@cursor) else @cursor end unless @cursor[:has_more] @cursor = nil @batch_proc = nil end final_result end |
#recalculate_count ⇒ Arango::DocumentCollection
recalculates the document count of a collection Note: This function is only useful on RocksDB storage engine.
266 267 268 269 270 271 272 |
# File 'lib/arango/document_collection/instance_methods.rb', line 266 def recalculate_count if @database.server.rocksdb? args = { name: @name, type: @type } Arango::Requests::Collection::RecalculateCount.execute(server: server, args: args) end self end |
#reload ⇒ Arango::DocumentCollection Also known as: refresh, revert
Reload collection properties and name from the database, reverting any changes.
276 277 278 279 280 281 282 283 284 285 |
# File 'lib/arango/document_collection/instance_methods.rb', line 276 def reload request_name = @name_changed ? @original_name : @name @name_changed = false @journal_size_changed = false @wait_for_sync_changed = false args = { name: request_name, type: @type } result = Arango::Requests::Collection::Get.execute(server: server, args: args) _update_attributes(result) self end |
#revision ⇒ String
Retrieve the collections revision id
206 207 208 209 |
# File 'lib/arango/document_collection/instance_methods.rb', line 206 def revision args = { name: @name, type: @type } Arango::Requests::Collection::Revision.execute(server: server, args: args).revision end |
#rotate_journal ⇒ Arango::DocumentCollection
Rotates the journal of a collection. Collection must have a journal. Note: This method is specific for the MMFiles storage engine, and there it is not available in a cluster.
255 256 257 258 259 260 261 |
# File 'lib/arango/document_collection/instance_methods.rb', line 255 def rotate_journal if @database.server.mmfiles? args = { name: @name, type: @type } Arango::Requests::Collection::RotateJournal.execute(server: server, args: args) end self end |
#save ⇒ Arango::DocumentCollection Also known as: update
Save changed collection properties and name changed, to the database. Note: except for wait_for_sync, journal_size and name, collection properties cannot be changed once a collection is created.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/arango/document_collection/instance_methods.rb', line 292 def save requests = [] args = { name: @name, type: @type } rename = nil if @name_changed args[:name] = @original_name @name_changed = false body = { name: @name } rename = Arango::Requests::Collection::Rename.execute(server: server, args: args, body: body) @original_name = @name end if @journal_size_changed || @wait_for_sync_changed body = {} body[:journalSize] = @journal_size if @journal_size_changed && @database.server.mmfiles? body[:waitForSync] = @wait_for_sync if @wait_for_sync_changed @journal_size_changed = false @wait_for_sync_changed = false result = Arango::Requests::Collection::SetProperties.execute(server: server, args: args, body: body) @journal_size = result.journal_size if result.key?(:journal_size) @wait_for_sync = result.wait_for_sync if result.key?(:wait_for_sync) end [rename, self] end |
#server ⇒ Object
124 125 126 |
# File 'lib/arango/document_collection/instance_methods.rb', line 124 def server @database.server end |
#shards(details: false) ⇒ Array, Hash
Return the shard ids of a collection Note: This method only works on a cluster coordinator.
196 197 198 199 200 201 202 |
# File 'lib/arango/document_collection/instance_methods.rb', line 196 def shards(details: false) if @database.server.coordinator? args = { name: @name, type: @type } body = { details: details } Arango::Requests::Collection::Shards.execute(server: server, args: args) end end |
#size ⇒ Integer Also known as: count, length
Counts the documents in a collection
178 179 180 181 |
# File 'lib/arango/document_collection/instance_methods.rb', line 178 def size args = { name: @name, type: @type } Arango::Requests::Collection::Count.execute(server: server, args: args).count end |
#statistics ⇒ Hash
Fetch the statistics of a collection
187 188 189 190 |
# File 'lib/arango/document_collection/instance_methods.rb', line 187 def statistics args = { name: @name, type: @type } Arango::Requests::Collection::Statistics.execute(server: server, args: args) end |
#status ⇒ Symbol
The status of the collection as symbol, one of:
-
:unknown
-
:new_born
-
:unloaded
-
:loaded
-
:being_unloaded
-
:deleted
-
:loading
95 96 97 |
# File 'lib/arango/document_collection/instance_methods.rb', line 95 def status STATES[@status] end |
#truncate ⇒ Arango::DocumentCollection
Truncates a collection.
170 171 172 173 174 |
# File 'lib/arango/document_collection/instance_methods.rb', line 170 def truncate args = { name: @name, type: @type } Arango::Requests::Collection::Truncate.execute(server: server, args: args) self end |
#type ⇒ Symbol
The type of the collection to create. The following values for type are valid:
- document collection, use the :document symbol
- edge collection, use the :edge symbol
The default collection type is :document. Can only be set by calling the constructor with the type param.
105 106 107 |
# File 'lib/arango/document_collection/instance_methods.rb', line 105 def type TYPES[@type] end |
#unload_from_memory ⇒ Arango::DocumentCollection
Unloads a collection into ArangoDBs memory. Returns the collection on success.
234 235 236 237 238 239 |
# File 'lib/arango/document_collection/instance_methods.rb', line 234 def unload_from_memory args = { name: @name, type: @type } result = Arango::Requests::Collection::Unload.execute(server: server, args: args).status _set_status(result) self end |
#wait_for_sync=(n) ⇒ Object
114 115 116 117 |
# File 'lib/arango/document_collection/instance_methods.rb', line 114 def wait_for_sync=(n) @wait_for_sync_changed = true @properties[:wait_for_sync] = n end |