-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from raulk/feat/pstore-ds-gc
pstoreds: migrate from strict TTL to GC-based expiry.
- Loading branch information
Showing
18 changed files
with
2,238 additions
and
534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
PB = $(wildcard *.proto) | ||
GO = $(PB:.proto=.pb.go) | ||
|
||
all: $(GO) | ||
|
||
%.pb.go: %.proto | ||
protoc --proto_path=$(GOPATH)/src:. --gogofaster_out=. $< | ||
|
||
clean: | ||
rm -f *.pb.go | ||
|
||
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package pstore_pb | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
proto "github.com/gogo/protobuf/proto" | ||
peer "github.com/libp2p/go-libp2p-peer" | ||
pt "github.com/libp2p/go-libp2p-peer/test" | ||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
// customGogoType aggregates the interfaces that custom Gogo types need to implement. | ||
// it is only used for type assertions. | ||
type customGogoType interface { | ||
proto.Marshaler | ||
proto.Unmarshaler | ||
json.Marshaler | ||
json.Unmarshaler | ||
proto.Sizer | ||
} | ||
|
||
// ProtoAddr is a custom type used by gogo to serde raw peer IDs into the peer.ID type, and back. | ||
type ProtoPeerID struct { | ||
peer.ID | ||
} | ||
|
||
var _ customGogoType = (*ProtoPeerID)(nil) | ||
|
||
func (id ProtoPeerID) Marshal() ([]byte, error) { | ||
return []byte(id.ID), nil | ||
} | ||
|
||
func (id ProtoPeerID) MarshalTo(data []byte) (n int, err error) { | ||
return copy(data, []byte(id.ID)), nil | ||
} | ||
|
||
func (id ProtoPeerID) MarshalJSON() ([]byte, error) { | ||
m, _ := id.Marshal() | ||
return json.Marshal(m) | ||
} | ||
|
||
func (id *ProtoPeerID) Unmarshal(data []byte) (err error) { | ||
id.ID = peer.ID(string(data)) | ||
return nil | ||
} | ||
|
||
func (id *ProtoPeerID) UnmarshalJSON(data []byte) error { | ||
var v []byte | ||
err := json.Unmarshal(data, v) | ||
if err != nil { | ||
return err | ||
} | ||
return id.Unmarshal(v) | ||
} | ||
|
||
func (id ProtoPeerID) Size() int { | ||
return len([]byte(id.ID)) | ||
} | ||
|
||
// ProtoAddr is a custom type used by gogo to serde raw multiaddresses into the ma.Multiaddr type, and back. | ||
type ProtoAddr struct { | ||
ma.Multiaddr | ||
} | ||
|
||
var _ customGogoType = (*ProtoAddr)(nil) | ||
|
||
func (a ProtoAddr) Marshal() ([]byte, error) { | ||
return a.Bytes(), nil | ||
} | ||
|
||
func (a ProtoAddr) MarshalTo(data []byte) (n int, err error) { | ||
return copy(data, a.Bytes()), nil | ||
} | ||
|
||
func (a ProtoAddr) MarshalJSON() ([]byte, error) { | ||
m, _ := a.Marshal() | ||
return json.Marshal(m) | ||
} | ||
|
||
func (a *ProtoAddr) Unmarshal(data []byte) (err error) { | ||
a.Multiaddr, err = ma.NewMultiaddrBytes(data) | ||
return err | ||
} | ||
|
||
func (a *ProtoAddr) UnmarshalJSON(data []byte) error { | ||
v := new([]byte) | ||
err := json.Unmarshal(data, v) | ||
if err != nil { | ||
return err | ||
} | ||
return a.Unmarshal(*v) | ||
} | ||
|
||
func (a ProtoAddr) Size() int { | ||
return len(a.Bytes()) | ||
} | ||
|
||
// NewPopulatedProtoAddr generates a populated instance of the custom gogo type ProtoAddr. | ||
// It is required by gogo-generated tests. | ||
func NewPopulatedProtoAddr(r randyPstore) *ProtoAddr { | ||
a, _ := ma.NewMultiaddr("/ip4/123.123.123.123/tcp/7001") | ||
return &ProtoAddr{Multiaddr: a} | ||
} | ||
|
||
// NewPopulatedProtoPeerID generates a populated instance of the custom gogo type ProtoPeerID. | ||
// It is required by gogo-generated tests. | ||
func NewPopulatedProtoPeerID(r randyPstore) *ProtoPeerID { | ||
id, _ := pt.RandPeerID() | ||
return &ProtoPeerID{ID: id} | ||
} |
Oops, something went wrong.