JCR 170 数据建模:节点名称

发布于 2024-07-15 18:50:14 字数 595 浏览 6 评论 0原文

情况:
假设我们正在实现一个基于 JCR 并支持本地化的博客引擎。
内容结构看起来像这样 /blogname/content/[节点名称]

问题: 命名内容节点 (/blogname/content/[nodename]) 的最佳方式是什么,以满足以下要求:

  1. 节点名称必须可在 HTML 中使用,以支持类似 REST 的 URL,即:blogname。 com/content/nodename 应指向单个内容项。
  2. 上述要求不得产生丑陋的 URL,即:/content/node_name 是好的,/content/node%20name 是坏的。
  3. 给定节点名称,编程检索应该很容易,即: //content[@node_name=some-name]
  4. 命名方案必须保证节点名称的唯一性。

PS:使用的 JCR 实现是 JackRabbit

The situation:
Lets say we are implementing a blog engine based on JCR with support for localization.
The content structure looks something like this /blogname/content/[node name]

The problem:
What is the best way to name the content nodes (/blogname/content/[nodename]) to satisfy the following requirements:

  1. The node name must be usable in HTML to support REST like URLs i.e.: blogname.com/content/nodename should point to a single content item.
  2. The above requirement must not produce ugly URLs i.e.: /content/node_name is good, /content/node%20name is bad.
  3. Programmatic retrieval should be easy given the node name i.e.: //content[@node_name=some-name]
  4. The naming scheme must guarantee node name uniqueness.

PS: The JCR implementation used is JackRabbit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§对你不离不弃 2024-07-22 18:50:14

对于 1. 到 3.,答案很简单:只需使用您希望在节点名称中看到的字符,即。 针对受限字符集转义您拥有的任何输入字符串(例如博客文章标题),例如 一个用于 URI

例如,不允许使用空格(JCR 节点名称允许使用空格,但会在 URL 中产生难看的 %20)和其他必须在 URL 中编码的字符。 您可以删除这些字符或简单地将它们替换为下划线,因为在大多数情况下这看起来不错。

关于唯一名称 (4.),您可以包含当前时间(包括时间)。 毫秒或您显式检查冲突。 第一个可能看起来有点难看,但对于博客场景来说可能永远不会失败。 后者可以通过对抛出的异常做出反应(如果具有此类名称的节点已存在)并添加例如来完成。 递增计数器并重试(例如 my_great_post1my_great_post2 等)。 您还可以锁定父节点,以便只有一个会话可以同时实际添加节点,这避免了尝试循环,但代价是阻塞。

注意: //content[@node_name=some-name] 不是有效的 JCR Xpath 查询。 您可能想使用 /jcr:root/content//some-name 来实现这一点。

For 1. to 3. the answer is simple: just use characters you want to see in the node name, ie. escape whatever input string you have (eg. the blog post title) against a restricted character set such as the one for URIs.

For example, do not allow spaces (which are allowed for JCR node names, but would produce the ugly %20 in URLs) and other chars that must be encoded in URLs. You can remove those chars or simply replace them with a underscore, because that looks good in most cases.

Regarding unique names (4.), you can either include the current time incl. milliseconds into it or you explicitly check for collisions. The first might look a bit ugly, but should probably never fail for a blog scenario. The latter can be done by reacting upon the exception thrown if a node with such a name already exists and adding eg. an incrementing counter and try again (eg. my_great_post1, my_great_post2, etc.). You can also lock the parent node so that only one session can actually add a node at the same time, which avoids a trial loop, but comes at the cost of blocking.

Note: //content[@node_name=some-name] is not a valid JCR Xpath query. You probably want to use /jcr:root/content//some-name for that.

浪荡不羁 2024-07-22 18:50:14

关于第3项。我最近了解到xpath查询不允许项目以数字开头。 如果您的节点名称以数字开头,仍然可以通过转义名称的第一个字节来查询,但如果所有节点名称都以字母开头,您的查询将更加简单。

(我不确定属性名称。还没有见过不以字母开头的属性名称。)

Regarding item 3. I recently learned that xpath queries do not allow items to start with a number. If your node name starts with a number it can still be queried by escaping the first byte of the name, but your queries will be more straightforward if you start all node names with a letter.

(I'm not sure about property names. Haven't ever seen one that didn't start with a letter.)

乱了心跳 2024-07-22 18:50:14

唯一名称:要从标题的第一个字符加上随机数(以解决冲突)快速生成唯一名称,您可以使用以下算法:

String title = "JCR 170 Data modeling: Node names";
String name = title.substring(0, Math.min(title.length(), 10)).trim().replace(' ', '_');
if (name is not unique) {
    name += "_";
    Random r = new Random();
    while (name is not unqiue) {
        name += Integer.toString(r.nextInt(10));
    }
}

使用随机数的优点是:即使您有许多相似的名称,这将很快解决冲突。

Unique names: To quickly generate a unique name from the first characters of a title plus a random number (to resolve conflicts), you could use the following algorithm:

String title = "JCR 170 Data modeling: Node names";
String name = title.substring(0, Math.min(title.length(), 10)).trim().replace(' ', '_');
if (name is not unique) {
    name += "_";
    Random r = new Random();
    while (name is not unqiue) {
        name += Integer.toString(r.nextInt(10));
    }
}

The advantage to use a random number is: even if you have many similar names, this will resolve conflicts very quickly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文