Module: RestDelete

Included in:
ActiveOrient::OrientDB
Defined in:
lib/rest/delete.rb

Instance Method Summary collapse

Instance Method Details

#delete_class(o_class) ⇒ Object

Deletes the specified class and returns true on success

The class-entries in ActiveOrient.database_classes and ORD.database_classes are removed. The Ruby-Class itself is untouched. However, any furter operation leads to an Error.

todo: remove all instances of the class



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rest/delete.rb', line 46

def delete_class o_class
  cl = classname(o_class).to_s
  return if cl.nil?
  logger.progname = 'RestDelete#DeleteClass'

  begin
    ## to do: if cl contains special characters, enclose with backticks
    response = nil
    ActiveOrient.db_pool.checkout do | conn |
      response = conn["/class/#{ActiveOrient.database}/#{cl}"].delete
    end
    if response.code == 204
      logger.info{"Class #{cl} deleted."}

      ActiveOrient.database_classes.delete(cl)
    end
  rescue RestClient::InternalServerError => e
    sentence=  JSON.parse( e.response)['errors'].last['content']
    if ActiveOrient.database_classes.has_key? cl
      logger.error{"Class #{cl} still present."}
      logger.error{ sentence }
      false
    else
      logger.error{e.inspect}
      true
    end
  rescue Exception => e
    logger.error{e.message}
    logger.error{e.inspect}
  end
end

#delete_database(database:) ⇒ Object

Deletes the database and returns true on success

After the removal of the database, the working-database might be empty


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rest/delete.rb', line 10

def delete_database database:
  logger.progname = 'RestDelete#DeleteDatabase'
  old_ds = ActiveOrient.database
  change_database database
  begin
  response = nil
  ActiveOrient.db_pool.checkout do | conn |
    response = conn["/database/#{ActiveOrient.database}"].delete
  end
    if database == old_ds
      change_database  'temp'
      logger.info{"Working database deleted, switched to temp"}
    else
      change_database old_ds
      logger.info{"Database #{database} deleted, working database is still #{ActiveOrient.database}"}
    end
  rescue RestClient::InternalServerError => e
    change_database old_ds
    logger.info{"Database #{database} NOT deleted, working database is still #{ActiveOrient.database}"}
  end
  !response.nil? && response.code == 204 ? true : false
end

#delete_property(o_class, field) ⇒ Object

PROPERTY #############



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rest/delete.rb', line 137

def delete_property o_class, field
  logger.progname = 'RestDelete#DeleteProperty'
  begin
  response = ActiveOrient.db_pool.checkout do | conn |
    r =  conn["/property/#{ActiveOrient.database}/#{classname(o_class)}/#{field}"].delete
    true if r.code == 204
  end
  rescue RestClient::InternalServerError => e
    logger.error{"Property #{field} in  class #{classname(o_class)} NOT deleted" }
      false
  end
end

#delete_record(*o) ⇒ Object Also known as: delete_document

Deletes a single Record when providing a single rid-link (#00:00) or a record

Deletes multible Records when providing a list of rid-links or a record

Todo: implement delete_edges after querying the database in one statement

Example:

V.create_class :test record = Test.new something: ‘something’ V.db.delete_record record

records= (1..100).map{|x| Test.create something: x } V.db.delete_record *records

delete_records provides the removal of datasets after quering the database.


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rest/delete.rb', line 98

def delete_record *o
   logger.progname = "ActiveOrient::RestDelete#DeleteRecord"
  #o.map( &:to_orient ).map do |r|
  o.orient_flatten.map do |r|
    rr =  r.to_human
    begin
      ActiveOrient::Base.remove_rid r 
#       rest_resource["/document/#{ActiveOrient.database}/#{r[1..-1].to_or}"].delete
      ActiveOrient.db_pool.checkout do | conn |
        conn["/document/#{ActiveOrient.database}/#{r.rid}"].delete
      end

    rescue RestClient::InternalServerError => e
      logger.error{"Record #{rr} NOT deleted"}
    rescue RestClient::ResourceNotFound
      logger.error{"Record #{rr} does not exist in the database"}
    rescue RestClient::BadRequest => e
      logger.error{"tried to delete  #{rr}, but something went wrong"}
      raise
    else
      logger.info{"Record #{rr} deleted"}
    end
  end
end

#delete_records(o_class, where: {}) ⇒ Object Also known as: delete_documents

Deletes records. They are defined by a query. All records which match the attributes are deleted.

An Array with freed index-values is returned


129
130
131
132
# File 'lib/rest/delete.rb', line 129

def delete_records o_class, where: {}
  logger.progname = 'RestDelete#DeleteRecords'
  get_records(from: o_class, where: where).each{|y| delete_record  y}
end