Skip to content

Commit

Permalink
Add a cidlink.Memory storage option (#266)
Browse files Browse the repository at this point in the history
* Add a cidlink.Memory storage option
  • Loading branch information
willscott authored Oct 14, 2021
1 parent 7d17700 commit 6a1bd1c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions linking/cid/memorystorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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.
//
// 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
}

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
}

0 comments on commit 6a1bd1c

Please sign in to comment.