Commit dbebd417 authored by Jeromy's avatar Jeromy

cid: integrate cid into bitswap and blockstores

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent 67ad3923
...@@ -2,15 +2,15 @@ ...@@ -2,15 +2,15 @@
package merkledag package merkledag
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"sync" "sync"
blocks "github.com/ipfs/go-ipfs/blocks"
bserv "github.com/ipfs/go-ipfs/blockservice" bserv "github.com/ipfs/go-ipfs/blockservice"
offline "github.com/ipfs/go-ipfs/exchange/offline" offline "github.com/ipfs/go-ipfs/exchange/offline"
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
"context"
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log" logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid" cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
) )
...@@ -60,7 +60,7 @@ func (n *dagService) Add(nd *Node) (*cid.Cid, error) { ...@@ -60,7 +60,7 @@ func (n *dagService) Add(nd *Node) (*cid.Cid, error) {
return nil, fmt.Errorf("dagService is nil") return nil, fmt.Errorf("dagService is nil")
} }
return n.Blocks.AddObject(nd) return n.Blocks.AddBlock(nd)
} }
func (n *dagService) Batch() *Batch { func (n *dagService) Batch() *Batch {
...@@ -122,7 +122,7 @@ func (n *dagService) GetOfflineLinkService() LinkService { ...@@ -122,7 +122,7 @@ func (n *dagService) GetOfflineLinkService() LinkService {
} }
func (n *dagService) Remove(nd *Node) error { func (n *dagService) Remove(nd *Node) error {
return n.Blocks.DeleteObject(nd) return n.Blocks.DeleteBlock(nd)
} }
// FetchGraph fetches all nodes that are children of the given node // FetchGraph fetches all nodes that are children of the given node
...@@ -147,27 +147,11 @@ type NodeOption struct { ...@@ -147,27 +147,11 @@ type NodeOption struct {
Err error Err error
} }
// TODO: this is a mid-term hack to get around the fact that blocks don't
// have full CIDs and potentially (though we don't know of any such scenario)
// may have the same block with multiple different encodings.
// We have discussed the possiblity of using CIDs as datastore keys
// in the future. This would be a much larger changeset than i want to make
// right now.
func cidsToKeyMapping(cids []*cid.Cid) map[key.Key]*cid.Cid {
mapping := make(map[key.Key]*cid.Cid)
for _, c := range cids {
mapping[key.Key(c.Hash())] = c
}
return mapping
}
func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *NodeOption { func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *NodeOption {
out := make(chan *NodeOption, len(keys)) out := make(chan *NodeOption, len(keys))
blocks := ds.Blocks.GetBlocks(ctx, keys) blocks := ds.Blocks.GetBlocks(ctx, keys)
var count int var count int
mapping := cidsToKeyMapping(keys)
go func() { go func() {
defer close(out) defer close(out)
for { for {
...@@ -180,7 +164,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *Node ...@@ -180,7 +164,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []*cid.Cid) <-chan *Node
return return
} }
c := mapping[b.Key()] c := b.Cid()
var nd *Node var nd *Node
switch c.Type() { switch c.Type() {
...@@ -361,7 +345,7 @@ func (np *nodePromise) Get(ctx context.Context) (*Node, error) { ...@@ -361,7 +345,7 @@ func (np *nodePromise) Get(ctx context.Context) (*Node, error) {
type Batch struct { type Batch struct {
ds *dagService ds *dagService
objects []bserv.Object blocks []blocks.Block
size int size int
MaxSize int MaxSize int
} }
...@@ -372,7 +356,7 @@ func (t *Batch) Add(nd *Node) (*cid.Cid, error) { ...@@ -372,7 +356,7 @@ func (t *Batch) Add(nd *Node) (*cid.Cid, error) {
return nil, err return nil, err
} }
t.objects = append(t.objects, nd) t.blocks = append(t.blocks, nd)
t.size += len(d) t.size += len(d)
if t.size > t.MaxSize { if t.size > t.MaxSize {
return nd.Cid(), t.Commit() return nd.Cid(), t.Commit()
...@@ -381,8 +365,8 @@ func (t *Batch) Add(nd *Node) (*cid.Cid, error) { ...@@ -381,8 +365,8 @@ func (t *Batch) Add(nd *Node) (*cid.Cid, error) {
} }
func (t *Batch) Commit() error { func (t *Batch) Commit() error {
_, err := t.ds.Blocks.AddObjects(t.objects) _, err := t.ds.Blocks.AddBlocks(t.blocks)
t.objects = nil t.blocks = nil
t.size = 0 t.size = 0
return err return err
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment