responsebuilder.go 3.09 KB
Newer Older
1 2 3
package responsebuilder

import (
4
	blocks "github.com/ipfs/go-block-format"
5 6
	"github.com/ipfs/go-graphsync/ipldbridge"
	gsmsg "github.com/ipfs/go-graphsync/message"
7
	"github.com/ipfs/go-graphsync/metadata"
8 9 10 11 12 13 14 15
	"github.com/ipld/go-ipld-prime"
)

// ResponseBuilder captures componenst of a response message across multiple
// requests for a given peer and then generates the corresponding
// GraphSync message components once responses are ready to send.
type ResponseBuilder struct {
	outgoingBlocks     []blocks.Block
16
	blkSize            int
17
	completedResponses map[gsmsg.GraphSyncRequestID]gsmsg.GraphSyncResponseStatusCode
18
	outgoingResponses  map[gsmsg.GraphSyncRequestID]metadata.Metadata
19 20 21 22 23
}

// New generates a new ResponseBuilder.
func New() *ResponseBuilder {
	return &ResponseBuilder{
24
		completedResponses: make(map[gsmsg.GraphSyncRequestID]gsmsg.GraphSyncResponseStatusCode),
25
		outgoingResponses:  make(map[gsmsg.GraphSyncRequestID]metadata.Metadata),
26 27 28 29 30
	}
}

// AddBlock adds the given block to the response.
func (rb *ResponseBuilder) AddBlock(block blocks.Block) {
31
	rb.blkSize += len(block.RawData())
32 33 34
	rb.outgoingBlocks = append(rb.outgoingBlocks, block)
}

35 36 37 38 39
// BlockSize returns the total size of all blocks in this response
func (rb *ResponseBuilder) BlockSize() int {
	return rb.blkSize
}

40 41 42
// AddLink adds the given link and whether its block is present
// to the response for the given request ID.
func (rb *ResponseBuilder) AddLink(requestID gsmsg.GraphSyncRequestID, link ipld.Link, blockPresent bool) {
43
	rb.outgoingResponses[requestID] = append(rb.outgoingResponses[requestID], metadata.Item{Link: link, BlockPresent: blockPresent})
44 45 46 47 48
}

// AddCompletedRequest marks the given request as completed in the response,
// as well as whether the graphsync request responded with complete or partial
// data.
49 50
func (rb *ResponseBuilder) AddCompletedRequest(requestID gsmsg.GraphSyncRequestID, status gsmsg.GraphSyncResponseStatusCode) {
	rb.completedResponses[requestID] = status
51 52 53
	// make sure this completion goes out in next response even if no links are sent
	_, ok := rb.outgoingResponses[requestID]
	if !ok {
54
		rb.outgoingResponses[requestID] = nil
55 56 57 58 59 60 61 62 63 64 65 66
	}
}

// Empty returns true if there is no content to send
func (rb *ResponseBuilder) Empty() bool {
	return len(rb.outgoingBlocks) == 0 && len(rb.outgoingResponses) == 0
}

// Build assembles and encodes response data from the added requests, links, and blocks.
func (rb *ResponseBuilder) Build(ipldBridge ipldbridge.IPLDBridge) ([]gsmsg.GraphSyncResponse, []blocks.Block, error) {
	responses := make([]gsmsg.GraphSyncResponse, 0, len(rb.outgoingResponses))
	for requestID, linkMap := range rb.outgoingResponses {
67
		extra, err := metadata.EncodeMetadata(linkMap, ipldBridge)
68 69 70
		if err != nil {
			return nil, nil, err
		}
71 72
		status, isComplete := rb.completedResponses[requestID]
		responses = append(responses, gsmsg.NewResponse(requestID, responseCode(status, isComplete), extra))
73 74 75 76
	}
	return responses, rb.outgoingBlocks, nil
}

77
func responseCode(status gsmsg.GraphSyncResponseStatusCode, isComplete bool) gsmsg.GraphSyncResponseStatusCode {
78 79 80
	if !isComplete {
		return gsmsg.PartialResponse
	}
81
	return status
82
}