일정한 길이의 string
child_path[:-path_length_per_depth] == parent_path
descendants_path[:depth_difference * path_length_per_depth] == ascendants_path
ee.jpg
의 file path/opt/aaa/window/ee.jpg
/opt/aaa
에 해당하는 폴더, 파일들/opt/aaa/aa.txt
, /opt/aaa/linux
, /opt/aaa/window/ddd
, /opt/aaa/window/ee.jpg
class MPNode(models.Model):
path_length_per_depth = 1
alphabet = [ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789]
path = models.CharField(
"materialized_path",
max_lenghth=256,
editable=False
)
...
class Meta:
indexes = [
models.Index(
fields=['path'],
name='materialized_path_index'
)
]
def get_descendants(node: MPNode) -> QuerySet:
descendants_query_set = MPNode.objects.filter(
path__startswith=node.path
)
descendants_query_set = descendants_query_set.exclude(
path=node.path
)
return descendants_query_set
# Descendants of node which is 'AAF'
node_AAF = MPNode.objects.get(path='AAF')
descendants_of_node_AAF = get_descendants(node_AAF)
def get_ascendants(node: MPNode) -> QuerySet:
ascendants_path_list = [
node.path[:depth * path_lenghth_per_depth]
for depth in range(1, len(node.path))
]
ascendants_query_set = MPNode.objects.filter(
path__in=ascendants_path_list
)
return ascendants_query_set
SELECT * FROM MPNode WHERE path LIKE 'AAF' AND NOT PATH = 'AAF'
Joe Celko's Trees and Hierarchies in SQL for Smarties