gpt4 book ai didi

postgresql - 在 postgres 数据库中保存文件树的最简单方法是什么?

转载 作者:行者123 更新时间:2023-11-29 12:31:24 24 4
gpt4 key购买 nike

我正在从保管箱 api 中检索文件树。在 api 中,每个文件夹都通过单独的 api 调用读取,因此我将遍历整个文件树以获取所有文件夹。这是通过 cron-job 完成的。

从保管箱中检索数据的函数如下所示:

function renderFolderTree($myobject, $path){
$entry = $myobject->getMetadataWithChildren($path);
foreach ($entry['contents'] as $child) {
if ($child['is_dir']){
$folderpath = $child['path'];
//this will retrieve the child-folder
renderFolderTree($myobject, $folderpath, $filetree);
//here I need something that saves the folder
}else{
print_r($child);
//here I need something that saves the file
}
}
}

我想将文件树保存到 postgres 数据库中,以便稍后可以将其输出为表示它的 json 对象。

我是数据库设计的新手,不确定用什么方式保存数据。我假设每个文件和文件夹都应该有自己的数据库条目。我可以让每个 child 都引用它的 parent ID,或者我可以让每个 parent 都包含它的 child 的列表。

因为我是新手,我想要一个相当简单的解决方案,阅读速度比写作更重要!

最佳答案

在关系数据库中存储树有多种选择。为了获得良好的概述,我推荐 slides比尔卡尔文。

既然您提到读取速度是最重要的,闭包表将是一种合适的、强大的编码。闭包表是一种多对多关系,它为每个路径(例如,/a/b/c)存储所有父/子(传递)。这样,可以使用一个 SQL 查询(非递归)完成对树的许多查询。

看起来像

create table nodes (
path varchar primary key
/* your other attributes here, can be null */
);

create table parents_children (
parent_path varchar,
child_path varchar,
primary key(parent_path,child_path),
foreign key (parent_path) references nodes (path),
foreign key (child_path) references nodes (path)
);

要在目录/a/b/下插入一个新文件/a/b/c,您可以这样做:

insert into nodes values ('/a/b/c');

insert into parents_children
select parent_path, '/a/b/c' from parents_children where child_path = '/a/b/'
union all select '/a/b/c','/a/b/c';

例如,要递归地查询来自“/a”的所有 child ,您可以这样做:

select * 
from nodes join parents_children on path = child_path
where parent_path = '/a';

一个更详尽的示例,它存储了以下文件树:

/
/a/
/a/b/
/a/b/d
/a/c
/b

插入数据:

insert into nodes values ('/');
insert into parents_children values ('/','/');

insert into nodes values ('/a/');
insert into parents_children
select parent_path, '/a/' from parents_children where child_path = '/'
union all select '/a/','/a/';

insert into nodes values ('/a/b/');
insert into parents_children
select parent_path, '/a/b/' from parents_children where child_path = '/a/'
union all select '/a/b/','/a/b/';

insert into nodes values ('/a/c');
insert into parents_children
select parent_path, '/a/c' from parents_children where child_path = '/a/'
union all select '/a/c','/a/c';

insert into nodes values ('/a/b/d');
insert into parents_children
select parent_path, '/a/b/d' from parents_children where child_path = '/a/b/'
union all select '/a/b/d','/a/b/d';

insert into nodes values ('/b');
insert into parents_children
select parent_path, '/b' from parents_children where child_path = '/'
union all select '/b','/b';

查询/a/的所有 child

select node.*
from nodes join parents_children on path = child_path
where parent_path = '/a/';

path
----------
/a/
/a/b/
/a/b/d
/a/c

关于postgresql - 在 postgres 数据库中保存文件树的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19834400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com