|
| 1 | +Storage Adapters |
| 2 | +================ |
| 3 | + |
| 4 | +The go-ipld-prime storage APIs were introduced in the v0.14.x ranges of go-ipld-prime, |
| 5 | +which happened in fall 2021. |
| 6 | + |
| 7 | +There are many other pieces of code in the IPLD (and even more so, the IPFS) ecosystem |
| 8 | +which predate this, and have interfaces that are very _similar_, but not quite exactly the same. |
| 9 | + |
| 10 | +In order to keep using that code, we've built a series of adapters. |
| 11 | + |
| 12 | +You can see these in packages beneath this one: |
| 13 | + |
| 14 | +- `go-ipld-prime/storage/bsadapter` is an adapter to `github.com/ipfs/go-ipfs-blockstore`. |
| 15 | +- `go-ipld-prime/storage/dsadapter` is an adapter to `github.com/ipfs/go-datastore`. |
| 16 | +- `go-ipld-prime/storage/bsrvadapter` is an adapter to `github.com/ipfs/go-blockservice`. |
| 17 | + |
| 18 | +Note that there are also other packages which implement the go-ipld-prime storage APIs, |
| 19 | +but are not considered "adapters" -- these just implement the storage APIs directly: |
| 20 | + |
| 21 | +- `go-ipld-prime/storage/memstore` is a simple in-memory storage system. |
| 22 | +- `go-ipld-prime/storage/fsstore` is a simple filesystem-backed storage system |
| 23 | + (comparable to, and compatible with [flatfs](https://pkg.go.dev/github.com/ipfs/go-ds-flatfs), |
| 24 | + if you're familiar with that -- but higher efficiency). |
| 25 | + |
| 26 | +Finally, note that there are some shared benchmarks across all this: |
| 27 | + |
| 28 | +- check out `go-ipld-prime/storage/benchmarks`! |
| 29 | + |
| 30 | + |
| 31 | +Why structured like this? |
| 32 | +------------------------- |
| 33 | + |
| 34 | +### Why is there adapter code at all? |
| 35 | + |
| 36 | +The `go-ipld-prime/storage` interfaces are a newer generation. |
| 37 | + |
| 38 | +A new generation of APIs was desirable because it unifies the old APIs, |
| 39 | +and also because we were able to improves and update several things in the process. |
| 40 | +(You can see some of the list of improvements in https://github.com/ipld/go-ipld-prime/pull/265, |
| 41 | +where these APIs were first introduced.) |
| 42 | +The new generation of APIs avoids several types present in the old APIs which forced otherwise-avoidable allocations. |
| 43 | +(See notes later in this document about "which adapter should I use" for more on that.) |
| 44 | +Finally, the new generation of APIs is carefully designed to support minimal implementations, |
| 45 | +by carefully avoiding use of non-standard-library types in key API definitions, |
| 46 | +and by keeping most advanced features behind a standardized convention of feature detection. |
| 47 | + |
| 48 | +Because the newer generation of APIs are not exactly the same as the multiple older APIs we're unifying and updating, |
| 49 | +some amount of adapter code is necessary. |
| 50 | + |
| 51 | +(Fortunately, it's not much! But it's not "none", either.) |
| 52 | + |
| 53 | +### Why have this code in a shared place? |
| 54 | + |
| 55 | +The glue code to connect `go-datastore` and the other older APIs |
| 56 | +to the new `go-ipld-prime/storage` APIs is fairly minimal... |
| 57 | +but there's also no reason for anyone to write it twice, |
| 58 | +so we want to put it somewhere easy to share. |
| 59 | + |
| 60 | +### Why do the adapters have their own go modules? |
| 61 | + |
| 62 | +A separate module is used because it's important that go-ipld-prime can be used |
| 63 | +without forming a dependency on `go-datastore` (or the other relevant modules, per adapter). |
| 64 | + |
| 65 | +We want this so that there's a reasonable deprecation pathway -- it must be |
| 66 | +possible to write new code that doesn't take on transitive dependencies to old code. |
| 67 | + |
| 68 | +(As a bonus, looking at the module dependency graphs makes an interestingly |
| 69 | +clear statement about why minimal APIs that don't force transitive dependencies are a good idea!) |
| 70 | + |
| 71 | +### Why is this code all together in this repo? |
| 72 | + |
| 73 | +We put these separate modules in the same git repo as `go-ipld-prime`... because we can. |
| 74 | + |
| 75 | +Technically, neither the storage adapter modules nor the `go-ipld-prime` module depend on each other -- |
| 76 | +they just have interfaces that are aligned with each other -- so it's very easy to |
| 77 | +hold them as separate go modules in the same repo, even though that can otherwise sometimes be tricky. |
| 78 | + |
| 79 | +You may want to make a point of pulling updated versions of the storage adapters that you use |
| 80 | +when pulling updates to go-ipld-prime, though. |
| 81 | + |
| 82 | +### Could we put these adapters upstream into the other relevant repos? |
| 83 | + |
| 84 | +Certainly! |
| 85 | + |
| 86 | +We started with them here because it seemed developmentally lower-friction. |
| 87 | +That may change; these APIs could move. |
| 88 | +This code is just interface satisfaction, so even having multiple copies of it is utterly harmless. |
| 89 | + |
| 90 | + |
| 91 | +Which of `dsadapter` vs `bsadapter` vs `bsrvadapter` should I use? |
| 92 | +------------------------------------------------------------------ |
| 93 | + |
| 94 | +None of them, ideally. |
| 95 | +A direct implementation of the storage APIs will almost certainly be able to perform better than any of these adapters. |
| 96 | +(Check out the `fsstore` package, for example.) |
| 97 | + |
| 98 | +Failing that: use the adapter matching whatever you've got on hand in your code. |
| 99 | + |
| 100 | +There is no correct choice. |
| 101 | + |
| 102 | +`dsadapter` suffers avoidable excessive allocs in processing its key type, |
| 103 | +due to choices in the interior of `github.com/ipfs/go-datastore`. |
| 104 | +It is also unable to support streaming operation, should you desire it. |
| 105 | + |
| 106 | +`bsadapter` and `bsrvadapter` both also suffer overhead due to their key type, |
| 107 | +because they require a transformation back from the plain binary strings used in the storage API to the concrete go-cid type, |
| 108 | +which spends some avoidable CPU time (and also, at present, causes avoidable allocs because of some interesting absenses in `go-cid`). |
| 109 | +Additionally, they suffer avoidable allocs because they wrap the raw binary data in a "block" type, |
| 110 | +which is an interface, and thus heap-escapes; and we need none of that in the storage APIs, and just return the raw data. |
| 111 | +They are also unable to support streaming operation, should you desire it. |
| 112 | + |
| 113 | +It's best to choose the shortest path and use the adapter to whatever layer you need to get to -- |
| 114 | +for example, if you really want to use a `go-datastore` implementation, |
| 115 | +*don't* use `bsadapter` and have it wrap a `go-blockstore` that wraps a `go-datastore` if you can help it: |
| 116 | +instead, use `dsadapter` and wrap the `go-datastore` without any extra layers of indirection. |
| 117 | +You should prefer this because most of the notes above about avoidable allocs are true when |
| 118 | +the legacy interfaces are communicating with each other, as well... |
| 119 | +so the less you use the internal layering of the legacy interfaces, the better off you'll be. |
| 120 | + |
| 121 | +Using a direct implementation of the storage APIs will suffer none of these overheads, |
| 122 | +and so will always be your best bet if possible. |
| 123 | + |
| 124 | +If you have to use one of these adapters, hopefully the performance overheads fall within an acceptable margin. |
| 125 | +If not: we'll be overjoyed to accept help porting things. |
0 commit comments