Skip to content

Properties

topofocus edited this page Apr 5, 2019 · 14 revisions

Although OrientDB has a schema-less-mode, where any properties are dynamically assigned, indexes and relations are as important as in conventional RDBMS.

ActiveOrient offers a Ruby way to define most common Properties and Indexes

ORD.create_class  :M  
--> INFO->CREATE CLASS M 
=> M 
M.properties
=> {:properties=>nil, :indexes=>nil} 
M.create_property :symbol 		# the default-case: type: :string, no index
M.create_property :con_id,   type: :integer
M.create_property :details,  type: :link,
                             linked_class: Contracts  # notice the Class-name here 
M.create_property :items,    type: :linklist, 
                             linked_class: Item
M.create_property( :name ){ :unique }                # create an automatic index, too

pp M.properties
{:properties=>
 [{"name"=>"symbol",    "type"=>"STRING",  ... }         
  {"name"=>"con_id",    "type"=>"INTEGER", ... },
  {"name"=>"name",      "type"=>"STRING",  ... },
  {"name"=>"details",   "linkedClass"=>"Contracts",    "type"=>"LINK", ...},
 ],
:indexes=>[{"name"=>"M.name", "type"=>"UNIQUE", "fields"=>["name"]}]
}

Indexes

Indexes are essential to get the work done efficiently.

Apart from creating in the process of creating a property ActiveOrient provides a simple DSL to create them separately.

> Model.create_index name [, on: » property, array of properties«, type: »:unique, :notunique « ]

Its just a wrapper to the OrientDB CreateIndex command with some reasonable defaults.

One simple usecase is shown in `lib/model/e.rb'

class E  < ActiveOrient::Model
  class << self
=begin
Establish constrains on Edges
After applying this method Edges are uniq!
=end
  def  uniq_index
    create_property  :in,  type: :link, linked_class: :V
    create_property  :out, type: :link, linked_class: :V
    create_index "#{ref_name}_idx", on: [ :in, :out ]
  end
end 

> ORD.create_edge_class :my_edge
INFO->CREATE CLASS hc_my_edge EXTENDS E
 => HC::MY_EDGE 
> HC::MY_EDGE.uniq_index
 INFO->CREATE INDEX hc_my_edge_idx ON hc_my_edge(in, out) UNIQUE
 INFO->Index on hc_my_edge based on hc_my_edge_idx created.
> HC::MY_EDGE.indexes
 => [{"name"=>"hc_my_edge_idx", "type"=>"UNIQUE", "fields"=>["in", "out"]}] 

Intention of the DSL is to provide a method to initialize a database structure via a readable script.

There is no explicit DSL to drop an Index.

This can be obtained with plain vanilla orientdb console commands:

## Example in ActiveOrient console
> HC::PortfolioPositions.indexes
 => [{"name"=>"hc_portfolio_positions.contract", "type"=>"UNIQUE", "fields"=>["contract"]}] 
> ORD.execute { "DROP INDEX hc_portfolio_positions.contract" }
 INFO->DROP INDEX hc_portfolio_positions.contract
 => [["drop index", "hc_portfolio_positions.contract"]] 
> HC::PortfolioPositions.indexes
 => nil 

(to be continued)

Clone this wiki locally