From 76353188a8a6f8b9093845ac6766573189b18a08 Mon Sep 17 00:00:00 2001 From: Will Scott Date: Wed, 13 Oct 2021 14:56:10 -0700 Subject: [PATCH 1/2] Add a cidlink.Memory storage option --- linking/cid/memorystorage.go | 51 ++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 linking/cid/memorystorage.go diff --git a/linking/cid/memorystorage.go b/linking/cid/memorystorage.go new file mode 100644 index 00000000..6666de03 --- /dev/null +++ b/linking/cid/memorystorage.go @@ -0,0 +1,51 @@ +package cidlink + +import ( + "bytes" + "fmt" + "io" + "os" + + "github.com/ipld/go-ipld-prime/datamodel" + "github.com/ipld/go-ipld-prime/linking" +) + +// Memory is a simple in-memory storage for cidlinks. It's the same as `storage.Memory` +// but uses typical multihash semantics used when reading/writing cidlinks. +type Memory struct { + Bag map[string][]byte +} + +func (store *Memory) beInitialized() { + if store.Bag != nil { + return + } + store.Bag = make(map[string][]byte) +} + +func (store *Memory) OpenRead(lnkCtx linking.LinkContext, lnk datamodel.Link) (io.Reader, error) { + store.beInitialized() + cl, ok := lnk.(Link) + if !ok { + return nil, fmt.Errorf("incompatible link type: %T", lnk) + } + data, exists := store.Bag[string(cl.Hash())] + if !exists { + return nil, os.ErrNotExist + } + return bytes.NewReader(data), nil +} + +func (store *Memory) OpenWrite(lnkCtx linking.LinkContext) (io.Writer, linking.BlockWriteCommitter, error) { + store.beInitialized() + buf := bytes.Buffer{} + return &buf, func(lnk datamodel.Link) error { + cl, ok := lnk.(Link) + if !ok { + return fmt.Errorf("incompatible link type: %T", lnk) + } + + store.Bag[string(cl.Hash())] = buf.Bytes() + return nil + }, nil +} From 4aa49264dced3bbe842960fa29bfe6cc678e32b8 Mon Sep 17 00:00:00 2001 From: Will Date: Wed, 13 Oct 2021 17:59:34 -0700 Subject: [PATCH 2/2] Update linking/cid/memorystorage.go Co-authored-by: Rod Vagg --- linking/cid/memorystorage.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/linking/cid/memorystorage.go b/linking/cid/memorystorage.go index 6666de03..cf1e3849 100644 --- a/linking/cid/memorystorage.go +++ b/linking/cid/memorystorage.go @@ -12,6 +12,11 @@ import ( // Memory is a simple in-memory storage for cidlinks. It's the same as `storage.Memory` // but uses typical multihash semantics used when reading/writing cidlinks. +// +// Using multihash as the storage key rather than the whole CID will remove the +// distinction between CIDv0 and their CIDv1 counterpart. It also removes the +// distinction between CIDs where the multihash is the same but the codec is +// different, e.g. `dag-cbor` and a `raw` version of the same data. type Memory struct { Bag map[string][]byte }