Implement synchronous version of DO KV storage API.#4895
Conversation
47516b5 to
c724567
Compare
|
Hmm, on further thought I think I want to take the opportunity to change It would be more efficient, particularly when you stop iterating early. With the old storage system, we didn't really have a way to cancel list operations early, so returning a Map made more sense. |
ac06a62 to
474ded1
Compare
|
Update:
|
474ded1 to
7f0ef0b
Compare
|
The generated output of |
7f0ef0b to
97f87b2
Compare
joshthoward
left a comment
There was a problem hiding this comment.
A few open questions, but nothing major.
97f87b2 to
9b75aa3
Compare
This adds `ctx.storage.kv`, which has methods `get()`, `put()`, `list()`, and `delete()`. All four methods have the same signatures as the same-named methods of `ctx.storage` except: 1. They return synchronously. No promises. 2. They require SQLite-backed DOs. 3. They don't have the `allowConcurrency` nor `noCache` options since those don't make sense for SQLite. (Nothing stops you from passing these options anyway; they will be ignored.) 4. The `allowUnconfirmed` option is also absent, not because it doesn't make sense, but becaues we haven't actually implemented it for SQLite-backed DOs in general. Maybe we will eventually. 5. Obscure: If you `get()` multiple keys at once, the returned map now iterates in the order in which the keys were specified, rather than in alphabetical order. The old interface's alphabetical ordering was a historical quirk that required an extra sort operation. It had been maintained for backwards-compatibility, but probably nobody really cares about it, so the new interface takes the opportunity to skip the sort. The name `storage.kv` nicely parallels `storage.sql` while avoiding any backwards-compatibility concerns. In the long term we should "deprecate" (but forever support) the old async methods.
This makes it more memory-efficient and permits the application to cancel the list early if it wants.
Since this code is no longer part of a template, we can move it to the .c++ file. This is a pure cut/paste, no changes except removing the `inline` keyword.
…rface. The batch versions of these functions just call the single versions in a loop, which is pointless; you might as well perform the loop on the JS side. Arguably keeping the batch versions could be nice for compatibility, but since list() has changed to be iterable, we aren't really maintaining perfect compatibility anyway. If we decide we do want compatibility later, we can revert this commit.
9b75aa3 to
9b2173d
Compare
|
How long before we can use this a released version in Cloudflare, |
|
@lmaccherone It should work immediately if you override workerd to the latest release, e.g. put this in your package.json: Otherwise, miniflare is released once a week and this change didn't make it into last week's release, but should land in this week's (on ~Thursday). The API went live in production last Friday. |
This adds
ctx.storage.kv, which has methodsget(),put(),list(), anddelete(). All four methods have the same signatures as the same-named methods ofctx.storageexcept:allowConcurrencynornoCacheoptions since those don't make sense for SQLite. (Nothing stops you from passing these options anyway; they will be ignored.)allowUnconfirmedoption is also absent, not because it doesn't make sense, but becaues we haven't actually implemented it for SQLite-backed DOs in general. Maybe we will eventually.get()multiple keys at once, the returned map now iterates in the order in which the keys were specified, rather than in alphabetical order. The old interface's alphabetical ordering was a historical quirk that required an extra sort operation. It had been maintained for backwards-compatibility, but probably nobody really cares about it, so the new interface takes the opportunity to skip the sort.The name
storage.kvnicely parallelsstorage.sqlwhile avoiding any backwards-compatibility concerns. In the long term we should "deprecate" (but forever support) the old async methods.