Databases Reference
In-Depth Information
Each year has its own set of month nodes; each month has its own set of day nodes. We
need only insert nodes into the timeline tree as and when they are needed. Assuming
the root
timeline
node has been indexed, or is in some other way discoverable, the
following Cypher statement will insert all necessary nodes for a particular event—year,
month, day, plus the node representing the event to be attached to the timeline:
START
timeline=
node
:timeline(name=
{timelineName}
)
CREATE UNIQUE
(timeline)-[:YEAR]->(year{value:
{year}
, name:
{yearName}
})
-[:MONTH]->(month{value:
{month}
, name:
{monthName}
})
-[:DAY]->(day{value:
{day}
, name:
{dayName}
})
<-[:BROADCAST_ON]-(n
{newNode}
)
Querying the calendar for all events between a start date (inclusive) and an end date
(exclusive) can be done with the following Cypher:
START
timeline=
node
:timeline(name=
{timelineName}
)
MATCH
(timeline)-[:YEAR]->(year)-[:MONTH]->(month)-[:DAY]->
(day)<-[:BROADCAST_ON]-(n)
WHERE
((year.value >
{startYear}
AND
year.value <
{endYear}
)
OR
(
{startYear}
=
{endYear}
AND
{startMonth}
=
{endMonth}
AND
year.value =
{startYear}
AND
month.value =
{startMonth}
AND
day.value >=
{startDay}
AND
day.value <
{endDay}
)
OR
(
{startYear}
=
{endYear}
AND
{startMonth}
<
{endMonth}
AND
year.value =
{startYear}
AND
((month.value =
{startMonth}
AND
day.value >=
{startDay}
)
OR
(month.value >
{startMonth}
AND
month.value <
{endMonth}
)
OR
(month.value =
{endMonth}
AND
day.value <
{endDay}
)))
OR
(
{startYear}
<
{endYear}
AND
year.value =
{startYear}
AND
((month.value >
{startMonth}
)
OR
(month.value =
{startMonth}
AND
day.value >=
{startDay}
)))
OR
(
{startYear}
<
{endYear}
AND
year.value =
{endYear}
AND
((month.value <
{endMonth}
)
OR
(month.value =
{endMonth}
AND
day.value <
{endDay}
))))
RETURN
n
The
WHERE
clause here, though somewhat verbose, simply filters each match based on
the start and end dates supplied to the query.
Linked lists
Many events have temporal relationships to the events that precede and follow them.
We can use
NEXT
and
PREVIOUS
relationships (or similar) to create linked lists that cap‐
ture this natural ordering, as shown in
Figure 4-7
. Linked lists allow for very rapid
traversal of time-ordered events.