数据库(数据模型)来建立一个文件夹结构 [英] DataBase (datamodel) to build a folder structure

查看:107
本文介绍了数据库(数据模型)来建立一个文件夹结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划在Java中构建基于文件夹的结构。

Planning on building a Folder based structure in Java.

我将使用jQuery插件的图形用户界面,所以我不需要需要关于如何显示文件夹结构的信息。

I will be using a jquery plugin for the GUI, so I don't need need information on how to display the folder structure.

我要寻找的后台逻辑上的信息如何存储文件夹,使得它能够被检索以快速和有效的方式。

I am looking for the backend logic on how folder information is stored, such that it can be retrieved in a quick and efficient manner.

每个文件夹将有多个子文件夹。 从页夹,我们应该能够访问根快速,高效地

Each folder will have multiple subfolders. From a leaf folder, we should be able access the root quickly and efficiently

例如:

+Folder1
  |__SubFolder1_1
  |__SubFolder1_2
        |_SubSubFolder1_2_1
        |_
+Folder2
  |__SubFolder2_1
        |_SubFolder2_1_1
        |_SubFolder2_1_2
             |_SubFolder2_1_2_1

新建文件夹,可以随意添加。 文件夹进行重命名。 文件夹删除即可。

New folders could be added randomly. Folder can be renamed. Folders can be deleted.

我的问题是:

如何将这些文件夹的详细信息被存储在数据库中?

How would these folder details be stored in the database?

此外,我要寻找一个快速,高效的存储和检索信息的方式。

Again, I am looking for a fast and efficient means of storing and retrieving this information.

推荐答案

对于存储在数据库中,最简单,最直接的方式是有一个parent_folder_id为每个文件夹/节点。这应该是在大多数情况下不够好,尤其是你要建立的文件夹对象的结构做处理基础上的对象模型。

For storing in DB, the easiest and most straight forward way is to have an parent_folder_id for each folder/node. This should be good enough in most scenario, especially you are going to construct the folder object structure and do manipulation base on the object model.

取决于你的需求,还有,你需要相当普遍的情况下

Depends on your requirement, there is a quite common case that you need to

  1. 找到了在特定的文件夹中所有子文件夹
  2. 直接从数据库进行查找,通过SQL。

如果这是你在找什么,再有就是,你可以看下一个有趣的方法: 每个数据库记录将有2个额外的数字领域,我们称之为左,右

If it is what you are looking for, then there is a interesting method that you may have a look: Each DB record will have 2 extra number field, let's call it LEFT and RIGHT

假设这样的树:

ROOT
  + A
  | + A1
  | + A2
  + B
    + B1

什么将被存储在DB是

What is going to be stored in DB is

Node  LEFT  RIGHT  ... other fields
ROOT   1    12
A      2    7
A1     3    4
A2     5    6
B      8    11
B1     9    10

  • 在每个父节点不舍=第一个孩子的左 - 1,和RIGHT =最后一个孩子的右+ 1
  • 在叶子节点有左,右是连续2号
  • 在每个节点的左应=前兄弟姐妹的右+ 1,右=下一个兄弟左岸 - 1
  • 当你需要找到在一定的节点(N)由SQL的所有节点,只需找出与左> N.LEFT和右其中的所有节点; N.RIGHT

    When you need to find all nodes under certain node (N) by SQL, simply find out all nodes with LEFT > N.LEFT and RIGHT < N.RIGHT

    您可以轻松地进行插入/通过批量更新删除相关的节点(不是一个艰巨的任务,它留给你:P)

    You can easily perform insert/delete by bulk updating related nodes by (not a difficult task, leave it to you :P )

    这可能不是很OO友好的,但如果我提到的要求是你需要什么,u可以考虑使用这种方法。

    This is probably not very OO friendly but in case the requirement I mentioned is what you need, u may consider using this method.

    这篇关于数据库(数据模型)来建立一个文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