JCR 170 数据建模:节点名称
情况:
假设我们正在实现一个基于 JCR 并支持本地化的博客引擎。
内容结构看起来像这样 /blogname/content/[节点名称]
问题: 命名内容节点 (/blogname/content/[nodename]) 的最佳方式是什么,以满足以下要求:
- 节点名称必须可在 HTML 中使用,以支持类似 REST 的 URL,即:blogname。 com/content/nodename 应指向单个内容项。
- 上述要求不得产生丑陋的 URL,即:/content/node_name 是好的,/content/node%20name 是坏的。
- 给定节点名称,编程检索应该很容易,即: //content[@node_name=some-name]
- 命名方案必须保证节点名称的唯一性。
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:
- 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.
- The above requirement must not produce ugly URLs i.e.: /content/node_name is good, /content/node%20name is bad.
- Programmatic retrieval should be easy given the node name i.e.: //content[@node_name=some-name]
- The naming scheme must guarantee node name uniqueness.
PS: The JCR implementation used is JackRabbit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 1. 到 3.,答案很简单:只需使用您希望在节点名称中看到的字符,即。 针对受限字符集转义您拥有的任何输入字符串(例如博客文章标题),例如 一个用于 URI。
例如,不允许使用空格(JCR 节点名称允许使用空格,但会在 URL 中产生难看的
%20
)和其他必须在 URL 中编码的字符。 您可以删除这些字符或简单地将它们替换为下划线,因为在大多数情况下这看起来不错。关于唯一名称 (4.),您可以包含当前时间(包括时间)。 毫秒或您显式检查冲突。 第一个可能看起来有点难看,但对于博客场景来说可能永远不会失败。 后者可以通过对抛出的异常做出反应(如果具有此类名称的节点已存在)并添加例如来完成。 递增计数器并重试(例如
my_great_post1
、my_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.关于第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.)
唯一名称:要从标题的第一个字符加上随机数(以解决冲突)快速生成唯一名称,您可以使用以下算法:
使用随机数的优点是:即使您有许多相似的名称,这将很快解决冲突。
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:
The advantage to use a random number is: even if you have many similar names, this will resolve conflicts very quickly.