class Ashikawa::Core::Collection

A certain Collection within the Database

Constants

CONTENT_CLASS

Map the content types to the classes from Ashikawa

CONTENT_TYPES

ArangoDB’s collections contain either only documents or only edges

Attributes

content_type[R]

The kind of content in the collection: Documents or Edges

@return [:document, :edge] @api public @example

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.content_type #=> :document
database[R]

The database the collection belongs to

@return [Database] @api public @example

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.database #=> #<Database: ...>
id[R]

The ID of the collection. Is set by the database and unique

@return [Fixnum] @api public @example Get the id of the collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.id #=> 4588
name[R]

The name of the collection, must be unique

@return [String] @api public @example Change the name of a collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.name # => 'example_1'
collection.name = 'example_2'
collection.name # => 'example_2'
status[R]

A wrapper around the status of the collection

@return [Status] @api public @example

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.status.loaded? #=> true
collection.status.new_born? #=> false

Public Class Methods

new(database, raw_collection) click to toggle source

Create a new Collection object with a name and an optional ID

@param [Database] database The database the connection belongs to @param [Hash] raw_collection The raw collection returned from the server @api public @example Create a Collection object from scratch

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
# File lib/ashikawa-core/collection.rb, line 144
def initialize(database, raw_collection)
  @database = database
  parse_raw_collection(raw_collection)
  @content_class = CONTENT_CLASS[@content_type]
end

Public Instance Methods

[](document_key) click to toggle source

Fetch a certain document by its key, return nil if the document does not exist

@param [Fixnum] document_key the id of the document @return Document @api public @example Fetch the document with the key 12345

document = collection[12345]
# File lib/ashikawa-core/collection.rb, line 366
def [](document_key)
  fetch(document_key)
rescue DocumentNotFoundException
  nil
end
add_index(type, options) click to toggle source

Add an index to the collection

@param [Symbol] type specify the type of the index, for example ‘:hash` @option options [Array<Symbol>] on fields on which to apply the index @option options [Boolean] unique Should the index be unique? Default is false @return Index @api public @example Add a hash-index to the fields :name and :profession of a collection

people = database['people']
people.add_index(:hash, :on => [:name, :profession])

@example Add a hash-index to the field :email of a collection

user = database['user']
user.add_index(:hash, :on => :email)
# File lib/ashikawa-core/collection.rb, line 429
def add_index(type, options)
  options[:on] = [options[:on]].flatten
  unique = options.fetch(:unique, false)
  response = send_request("index?collection=#{@id}", post: {
    'type' => type.to_s,
    'fields' => options.fetch(:on).map { |field| field.to_s },
    'unique' => unique
  })

  Index.new(self, response)
end
build_content_class(data, additional_attributes = {}) click to toggle source

Builds an instance for the content class

@param [Hash] data The raw data to be used to instatiate the class @param [Hash] additional_attributes Initial attributes to be passed to the underlying content class @return [Document] The instatiated document @api private

# File lib/ashikawa-core/collection.rb, line 497
def build_content_class(data, additional_attributes = {})
  @content_class.new(@database, data, additional_attributes)
end
create_document(attributes) click to toggle source

Create a new document with given attributes

@param [Hash] attributes @return [Document] The created document @api public @example Create a new document from raw data

collection.create_document(attributes)
# File lib/ashikawa-core/collection.rb, line 391
def create_document(attributes)
  raise "Can't create a document in an edge collection" if @content_type == :edge
  response = send_request_for_content(post: attributes)
  build_content_class(response, attributes)
end
create_edge(from, to, attributes) click to toggle source

Create a new edge between two documents with certain attributes

@deprecated Since we introduced the dedicated Graph module (‘gharial’) all operations regarding edges

and vertices should be done through that module. Due to this please use EdgeCollection#add
instead.

@param [Document] from @param [Document] to @param [Hash] attributes @return [Edge] The created edge @api public @example Create a new document from raw data

collection.create_edge(node_a, node_b, {'name' => 'incredible edge'})
# File lib/ashikawa-core/collection.rb, line 409
def create_edge(from, to, attributes)
  warn '[DEPRECATION] `create_edge` is deprecated.  Please use `EdgeCollection#add` instead.'
  raise "Can't create an edge in a document collection" if @content_type == :document
  response = send_request("edge?collection=#{@id}&from=#{from.id}&to=#{to.id}", post: attributes)
  Edge.new(@database, response, attributes)
