Skip to content
Snippets Groups Projects
Commit 32d7af3f authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Merge branch 'optimize-remove-child' into 'master'

Optimize Element.remove_child

See merge request !1411
parents 8f2650f2 a7be6209
No related branches found
No related tags found
1 merge request!1411Optimize Element.remove_child
......@@ -294,24 +294,25 @@ class Element(IndexableModel):
# Get all the child's children's paths
children_epaths = list(ElementPath.objects.filter(
element__in=list(Element.objects.get_descending(child.id)),
element__in=Element.objects.get_descending(child.id),
path__contains=[self.id]
))
for epath in children_epaths:
new_path = epath.path[epath.path.index(self.id) + 1:] # Strip this parent's path
new_epaths[epath.element, tuple(new_path)] = ElementPath(
element=epath.element,
new_epaths[epath.element_id, tuple(new_path)] = ElementPath(
element_id=epath.element_id,
path=new_path,
ordering=epath.ordering,
)
# Delete everything
ElementPath.objects.filter(
element__in=list(Element.objects.get_descending(child.id)) + [child],
element__in=Element.objects.get_descending(child.id),
path__contains=[self.id]
).delete()
child.paths.filter(path__contains=[self.id]).delete()
# Recreate paths as needed
ElementPath.objects.bulk_create(new_epaths.values())
......
......@@ -19,108 +19,33 @@ WHERE ("documents_elementpath"."element_id" = '{source_id}'::uuid
AND NOT ("documents_elementpath"."path"[array_length("documents_elementpath"."path", 1)] = '{parent_id}'::uuid))
LIMIT 1;
SELECT DISTINCT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea,
"documents_elementpath"."ordering"
FROM "documents_element"
INNER JOIN "documents_elementpath" ON ("documents_element"."id" = "documents_elementpath"."element_id")
WHERE "documents_elementpath"."path" @ > ARRAY['{source_id}'::uuid]::uuid[]
ORDER BY "documents_elementpath"."ordering" ASC;
SELECT "documents_elementpath"."id",
"documents_elementpath"."element_id",
"documents_elementpath"."path",
"documents_elementpath"."ordering"
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN ('{children_ids[0]}'::uuid,
'{children_ids[3]}'::uuid,
'{children_ids[1]}'::uuid,
'{children_ids[2]}'::uuid)
WHERE ("documents_elementpath"."element_id" IN
(SELECT DISTINCT U0."id"
FROM "documents_element" U0
INNER JOIN "documents_elementpath" U1 ON (U0."id" = U1."element_id")
WHERE U1."path" @ > ARRAY['{source_id}'::uuid]::uuid[])
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
SELECT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea
FROM "documents_element"
WHERE "documents_element"."id" = '{children_ids[0]}'::uuid
LIMIT 21;
SELECT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea
FROM "documents_element"
WHERE "documents_element"."id" = '{children_ids[1]}'::uuid
LIMIT 21;
SELECT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea
FROM "documents_element"
WHERE "documents_element"."id" = '{children_ids[2]}'::uuid
LIMIT 21;
SELECT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea
FROM "documents_element"
WHERE "documents_element"."id" = '{children_ids[3]}'::uuid
LIMIT 21;
SELECT DISTINCT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea,
"documents_elementpath"."ordering"
FROM "documents_element"
INNER JOIN "documents_elementpath" ON ("documents_element"."id" = "documents_elementpath"."element_id")
WHERE "documents_elementpath"."path" @ > ARRAY['{source_id}'::uuid]::uuid[]
ORDER BY "documents_elementpath"."ordering" ASC;
DELETE
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN
(SELECT DISTINCT U0."id"
FROM "documents_element" U0
INNER JOIN "documents_elementpath" U1 ON (U0."id" = U1."element_id")
WHERE U1."path" @ > ARRAY['{source_id}'::uuid]::uuid[])
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
DELETE
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN ('{children_ids[0]}'::uuid,
'{children_ids[3]}'::uuid,
'{children_ids[1]}'::uuid,
'{children_ids[2]}'::uuid,
'{source_id}'::uuid)
WHERE ("documents_elementpath"."element_id" = '{source_id}'::uuid
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
INSERT INTO "documents_elementpath" ("id",
"element_id",
"path",
......
......@@ -19,39 +19,31 @@ WHERE ("documents_elementpath"."element_id" = '{source_id}'::uuid
AND NOT ("documents_elementpath"."path"[array_length("documents_elementpath"."path", 1)] = '{parent_id}'::uuid))
LIMIT 1;
SELECT DISTINCT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea,
"documents_elementpath"."ordering"
FROM "documents_element"
INNER JOIN "documents_elementpath" ON ("documents_element"."id" = "documents_elementpath"."element_id")
WHERE "documents_elementpath"."path" @ > ARRAY['{source_id}'::uuid]::uuid[]
ORDER BY "documents_elementpath"."ordering" ASC;
SELECT "documents_elementpath"."id",
"documents_elementpath"."element_id",
"documents_elementpath"."path",
"documents_elementpath"."ordering"
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN
(SELECT DISTINCT U0."id"
FROM "documents_element" U0
INNER JOIN "documents_elementpath" U1 ON (U0."id" = U1."element_id")
WHERE U1."path" @ > ARRAY['{source_id}'::uuid]::uuid[])
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
DELETE
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN
(SELECT DISTINCT U0."id"
FROM "documents_element" U0
INNER JOIN "documents_elementpath" U1 ON (U0."id" = U1."element_id")
WHERE U1."path" @ > ARRAY['{source_id}'::uuid]::uuid[])
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
SELECT DISTINCT "documents_element"."id",
"documents_element"."created",
"documents_element"."updated",
"documents_element"."corpus_id",
"documents_element"."type_id",
"documents_element"."name",
"documents_element"."worker_version_id",
"documents_element"."image_id",
"documents_element"."polygon"::bytea,
"documents_elementpath"."ordering"
FROM "documents_element"
INNER JOIN "documents_elementpath" ON ("documents_element"."id" = "documents_elementpath"."element_id")
WHERE "documents_elementpath"."path" @ > ARRAY['{source_id}'::uuid]::uuid[]
ORDER BY "documents_elementpath"."ordering" ASC;
DELETE
FROM "documents_elementpath"
WHERE ("documents_elementpath"."element_id" IN ('{source_id}'::uuid)
WHERE ("documents_elementpath"."element_id" = '{source_id}'::uuid
AND "documents_elementpath"."path" @ > ARRAY['{parent_id}'::uuid]::uuid[]);
RELEASE SAVEPOINT "{savepoints[0]}";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment