如何在该文件系统模型中查找节点(文件或目录)的绝对路径
这实际上是在最近的一次采访中向我提出的问题,我的回答是干净的。
问题是设计一个数据库来表示一个文件系统,其中 -
- 有一个根目录,并且其中存在许多文件/目录。
- 一个目录内可以有任意数量的目录/文件。
要求
- 查找给定文件的同一目录中存在的所有文件。
- 给定一个文件/目录,从根找到它的路径。
条件 - 该模型必须有一个数据库表。不是强制性的,但最好有,因为面试问题有这个条件。
我不知道制作一棵树并如下图所示对其进行编号可以使其更加优化 -
上述树结构的有用属性是 -
- 考虑任意两个子树,例如子树 A(dir2 及其子树)和子树 B(dir3 及其子树),子树 A 中所有节点的数量将小于这Next_Subtree_First_Node,在本例中为 dir3。
然后,如果我们将信息存储在这样的数据库结构中 -
_______________________________________________________________________
Sequence_Number Name type
-----------------------------------------------------------------------
0 Parent Directory Dir
1 dir1 Dir
2 file1 File
3 file2 File
4 file3 File
5 dir2 Dir
...
________________________________________________________________________
注意 - 上面的结构是我在讨论过程中记得的。这可能不是解决问题的最佳结构。如果需要进行一些更改,请告诉我。
第一个查询将是这样的 -
查找同一目录中的所有文件
Select Name From File_System
where Sequence_Number Between Next_Subtree_First_Node
and Previous_Subtree_Last_Node
and type = "File";
我的问题
- 上面的查询将返回所有必需的文件,但我如何知道 Next_Subtree_First_Node 和 Previous_Subtree_Last_Node预先。
例如,对于 file4,
Next_Subtree_First_Node = 7 且 Previous_Subtree_Last_Node = 4。
- 我不确定绝对路径查找查询的逻辑应该是什么。请给一些想法。
例如,给定 file7,结果应该是 -
Parent Directory / dir3 / dir4 / file7。
This was actually a question asked to me during a recent interview and I was clean bowled.
The question was to design a database to represent a file system where -
- there is a root directory and many files/directories are present inside it.
- there can be any number of directories/files inside a directory.
Requirements
- find all the files present in the same directory of a given a file.
- given a file/directory, find its path from the root.
Condition
- There has to be one database table for the model. Not mandatory, but good to have as the Interview question had this condition.
I did not know that making a tree and numbering it as shown in the following diagram makes it so much more optimized -
The useful property of the above tree structure is that -
- Considering any two subtrees, e.g. Subtree A (dir2 and its children) and Subtree B (dir3 and its children), the numbers of all the nodes in Subtree A will be lesser than the Next_Subtree_First_Node, which is dir3 in this case.
Then if we store the info in a database structure like this -
_______________________________________________________________________
Sequence_Number Name type
-----------------------------------------------------------------------
0 Parent Directory Dir
1 dir1 Dir
2 file1 File
3 file2 File
4 file3 File
5 dir2 Dir
...
________________________________________________________________________
NOTE - The above structure is what I remember during the discussion. This may not be the best structure to solve the problem. Please let me know if some change is needed.
The first query will be like this -
Find all files in the same directory
Select Name From File_System
where Sequence_Number Between Next_Subtree_First_Node
and Previous_Subtree_Last_Node
and type = "File";
My Questions
- The above query will return all the required files, but how do I know the Next_Subtree_First_Node and Previous_Subtree_Last_Node beforehand.
E.g. for file4,
Next_Subtree_First_Node = 7 and
Previous_Subtree_Last_Node = 4.
- I am not sure what should be the logic of the absolute path finding query. Please give some ideas.
E.g. given file7, the result should be -
Parent Directory / dir3 / dir4 / file7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果文件系统的每个文件都有一个名称(如在 Windows 中;因此没有硬链接,如在 Unix 中),那么您可以将包含目录与每个文件一起存储,并以自下而上的方式跟踪路径回到根目录。
用数据库术语来说,目录及其包含的文件之间存在一对多关系,并迭代
SELECT
父级,直到到达根目录。If the filesystem has one name per file (as in Windows; so no hard links, as in Unix) then you can store the containing directory with each file and trace the path back to the root in a bottom-up fashion.
In database terms, you'd have a one-to-many relation between directories and the files they contain and iteratively
SELECT
the parent until you're at the root.我认为你需要在哪里创建表。
1:根表:
存在子进程的地方(包括:目录和文件)。
2:childe_表:
现在,您可以在此处指定每个的类型和根的 ID。
3:目录表:
其中目录名称和父 ID。
4:文件表:
其中文件名,父目录id。
然后您创建关系...
对于查询,您将需要加入以便找到所有子项和绝对路径。
I think you need to create tables where.
1: root_table:
Where there is childes (include: directories, and files).
2: childe_table:
Now here you specify each with type of each and Id of root.
3: directory_table:
where directory name and parent id.
4: file_table:
Where file name, parent directory id.
So then you create the relation...
And for query you will need join so to find all childes and absolute path.