Class: Arango::Index

Inherits:
Object
  • Object
show all
Includes:
Helper::DatabaseAssignment, Helper::Satisfaction
Defined in:
lib/arango/index.rb

Overview

Arango Index

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::Satisfaction

#satisfy_category?, #satisfy_class?, #satisfy_class_or_string?, #satisfy_module?, #satisfy_module_or_nil?, #satisfy_module_or_string?, #warning_deprecated

Constructor Details

#initialize(collection:, fields:, cache_name: nil, deduplicate: nil, geo_json: nil, min_length: nil, sparse: nil, type: "hash", unique: nil) ⇒ Index

create new Arango::Index instance



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/arango/index.rb', line 34

def initialize(collection:, fields:, cache_name: nil, deduplicate: nil, geo_json: nil, min_length: nil, sparse: nil,
               type: "hash", unique: nil)
  @collection = collection
  assign_database(@collection.database)
  unless cache_name.nil?
    @cache_name = cache_name
    @server.cache.save(:index, cache_name, self)
  end
  body = {}
  body[:type]        ||= type
  body[:sparse]      ||= sparse
  body[:unique]      ||= unique
  body[:fields]      ||= fields.is_a?(String) ? [fields] : fields
  body[:deduplicate] ||= deduplicate
  body[:geo_json]     ||= geo_json
  body[:min_length]   ||= min_length

  assign_attributes(body)
end

Instance Attribute Details

#cache_nameObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def cache_name
  @cache_name
end

#collectionObject (readonly)

Returns the value of attribute collection.



57
58
59
# File 'lib/arango/index.rb', line 57

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



57
58
59
# File 'lib/arango/index.rb', line 57

def database
  @database
end

#deduplicateObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def deduplicate
  @deduplicate
end

#fieldsObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def fields
  @fields
end

#geo_jsonObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def geo_json
  @geo_json
end

#idObject (readonly)

Returns the value of attribute id.



57
58
59
# File 'lib/arango/index.rb', line 57

def id
  @id
end

#is_newly_createdObject (readonly)

Returns the value of attribute is_newly_created.



57
58
59
# File 'lib/arango/index.rb', line 57

def is_newly_created
  @is_newly_created
end

#keyObject (readonly)

Returns the value of attribute key.



57
58
59
# File 'lib/arango/index.rb', line 57

def key
  @key
end

#min_lengthObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def min_length
  @min_length
end

#nameObject (readonly)

Returns the value of attribute name.



57
58
59
# File 'lib/arango/index.rb', line 57

def name
  @name
end

#serverObject (readonly)

Returns the value of attribute server.



57
58
59
# File 'lib/arango/index.rb', line 57

def server
  @server
end

#sparseObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def sparse
  @sparse
end

#typeObject

Returns the value of attribute type.



57
58
59
# File 'lib/arango/index.rb', line 57

def type
  @type
end

#uniqueObject

DEFINE ===



56
57
58
# File 'lib/arango/index.rb', line 56

def unique
  @unique
end

Class Method Details

.get(collection:, id:) ⇒ Arango::Result

get collection for index

Parameters:

Returns:



28
29
30
31
# File 'lib/arango/index.rb', line 28

def self.get collection:, id:
  c, i = id.split '/' # Requests would convert / to %2F
  Arango::Requests::Index::Get.execute(server: collection.database.server, args: {collection: c, id: i})
end

.list(collection:) ⇒ Array of Arango::Index

list indexes defined for collection

Parameters:

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arango/index.rb', line 11

def self.list(collection:)
  params = { collection: collection.name }
  result = Arango::Requests::Index::ListAll.execute(server: collection.database.server, params: params)
  if result.response_code == 200
    return result.indexes.map do |v|
       self.new collection: collection, fields: v[:fields], type: v[:type], deduplicate: v[:deduplicate],
                geo_json: v[:geo_json], min_length: v[:min_length], sparse: v[:sparse], unique: v[:unique]
    end
  end
  # FIXME - raise error
  nil
end

Instance Method Details

#assign_attributes(result) ⇒ Object

set index attributes from hash

Parameters:

  • result (Hash)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/arango/index.rb', line 69

def assign_attributes(result)
  @id          = result[:id] || @id
  @name        = result[:name] || @name
  @key         = @id&.split("/")&.dig(1)
  @type        = assign_type(result[:type] || @type)
  @unique      = result[:unique]      || @unique
  @fields      = result[:fields]      || @fields
  @sparse      = result[:sparse]      || @sparse
  @geo_json    = result[:geoJson]     || @geo_json
  @min_length  = result[:minLength]   || @min_length
  @deduplicate = result[:deduplicate] || @deduplicate
  @is_newly_created = result[:is_newly_created]
  @estimates   = result[:estimates]   || @estimates
end

#createObject

create Index in database



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/arango/index.rb', line 109

def create
  body = {
    fields:      @fields,
    type:        @type
  }
  params = { collection: @collection.name }
  case @type.to_sym
  when :hash
    body[:deduplicate] = @deduplicate if @deduplicate
    body[:sparse] = @sparse if @sparse
    body[:unique] = @unique if @unique
  when :fulltext
    body[:min_length] = @min_length if @min_length
  when :general
    body[:deduplicate] = @deduplicate if @deduplicate
    body[:name] = @name if @name
    body[:sparse] = @sparse if @sparse
    body[:unique] = @unique if @unique
  when :geo
    body[:geo_jso] = @geo_json if @geo_json
  when :persistent
    body[:sparse] = @sparse if @sparse
    body[:unique] = @unique if @unique
  when :skiplist
    body[:deduplicate] = @deduplicate if @deduplicate
    body[:sparse] = @sparse if @sparse
    body[:unique] = @unique if @unique
  when :ttl
    body[:expire_after] = @expire_after if @expire_after
  else
    raise "Unknown index type #{@type.to_sym}"
  end
  result = Arango::Requests::Index::Create.execute(server: @database.server, params: params, body: body)
  assign_attributes result
  self
end

#deleteObject

delete datbase index



152
153
154
155
# File 'lib/arango/index.rb', line 152

def delete
  c, i = @id.split '/' # Requests would convert / to %2F
  Arango::Requests::Index::Delete.execute(server: @database.server, args: { collection: c, id: i})
end

#to_hHash

convert index to hash

Returns:

  • (Hash)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/arango/index.rb', line 88

def to_h
  {
    key: @key,
    id: @id,
    name: @name,
    body: @body,
    type: @type,
    sparse: @sparse,
    unique: @unique,
    fields: @fields,
    idCache: @idCache,
    geoJson: @geo_json,
    minLength: @min_length,
    deduplicate: @deduplicate,
    collection: @collection.name
  }.delete_if{|k,v| v.nil?}
end

#to_sObject

String representation



147
148
149
# File 'lib/arango/index.rb', line 147

def to_s
  "Index(#{@id}:#{@type.to_sym}-#{@fields})"
end