Use event time instead of transaction time in Datomic?

那年仲夏 提交于 2019-12-07 15:01:00

问题


Background

I'm using Datomic to store projections of events generated by other systems (projections in this case can be regarded as an entity in Datomic). These events have a timestamp associated with them that tells when the event was (for example) created. This is obviously not the same as the transaction time that Datomic will assign to the transaction when it stores a new attribute (based on the event) in the projection. Users of my application are not interested in the transaction time but rather the event time. The reason why I'm using Datomic in the first place is to be able to get an entity (or query the database) as it were at a specific time. However this time should not be the Datomic transaction time but rather the event time. For example I want be able to get an entity based on this event time:

(datomic/entity (datomic/as-of db event-time) id)

Possible solution

One idea that I'm thinking of is to set :db/txInstant for each transaction to the event time but I've been advised not to do this as a general principle. Another potential problem with this is that you can't assign a :db/txInstant that is older than the schema (which will be the case in my application). A solution to this would be to set a really early :db/txInstant to the transaction creating the schema.

Question

How does one typically deal with event time rather than transaction time in Datomic? How does the "possible solution" hold up?


回答1:


In general, Datomic's transaction time (t) is intended to record when the system found out about a fact, not the domain time of the fact.

If you need to handle domain time (i.e. the time something occurred in the 'real world', or event time in your example), I'd strongly recommend modeling the domain time explicitly with an attribute (you can use an attribute of type :db.type/instant). This would allow you to set dates without any restriction as well as query both domain time and system time separately for questions like "when did X happen AND when did my database find out about X happening?".



来源:https://stackoverflow.com/questions/39287619/use-event-time-instead-of-transaction-time-in-datomic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!