rwservice.go 1.33 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
package merkledag

import (
	"context"

6 7
	cid "gitlab.dms3.io/dms3/go-cid"
	ld "gitlab.dms3.io/dms3/go-ld-format"
Jeromy's avatar
Jeromy committed
8 9
)

10
// ComboService implements ld.DAGService, using 'Read' for all fetch methods,
Jeromy's avatar
Jeromy committed
11 12
// and 'Write' for all methods that add new objects.
type ComboService struct {
13 14
	Read  ld.NodeGetter
	Write ld.DAGService
Jeromy's avatar
Jeromy committed
15 16
}

17
var _ ld.DAGService = (*ComboService)(nil)
Jeromy's avatar
Jeromy committed
18

19
// Add writes a new node using the Write DAGService.
20
func (cs *ComboService) Add(ctx context.Context, nd ld.Node) error {
Jeromy's avatar
Jeromy committed
21 22 23
	return cs.Write.Add(ctx, nd)
}

24
// AddMany adds nodes using the Write DAGService.
25
func (cs *ComboService) AddMany(ctx context.Context, nds []ld.Node) error {
Jeromy's avatar
Jeromy committed
26 27 28
	return cs.Write.AddMany(ctx, nds)
}

29
// Get fetches a node using the Read DAGService.
30
func (cs *ComboService) Get(ctx context.Context, c cid.Cid) (ld.Node, error) {
Jeromy's avatar
Jeromy committed
31 32 33
	return cs.Read.Get(ctx, c)
}

34
// GetMany fetches nodes using the Read DAGService.
35
func (cs *ComboService) GetMany(ctx context.Context, cids []cid.Cid) <-chan *ld.NodeOption {
Jeromy's avatar
Jeromy committed
36 37 38
	return cs.Read.GetMany(ctx, cids)
}

39
// Remove deletes a node using the Write DAGService.
40
func (cs *ComboService) Remove(ctx context.Context, c cid.Cid) error {
Jeromy's avatar
Jeromy committed
41 42 43
	return cs.Write.Remove(ctx, c)
}

44
// RemoveMany deletes nodes using the Write DAGService.
45
func (cs *ComboService) RemoveMany(ctx context.Context, cids []cid.Cid) error {
Jeromy's avatar
Jeromy committed
46 47
	return cs.Write.RemoveMany(ctx, cids)
}