I had an insight this morning, with regards to the mismatch between d/with and d/as-of. Famously, frustratingly, using these together does not work, since the filter added by as-of filters out any datoms installed by with. π§΅
The insight was this: Datomic already has the ability to use d/with on a database from a specific point in time, and has had that ability since its release.
Consider (d/db conn) - this db is now forever frozen at the moment of invocation. It is the core promise of "database as a value". And this db will work perfectly fine however many transactions come after it. I can hold on to this db for as long as I like.
So, in order for Datomic to deliver on its promise, it has to have the ability to maintain the value-semantics of a db back in time in a way that d/with can work with.
This leads me to believe that - had as-of used these same mechanics that are already in play, instead of adding a filter - we could have gotten the capability to use these together.
Any thoughts on this from the Datomic team? π
not on the datomic team, and I have not run into this particular issue, but when using d/with you generally have application control of the creation of new "with-versions" and could/maybe should roll your own as-of for your "with-versions" that just returns the pointer to the db-version in question for your particular domain.
I assume the d/as-of is a trade of in performance vs memory to present a view of an older database version without having to realize intermediate database versions when having multiple peers etc.
Implementation wise it is certainly possible to realize a "true" (not access filtered) version of a particular database version (just grab an older database version and internally "run the log" on this up until the correct version and return this database pointer), but I it has different performance characteristics in terms of memory access and usage (essentially you will have to realize collections stripped on not yet known novelty, which I assume is memory intensive). Also things like excise, gc etc needs close consideration.
Thanks for chiming in, Linus, but that doesn't really address my question. I'm still hoping for some insight from the team. π
While I understand the desire here or at least I believe I do, this wasn't an implementation choice that we failed to make, but is built into how with/asOf/since/history are specified to work and how we maintain indexes going forward in a Datomic DB. Sure we could have made different choices, but maybe this is a better way to approach it. A filtered database cannot be passed to with. A filtered database cannot have it's index structure altered as it isn't a real index, its the current index + a read time exclusion.
d/db conn isn't "frozen" per say (sure you can save off DB values in memory of your app by deffing dbs but only while your running that peer), d/db conn is just the current basis. Every transaction commit produces a new root as a normal side effect and d/db just returns a reference to whatever the root is currently. It's not really "sitting around" as a separate thing. Sure you can point to a t included in that db to get a filtered view of the DB but it's not a place/saved off as you would want. If it were Datomic DB sizes on disk would be much larger and the maintenance of past DBs might look like forking (I am not saying you can't design around that, but forking introduces a lot of overhead). Index segments are replaced when an indexing job completes and the replaced index segments are what Datomic GC is collecting they are completely discarded and replaced.
I am sure more folks with the team will chime in. I do think there are features in this area that we have yet to explore fully and we do have some thinking about enabling more power using DB values. It might be worth it Magnars for you to share "in an ideal world I want to be able to X/Y with my Datomic DB". Rather than letting me speculate (haha) on what you are trying to do here. If you'd like to e-mail that to me or DM me I am open to sharing that with the development team during our bi-weekly planning calls.
Thank you for the detailed answer, @jaret! If I understand correctly, then the db created with (d/db conn) is holding onto a root that will be garbage collected as soon as it is out of scope, and thus not something that can be reconstructed at a later time, leading to the need for a filter to "time travel". I would be happy to DM you if that is preferable, but let me summarise briefly here anyway. βΊοΈ
β’ An error happens in production.
β’ We log the basis-t.
β’ Using the failing command-data alongside an as-of'ed db, we get to reproduce the error locally.
This all works wonderfully, and is a big help to track down gnarly bugs. However, this workflow precludes us from using d/with anywhere in our code, since that would break this ability to reliably reproduce errors.
Recovering past states can be done (fairly) efficiently by filtering, and that is what d/since and d/as-of do. But a d/with with a past state as basis must not merely filter but truncate all newer information. This cannot be done efficiently with indexes if its basis is after the basis you want to branch from: you need to remove all datoms after the new basis T (which could be anywhere in the index) because they may be replaced by the same T with different contents.
For some particular, naΓ―ve filter expressions (for instance, "ignore all datoms where attribute=X") it would be possible to create a consistent new db in a d/with on the fly with much structural sharing. For a complex filter such as as-of - complex wrt to distribution as well as unique values etc, the transact "algebra" becomes much more complex, but there might be tricks to work around the transactions in the shadowed part of the transaction log to make the reported tx-log consistent.
In the debug case @magnars describes the function I think is missing is a way to replay a tx-log from, say, prepare a db from a backup, with the closest earlier T, with stable assignment of entity and tx ids (use the ones from the tx-log). For a debug feature, memory usage is probably less important.
The limiting factor is just the index basis. Any unindexed portion of log can be truncated and its in-memory index rebuilt. Thus your T can go back as far as the index basis T. Going back farther requires retaining older indexes with lower basis Ts (<= your intended branch T)
If you don't care about the operation being quick, in theory you could make a new index at some older basis by filtering out datoms.
Or replaying the entire log (likely much slower)
There are no user serviceable parts that allows creating a new index based on some filtered collection of datoms, right?
you can't create a durable new index from an arbitrary set of datoms, only from a log
More precisely a prior index + a log
(every fresh database starts with a bootstrap index)
so that is the fundamental limit: you can't branch from before the bootstrap index basis (t=1000)
Makes sense, but is there currently a way to arrive at a "realized" (not filtered) database (in Peer memory) with deterministic entity ids given a known tx-log (and some known previous index, preferably not too far behind)?
It is not impossible, but the interfaces necessary to do this are not public
I wonder if you two would be interested in a https://conf.datomic.com/ in December--we could talk about this in person.
A datomic conference sounds very interesting and fun, but travel to the US is not viable for me at the moment, unfortunately.
For my use case, if it could be possible to get ahold of a db that is functionally the same as the one I would have had if I held onto a (d/db conn) at a given point in time - it would not be a problem if that took a few seconds to build. Speed is not of the essence, only the reproducibility of past states. Such a feature would then allow us to use d/with.