end
delete() click to toggle source

Deletes the collection

@return [String] Response from the server @api public @example Delete a collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.delete
# File lib/ashikawa-core/collection.rb, line 282
def delete
  send_request_for_this_collection('', delete: {})
end
fetch(document_key) click to toggle source

Fetch a certain document by its key

@param [Fixnum] document_key the key of the document @raise [DocumentNotFoundException] If the requested document was not found @return Document @api public @example Fetch the document with the key 12345

document = collection.fetch(12345)
# File lib/ashikawa-core/collection.rb, line 354
def fetch(document_key)
  response = send_request_for_content_key(document_key)
  build_content_class(response)
end
figure() click to toggle source

Return a Figure initialized with current data for the collection

@return [Figure] @api public @example Get the datafile count for a collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.figure.datafiles_count #=> 0
# File lib/ashikawa-core/collection.rb, line 261
def figure
  raw_figure = get_information_from_server(:figures, :figures)
  Figure.new(raw_figure)
end
index(id) click to toggle source

Get an index by ID

@param [Fixnum] id @return Index @api public @example Get an Index by its ID

people = database['people']
people.index(1244) #=> #<Index: id=1244...>
# File lib/ashikawa-core/collection.rb, line 449
def index(id)
  response = send_request("index/#{@name}/#{id}")
  Index.new(self, response)
end
indices() click to toggle source

Get all indices

@return [Array<Index>] @api public @example Get all indices

people = database['people']
people.indices #=> [#<Index: id=1244...>, ...]
# File lib/ashikawa-core/collection.rb, line 461
def indices
  response = send_request("index?collection=#{@id}")

  response['indexes'].map do |raw_index|
    Index.new(self, raw_index)
  end
end
key_options() click to toggle source

Get information about the type of keys of this collection

@return [KeyOptions] @api public @example Check if this collection has autoincrementing keys

collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.key_options.type # => 'autoincrement'
# File lib/ashikawa-core/collection.rb, line 221
def key_options
  KeyOptions.new(get_information_from_server(:properties, :keyOptions))
end
length() click to toggle source

Returns the number of documents in the collection

@return [Fixnum] Number of documents @api public @example How many documents are in the collection?

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.length # => 0
# File lib/ashikawa-core/collection.rb, line 241
def length
  get_information_from_server(:count, :count)
end
load() click to toggle source

Load the collection into memory

@return [String] Response from the server @api public @example Load a collection into memory

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.load
# File lib/ashikawa-core/collection.rb, line 302
def load
  send_command_to_server(:load)
end
name=(new_name) click to toggle source

Change the name of the collection

@param [String] new_name New Name @return [String] New Name @api public @example Change the name of a collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.name # => 'example_1'
collection.name = 'example_2'
collection.name # => 'example_2'
# File lib/ashikawa-core/collection.rb, line 169
def name=(new_name)
  send_information_to_server(:rename, :name, new_name)
  @name = new_name
end
query() click to toggle source

Return a Query initialized with this collection

@return [Query] @api public @example Get all documents in this collection

people = database['people']
people.query.all #=> #<Cursor: id=1244...>
# File lib/ashikawa-core/collection.rb, line 476
def query
  Query.new(self)
end
replace(document_key, raw_document) click to toggle source

Replace a document by its key

@param [Fixnum] document_key the key of the document @param [Hash] raw_document the data you want to replace it with @return [Hash] parsed JSON response from the server @api public @example Replace the document with the key 12345

collection.replace(12345, document)
# File lib/ashikawa-core/collection.rb, line 380
def replace(document_key, raw_document)
  send_request_for_content_key(document_key, put: raw_document)
end
truncate() click to toggle source

Delete all documents from the collection

@return [String] Response from the server @api public @example Remove all documents from a collection

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.truncate
# File lib/ashikawa-core/collection.rb, line 342
def truncate
  send_command_to_server(:truncate)
end
unload() click to toggle source

Load the collection into memory

