Skip to content

Commit 84386ea

Browse files
author
Harshil Goel
authored
fix(core): fix unmarshal protobuf when len val is 0 (#9347) (#9349)
1 parent fb78dd7 commit 84386ea

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

schema/schema.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,9 @@ func loadFromDB(ctx context.Context, loadType int) error {
616616
err := item.Value(func(val []byte) error {
617617
if len(val) == 0 {
618618
s = pb.SchemaUpdate{Predicate: pk.Attr, ValueType: pb.Posting_DEFAULT}
619+
} else {
620+
x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db")
619621
}
620-
x.Checkf(proto.Unmarshal(val, &s), "Error while loading schema from db")
621622
State().Set(pk.Attr, &s)
622623
return nil
623624
})
@@ -627,8 +628,9 @@ func loadFromDB(ctx context.Context, loadType int) error {
627628
err := item.Value(func(val []byte) error {
628629
if len(val) == 0 {
629630
t = pb.TypeUpdate{TypeName: pk.Attr}
631+
} else {
632+
x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db")
630633
}
631-
x.Checkf(proto.Unmarshal(val, &t), "Error while loading types from db")
632634
State().SetType(pk.Attr, &t)
633635
return nil
634636
})

worker/sort_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,41 @@ func writePostingListToDisk(kvs []*bpb.KV) error {
6666
return writer.Flush()
6767
}
6868

69+
func TestEmptyTypeSchema(t *testing.T) {
70+
dir, err := os.MkdirTemp("", "storetest_")
71+
x.Check(err)
72+
defer os.RemoveAll(dir)
73+
74+
opt := badger.DefaultOptions(dir)
75+
ps, err := badger.OpenManaged(opt)
76+
x.Check(err)
77+
pstore = ps
78+
posting.Init(ps, 0, false)
79+
Init(ps)
80+
81+
typeName := "1-temp"
82+
ts := uint64(10)
83+
txn := pstore.NewTransactionAt(ts, true)
84+
defer txn.Discard()
85+
e := &badger.Entry{
86+
Key: x.TypeKey(typeName),
87+
Value: make([]byte, 0),
88+
UserMeta: posting.BitSchemaPosting,
89+
}
90+
require.Nil(t, txn.SetEntry(e.WithDiscard()))
91+
require.Nil(t, txn.CommitAt(ts, nil))
92+
93+
schema.Init(ps)
94+
require.Nil(t, schema.LoadFromDb(context.Background()))
95+
96+
req := &pb.SchemaRequest{}
97+
types, err := GetTypes(context.Background(), req)
98+
require.Nil(t, err)
99+
100+
require.Equal(t, 1, len(types))
101+
x.ParseNamespaceAttr(types[0].TypeName)
102+
}
103+
69104
func TestMultipleTxnListCount(t *testing.T) {
70105
dir, err := os.MkdirTemp("", "storetest_")
71106
x.Check(err)

0 commit comments

Comments
 (0)