func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { if k == nil { log.Error("nil cid in blockstore") return nil, ErrNotFound } maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k)) if err == ds.ErrNotFound { return nil, ErrNotFound } if err != nil { return nil, err } bdata, ok := maybeData.([]byte) if !ok { return nil, ValueTypeMismatch } if bs.rehash { rbcid, err := k.Prefix().Sum(bdata) if err != nil { return nil, err } if !rbcid.Equals(k) { return nil, ErrHashMismatch } return blocks.NewBlockWithCid(bdata, rbcid) } else { return blocks.NewBlockWithCid(bdata, k) } }
func (bs *blockstore) Put(block blocks.Block) error { k := dshelp.CidToDsKey(block.Cid()) // Has is cheaper than Put, so see if we already have it exists, err := bs.datastore.Has(k) if err == nil && exists { return nil // already stored. } return bs.datastore.Put(k, block.RawData()) }
func (bs *blockstore) PutMany(blocks []blocks.Block) error { t, err := bs.datastore.Batch() if err != nil { return err } for _, b := range blocks { k := dshelp.CidToDsKey(b.Cid()) exists, err := bs.datastore.Has(k) if err == nil && exists { continue } err = t.Put(k, b.RawData()) if err != nil { return err } } return t.Commit() }
func (s *blockstore) DeleteBlock(k *cid.Cid) error { return s.datastore.Delete(dshelp.CidToDsKey(k)) }
func (bs *blockstore) Has(k *cid.Cid) (bool, error) { return bs.datastore.Has(dshelp.CidToDsKey(k)) }