@return [String] Response from the server @api public @example Unload a collection into memory

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.unload
# File lib/ashikawa-core/collection.rb, line 322
def unload
  send_command_to_server(:unload)
end
volatile?() click to toggle source

Check if the collection is volatile

@return [Boolean] @api public @example Is the people collection volatile?

people = database['people']
people.volatile? #=> false
# File lib/ashikawa-core/collection.rb, line 487
def volatile?
  get_information_from_server(:properties, :isVolatile)
end
wait_for_sync=(new_value) click to toggle source

Change if the document will wait until the data has been synchronised to disk

@return [String] Response from the server @api public @example Tell the collection to wait for file synchronization

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.wait_for_sync = true
# File lib/ashikawa-core/collection.rb, line 210
def wait_for_sync=(new_value)
  send_information_to_server(:properties, :waitForSync, new_value)
end
wait_for_sync?() click to toggle source

Does the document wait until the data has been synchronised to disk?

@return [Boolean] @api public @example Does the collection wait for file synchronization?

database = Ashikawa::Core::Database.new('http://localhost:8529')
raw_collection = {
  'name' => 'example_1',
  'waitForSync' => true,
  'id' => 4588,
  'status' => 3,
  'error' => false,
  'code' => 200
}
collection = Ashikawa::Core::Collection.new(database, raw_collection)
collection.wait_for_sync? #=> false
# File lib/ashikawa-core/collection.rb, line 190
def wait_for_sync?
  get_information_from_server(:properties, :waitForSync)
end

Private Instance Methods

get_information_from_server(path, attribute) click to toggle source

Send a get request to the server and return a certain attribute

@param [Symbol] path The path without trailing slash @param [Symbol] attribute The attribute of the answer that should be returned @return [Object] The result @api private

# File lib/ashikawa-core/collection.rb, line 529
def get_information_from_server(path, attribute)
  response = send_request_for_this_collection("#{path}")
  response[attribute.to_s]
end
parse_raw_collection(raw_collection) click to toggle source

Parse information returned from the server

@param [Hash] raw_collection @return self @api private

# File lib/ashikawa-core/collection.rb, line 547
def parse_raw_collection(raw_collection)
  @name         = raw_collection['name']
  @id           = raw_collection['id']
  @content_type = CONTENT_TYPES[raw_collection['type']] || :document
  @status       = Status.new(raw_collection['status'].to_i) if raw_collection.key?('status')
  self
end
send_command_to_server(command) click to toggle source

Send a put request with the given command

@param [Symbol] command The command you want to execute @return [Object] The result @api private

# File lib/ashikawa-core/collection.rb, line 519
def send_command_to_server(command)
  send_request_for_this_collection("#{command}", put: {})
end
send_information_to_server(path, key, value) click to toggle source

Send a put request with a given key and value to the server

@param [Symbol] path @param [Symbol] key @param [Symbol] value @return [Object] The result @api private

# File lib/ashikawa-core/collection.rb, line 510
def send_information_to_server(path, key, value)
  send_request_for_this_collection("#{path}", put: { key.to_s => value })
end
send_request_for_content(opts = {}) click to toggle source

Send a request for the content of this collection

@param [Hash] opts The options for the request @return [Hash] parsed JSON response from the server @api private

# File lib/ashikawa-core/collection.rb, line 574
def send_request_for_content(opts = {})
  send_request("#{@content_type}?collection=#{@id}", opts)
end
send_request_for_content_key(document_key, opts = {}) click to toggle source

Send a request for the content with the given key

@param [Fixnum] document_key The id of the document @param [Hash] opts The options for the request @return [Hash] parsed JSON response from the server @api private

# File lib/ashikawa-core/collection.rb, line 561
def send_request_for_content_key(document_key, opts = {})
  if document_key.to_s.include? '/'
    send_request("#{@content_type}/#{document_key}", opts)
  else
    send_request("#{@content_type}/#{@id}/#{document_key}", opts)
  end
end
send_request_for_this_collection(path, method = {}) click to toggle source

Send a request to the server with the name of the collection prepended

@return [String] Response from the server @api private

# File lib/ashikawa-core/collection.rb, line 538
def send_request_for_this_collection(path, method = {})
  send_request("collection/#{id}/#{path}", method)
end