Class: MarkLogic::Database

Inherits:
Object
  • Object
show all
Includes:
Persistence
Defined in:
lib/marklogic/database.rb

Constant Summary collapse

INDEX_KEYS =
[
  'range-element-index',
  'element-word-lexicon',
  'element-attribute-word-lexicon',
  # 'path-namespace',
  # 'field',
  'range-path-index',
  'range-field-index',
  'geospatial-element-index',
  'geospatial-element-child-index',
  'geospatial-element-pair-index',
  'geospatial-path-index'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#admin_connection, #admin_connection=, #connection, #connection=, #manage_connection, #manage_connection=

Methods included from Loggable

#default_logger, #logger, #logger=, #rails_logger

Constructor Details

#initialize(database_name, conn = nil) ⇒ Database

Returns a new instance of Database.


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/marklogic/database.rb', line 22

def initialize(database_name, conn = nil)
  self.connection = conn

  @database_name = database_name
  @options = {
    "database-name" => @database_name,
    "collection-lexicon" => true
  }

  reset_indexes
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application


21
22
23
# File 'lib/marklogic/database.rb', line 21

def application
  @application
end

#database_nameObject

Returns the value of attribute database_name


21
22
23
# File 'lib/marklogic/database.rb', line 21

def database_name
  @database_name
end

Class Method Details

.load(database_name, conn = nil) ⇒ Object


34
35
36
37
38
# File 'lib/marklogic/database.rb', line 34

def self.load(database_name, conn = nil)
  db = Database.new(database_name, conn)
  db.load
  db
end

Instance Method Details

#[](key) ⇒ Object


61
62
63
# File 'lib/marklogic/database.rb', line 61

def [](key)
  @options[key]
end

#[]=(key, value) ⇒ Object


57
58
59
# File 'lib/marklogic/database.rb', line 57

def []=(key, value)
  @options[key] = value
end

#add_element_word_lexicon(localname, options) ⇒ Object


93
94
95
# File 'lib/marklogic/database.rb', line 93

def add_element_word_lexicon(localname, options)
   add_index "element-word-lexicons", MarkLogic::DatabaseSettings::ElementWordLexicon.new(localname, options)
end

#add_fieldObject


105
106
107
# File 'lib/marklogic/database.rb', line 105

def add_field()
  # add_index "fields"
end

#add_geospatial_element_child_index(element_name, latitude_localname, longitude_localname, options = {}) ⇒ Object


117
118
119
# File 'lib/marklogic/database.rb', line 117

def add_geospatial_element_child_index(element_name, latitude_localname, longitude_localname, options = {})
  add_index "geospatial-element-child-index", MarkLogic::DatabaseSettings::GeospatialElementChildIndex.new(element_name, latitude_localname, longitude_localname, options)
end

#add_geospatial_element_index(element_name, latitude_localname, longitude_localname, options = {}) ⇒ Object


113
114
115
# File 'lib/marklogic/database.rb', line 113

def add_geospatial_element_index(element_name, latitude_localname, longitude_localname, options = {})
   add_index "geospatial-element-index", MarkLogic::DatabaseSettings::GeospatialElementIndex.new(element_name, latitude_localname, longitude_localname, options)
end

#add_geospatial_element_pair_index(element_name, latitude_localname, longitude_localname, options = {}) ⇒ Object


121
122
123
# File 'lib/marklogic/database.rb', line 121

def add_geospatial_element_pair_index(element_name, latitude_localname, longitude_localname, options = {})
  add_index "geospatial-element-pair-index", MarkLogic::DatabaseSettings::GeospatialElementPairIndex(element_name, latitude_localname, longitude_localname, options)
end

#add_geospatial_path_index(path_expression, latitude_localname, longitude_localname, options = {}) ⇒ Object


125
126
127
# File 'lib/marklogic/database.rb', line 125

def add_geospatial_path_index(path_expression, latitude_localname, longitude_localname, options = {})
  add_index "geospatial-path-index", MarkLogic::DatabaseSettings::GeospatialPathIndex.new(path_expression, latitude_localname, longitude_localname, options)
end

#add_index(index_type, index) ⇒ Object


194
195
196
197
198
199
# File 'lib/marklogic/database.rb', line 194

def add_index(index_type, index)
  @options[index_type] = [] unless @options[index_type]
  @options[index_type] << index
  @options[index_type].uniq! { |ii| ii.key }
  application.add_index(index) if application
end

#add_path_namespaceObject


97
98
99
# File 'lib/marklogic/database.rb', line 97

def add_path_namespace()
  # add_index "path-namespace"
end

#add_range_element_index(name, options = {}) ⇒ Object

def add_phrase_through()

# @options["phrase-throughs"] <<

end


89
90
91
# File 'lib/marklogic/database.rb', line 89

def add_range_element_index(name, options = {})
  add_index "range-element-index", MarkLogic::DatabaseSettings::RangeElementIndex.new(name, options)
end

#add_range_field_index(field_name, options = {}) ⇒ Object


109
110
111
# File 'lib/marklogic/database.rb', line 109

def add_range_field_index(field_name, options = {})
  add_index "range-field-index", MarkLogic::DatabaseSettings::RangeFieldIndex.new(field_name, options)
end

#add_range_path_index(path_expression, options = {}) ⇒ Object


101
102
103
# File 'lib/marklogic/database.rb', line 101

def add_range_path_index(path_expression, options = {})
  add_index "range-path-index", MarkLogic::DatabaseSettings::RangePathIndex.new(path_expression, options)
end

#clearObject


219
220
221
# File 'lib/marklogic/database.rb', line 219

def clear
  r = connection.delete(%Q{/v1/search})
end

#collection(name) ⇒ Object


215
216
217
# File 'lib/marklogic/database.rb', line 215

def collection(name)
  MarkLogic::Collection.new(name, self)
end

#collectionsObject


223
224
225
226
227
228
229
230
# File 'lib/marklogic/database.rb', line 223

def collections()
  res = connection.run_query('cts:collections()', "xquery")
  if res.code.to_i == 200
    return res.body || []
  else
    raise MissingCollectionLexiconError.new if res.body =~ /XDMP-COLLXCNNOTFOUND/
  end
end

#createObject

def add_foreign_database()

add_index "foreign-database"

end


133
134
135
136
137
# File 'lib/marklogic/database.rb', line 133

def create
  r = manage_connection.post_json(
    %Q{/manage/v2/databases?format=json},
    to_json)
end

#dropObject


166
167
168
# File 'lib/marklogic/database.rb', line 166

def drop
  r = manage_connection.delete(%Q{/manage/v2/databases/#{database_name}?format=json})
end

#exists?Boolean

Returns:

  • (Boolean)

139
140
141
# File 'lib/marklogic/database.rb', line 139

def exists?
  manage_connection.head(%Q{/manage/v2/databases/#{database_name}}).code.to_i  == 200
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)

65
66
67
# File 'lib/marklogic/database.rb', line 65

def has_key?(key)
  @options.has_key?(key)
end

#has_range_index?(name) ⇒ Boolean

Returns:

  • (Boolean)

207
208
209
210
211
212
213
# File 'lib/marklogic/database.rb', line 207

def has_range_index?(name)
  @options["range-element-index"].each do |index|
    return true if index.localname == name
  end

  return false
end

#inspectObject


50
51
52
53
54
55
# File 'lib/marklogic/database.rb', line 50

def inspect
  as_nice_string = @options.collect do |key, value|
    " #{key}: #{value.inspect}"
  end.sort.join(",")
  "#<#{self.class}#{as_nice_string}>"
end

#loadObject


40
41
42
43
44
45
46
47
48
# File 'lib/marklogic/database.rb', line 40

def load
  resp = manage_connection.get(%Q{/manage/v2/databases/#{database_name}/properties?format=json})
  if resp.code.to_i == 200
    options = Oj.load(resp.body)
    options.each do |key, value|
      self[key] = value
    end
  end
end

#range_index(name) ⇒ Object


201
202
203
204
205
# File 'lib/marklogic/database.rb', line 201

def range_index(name)
  @options["range-element-index"].each do |index|
    return index if index.localname == name
  end
end

#reset_indexesObject


188
189
190
191
192
# File 'lib/marklogic/database.rb', line 188

def reset_indexes
  INDEX_KEYS.each do |key|
    @options[key] = []
   end
end

#stale?Boolean

Returns:

  • (Boolean)

Raises:

  • (Exception)

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/marklogic/database.rb', line 143

def stale?
  response = manage_connection.get(%Q{/manage/v2/databases/#{database_name}/properties?format=json})
  raise Exception.new("Invalid response: #{response.code.to_i}: #{response.body}") if (response.code.to_i != 200)

  props = Oj.load(response.body)

  INDEX_KEYS.each do |key|
    if props[key]
      local = @options[key].uniq.sort
      remote = props[key].map { |json| MarkLogic::DatabaseSettings::Index.from_json(key, json) }.uniq.sort
      unless local == remote
        logger.debug "#{database_name}: #{local} != #{remote}"
        return true
      end
    elsif @options.has_key?(key) && @options[key] != []
      logger.debug "#{database_name}: #{key} is not on the remote end"
      return true
    end
  end

  return false
end

#to_jsonObject


170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/marklogic/database.rb', line 170

def to_json
  json = {}
  @options.each do |k, v|
    if v.kind_of?(Array)
      value = v.map { |item| item.to_json }
    else
      value = v
    end
    json[k] = value
  end
  json
end

#updateObject


183
184
185
186
# File 'lib/marklogic/database.rb', line 183

def update
  url = %Q{/manage/v2/databases/#{database_name}/properties?format=json}
  r = manage_connection.put(url, ::Oj.dump(to_json, mode: :compat))
end