Use enumerated kind in boltdb proto message

Signed-off-by: Derek McGowan <derek@mcgstyle.net> (github: dmcgowan)
This commit is contained in:
Derek McGowan 2017-03-16 10:15:56 -07:00
parent 912746b016
commit 73f467a32b
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB
3 changed files with 89 additions and 42 deletions

View File

@ -96,13 +96,24 @@ func (ms *boltMetastore) createBucketIfNotExists(ctx context.Context, fn func(ct
return fn(ctx, bkt) return fn(ctx, bkt)
} }
func fromProtoActive(active bool) snapshot.Kind { func fromProtoKind(k Kind) snapshot.Kind {
if active { if k == KindActive {
return snapshot.KindActive return snapshot.KindActive
} }
return snapshot.KindCommitted return snapshot.KindCommitted
} }
func toProtoKind(k snapshot.Kind) Kind {
switch k {
case snapshot.KindActive:
return KindActive
case snapshot.KindCommitted:
return KindCommitted
default:
panic("unknown kind")
}
}
func parentKey(parent, child uint64) []byte { func parentKey(parent, child uint64) []byte {
b := make([]byte, binary.Size([]uint64{parent, child})) b := make([]byte, binary.Size([]uint64{parent, child}))
i := binary.PutUvarint(b, parent) i := binary.PutUvarint(b, parent)
@ -127,7 +138,7 @@ func (ms *boltMetastore) Stat(ctx context.Context, key string) (snapshot.Info, e
return snapshot.Info{ return snapshot.Info{
Name: key, Name: key,
Parent: ss.Parent, Parent: ss.Parent,
Kind: fromProtoActive(ss.Active), Kind: fromProtoKind(ss.Kind),
Readonly: ss.Readonly, Readonly: ss.Readonly,
}, nil }, nil
} }
@ -147,7 +158,7 @@ func (ms *boltMetastore) Walk(ctx context.Context, fn func(context.Context, snap
info := snapshot.Info{ info := snapshot.Info{
Name: string(k), Name: string(k),
Parent: ss.Parent, Parent: ss.Parent,
Kind: fromProtoActive(ss.Active), Kind: fromProtoKind(ss.Kind),
Readonly: ss.Readonly, Readonly: ss.Readonly,
} }
return fn(ctx, info) return fn(ctx, info)
@ -166,7 +177,7 @@ func (ms *boltMetastore) CreateActive(ctx context.Context, key, parent string, r
return errors.Wrap(err, "failed to get parent snapshot") return errors.Wrap(err, "failed to get parent snapshot")
} }
if parentS.Active { if parentS.Kind == KindActive {
return errors.Errorf("cannot create active from active") return errors.Errorf("cannot create active from active")
} }
} }
@ -183,7 +194,7 @@ func (ms *boltMetastore) CreateActive(ctx context.Context, key, parent string, r
ss := Snapshot{ ss := Snapshot{
ID: id, ID: id,
Parent: parent, Parent: parent,
Active: true, Kind: KindActive,
Readonly: readonly, Readonly: readonly,
} }
if err := putSnapshot(bkt, key, &ss); err != nil { if err := putSnapshot(bkt, key, &ss); err != nil {
@ -228,7 +239,7 @@ func (ms *boltMetastore) GetActive(ctx context.Context, key string) (a storage.A
if err := proto.Unmarshal(b, &ss); err != nil { if err := proto.Unmarshal(b, &ss); err != nil {
return errors.Wrap(err, "failed to unmarshal snapshot") return errors.Wrap(err, "failed to unmarshal snapshot")
} }
if !ss.Active { if ss.Kind != KindActive {
return errors.Errorf("active not found") return errors.Errorf("active not found")
} }
@ -309,7 +320,7 @@ func (ms *boltMetastore) Remove(ctx context.Context, key string) (id string, k s
} }
id = fmt.Sprintf("%d", ss.ID) id = fmt.Sprintf("%d", ss.ID)
k = fromProtoActive(ss.Active) k = fromProtoKind(ss.Kind)
return nil return nil
}) })
@ -328,14 +339,14 @@ func (ms *boltMetastore) Commit(ctx context.Context, key, name string) (id strin
if err := getSnapshot(bkt, key, &ss); err != nil { if err := getSnapshot(bkt, key, &ss); err != nil {
return errors.Wrap(err, "failed to get active snapshot") return errors.Wrap(err, "failed to get active snapshot")
} }
if !ss.Active { if ss.Kind != KindActive {
return errors.Errorf("key is not active snapshot") return errors.Errorf("key is not active snapshot")
} }
if ss.Readonly { if ss.Readonly {
return errors.Errorf("active snapshot is readonly") return errors.Errorf("active snapshot is readonly")
} }
ss.Active = false ss.Kind = KindCommitted
ss.Readonly = true ss.Readonly = true
if err := putSnapshot(bkt, name, &ss); err != nil { if err := putSnapshot(bkt, name, &ss); err != nil {

View File

@ -34,10 +34,34 @@ var _ = math.Inf
// proto package needs to be updated. // proto package needs to be updated.
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
// Kind defines the kind of snapshot.
type Kind int32
const (
// KindActive represents an active snapshot
KindActive Kind = 0
// KindCommitted represents a committed immutable snapshot
KindCommitted Kind = 1
)
var Kind_name = map[int32]string{
0: "ACTIVE",
1: "COMMITTED",
}
var Kind_value = map[string]int32{
"ACTIVE": 0,
"COMMITTED": 1,
}
func (x Kind) String() string {
return proto.EnumName(Kind_name, int32(x))
}
func (Kind) EnumDescriptor() ([]byte, []int) { return fileDescriptorRecord, []int{0} }
type Snapshot struct { type Snapshot struct {
ID uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` ID uint64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
Parent string `protobuf:"bytes,2,opt,name=parent,proto3" json:"parent,omitempty"` Parent string `protobuf:"bytes,2,opt,name=parent,proto3" json:"parent,omitempty"`
Active bool `protobuf:"varint,4,opt,name=active,proto3" json:"active,omitempty"` Kind Kind `protobuf:"varint,4,opt,name=kind,proto3,enum=containerd.v1.Kind" json:"kind,omitempty"`
Readonly bool `protobuf:"varint,5,opt,name=readonly,proto3" json:"readonly,omitempty"` Readonly bool `protobuf:"varint,5,opt,name=readonly,proto3" json:"readonly,omitempty"`
} }
@ -47,6 +71,7 @@ func (*Snapshot) Descriptor() ([]byte, []int) { return fileDescriptorRecord, []i
func init() { func init() {
proto.RegisterType((*Snapshot)(nil), "containerd.v1.Snapshot") proto.RegisterType((*Snapshot)(nil), "containerd.v1.Snapshot")
proto.RegisterEnum("containerd.v1.Kind", Kind_name, Kind_value)
} }
func (m *Snapshot) Marshal() (dAtA []byte, err error) { func (m *Snapshot) Marshal() (dAtA []byte, err error) {
size := m.Size() size := m.Size()
@ -74,15 +99,10 @@ func (m *Snapshot) MarshalTo(dAtA []byte) (int, error) {
i = encodeVarintRecord(dAtA, i, uint64(len(m.Parent))) i = encodeVarintRecord(dAtA, i, uint64(len(m.Parent)))
i += copy(dAtA[i:], m.Parent) i += copy(dAtA[i:], m.Parent)
} }
if m.Active { if m.Kind != 0 {
dAtA[i] = 0x20 dAtA[i] = 0x20
i++ i++
if m.Active { i = encodeVarintRecord(dAtA, i, uint64(m.Kind))
dAtA[i] = 1
} else {
dAtA[i] = 0
}
i++
} }
if m.Readonly { if m.Readonly {
dAtA[i] = 0x28 dAtA[i] = 0x28
@ -134,8 +154,8 @@ func (m *Snapshot) Size() (n int) {
if l > 0 { if l > 0 {
n += 1 + l + sovRecord(uint64(l)) n += 1 + l + sovRecord(uint64(l))
} }
if m.Active { if m.Kind != 0 {
n += 2 n += 1 + sovRecord(uint64(m.Kind))
} }
if m.Readonly { if m.Readonly {
n += 2 n += 2
@ -163,7 +183,7 @@ func (this *Snapshot) String() string {
s := strings.Join([]string{`&Snapshot{`, s := strings.Join([]string{`&Snapshot{`,
`ID:` + fmt.Sprintf("%v", this.ID) + `,`, `ID:` + fmt.Sprintf("%v", this.ID) + `,`,
`Parent:` + fmt.Sprintf("%v", this.Parent) + `,`, `Parent:` + fmt.Sprintf("%v", this.Parent) + `,`,
`Active:` + fmt.Sprintf("%v", this.Active) + `,`, `Kind:` + fmt.Sprintf("%v", this.Kind) + `,`,
`Readonly:` + fmt.Sprintf("%v", this.Readonly) + `,`, `Readonly:` + fmt.Sprintf("%v", this.Readonly) + `,`,
`}`, `}`,
}, "") }, "")
@ -256,9 +276,9 @@ func (m *Snapshot) Unmarshal(dAtA []byte) error {
iNdEx = postIndex iNdEx = postIndex
case 4: case 4:
if wireType != 0 { if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Active", wireType) return fmt.Errorf("proto: wrong wireType = %d for field Kind", wireType)
} }
var v int m.Kind = 0
for shift := uint(0); ; shift += 7 { for shift := uint(0); ; shift += 7 {
if shift >= 64 { if shift >= 64 {
return ErrIntOverflowRecord return ErrIntOverflowRecord
@ -268,12 +288,11 @@ func (m *Snapshot) Unmarshal(dAtA []byte) error {
} }
b := dAtA[iNdEx] b := dAtA[iNdEx]
iNdEx++ iNdEx++
v |= (int(b) & 0x7F) << shift m.Kind |= (Kind(b) & 0x7F) << shift
if b < 0x80 { if b < 0x80 {
break break
} }
} }
m.Active = bool(v != 0)
case 5: case 5:
if wireType != 0 { if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field Readonly", wireType) return fmt.Errorf("proto: wrong wireType = %d for field Readonly", wireType)
@ -425,20 +444,25 @@ func init() {
} }
var fileDescriptorRecord = []byte{ var fileDescriptorRecord = []byte{
// 225 bytes of a gzipped FileDescriptorProto // 314 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x44, 0x8e, 0xb1, 0x4e, 0xc3, 0x30, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x8f, 0xcf, 0x4a, 0xf3, 0x40,
0x10, 0x86, 0x7b, 0x51, 0x89, 0x82, 0x25, 0x96, 0x08, 0x55, 0x56, 0x07, 0x13, 0x31, 0x65, 0x8a, 0x14, 0xc5, 0x33, 0x21, 0x5f, 0x68, 0x87, 0xaf, 0xa5, 0x46, 0x29, 0x21, 0x8b, 0x71, 0x70, 0x63,
0x85, 0x78, 0x02, 0x2a, 0x16, 0xd6, 0xf0, 0x04, 0x8e, 0x6d, 0xa5, 0x16, 0xc5, 0x17, 0x5d, 0x8e, 0x70, 0x91, 0x41, 0x7d, 0x82, 0xfe, 0x5b, 0x94, 0x52, 0x84, 0x58, 0xdc, 0x27, 0x99, 0x21, 0x1d,
0x4a, 0x6c, 0x3c, 0x5e, 0x47, 0x46, 0x26, 0x44, 0xfd, 0x24, 0xa8, 0x21, 0xa2, 0xdb, 0x7d, 0xf7, 0xda, 0xce, 0x2d, 0xd3, 0xb1, 0xe0, 0x4a, 0x97, 0xd2, 0x77, 0xe8, 0x4a, 0x9f, 0xc2, 0x27, 0xe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b, 0xb1, 0xde,
0x5e, 0x49, 0x99, 0xd5, 0xf6, 0x37, 0x5b, 0x62, 0x7d, 0x6c, 0x89, 0xf5, 0x54, 0x10, 0xb4, 0x29,
0x08, 0x7a, 0x2f, 0x08, 0xfa, 0x2a, 0x08, 0x4a, 0xdd, 0xb2, 0xf5, 0xf5, 0x77, 0x00, 0x00, 0x00,
0xff, 0xff, 0x9c, 0xce, 0xfc, 0xc2, 0x5f, 0x01, 0x00, 0x00,
} }

View File

@ -4,9 +4,21 @@ package containerd.v1;
import "gogoproto/gogo.proto"; import "gogoproto/gogo.proto";
// Kind defines the kind of snapshot.
enum Kind {
option (gogoproto.goproto_enum_prefix) = false;
option (gogoproto.enum_customname) = "Kind";
// KindActive represents an active snapshot
ACTIVE = 0 [(gogoproto.enumvalue_customname) = "KindActive"];
// KindCommitted represents a committed immutable snapshot
COMMITTED = 1 [(gogoproto.enumvalue_customname) = "KindCommitted"];
}
message Snapshot { message Snapshot {
uint64 id = 1 [(gogoproto.customname) = "ID"]; uint64 id = 1 [(gogoproto.customname) = "ID"];
string parent = 2; string parent = 2;
bool active = 4; Kind kind = 4;
bool readonly = 5; bool readonly = 5;
} }