Skip to content
Permalink
Browse files

Merge pull request #655 from ipfs/feat/datastore

State: Use go-datastore to implement the state interface
  • Loading branch information...
hsanjuan committed Feb 20, 2019
2 parents 5fb2b6a + 46801aa commit f57c5e40663d2e8500f67e842af4350acd3583c9

Some generated files are not rendered by default. Learn more.

@@ -0,0 +1,23 @@
syntax = "proto3";
package api.pb;

message Pin {
bytes Cid = 1;
enum PinType {
BadType = 0; // 1 << iota
DataType = 1; // 2 << iota
MetaType = 2;
ClusterDAGType = 3;
ShardType = 4;
}
PinType Type = 2;
repeated bytes Allocations = 3;
sint32 MaxDepth = 4;
bytes Reference = 5;
reserved 6,7;
sint32 ReplicationFactorMin = 8;
sint32 ReplicationFactorMax = 9;
string Name = 10;
uint64 ShardSize = 11;
map<string, string> Metadata = 12;
}
@@ -19,6 +19,9 @@ import (
"strings"
"time"

pb "github.com/ipfs/ipfs-cluster/api/pb"

proto "github.com/gogo/protobuf/proto"
cid "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p-peer"
@@ -706,10 +709,11 @@ func (pT PinType) String() string {

// PinOptions wraps user-defined options for Pins
type PinOptions struct {
ReplicationFactorMin int `json:"replication_factor_min"`
ReplicationFactorMax int `json:"replication_factor_max"`
Name string `json:"name"`
ShardSize uint64 `json:"shard_size"`
ReplicationFactorMin int `json:"replication_factor_min" codec:"rn,omitempty"`
ReplicationFactorMax int `json:"replication_factor_max" codec:"rx,omitempty"`
Name string `json:"name" codec:"n,omitempty"`
ShardSize uint64 `json:"shard_size" codec:"s,omitempty"`
Metadata map[string]string `json:"metadata" codec:"m,omitempty"`
}

// ToQuery returns the PinOption as query arguments.
@@ -793,22 +797,19 @@ func PinCid(c cid.Cid) Pin {
// its PinOptions fields with the given options.
func PinWithOpts(c cid.Cid, opts PinOptions) Pin {
p := PinCid(c)
p.ReplicationFactorMin = opts.ReplicationFactorMin
p.ReplicationFactorMax = opts.ReplicationFactorMax
p.Name = opts.Name
p.ShardSize = opts.ShardSize
p.PinOptions = opts
return p
}

// PinSerial is a serializable version of Pin
type PinSerial struct {
PinOptions

Cid string `json:"cid"`
Type uint64 `json:"type"`
Allocations []string `json:"allocations"`
MaxDepth int `json:"max_depth"`
Reference string `json:"reference"`
Cid string `json:"cid" codec:"c,omitempty"`
Type uint64 `json:"type" codec:"t,omitempty"`
Allocations []string `json:"allocations" codec:"a,omitempty"`
MaxDepth int `json:"max_depth" codec:"d,omitempty"`
Reference string `json:"reference" codec:"r,omitempty"`
}

// ToSerial converts a Pin to PinSerial.
@@ -822,7 +823,6 @@ func (pin Pin) ToSerial() PinSerial {
ref = pin.Reference.String()
}

n := pin.Name
allocs := PeersToStrings(pin.Allocations)

return PinSerial{
@@ -831,13 +831,91 @@ func (pin Pin) ToSerial() PinSerial {
Type: uint64(pin.Type),
MaxDepth: pin.MaxDepth,
Reference: ref,
PinOptions: PinOptions{
Name: n,
ReplicationFactorMin: pin.ReplicationFactorMin,
ReplicationFactorMax: pin.ReplicationFactorMax,
ShardSize: pin.ShardSize,
},
PinOptions: pin.PinOptions,
}
}

func convertPinType(t PinType) pb.Pin_PinType {
var i pb.Pin_PinType
for t != 1 {
if t == 0 {
return pb.Pin_BadType
}
t = t >> 1
i++
}
return i
}

// ProtoMarshal marshals this Pin using probobuf.
func (pin *Pin) ProtoMarshal() ([]byte, error) {
allocs := make([][]byte, len(pin.Allocations), len(pin.Allocations))
for i, pid := range pin.Allocations {
bs, err := pid.Marshal()
if err != nil {
return nil, err
}
allocs[i] = bs
}

pbPin := &pb.Pin{
Cid: pin.Cid.Bytes(),
Type: convertPinType(pin.Type),
Allocations: allocs,
MaxDepth: int32(pin.MaxDepth),
Reference: pin.Reference.Bytes(),
ReplicationFactorMin: int32(pin.ReplicationFactorMin),
ReplicationFactorMax: int32(pin.ReplicationFactorMax),
Name: pin.Name,
ShardSize: pin.ShardSize,
Metadata: pin.Metadata,
}
return proto.Marshal(pbPin)
}

// ProtoUnmarshal unmarshals this fields from protobuf-encoded bytes.
func (pin *Pin) ProtoUnmarshal(data []byte) error {
pbPin := pb.Pin{}
err := proto.Unmarshal(data, &pbPin)
if err != nil {
return err
}
ci, err := cid.Cast(pbPin.GetCid())
if err != nil {
pin.Cid = cid.Undef
} else {
pin.Cid = ci
}

pin.Type = 1 << uint64(pbPin.GetType())

pbAllocs := pbPin.GetAllocations()
lenAllocs := len(pbAllocs)
allocs := make([]peer.ID, lenAllocs, lenAllocs)
for i, pidb := range pbAllocs {
pid, err := peer.IDFromBytes(pidb)
if err != nil {
return err
}
allocs[i] = pid
}

pin.Allocations = allocs
pin.MaxDepth = int(pbPin.GetMaxDepth())
ref, err := cid.Cast(pbPin.GetReference())
if err != nil {
pin.Reference = cid.Undef

} else {
pin.Reference = ref
}
pin.Reference = ref
pin.ReplicationFactorMin = int(pbPin.GetReplicationFactorMin())
pin.ReplicationFactorMax = int(pbPin.GetReplicationFactorMax())
pin.Name = pbPin.GetName()
pin.ShardSize = pbPin.GetShardSize()
pin.Metadata = pbPin.GetMetadata()
return nil
}

// Equals checks if two pins are the same (with the same allocations).

0 comments on commit f57c5e4

Please sign in to comment.
You can’t perform that action at this time.