Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1547: Store pin metadata in a sorted array #1664

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 117 additions & 31 deletions api/pb/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion api/pb/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ message PinOptions {
string Name = 3;
uint64 ShardSize = 4;
reserved 5; // reserved for UserAllocations
map<string, string> Metadata = 6;
map<string, string> Metadata = 6 [deprecated = true];
bytes PinUpdate = 7;
uint64 ExpireAt = 8;
repeated bytes Origins = 9;
repeated Metadata SortedMetadata = 10;
}

message Metadata {
string Key = 1;
string Value = 2;
}
38 changes: 34 additions & 4 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,17 +1106,36 @@ func (pin Pin) ProtoMarshal() ([]byte, error) {
timestampProto = uint64(pin.Timestamp.Unix())
}

// Our metadata needs to always be seralized in exactly the same way,
// and that is why we use an array sorted by key and deprecated using
// a protobuf map.
var sortedMetadata []*pb.Metadata
var metaKeys []string
for k := range pin.Metadata {
metaKeys = append(metaKeys, k)
}
sort.Strings(metaKeys)

for _, k := range metaKeys {
metadata := &pb.Metadata{
Key: k,
Value: pin.Metadata[k],
}
sortedMetadata = append(sortedMetadata, metadata)
}

opts := &pb.PinOptions{
ReplicationFactorMin: int32(pin.ReplicationFactorMin),
ReplicationFactorMax: int32(pin.ReplicationFactorMax),
Name: pin.Name,
ShardSize: pin.ShardSize,
Metadata: pin.Metadata,
PinUpdate: pin.PinUpdate.Bytes(),
ExpireAt: expireAtProto,
// Metadata: pin.Metadata,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Peers on older versions will not be able to see metadata for pins submitted on newer versions.

PinUpdate: pin.PinUpdate.Bytes(),
ExpireAt: expireAtProto,
// Mode: pin.Mode,
// UserAllocations: pin.UserAllocations,
Origins: origins,
Origins: origins,
SortedMetadata: sortedMetadata,
}

pbPin := &pb.Pin{
Expand Down Expand Up @@ -1186,7 +1205,18 @@ func (pin *Pin) ProtoUnmarshal(data []byte) error {
if exp > 0 {
pin.ExpireAt = time.Unix(int64(exp), 0)
}

// Use whatever metadata is available.
//lint:ignore SA1019 we keed to keep backwards compat
pin.Metadata = opts.GetMetadata()
sortedMetadata := opts.GetSortedMetadata()
if len(sortedMetadata) > 0 && pin.Metadata == nil {
pin.Metadata = make(map[string]string, len(sortedMetadata))
}
for _, md := range opts.GetSortedMetadata() {
pin.Metadata[md.Key] = md.Value
}

pinUpdate, err := CastCid(opts.GetPinUpdate())
if err == nil {
pin.PinUpdate = pinUpdate
Expand Down