When I hit the first infinite loop bug on a code path, I frequently add code to assert that the number of calls is less than $A_LARGE_NUMBER to catch future occurrences of the same root cause.
http://www.cs.cmu.edu/~pane/research.html
http://www.cs.cmu.edu/~pane/thesis/
Pretty cool work regardless, I really like the way it deals with aggregates, for example.
assert(is_tree(comment_graph))
Typically, a composite entity (like an "item" on HN which has many "comments") will define invariants to ensure data integrity. In this case, the invariant is that an "item"'s comments form a tree.The database layer often contains this logic, but it depends on how you're building your application; NoSQL backends for example typically must put validation in the application layer. Since HN just uses files, a well-developed application layer should be riddled with invariants like this.
Also, HN uses flat files, not database.
Might not be a bad idea, if the site were to have the two requirements "maintenance must be done on the live site from a repl" and "5 nines availability".
create table post (id int primary_key, parent_id int references post(id), child_id int references post(id), created_at timestamp)
How will you place the check constraint? You only have parent_id and child_id, not parent and child entities. You will have to write a trigger.I am not saying this can't or shouldn't be done. I am saying a db won't directly solve it.
However, you example will work perfectly for enforcing constraints in the code via the mutator which can compare child and parent timestamps, provided pg was doing it via a mutator, and not directly changing the ids.
create table post (
id int primary_key,
parent_id int references post(id),
CONSTRAINT foo CHECK (id > parent_id)
); CREATE TABLE comment_tree (
ascestor_id REFERENCES comments(id) NOT NULL,
descendant_id REFERENCES comments(id) NOT NULL,
CHECK ( ascestor_id <> descendant_id )
)
but I'm probably overlooking something. (I'm aware that HN uses flat files, I was just making a counter-point to the "simple assert" solution...)