Skip to content

Commit

Permalink
dont drop table when deleting entities, use a separate endpoint for t…
Browse files Browse the repository at this point in the history
…hat.
  • Loading branch information
c0c0n3 committed Sep 23, 2020
1 parent 1b1417f commit aa64b37
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 75 deletions.
20 changes: 19 additions & 1 deletion specification/quantumleap.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
swagger: '2.0' # For 3.0 see (https://github.com/zalando/connexion/issues/420)
info:
title: "QuantumLeap API"
version: "0.7.5" # we'll keep it aligned with QL version
version: "0.7.6" # we'll keep it aligned with QL version
host: "localhost:8668" # it'll run in the same container, hence localhost.
produces:
- text/plain
Expand Down Expand Up @@ -1759,3 +1759,21 @@ paths:
}
]
}

/v2/storage/{entityType}:
delete:
operationId: reporter.delete.drop_entity_storage
summary: "Drop supporting storage for the given entity type."
description: "Given an entity type, delete all the records of that
type, drop the table in which they were stored, and clean up the
metadata table accordingly."
tags:
- input
parameters:
# In Path...
- $ref: '#/parameters/entityTypeInPath'
# In Header...
- $ref: '#/parameters/fiware-Service'
responses:
204:
description: "Storage successfully deleted."
12 changes: 9 additions & 3 deletions src/reporter/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def delete_entity(entity_id, type_=None, from_date=None, to_date=None):

try:
with crate.CrateTranslatorInstance() as trans:
deleted = trans.delete_entity(entity_id=entity_id,
entity_type=type_,
deleted = trans.delete_entity(eid=entity_id,
etype=type_,
from_date=from_date,
to_date=to_date,
fiware_service=fiware_s,
Expand All @@ -37,7 +37,7 @@ def delete_entities(entity_type, from_date=None, to_date=None):
fiware_sp = request.headers.get('fiware-servicepath', None)

with crate.CrateTranslatorInstance() as trans:
deleted = trans.delete_entities(entity_type=entity_type,
deleted = trans.delete_entities(etype=entity_type,
from_date=from_date,
to_date=to_date,
fiware_service=fiware_s,
Expand All @@ -51,3 +51,9 @@ def delete_entities(entity_type, from_date=None, to_date=None):

if deleted > 0:
return '{} records successfully deleted.'.format(deleted), 204


def drop_entity_storage(entity_type: str):
fiware_s = request.headers.get('fiware-service', None)
with crate.CrateTranslatorInstance() as trans:
trans.drop_table(etype=entity_type, fiware_service=fiware_s)
2 changes: 1 addition & 1 deletion src/reporter/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def delete_entity_type(service, entity_type, service_path=None):
if service_path:
h['Fiware-ServicePath'] = service_path

url = '{}/types/{}'.format(QL_URL, entity_type)
url = '{}/storage/{}'.format(QL_URL, entity_type)

r = requests.delete(url, headers=h)
# assert r.status_code == 204
Expand Down
92 changes: 22 additions & 70 deletions src/translators/sql_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,96 +992,48 @@ def _format_response(self, resultset, col_names, table_name, last_n):

return [entities[k] for k in sorted(entities.keys())]

def delete_entity(self, entity_id, entity_type=None, from_date=None,
def delete_entity(self, eid, etype=None, from_date=None,
to_date=None, fiware_service=None,
fiware_servicepath=None):
if not entity_id:
if not eid:
raise NGSIUsageError("entity_id cannot be None nor empty")

if not entity_type:
entity_type = self._get_entity_type(entity_id, fiware_service)

if not entity_type:
return 0

if len(entity_type.split(',')) > 1:
raise AmbiguousNGSIIdError(entity_id)

# First delete entries from table
table_name = self._et2tn(entity_type, fiware_service)

# how many rows in total?
try:
self.cursor.execute("select count(*) from {}".format(table_name))
except Exception as e:
logging.error("{}".format(e))
return 0
# TODO why the result still keeps into account the deleted rows???
# this query was moved up to make it "consistent" also with db that
# may execute delete instantly (this does not seem the case of crate)
count = self.cursor.fetchall()[0][0]

where_clause = self._get_where_clause([entity_id, ],
from_date,
to_date,
fiware_servicepath)
op = "delete from {} {}".format(table_name, where_clause)
if not etype:
# TODO: rahter make entity type mandatory and bail out if not
# present.
# In fact, _get_entity_type will scan **all** DB tables to
# figure out candidate entity types for the given entity ID!

try:
self.cursor.execute(op)
except Exception as e:
logging.error("{}".format(e))
return 0

deleted_rows = self.cursor.rowcount
etype = self._get_entity_type(eid, fiware_service)

count = count - deleted_rows
if not etype:
return 0

if count == 0:
# Drop whole table
self._drop_table(table_name)
if len(etype.split(',')) > 1:
raise AmbiguousNGSIIdError(eid)

return deleted_rows
return self.delete_entities(etype, eid=[eid],
from_date=from_date, to_date=to_date,
fiware_service=fiware_service,
fiware_servicepath=fiware_servicepath)

def delete_entities(self, entity_type, from_date=None, to_date=None,
def delete_entities(self, etype, eid=None, from_date=None, to_date=None,
fiware_service=None, fiware_servicepath=None):
table_name = self._et2tn(entity_type, fiware_service)

# how many rows in total?
try:
self.cursor.execute("select count(*) from {}".format(table_name))
except Exception as e:
logging.error("{}".format(e))
return 0
# TODO why the result still keeps into account the deleted rows???
# this query was moved up to make it "consistent" also with db that
# may execute delete instantly (this does not seem the case of crate)
count = self.cursor.fetchall()[0][0]

# Delete only requested range
entity_id = None
where_clause = self._get_where_clause(entity_id,
table_name = self._et2tn(etype, fiware_service)
where_clause = self._get_where_clause(eid,
from_date,
to_date,
fiware_servicepath)
op = "delete from {} {}".format(table_name, where_clause)
try:
self.cursor.execute(op)
return self.cursor.rowcount
except Exception as e:
logging.error("{}".format(e))
return 0

deleted_rows = self.cursor.rowcount

count = count - deleted_rows

if count == 0:
# Drop whole table
self._drop_table(table_name)

return deleted_rows

def _drop_table(self, table_name):
def drop_table(self, etype, fiware_service=None):
table_name = self._et2tn(etype, fiware_service)
op = "drop table {}".format(table_name)
try:
self.cursor.execute(op)
Expand Down

0 comments on commit aa64b37

Please sign in to comment.