sql - In a reversely linked list, how to get a sublist of it efficiently?
问题描述
Supposed I have a reversely linked list, i.e. a data structure where each node points to its predecessor:
A <- B <- C <- D
This is a pattern you can find in Git, for example, where each commit contains the ID of its predecessor.
Now let's assume the following scenario: Every time a new node is added to the list, another component gets notified. Unfortunately, from time to time, some of these notifications get lost, so there is no guarantee that all the notifications arrive. For the list given above, the notifications should be:
A
B
C
D
But, as an example, the following is received:
A
D
Now I would like to detect "holes" in the receiving component. I.e., when D
is received, the component can detect that something is missing, since the predecessor of D
has not been received as well. So it asks the sending component for the part that is missing. What can be told is: The last one that was received is A
, and the newest one received is D
.
So now the component managing the original list has the task of effectively figuring out that what is missing is the sublist B <- C
.
And this is finally, where my question comes into play: How to do this effectively? I mean, the simplest approach would be to move backwards from D
, until we reach A
. Everything in between is apparently what's missing.
Supposed every node is stored as a single record in a table in a relational database system: What is the most efficient way to figure out this sublist? Obviously, I could run SELECT
in a loop over and over and over again. Is there a better way for this?
The table layout of the Nodes
table basically looks like this:
ID | PredecessorID | Data
-------------------------------------|--------------------------------------|-----------------
43b1e103-d8c6-40f9-b031-e5d9ef18a739 | null | ...
55f6951b-5ed3-46c8-9ad5-64e496cb521a | 43b1e103-d8c6-40f9-b031-e5d9ef18a739 | ...
3eaa0889-31a6-449d-a499-e4beb9e4cad1 | 55f6951b-5ed3-46c8-9ad5-64e496cb521a | ...
This means, as the IDs are not numerical, you can not simply select the range of the missing nodes.
PS: The only solution I'm aware of is introducing a position
field which actually is an increasing numerical ID, but I explicitly do not want to have something as this, as then I need a single point of failure which consistently hands out the next ID, and this is something I would like to avoid.
解决方案
推荐阅读
- python-3.x - 在 python selenium 中使用 send_key() 函数时出现错误“ElementNotInteractableException:消息:元素不可交互”
- node.js - 码头工人之间的redis和nodejs连接问题
- racket - 收集列表中的值
- http - 域转发:重定向到专用网络中的站点
- mysql - 如何在 Ubuntu 中访问 Mysql?
- django - Django 模板 - 按块生成和保存 XML 文件(以节省 RAM)
- javascript - 出现错误无法将属性“innerHTML”设置为空?
- django - 从 Django 的项目目录中提供静态文件
- java - javafx vbox 和 gridpane
- javascript - javascript - Uncaught (in promise) TypeError: e.iterator is not a function。如何解决此类错误?